Class: OpenxmlDocxTemplater::XmlReader

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

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ XmlReader

Returns a new instance of XmlReader.



5
6
7
# File 'lib/openxml_docx_templater/xml_reader.rb', line 5

def initialize(src)
  @src = src
end

Instance Method Details

#each_nodeObject



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

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)
    end

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

#node_type(text) ⇒ Object



24
25
26
27
# File 'lib/openxml_docx_templater/xml_reader.rb', line 24

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