Class: Doct::Formats::Docx

Inherits:
OpenFormat show all
Defined in:
lib/doct/formats/docx.rb

Instance Method Summary collapse

Methods inherited from OpenFormat

#content_entries, #placeholders, #render

Methods inherited from Base

#initialize, #placeholders, #render

Constructor Details

This class inherits a constructor from Doct::Formats::Base

Instance Method Details

#content_entry?(entry) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/doct/formats/docx.rb', line 6

def content_entry?(entry)
  return true if entry == 'word/document.xml'
  entry =~ /word\/(header|footer)\d*.xml/
end

#entry_placeholders(entry, content) ⇒ Object



36
37
38
39
# File 'lib/doct/formats/docx.rb', line 36

def entry_placeholders(entry, content)
  xml = Nokogiri::XML(content)
  xml.xpath("//w:sdt//w:t").collect {|node| node.content.to_s}
end

#entry_render(entry, content, params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/doct/formats/docx.rb', line 11

def entry_render(entry, content, params)
  xml = Nokogiri::XML(content)
  orig_xml = xml.dup
  current_xml = xml
  sectpr_node = xml.xpath("w:document/w:body/w:sectPr").first
  if entry == 'word/document.xml'
    for page in params
      nodes = current_xml.xpath("w:document/w:body/*[name()!='w:sectPr']")
      nodes.xpath("//w:sdt//w:t").each do |node|
        node.content = page[node.content.to_sym].to_s
      end
      sectpr_node.before(nodes)
      if page != params.last
        sectpr_node.before("<w:p><w:r><w:br w:type='page'/></w:r></w:p>")
      end
      current_xml = orig_xml.dup
    end
  elsif not params.empty?
    xml.xpath("//w:sdt//w:t").each do |node|
      node.content = params.first[node.content.to_sym]
    end
  end
  xml.to_s
end