Class: Smck::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/smck.rb

Overview

represents a Smck document

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



27
28
29
30
31
32
33
34
# File 'lib/smck.rb', line 27

def initialize
  @doc = Nokogiri::HTML5::Document.parse <<~'END HTML'
    <!DOCTYPE html>
  END HTML
  @html = @doc.root
  @head, = @html > 'head'
  @body, = @html > 'body'
end

Class Method Details

.render(data) ⇒ Object



76
77
78
79
80
# File 'lib/smck.rb', line 76

def self.render(data)
  doc = new
  doc.render data
  doc.to_html
end

Instance Method Details

#create_elementObject



36
37
38
# File 'lib/smck.rb', line 36

def create_element(...)
  @doc.create_element(...)
end

#create_text_nodeObject



40
41
42
# File 'lib/smck.rb', line 40

def create_text_node(...)
  @doc.create_text_node(...)
end

#render(data) ⇒ Object

render YAML data to the document’s body



45
46
47
# File 'lib/smck.rb', line 45

def render(data)
  render_item data, @body
end

#render_component(name, value, parent) ⇒ Object

Raises:



66
67
68
69
70
# File 'lib/smck.rb', line 66

def render_component(name, value, parent)
  raise Error, "unknown component #{name}" unless Components.instance_methods.include? name

  Components.instance_method(name).bind(self).call(value, parent)
end

#render_item(data, parent) ⇒ Object

render YAML data as new children of parent



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

def render_item(data, parent)
  case data
  in String
    render_component :str, data, parent
  in Array
    data.each do |child|
      render_item(child, parent)
    end
  in Smck::TaggedValue[tag:, value:]
    component = tag.delete_prefix('!').to_sym
    render_component component, value, parent
  else
    raise Error, "unsupported value type #{data.class}"
  end
end

#to_htmlObject



72
73
74
# File 'lib/smck.rb', line 72

def to_html
  @doc.to_html
end