docx 0.9.1 → 0.10.1

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: f170295a0362d6fcbbe6313ddd9f3ccf50379eb220ab52b26caee581786471a4
4
- data.tar.gz: f6050fc0a2959392184da37012e0a2efcb5290f8bb1d015510e9dd52abf71ee7
3
+ metadata.gz: 00fd317c3d0d2f4ee4aafddd30ff41f29830f23c2e5f913c114cbc95b2bb58d1
4
+ data.tar.gz: 0ad6844aeb26d84f5275f86c43bb592f821be2541e797fa1318416dd20e77bf1
5
5
  SHA512:
6
- metadata.gz: de8c52dced6b5d95b7fa3d34ab8c124ce5e1539fc5874fac1b5a71d31c3f7c46af7b27cc53bd89960f122a01a5a36372825f91acd8c1d0cae715f7380a2b7625
7
- data.tar.gz: bb39bab159ac4f5ccec77ddd3c64bd9a1a8863f80a948c7d963cfde2e6904ecc112f2c4c79e080dbfc37b2580ecc37ab4249c0d303f945a02ccae432a33671fc
6
+ metadata.gz: 32e693b1114347678865a0b5f409a745e1f4a3a195ef70d2ab6cc255022abaad79b3bed9dd66426746b2b207ab176af19178207c4e2e6af790cd8213575ee605
7
+ data.tar.gz: 438dbbb6ca717e985c80f0ff4eca824c44fd1d2c94832b40121fb4423e6bf59389b03dce6d66b2f6bbbf9715e68df62c9db3db8e5a4cc60772b7cd1cdd95983d
data/README.md CHANGED
@@ -52,7 +52,7 @@ doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
52
52
  end
53
53
  ```
54
54
 
55
- Don't have a local file but a buffer? Docx handles those to:
55
+ Don't have a local file but a buffer? Docx handles those too:
56
56
 
57
57
  ```ruby
58
58
  require 'docx'
@@ -130,6 +130,14 @@ doc.paragraphs.each do |p|
130
130
  end
131
131
  end
132
132
 
133
+ # Substitute text with access to captures, note block arg is a MatchData, a bit
134
+ # different than String.gsub. https://ruby-doc.org/3.3.7/MatchData.html
135
+ doc.paragraphs.each do |p|
136
+ p.each_text_run do |tr|
137
+ tr.substitute_with_block(/total: (\d+)/) { |match_data| "total: #{match_data[1].to_i * 10}" }
138
+ end
139
+ end
140
+
133
141
  # Save document to specified path
134
142
  doc.save('example-edited.docx')
135
143
  ```
@@ -145,7 +153,7 @@ doc = Docx::Document.open('tables.docx')
145
153
  # Iterate over each table
146
154
  doc.tables.each do |table|
147
155
  last_row = table.rows.last
148
-
156
+
149
157
  # Copy last row and insert a new one before last row
150
158
  new_row = last_row.copy
151
159
  new_row.insert_before(last_row)
@@ -261,3 +269,4 @@ The following is a list of attributes and what they control within the style.
261
269
  * Default formatting of inserted elements to inherited values
262
270
  * Implement formattable elements.
263
271
  * Easier multi-line text insertion at a single bookmark (inserting paragraph nodes after the one containing the bookmark)
272
+
@@ -57,6 +57,19 @@ module Docx
57
57
  reset_text
58
58
  end
59
59
 
60
+ # Weird things with how $1/$2 in regex blocks are handled means we can't just delegate
61
+ # block to gsub to get block, we have to do it this way, with a block that gets a MatchData,
62
+ # from which captures and other match data can be retrieved.
63
+ # https://ruby-doc.org/3.3.7/MatchData.html
64
+ def substitute_with_block(match, &block)
65
+ @text_nodes.each do |text_node|
66
+ text_node.content = text_node.content.gsub(match) { |_unused_matched_string|
67
+ block.call(Regexp.last_match)
68
+ }
69
+ end
70
+ reset_text
71
+ end
72
+
60
73
  def parse_formatting
61
74
  {
62
75
  italic: !@node.xpath('.//w:i').empty?,
data/lib/docx/document.rb CHANGED
@@ -128,40 +128,44 @@ module Docx
128
128
  # call-seq:
129
129
  # save(filepath) => void
130
130
  def save(path)
131
- update
132
- Zip::OutputStream.open(path) do |out|
133
- zip.each do |entry|
134
- next unless entry.file?
131
+ with_zip64_disabled do
132
+ update
133
+ Zip::OutputStream.open(path) do |out|
134
+ zip.each do |entry|
135
+ next unless entry.file?
135
136
 
136
- out.put_next_entry(entry.name)
137
- value = @replace[entry.name] || zip.read(entry.name)
137
+ out.put_next_entry(entry.name)
138
+ value = @replace[entry.name] || zip.read(entry.name)
138
139
 
139
- out.write(value)
140
- end
140
+ out.write(value)
141
+ end
141
142
 
143
+ end
144
+ zip.close
142
145
  end
143
- zip.close
144
146
  end
145
147
 
146
148
  # Output entire document as a StringIO object
147
149
  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))
150
+ with_zip64_disabled do
151
+ update
152
+ stream = Zip::OutputStream.write_buffer do |out|
153
+ zip.each do |entry|
154
+ next unless entry.file?
155
+
156
+ out.put_next_entry(entry.name)
157
+
158
+ if @replace[entry.name]
159
+ out.write(@replace[entry.name])
160
+ else
161
+ out.write(zip.read(entry.name))
162
+ end
159
163
  end
160
164
  end
161
- end
162
165
 
163
- stream.rewind
164
- stream
166
+ stream.rewind
167
+ stream
168
+ end
165
169
  end
166
170
 
167
171
  alias text to_s
@@ -184,6 +188,18 @@ module Docx
184
188
 
185
189
  private
186
190
 
191
+ # rubyzip 3.x enables ZIP64 by default, which breaks readers (e.g. pandoc)
192
+ # that don't support it for small files (issue #168). Disable ZIP64 only
193
+ # while writing, and restore the previous global value afterwards so we
194
+ # don't affect other rubyzip users in the consuming application.
195
+ def with_zip64_disabled
196
+ previous = Zip.write_zip64_support
197
+ Zip.write_zip64_support = false
198
+ yield
199
+ ensure
200
+ Zip.write_zip64_support = previous
201
+ end
202
+
187
203
  def load_styles
188
204
  @styles_xml = @zip.read('word/styles.xml')
189
205
  @styles = Nokogiri::XML(@styles_xml)
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.9.1'
4
+ VERSION = '0.10.1'
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.9.1
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt
@@ -11,7 +11,7 @@ authors:
11
11
  - Sebastian Wittenkamp
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2025-05-03 00:00:00.000000000 Z
14
+ date: 1980-01-02 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: nokogiri
@@ -37,16 +37,22 @@ dependencies:
37
37
  name: rubyzip
38
38
  requirement: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - "~>"
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '2.0'
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '4'
43
46
  type: :runtime
44
47
  prerelease: false
45
48
  version_requirements: !ruby/object:Gem::Requirement
46
49
  requirements:
47
- - - "~>"
50
+ - - ">="
48
51
  - !ruby/object:Gem::Version
49
52
  version: '2.0'
53
+ - - "<"
54
+ - !ruby/object:Gem::Version
55
+ version: '4'
50
56
  - !ruby/object:Gem::Dependency
51
57
  name: coveralls_reborn
52
58
  requirement: !ruby/object:Gem::Requirement
@@ -138,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
144
  - !ruby/object:Gem::Version
139
145
  version: '0'
140
146
  requirements: []
141
- rubygems_version: 3.6.2
147
+ rubygems_version: 3.6.9
142
148
  specification_version: 4
143
149
  summary: a ruby library/gem for interacting with .docx files
144
150
  test_files: []