Class: Mobiledoc::Renderer_0_2

Inherits:
Object
  • Object
show all
Includes:
Utils::SectionTypes, Utils::TagNames
Defined in:
lib/mobiledoc/renderers/0.2.rb

Direct Known Subclasses

Renderer_0_3

Constant Summary collapse

MOBILEDOC_VERSIONS =
['0.2.0']

Constants included from Utils::TagNames

Utils::TagNames::LIST_SECTION_TAG_NAMES, Utils::TagNames::MARKUP_SECTION_TAG_NAMES, Utils::TagNames::MARKUP_TYPES

Constants included from Utils::SectionTypes

Utils::SectionTypes::CARD_SECTION_TYPE, Utils::SectionTypes::IMAGE_SECTION_TYPE, Utils::SectionTypes::LIST_SECTION_TYPE, Utils::SectionTypes::MARKUP_SECTION_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::TagNames

#normalize_tag_name

Constructor Details

#initialize(mobiledoc, state) ⇒ Renderer_0_2

Returns a new instance of Renderer_0_2.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mobiledoc/renderers/0.2.rb', line 16

def initialize(mobiledoc, state)
  version, section_data = *mobiledoc.values_at('version', 'sections')
  validate_version(version)

  self.marker_types, self.sections = *section_data

  self.doc = Nokogiri::HTML.fragment('')
  self.root = create_document_fragment
  self.cards = state[:cards]
  self.card_options = state[:card_options]
  self.unknown_card_handler = state[:unknown_card_handler]
end

Instance Attribute Details

#card_optionsObject

Returns the value of attribute card_options.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def card_options
  @card_options
end

#cardsObject

Returns the value of attribute cards.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def cards
  @cards
end

#docObject

Returns the value of attribute doc.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def doc
  @doc
end

#marker_typesObject

Returns the value of attribute marker_types.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def marker_types
  @marker_types
end

#rootObject

Returns the value of attribute root.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def root
  @root
end

#sectionsObject

Returns the value of attribute sections.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def sections
  @sections
end

#unknown_card_handlerObject

Returns the value of attribute unknown_card_handler.



14
15
16
# File 'lib/mobiledoc/renderers/0.2.rb', line 14

def unknown_card_handler
  @unknown_card_handler
end

Instance Method Details

#_create_card_argument(card_name, payload = {}) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/mobiledoc/renderers/0.2.rb', line 162

def _create_card_argument(card_name, payload={})
  env = {
    name: card_name
  }

  [ env, payload, card_options ]
end

#_create_card_elementObject



158
159
160
# File 'lib/mobiledoc/renderers/0.2.rb', line 158

def _create_card_element
  create_element('div')
end

#_render_card_section(card, name, payload) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mobiledoc/renderers/0.2.rb', line 131

def _render_card_section(card, name, payload)
  card_wrapper = _create_card_element
  card_arg = _create_card_argument(name, payload)
  rendered = card.render(*card_arg)

  _validate_card_render(rendered, card.name)

  if rendered
    append_child(card_wrapper, rendered)
  end

  card_wrapper
end

#_render_markers_on_element(element, markers) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mobiledoc/renderers/0.2.rb', line 178

def _render_markers_on_element(element, markers)
  elements = [element]
  current_element = element

  markers.each do |marker|
    open_types, close_count, text = *marker

    open_types.each do |open_type|
      marker_type = marker_types[open_type]
      tag_name = marker_type.first

      if valid_marker_type?(tag_name)
        opened_element = create_element_from_marker_type(*marker_type)
        append_child(current_element, opened_element)
        elements.push(opened_element)
        current_element = opened_element
      else
        close_count -= 1
      end
    end

    append_child(current_element, create_text_node(text))

    close_count.times do
      elements.pop
      current_element = elements.last
    end
  end
end

#_validate_card_render(rendered, card_name) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/mobiledoc/renderers/0.2.rb', line 170

