Class: Woa::Energon::WordDocument

Inherits:
Document
  • Object
show all
Includes:
REXML
Defined in:
lib/energon/word_document.rb

Overview

This is a subclass of Document but it deals with Word templates instead of Text templates.

Everything is the same as Document (Except the input and the output). See Document for more details.

:include: rdoc-header

Constant Summary

Constants inherited from Document

Document::DefaultDelimiter, Document::DefaultNewLine

Instance Attribute Summary

Attributes inherited from Document

#delimiter, #new_line

Instance Method Summary collapse

Methods inherited from Document

#add_value, #add_values, #initialize, #valid?

Constructor Details

This class inherits a constructor from Woa::Energon::Document

Instance Method Details

#writeObject Also known as: close

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/energon/word_document.rb', line 16

def write()
  raise NoPlaceholderFound if @placeholders.empty?
  rows = {}
  @placeholders.each do |placeholder|
    data = Parser.merge(placeholder[:placeholder], @datas)
    element = placeholder[:element]
    delimiter = placeholder[:delimiter]
    if data.is_a?(Array)
      row = XPath.first(element, 'ancestor::w:tr')
      if row.nil?
        element.text = element.text.to_s.gsub("#{delimiter}#{placeholder[:placeholder]}#{delimiter}", data.shift.to_s)
        while !data.empty?
          t = Element.new('w:t')
          t.text = data.shift.to_s
          element.parent.insert_after(element, t)
          element.parent.insert_after(element, Element.new('w:br'))
          element = t
        end
      else
        rows[row] = [] if rows[row].nil?
        rows[row] << {:element => element, :data => data}
      end
    else
      element.text = element.text.to_s.gsub("#{delimiter}#{placeholder[:placeholder]}#{delimiter}", data.to_s)
    end
  end
  rows.each do |row, cells|
    continue = true
    while continue
      cells.each do |cell|
        size = cell[:data].size
        data = cell[:data].pop
        element = cell[:element]
        if data.nil?
          continue = false
          break
        end
        element.text = data
      end
      row.parent.insert_after(row, row.deep_clone) if continue          
    end
    row.parent.delete(row)
  end
  @documents.each {|document| @openxml.save(document) }
  @openxml.write
end