Class: Thermal::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/thermal/parser.rb', line 7

def initialize(device)
  @device = device
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



5
6
7
# File 'lib/thermal/parser.rb', line 5

def device
  @device
end

Instance Method Details

#process(str) ⇒ Object



11
12
13
14
# File 'lib/thermal/parser.rb', line 11

def process(str)
  fragment = Nokogiri::HTML.fragment(str).children
  traverse(fragment)
end

#traverse(fragment) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/thermal/parser.rb', line 16

def traverse(fragment)
  fragment.map do |node|
    output = ""
    if node.class == Nokogiri::XML::Text
      output << node.content.gsub("\n","")
    elsif node.class == Nokogiri::XML::Element
      output << @device.startCode(node.name)
      output << traverse(node.children)
      output << @device.endCode(node.name)
    end
    output
  end.join('')
end