Class: Mobiledoc::Renderer_0_3

Inherits:
Renderer_0_2 show all
Includes:
Utils::MarkerTypes
Defined in:
lib/mobiledoc/renderers/0.3.rb

Constant Summary collapse

MOBILEDOC_VERSIONS =
['0.3.0', '0.3.1', '0.3.2']

Constants included from Utils::MarkerTypes

Utils::MarkerTypes::ATOM_MARKER_TYPE, Utils::MarkerTypes::MARKUP_MARKER_TYPE

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

Attributes inherited from Renderer_0_2

#card_options, #cards, #doc, #marker_types, #root, #sections, #unknown_card_handler

Instance Method Summary collapse

Methods inherited from Renderer_0_2

#_create_card_argument, #_create_card_element, #_render_card_section, #_validate_card_render, #append_child, #create_document_fragment, #create_element, #create_element_from_marker_type, #create_text_node, #find_card, #render, #render_image_section, #render_list_item, #render_list_section, #render_section, #set_attribute, #valid_marker_type?, #valid_section_tag_name?, #validate_version

Methods included from Utils::TagNames

#normalize_tag_name

Constructor Details

#initialize(mobiledoc, state) ⇒ Renderer_0_3

Returns a new instance of Renderer_0_3.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mobiledoc/renderers/0.3.rb', line 13

def initialize(mobiledoc, state)
  version, sections, atom_types, card_types, marker_types = *mobiledoc.values_at('version', 'sections', 'atoms', 'cards', 'markups')
  validate_version(version)

  self.doc = Nokogiri::HTML.fragment('')
  self.root = create_document_fragment
  self.sections = sections
  self.atom_types = atom_types
  self.card_types = card_types
  self.marker_types = marker_types
  self.cards = state[:cards]
  self.atoms = state[:atoms]
  self.card_options = state[:card_options]
  self.unknown_card_handler = state[:unknown_card_handler]
  self.unknown_atom_handler = state[:unknown_atom_handler]
end

Instance Attribute Details

#atom_typesObject

Returns the value of attribute atom_types.



11
12
13
# File 'lib/mobiledoc/renderers/0.3.rb', line 11

def atom_types
  @atom_types
end

#atomsObject

Returns the value of attribute atoms.



11
12
13
# File 'lib/mobiledoc/renderers/0.3.rb', line 11

def atoms
  @atoms
end

#card_typesObject

Returns the value of attribute card_types.



11
12
13
# File 'lib/mobiledoc/renderers/0.3.rb', line 11

def card_types
  @card_types
end

#unknown_atom_handlerObject

Returns the value of attribute unknown_atom_handler.



11
12
13
# File 'lib/mobiledoc/renderers/0.3.rb', line 11

def unknown_atom_handler
  @unknown_atom_handler
end

Instance Method Details

#_create_atom_argument(atom, atom_name, value, payload = {}) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/mobiledoc/renderers/0.3.rb', line 126

def _create_atom_argument(atom, atom_name, value, payload={})
  env = {
    name: atom_name
  }

  [ env, value, payload, card_options ]
end

#_find_atom_by_index(index) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mobiledoc/renderers/0.3.rb', line 113

def _find_atom_by_index(index)
  atom_type = atom_types[index]

  unless atom_type
    raise Mobiledoc::Error.new("No atom definition found at index #{index}")
  end

  name, value, payload = *atom_type
  atom = find_atom(name)

  [ atom, name, value, payload ]
end

#_find_card_by_index(index) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mobiledoc/renderers/0.3.rb', line 47

def _find_card_by_index(index)
  card_type = card_types[index]

  unless card_type
    raise Mobiledoc::Error.new("No card definition found at index #{index}")
  end

  name, payload = *card_type
  card = find_card(name)

  [ card, name, payload ]
end

#_render_atom(index) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/mobiledoc/renderers/0.3.rb', line 103

def _render_atom(index)
  atom, name, value, payload = _find_atom_by_index(index)
  atom_arg = _create_atom_argument(atom, name, value, payload)
  rendered = atom.render(*atom_arg)

  _validate_atom_render(rendered, atom.name)

  rendered || create_text_node('')
end

#_render_markers_on_element(element, markers) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mobiledoc/renderers/0.3.rb', line 60

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

  markers.each do |marker|
    type, open_types, close_count, value = *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

    case type
    when MARKUP_MARKER_TYPE
      append_child(current_element, create_text_node(value))
    when ATOM_MARKER_TYPE
      append_child(current_element, _render_atom(value))
    else
      raise Mobiledoc::Error.new("Unknown markup type (#{type})");
    end

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

#_validate_atom_render(rendered, atom_name) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/mobiledoc/renderers/0.3.rb', line 134

def _validate_atom_render(rendered, atom_name)
  return unless rendered

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

#find_atom(name) ⇒ Object



97
98
99
100
101
# File 'lib/mobiledoc/renderers/0.3.rb', line 97

def find_atom(name)
  atom = atoms.find { |a| a.name == name }

  atom || unknown_atom_handler
end

#render_card_section(type, index) ⇒ Object



41
42
43
44
45
# File 'lib/mobiledoc/renderers/0.3.rb', line 41

def render_card_section(type, index)
  card, name, payload = _find_card_by_index(index)

  _render_card_section(card, name, payload)
end

#render_markup_section(type, tag_name, markers, attributes = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/mobiledoc/renderers/0.3.rb', line 30

def render_markup_section(type, tag_name, markers, attributes = [])
  return unless (tag_name, MARKUP_SECTION_TYPE)

  element = create_element(tag_name)
  attributes.each_slice(2) do |prop_name, prop_value|
    set_attribute(element, prop_name, prop_value)
  end
  _render_markers_on_element(element, markers)
  element
end