docx 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fc798e71a79265389a2dcf22d0ecff1268a920f92463c23b9ccbce94517b7be
4
- data.tar.gz: 1b21fef113ce8a84a7aeaf97b05139822763a4ac96835efd59971b52fb34ce98
3
+ metadata.gz: bba1301a8a8e00e7fd9ee748af89280af2348120628c9a3d806dbcef78b17bb4
4
+ data.tar.gz: 699e0f10746445987737d090a0cf11886251f98385224dd0c9ec543b59676735
5
5
  SHA512:
6
- metadata.gz: eab6dae181b61b9b7103460a4e5d582ef534b36f2f09cbfaba9c5ef16d880b9bca6a97d76e7c671fbb24a897189e6b1861a6f4874e589075d781b3a660e796c9
7
- data.tar.gz: ca39d10248be1d18d173293fc8a918fe86e615cbd6fbdb5340a532792d9ef478ac563fb3ef61a6fdd4e1542a9d7258015e5bc34ecd69c86e6edb39ce422ec636
6
+ metadata.gz: 52fd4f0aeafbf5a9ce0f5c3b4b75b8235fa291806a95dcb9455c993fe322b949aea7780a56a92ce78c2d51248eab4c6366e3511eda11991a739f53cd052460b7
7
+ data.tar.gz: 598be461460ae0aee15f4f13028850497df9a9bb30d47594f3ce678725095a8dd3b72ebc5cc1bb32825f10efef8c4cf27a7c54736c2875b344e0374af8daa92b
@@ -44,7 +44,7 @@ module Docx
44
44
 
45
45
  # Return paragraph as a <p></p> HTML fragment with formatting based on properties.
46
46
  def to_html
47
- html = ''
47
+ html = +''
48
48
  text_runs.each do |text_run|
49
49
  html << text_run.to_html
50
50
  end
data/lib/docx/document.rb CHANGED
@@ -22,7 +22,7 @@ module Docx
22
22
  class Document
23
23
  include Docx::SimpleInspect
24
24
 
25
- attr_reader :xml, :doc, :zip, :styles
25
+ attr_reader :xml, :doc, :zip, :styles, :headers, :footers
26
26
 
27
27
  def initialize(path_or_io, options = {})
28
28
  @replace = {}
@@ -40,6 +40,8 @@ module Docx
40
40
  @document_xml = document.get_input_stream.read
41
41
  @doc = Nokogiri::XML(@document_xml)
42
42
  load_styles
43
+ load_headers
44
+ load_footers
43
45
  yield(self) if block_given?
44
46
  ensure
45
47
  @zip.close unless @zip.nil?
@@ -128,40 +130,44 @@ module Docx
128
130
  # call-seq:
129
131
  # save(filepath) => void
130
132
  def save(path)
131
- update
132
- Zip::OutputStream.open(path) do |out|
133
- zip.each do |entry|
134
- next unless entry.file?
133
+ with_zip64_disabled do
134
+ update
135
+ Zip::OutputStream.open(path) do |out|
136
+ zip.each do |entry|
137
+ next unless entry.file?
135
138
 
136
- out.put_next_entry(entry.name)
137
- value = @replace[entry.name] || zip.read(entry.name)
139
+ out.put_next_entry(entry.name)
140
+ value = @replace[entry.name] || zip.read(entry.name)
138
141
 
139
- out.write(value)
140
- end
142
+ out.write(value)
143
+ end
141
144
 
145
+ end
146
+ zip.close
142
147
  end
143
- zip.close
144
148
  end
145
149
 
146
150
  # Output entire document as a StringIO object
147
151
  def stream
148
- update
149
- stream = Zip::OutputStream.write_buffer do |out|
150
- zip.each do |entry|
151
- next unless entry.file?
152
-
153
- out.put_next_entry(entry.name)
154
-
155
- if @replace[entry.name]
156
- out.write(@replace[entry.name])
157
- else
158
- out.write(zip.read(entry.name))
152
+ with_zip64_disabled do
153
+ update
154
+ stream = Zip::OutputStream.write_buffer do |out|
155
+ zip.each do |entry|
156
+ next unless entry.file?
157
+
158
+ out.put_next_entry(entry.name)
159
+
160
+ if @replace[entry.name]
161
+ out.write(@replace[entry.name])
162
+ else
163
+ out.write(zip.read(entry.name))
164
+ end
159
165
  end
160
166
  end
161
- end
162
167
 
163
- stream.rewind
164
- stream
168
+ stream.rewind
169
+ stream
170
+ end
165
171
  end
166
172
 
167
173
  alias text to_s
@@ -184,6 +190,36 @@ module Docx
184
190
 
185
191
  private
186
192
 
193
+ # rubyzip 3.x enables ZIP64 by default, which breaks readers (e.g. pandoc)
194
+ # that don't support it for small files (issue #168). Disable ZIP64 only
195
+ # while writing, and restore the previous global value afterwards so we
196
+ # don't affect other rubyzip users in the consuming application.
197
+ def with_zip64_disabled
198
+ previous = Zip.write_zip64_support
199
+ Zip.write_zip64_support = false
200
+ yield
201
+ ensure
202
+ Zip.write_zip64_support = previous
203
+ end
204
+
205
+ def load_headers
206
+ header_files = @zip.glob("word/header*.xml").map{|h| h.name}
207
+ filename_and_contents_pairs = header_files.map do |file|
208
+ simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
209
+ [simple_file_name, Nokogiri::XML(@zip.read(file))]
210
+ end
211
+ @headers = Hash[filename_and_contents_pairs]
212
+ end
213
+
214
+ def load_footers
215
+ footer_files = @zip.glob("word/footer*.xml").map{|h| h.name}
216
+ filename_and_contents_pairs = footer_files.map do |file|
217
+ simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
218
+ [simple_file_name, Nokogiri::XML(@zip.read(file))]
219
+ end
220
+ @footers = Hash[filename_and_contents_pairs]
221
+ end
222
+
187
223
  def load_styles
188
224
  @styles_xml = @zip.read('word/styles.xml')
189
225
  @styles = Nokogiri::XML(@styles_xml)
@@ -209,6 +245,12 @@ module Docx
209
245
  def update
210
246
  replace_entry 'word/document.xml', doc.serialize(save_with: 0)
211
247
  replace_entry 'word/styles.xml', styles_configuration.serialize(save_with: 0)
248
+ headers.each do |name, content|
249
+ replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
250
+ end
251
+ footers.each do |name, content|
252
+ replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
253
+ end
212
254
  end
213
255
 
214
256
  # generate Elements::Containers::Paragraph from paragraph XML node
data/lib/docx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Docx #:nodoc:
4
- VERSION = '0.10.0'
4
+ VERSION = '0.11.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt