Class: Mobiledoc::HTMLRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/mobiledoc_html_renderer.rb,
lib/mobiledoc_html_renderer/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTMLRenderer

Returns a new instance of HTMLRenderer.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mobiledoc_html_renderer.rb', line 12

def initialize(options={})
  cards = options[:cards] || []
  validate_cards(cards)

  atoms = options[:atoms] || []
  validate_atoms(atoms)

  card_options = options[:card_options] || {}

  unknown_card_handler = options[:unknown_card_handler] || UnknownCard
  unknown_atom_handler = options[:unknown_atom_handler] || UnknownAtom

  self.state = {
    cards: cards,
    atoms: atoms,
    card_options: card_options,
    unknown_card_handler: unknown_card_handler,
    unknown_atom_handler: unknown_atom_handler
  }
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



10
11
12
# File 'lib/mobiledoc_html_renderer.rb', line 10

def state
  @state
end

Instance Method Details

#render(mobiledoc) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mobiledoc_html_renderer.rb', line 65

def render(mobiledoc)
  version = mobiledoc['version']

  case version
  when '0.2.0'
    Renderer_0_2.new(mobiledoc, state).render
  when '0.3.0', '0.3.1', '0.3.2', nil
    Renderer_0_3.new(mobiledoc, state).render
  else
    raise Mobiledoc::Error.new(%Q[Unexpected Mobiledoc version "#{version}"])
  end
end

#validate_atoms(atoms) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mobiledoc_html_renderer.rb', line 49

def validate_atoms(atoms)
  unless atoms.is_a?(Array)
    raise Mobiledoc::Error.new("`atoms` must be passed as an array")
  end

  atoms.each do |atom|
    unless atom.type == 'html'
      raise Mobiledoc::Error.new(%Q[Atom "#{atom.name}" must be of type "html", was "#{atom.type}"])
    end

    unless atom.respond_to?(:render)
      raise Mobiledoc::Error.new(%Q[Atom "#{atom.name}" must define \`render\`])
    end
  end
end

#validate_cards(cards) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mobiledoc_html_renderer.rb', line 33

def validate_cards(cards)
  unless cards.is_a?(Array)
    raise Mobiledoc::Error.new("`cards` must be passed as an array")
  end

  cards.each do |card|
    unless card.type == 'html'
      raise Mobiledoc::Error.new(%Q[Card "#{card.name}" must be of type "html", was "#{card.type}"])
    end

    unless card.respond_to?(:render)
      raise Mobiledoc::Error.new(%Q[Card "#{card.name}" must define \`render\`])
    end
  end
end