Skip to content

Add substitute_with_block method to TextRun, that can take a block wtih access to MatchData with regex capture groups etc #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrochkind
Copy link

I needed to do a substitute where the match arg was a regex, and if it were gsub I'd be using a block to have access to capture groups in $1 $2 $3 etc. Because of the weird way variables $1 $2 $3 are handled in ruby and block scope, I couldn't provide a delegated block to give exactly the same API as ordinary gsub. My original idea was to do that, added on to existing #substitute.

But instead, had to provide a new/alternate #substitute_block method, with a block that actually gets a MatchData object as arg, and can access whatever it needs from there, including capture groups and match string.

…ih access to MatchData with regex capture groups etc

I needed to do a `substitute` where the match arg was a regex, and if it were gsub I'd be using a block to have access to capture groups in $1 $2 $3 etc. Because of the weird way variables $1 $2 $3 are handled in ruby and block scope, I couldn't provide a delegated block to give exactly the same API as ordinary gsub. My original idea was to do that, added on to existing #substitute.

But instead, had to provide a new/alternate #substitute_with_block method, with a block that actually gets a MatchData object as arg, and can access whatever it needs from there, including capture groups and match string.
@jrochkind jrochkind changed the title Add substitute_block method to TextRun, that can take a block wtih access to MatchData with regex capture groups etc Add substitute_with_block method to TextRun, that can take a block wtih access to MatchData with regex capture groups etc May 29, 2025
@jrochkind
Copy link
Author

jrochkind commented Jun 2, 2025

Hm, CI failure is weird and annoying.

bundle exec rake passes for me locally.

Any ideas what would be different on CI?

Failures:

  1) Docx::Document replacing contents should replace existing file within the document
     Failure/Error: expect(Zip::File.open(temp_file_path).read(entry_path)).to eq io.read

     Zip::Error:
       File /tmp/docx_gem[20](https://github.com/ruby-docx/docx/actions/runs/15328470377/job/43194224826?pr=161#step:4:21)250530-[21](https://github.com/ruby-docx/docx/actions/runs/15328470377/job/43194224826?pr=161#step:4:22)39-2luizi.docx not found
     # ./vendor/bundle/ruby/3.2.0/gems/rubyzip-2.4.1/lib/zip/file.rb:112:in `initialize'
     # ./vendor/bundle/ruby/3.2.0/gems/rubyzip-2.4.1/lib/zip/file.rb:129:in `new'
     # ./vendor/bundle/ruby/3.2.0/gems/rubyzip-2.4.1/lib/zip/file.rb:129:in `open'
     # ./spec/docx/document_spec.rb:523:in `block (4 levels) in <top (required)>'
     # ./spec/docx/document_spec.rb:5[22](https://github.com/ruby-docx/docx/actions/runs/15328470377/job/43194224826?pr=161#step:4:23):in `open'
     # ./spec/docx/document_spec.rb:522:in `block (3 levels) in <top (required)>'

Finished in 1.71 seconds (files took 0.5[23](https://github.com/ruby-docx/docx/actions/runs/15328470377/job/43194224826?pr=161#step:4:24)19 seconds to load)
136 examples, 1 failure

Failed examples:

rspec ./spec/docx/document_spec.rb:515 # Docx::Document replacing contents should replace existing file within the document

The failed test is NOT my new test, but an existing test... don't understand how this PR could cause it to break... is my new test somehow resulting in deletion of a file? But why wouldn't that reproduce locally?

describe 'replacing contents' do
let(:replacement_file_path) { @fixtures_path + '/replacement.png' }
let(:temp_file_path) { Tempfile.new(['docx_gem', '.docx']).path }
let(:entry_path) { 'word/media/image1.png' }
let(:doc) { Docx::Document.open(@fixtures_path + '/replacement.docx') }
it 'should replace existing file within the document' do
File.open replacement_file_path, 'rb' do |io|
doc.replace_entry entry_path, io.read
end
doc.save(temp_file_path)
File.open replacement_file_path, 'rb' do |io|
expect(Zip::File.open(temp_file_path).read(entry_path)).to eq io.read
end
end
after do
File.delete(temp_file_path) if File.exist?(temp_file_path)
end
end

Any advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants