docx icon indicating copy to clipboard operation
docx copied to clipboard

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

Open jrochkind opened this issue 8 months ago • 1 comments

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.

jrochkind avatar Apr 09 '25 17:04 jrochkind

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?

https://github.com/ruby-docx/docx/blob/1a51b4eb05937fd46adbec427249ebe70f05f9a6/spec/docx/document_spec.rb#L515-L536

Any advice?

jrochkind avatar Jun 02 '25 15:06 jrochkind

The CI failure sometimes happens only on GitHub Actions. I reran the tests, and all of them passed.

satoryu avatar Sep 11 '25 06:09 satoryu

thank you!

jrochkind avatar Sep 11 '25 13:09 jrochkind