Skip to content

Commit 5f24e52

Browse files
authored
Merge pull request #3 from ruby-docx/master
Merge latest from upstream
2 parents b4a303e + 6889c47 commit 5f24e52

8 files changed

Lines changed: 70 additions & 17 deletions

File tree

.github/workflows/release.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@ on:
88
jobs:
99
build:
1010
name: Build a package
11-
1211
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: ruby/setup-ruby@v1
15+
with:
16+
ruby-version: 3.4
17+
bundler-cache: true
18+
- run: bundle exec rake build
1319

20+
release:
21+
name: Release
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: write
25+
id-token: write
26+
if: startsWith(github.ref, 'refs/tags/') # Run only when tagged like v1.0.1
1427
steps:
15-
- uses: actions/checkout@v3
28+
- uses: actions/checkout@v4
29+
- uses: ruby/setup-ruby@v1
30+
with:
31+
ruby-version: 3.4
32+
bundler-cache: true
33+
- id: package_name
34+
run: |
35+
echo "package_name=$(basename pkg/*.gem | tail -n1)" >> $GITHUB_OUTPUT
1636
17-
- uses: softprops/action-gh-release@v1
18-
if: startsWith(github.ref, 'refs/tags/') # Run only when tagged like v1.0.1
37+
- uses: softprops/action-gh-release@v2
1938
with:
20-
files: packages/${{steps.package_name.outputs.package_name}}.zip
39+
files: pkg/${{steps.package_name.outputs.package_name}}
2140
generate_release_notes: true
41+
- uses: rubygems/release-gem@v1

.github/workflows/ruby.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ jobs:
1717

1818
strategy:
1919
matrix:
20-
ruby: [2.7, "3.0", 3.1, 3.2]
20+
ruby:
21+
- 3.1
22+
- 3.2
23+
- 3.3
24+
- 3.4
25+
- head
2126

2227
steps:
2328
- uses: actions/checkout@v2

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ doc.bookmarks.each_pair do |bookmark_name, bookmark_object|
5252
end
5353
```
5454

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:
5656

5757
```ruby
5858
require 'docx'
@@ -130,6 +130,14 @@ doc.paragraphs.each do |p|
130130
end
131131
end
132132

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+
133141
# Save document to specified path
134142
doc.save('example-edited.docx')
135143
```
@@ -145,7 +153,7 @@ doc = Docx::Document.open('tables.docx')
145153
# Iterate over each table
146154
doc.tables.each do |table|
147155
last_row = table.rows.last
148-
156+
149157
# Copy last row and insert a new one before last row
150158
new_row = last_row.copy
151159
new_row.insert_before(last_row)
@@ -261,3 +269,4 @@ The following is a list of attributes and what they control within the style.
261269
* Default formatting of inserted elements to inherited values
262270
* Implement formattable elements.
263271
* Easier multi-line text insertion at a single bookmark (inserting paragraph nodes after the one containing the bookmark)
272+

docx.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
1414
s.required_ruby_version = '>= 2.7.0'
1515

1616
s.add_dependency 'nokogiri', '~> 1.13', '>= 1.13.0'
17-
s.add_dependency 'rubyzip', '~> 2.0'
17+
s.add_dependency 'rubyzip', '>= 2.0', "< 4"
1818

1919
s.add_development_dependency 'coveralls_reborn', '~> 0.21'
2020
s.add_development_dependency 'rake', '~> 13.0'

lib/docx/containers/text_run.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ def substitute(match, replacement)
5757
reset_text
5858
end
5959

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+
6073
def parse_formatting
6174
{
6275
italic: !@node.xpath('.//w:i').empty?,

lib/docx/document.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def initialize(path_or_io, options = {})
3030

3131
# if path-or_io is string && does not contain a null byte
3232
if (path_or_io.instance_of?(String) && !/\u0000/.match?(path_or_io))
33-
raise Errno::EIO.new('Invalid file format') if !File.extname(path_or_io).eql?('.docx')
3433
@zip = Zip::File.open(path_or_io)
3534
else
3635
@zip = Zip::File.open_buffer(path_or_io)

lib/docx/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Docx #:nodoc:
4-
VERSION = '0.8.0'
4+
VERSION = '0.10.0'
55
end

spec/docx/document_spec.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
end
2121

2222
context 'When reading a un-supported file' do
23-
it 'should throw file not supported error' do
24-
expect do
25-
Docx::Document.open(@fixtures_path + '/invalid_format.pdf')
26-
end.to raise_error(Errno::EIO, 'Input/output error - Invalid file format')
27-
end
28-
2923
it 'should throw file not found error' do
3024
invalid_path = @fixtures_path + '/invalid_file_path.docx'
3125
expect do
@@ -206,6 +200,19 @@
206200

207201
expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1same paragraph line 2yet the same paragraph line3 ')
208202
end
203+
204+
it "should replace placeholder in any line of paragraph using substitute_with_block" do
205+
expect(@doc.paragraphs[0].text).to eq('Page title')
206+
expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1_placeholder2_ line 2_placeholder3_ line3 ')
207+
208+
@doc.paragraphs[1].each_text_run do |text_run|
209+
text_run.substitute_with_block(/_placeholder(\d)_/) { |match_data|
210+
"_replacement_#{match_data[1]}"
211+
}
212+
end
213+
214+
expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1_replacement_2 line 2_replacement_3 line3 ')
215+
end
209216
end
210217

211218
describe 'read formatting' do

0 commit comments

Comments
 (0)