Class: OpenXml::TemplateDocument

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateDocument

Returns a new instance of TemplateDocument.



8
9
10
11
12
13
# File 'lib/open_xml/template_document.rb', line 8

def initialize(options)
  @template_path = options.fetch(:path)
  @parts = {}

  read_files
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



6
7
8
# File 'lib/open_xml/template_document.rb', line 6

def parts
  @parts
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



6
7
8
# File 'lib/open_xml/template_document.rb', line 6

def template_path
  @template_path
end

Instance Method Details

#process(data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/open_xml/template_document.rb', line 24

def process(data)
  @parts = @parts_cache.clone
  register_type 'message/rfc822', 'mht'

  doc = Nokogiri::XML(parts['word/document.xml'])
  doc.xpath('//w:t').each do |node|
    data.each do |key, value|

      if node.content[/#{key}/]
        process_plain_text(node, key, value, doc) unless value[:html]
        process_html(node, key, value, doc) if value[:html]
      end

    end
  end

  parts['word/document.xml'] = flatten_xml doc
end

#to_zip_bufferObject



15
16
17
18
19
20
21
22
# File 'lib/open_xml/template_document.rb', line 15

def to_zip_buffer
  Zip::OutputStream.write_buffer do |w|
    parts.each do |key, value|
      w.put_next_entry key
      w.write value
    end
  end
end