Class: Saxerator::Parser::Nokogiri::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/saxerator/parser/nokogiri.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, tag, block) ⇒ Document

Returns a new instance of Document.



27
28
29
30
31
32
# File 'lib/saxerator/parser/nokogiri.rb', line 27

def initialize(config, tag, block)
  @config = config
  @tag = tag
  @stack = []
  @block = block
end

Instance Attribute Details

#stackObject

Returns the value of attribute stack.



25
26
27
# File 'lib/saxerator/parser/nokogiri.rb', line 25

def stack
  @stack
end

Instance Method Details

#characters(string) ⇒ Object Also known as: cdata_block



49
50
51
# File 'lib/saxerator/parser/nokogiri.rb', line 49

def characters(string)
  stack.last.add_node(string) unless string.strip.length == 0 || stack.empty?
end

#end_element(name) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/saxerator/parser/nokogiri.rb', line 40

def end_element(name)
  if stack.size > 1
    last = stack.pop
    stack.last.add_node last
  elsif stack.size == 1
    @block.yield(stack.pop.to_hash)
  end
end

#start_element(name, attrs = []) ⇒ Object



34
35
36
37
38
# File 'lib/saxerator/parser/nokogiri.rb', line 34

def start_element(name, attrs = [])
  if stack.size > 0 || name == @tag
    stack.push XmlNode.new(@config, name, Hash[*attrs.flatten])
  end
end