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 +4 -4
- data/lib/docx/containers/paragraph.rb +1 -1
- data/lib/docx/document.rb +66 -24
- data/lib/docx/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bba1301a8a8e00e7fd9ee748af89280af2348120628c9a3d806dbcef78b17bb4
|
|
4
|
+
data.tar.gz: 699e0f10746445987737d090a0cf11886251f98385224dd0c9ec543b59676735
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 52fd4f0aeafbf5a9ce0f5c3b4b75b8235fa291806a95dcb9455c993fe322b949aea7780a56a92ce78c2d51248eab4c6366e3511eda11991a739f53cd052460b7
|
|
7
|
+
data.tar.gz: 598be461460ae0aee15f4f13028850497df9a9bb30d47594f3ce678725095a8dd3b72ebc5cc1bb32825f10efef8c4cf27a7c54736c2875b344e0374af8daa92b
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
137
|
-
|
|
139
|
+
out.put_next_entry(entry.name)
|
|
140
|
+
value = @replace[entry.name] || zip.read(entry.name)
|
|
138
141
|
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
164
|
-
|
|
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