Class: PureDocx::DocArchive

Inherits:
Object
  • Object
show all
Defined in:
lib/puredocx/doc_archive.rb

Constant Summary collapse

HEADER_TEMPLATE_PATH =
'word/header1.xml'.freeze
'word/footer1.xml'.freeze
DOCUMENT_TEMPLATE_PATH =
'word/document.xml'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, rels) ⇒ DocArchive

Returns a new instance of DocArchive.



9
10
11
12
13
14
# File 'lib/puredocx/doc_archive.rb', line 9

def initialize(io, rels)
  @io = io
  @basic_rels  = rels[:basic_rels]
  @word_rels   = rels[:word_rels]
  @header_rels = rels[:header_rels]
end

Instance Attribute Details

#basic_relsObject (readonly)

Returns the value of attribute basic_rels.



7
8
9
# File 'lib/puredocx/doc_archive.rb', line 7

def basic_rels
  @basic_rels
end

#header_relsObject (readonly)

Returns the value of attribute header_rels.



7
8
9
# File 'lib/puredocx/doc_archive.rb', line 7

def header_rels
  @header_rels
end

#ioObject (readonly)

Returns the value of attribute io.



7
8
9
# File 'lib/puredocx/doc_archive.rb', line 7

def io
  @io
end

#word_relsObject (readonly)

Returns the value of attribute word_rels.



7
8
9
# File 'lib/puredocx/doc_archive.rb', line 7

def word_rels
  @word_rels
end

Class Method Details

.dir_pathObject



102
103
104
# File 'lib/puredocx/doc_archive.rb', line 102

def self.dir_path
  Pathname.new(__FILE__).expand_path.parent.dirname
end

.open(file_path, rels) ⇒ Object



16
17
18
19
20
21
# File 'lib/puredocx/doc_archive.rb', line 16

def self.open(file_path, rels)
  Zip::File.open(file_path, Zip::File::CREATE) do |zip_file|
    file = new(zip_file, rels)
    yield file
  end
end

.template_path(file_name) ⇒ Object



98
99
100
# File 'lib/puredocx/doc_archive.rb', line 98

def self.template_path(file_name)
  File.join(dir_path, 'template', file_name)
end

Instance Method Details

#add(file, path) ⇒ Object



23
24
25
# File 'lib/puredocx/doc_archive.rb', line 23

def add(file, path)
  io.add(file, path)
end

#add_rels!(file_name, rels, path = '') ⇒ Object



42
43
44
45
# File 'lib/puredocx/doc_archive.rb', line 42

def add_rels!(file_name, rels, path = '')
  generate_template_files!(rels, path)
  generate_files_with_rels_extension!(file_name, rels, path)
end

#colontitle_reference_xml(reference_name, file_name) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/puredocx/doc_archive.rb', line 84

def colontitle_reference_xml(reference_name, file_name)
  "    <w:\#{reference_name}\n    r:id=\"rId\#{word_rels.keys.index(file_name)}\"\n    w:type=\"default\"/>\n  HEREDOC\nend\n".gsub(/\s+/, ' ').strip

#document_colontitle!(content, content_path) ⇒ Object



78
79
80
81
82
# File 'lib/puredocx/doc_archive.rb', line 78

def document_colontitle!(content, content_path)
  content_xml = File.read(self.class.template_path(content_path))
  content_xml.gsub!('{CONTENT}', content || '')
  io.get_output_stream(content_path) { |os| os.write content_xml }
end

#document_content(content, header, pagination_position) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/puredocx/doc_archive.rb', line 67

def document_content(content, header, pagination_position)
  header_reference = colontitle_reference_xml('headerReference', 'header1.xml') unless header.empty?
  footer_reference = colontitle_reference_xml('footerReference', 'footer1.xml') if pagination_position

  File.read(self.class.template_path(DOCUMENT_TEMPLATE_PATH)).tap do |document_content|
    document_content.gsub!('{HEADER}',  header_reference || '')
    document_content.gsub!('{CONTENT}', content)
    document_content.gsub!('{FOOTER}',  footer_reference || '')
  end
end

#generate_files_with_rels_extension!(file_name, rels, path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puredocx/doc_archive.rb', line 54

def generate_files_with_rels_extension!(file_name, rels, path)
  io.get_output_stream("#{path}#{file_name}") do |os|
    os.write(
      "        <?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"yes\\\"?>\\n\n        <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n          \#{rels_content(rels)}\n        </Relationships>\n      HEREDOC\n    )\n  end\nend\n".gsub(/\s+/, ' ').strip

#generate_template_files!(rels, path) ⇒ Object



47
48
49
50
51
52
# File 'lib/puredocx/doc_archive.rb', line 47

def generate_template_files!(rels, path)
  rels.each do |target_path, (_, image_path)|
    pathfile = image_path || self.class.template_path(File.join(path, target_path))
    io.add("#{path}#{target_path}", pathfile)
  end
end

#rels_content(rels) ⇒ Object



92
93
94
95
96
# File 'lib/puredocx/doc_archive.rb', line 92

def rels_content(rels)
  rels.map.with_index do |(target_path, (target_type, _)), index|
    %(<Relationship Id="rId#{index}" Type="#{target_type}" Target="#{target_path}"/>)
  end.join
end

#save_document_content(content, header, pagination_position) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/puredocx/doc_archive.rb', line 27

def save_document_content(content, header, pagination_position)
  document_colontitle!(header,              HEADER_TEMPLATE_PATH)
  document_colontitle!(pagination_position, FOOTER_TEMPLATE_PATH)

  io.get_output_stream(DOCUMENT_TEMPLATE_PATH) do |os|
    os.write document_content(content, header, pagination_position)
  end
end

#save_relsObject



36
37
38
39
40
# File 'lib/puredocx/doc_archive.rb', line 36

def save_rels
  add_rels!('_rels/.rels',             basic_rels)
  add_rels!('_rels/document.xml.rels', word_rels,   'word/') unless word_rels.empty?
  add_rels!('_rels/header1.xml.rels',  header_rels, 'word/') unless header_rels.empty?
end