Class: Serenity::XmlReader

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

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ XmlReader

Returns a new instance of XmlReader.



4
5
6
# File 'lib/serenity/serenity/xml_reader.rb', line 4

def initialize src
  @src = src.force_encoding("UTF-8")
end

Instance Method Details

#each_nodeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/serenity/serenity/xml_reader.rb', line 8

def each_node
  last_match_pos = 0

  @src.scan(/<.*?>/) do |node|
    m = Regexp.last_match
    if m.begin(0) > last_match_pos
      text = @src[last_match_pos...m.begin(0)]
      yield text, node_type(text) if text.gsub(/\s+/, '') != ''
    end

    last_match_pos = m.end(0)
    yield node, NodeType::TAG
  end
end

#node_type(text) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/serenity/serenity/xml_reader.rb', line 23

def node_type text
  if text =~ /\s*\{%[^=#].+?%\}\s*/
    NodeType::CONTROL
  else
    NodeType::TEMPLATE
  end
end