def _validate_card_render(rendered, card_name)
  return unless rendered

  unless rendered.is_a?(String)
    raise Mobiledoc::Error.new(%Q[Card "#{card_name}" must render html, but result was #{rendered.class}"]);
  end
end

#append_child(target, child) ⇒ Object



76
77
78
# File 'lib/mobiledoc/renderers/0.2.rb', line 76

def append_child(target, child)
  target.add_child(child)
end

#create_document_fragmentObject



49
50
51
# File 'lib/mobiledoc/renderers/0.2.rb', line 49

def create_document_fragment
  create_element('div')
end

#create_element(tag_name) ⇒ Object



53
54
55
56
# File 'lib/mobiledoc/renderers/0.2.rb', line 53

def create_element(tag_name)
  tag_name = normalize_tag_name(tag_name)
  Nokogiri::XML::Node.new(tag_name, doc)
end

#create_element_from_marker_type(tag_name = '', attributes = []) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/mobiledoc/renderers/0.2.rb', line 66

def create_element_from_marker_type(tag_name='', attributes=[])
  element = create_element(tag_name)

  attributes.each_slice(2) do |prop_name, prop_value|
    set_attribute(element, prop_name, prop_value)
  end

  element
end

#create_text_node(text) ⇒ Object



62
63
64
# File 'lib/mobiledoc/renderers/0.2.rb', line 62

def create_text_node(text)
  Nokogiri::XML::Text.new(text, doc)
end

#find_card(name) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mobiledoc/renderers/0.2.rb', line 145

def find_card(name)
  card = cards.find { |c| c.name == name }

  case
  when card
    card
  when ImageCard.name == name
    ImageCard
  else
    unknown_card_handler
  end
end

#renderObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mobiledoc/renderers/0.2.rb', line 35

def render
  root = create_document_fragment

  sections.each do |section|
    rendered = render_section(section)

    if rendered
      append_child(root, rendered)
    end
  end

  root.to_html(save_with: 0).gsub('  ', '  ')
end

#render_card_section(type, name, payload = {}) ⇒ Object



125
126
127
128
129
# File 'lib/mobiledoc/renderers/0.2.rb', line 125

def render_card_section(type, name, payload={})
  card = find_card(name)

  _render_card_section(card, name, payload)
end

#render_image_section(type, url) ⇒ Object



102
103
104
105
106
# File 'lib/mobiledoc/renderers/0.2.rb', line 102

def render_image_section(type, url)
  element = create_element('img')
  set_attribute(element, 'src', url)
  element
end

#render_list_item(markers) ⇒ Object



119
120
121
122
123
# File 'lib/mobiledoc/renderers/0.2.rb', line 119

def render_list_item(markers)
  element = create_element('li')
  _render_markers_on_element(element, markers)
  element
end

#render_list_section(type, tag_name, items) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/mobiledoc/renderers/0.2.rb', line 108

def render_list_section(type, tag_name, items)
  return unless (tag_name, LIST_SECTION_TYPE)

  element = create_element(tag_name)
  items.each do |item|
    append_child(element, render_list_item(item))
  end

  element
end

#render_markup_section(type, tag_name, markers) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/mobiledoc/renderers/0.2.rb', line 94

def render_markup_section(type, tag_name, markers)
  return unless (tag_name, MARKUP_SECTION_TYPE)

  element = create_element(tag_name)
  _render_markers_on_element(element, markers)
  element
end

#render_section(section) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mobiledoc/renderers/0.2.rb', line 80

def render_section(section)
  type = section.first
  case type
  when MARKUP_SECTION_TYPE
    render_markup_section(*section)
  when IMAGE_SECTION_TYPE
    render_image_section(*section)
  when LIST_SECTION_TYPE
    render_list_section(*section)
  when CARD_SECTION_TYPE
    render_card_section(*section)
  end
end

#set_attribute(element, prop_name, prop_value) ⇒ Object



58
59
60
# File 'lib/mobiledoc/renderers/0.2.rb', line 58

def set_attribute(element, prop_name, prop_value)
  element.set_attribute(prop_name, prop_value)
end

#valid_marker_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
# File 'lib/mobiledoc/renderers/0.2.rb', line 221

def valid_marker_type?(type)
  type = normalize_tag_name(type)

  MARKUP_TYPES.include?(type)
end

#valid_section_tag_name?(tag_name, section_type) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/mobiledoc/renderers/0.2.rb', line 208

def (tag_name, section_type)
  tag_name = normalize_tag_name(tag_name)

  case section_type
  when MARKUP_SECTION_TYPE
    MARKUP_SECTION_TAG_NAMES.include?(tag_name)
  when LIST_SECTION_TYPE
    LIST_SECTION_TAG_NAMES.include?(tag_name)
  else
    raise Mobiledoc::Error.new(%Q[Cannot validate tag_name for unknown section type "#{section_type}"])
  end
end

#validate_version(version) ⇒ Object



29
30
31
32
33
# File 'lib/mobiledoc/renderers/0.2.rb', line 29

def validate_version(version)
  unless self.class::MOBILEDOC_VERSIONS.to_a.include? version
    raise Mobiledoc::Error.new(%Q[Unexpected Mobiledoc version "#{version}"])
  end
end