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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ doc.paragraphs.each do |p|
end
end

# Substitute text with access to captures, note block arg is a MatchData, a bit
# different than String.gsub. https://ruby-doc.org/3.3.7/MatchData.html
doc.paragraphs.each do |p|
p.each_text_run do |tr|
tr.substitute_with_block(/total: (\d+)/) { |match_data| "total: #{match_data[1].to_i * 10}" }
end
end

# Save document to specified path
doc.save('example-edited.docx')
```
Expand All @@ -145,7 +153,7 @@ doc = Docx::Document.open('tables.docx')
# Iterate over each table
doc.tables.each do |table|
last_row = table.rows.last

# Copy last row and insert a new one before last row
new_row = last_row.copy
new_row.insert_before(last_row)
Expand Down
13 changes: 13 additions & 0 deletions lib/docx/containers/text_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ def substitute(match, replacement)
reset_text
end

# Weird things with how $1/$2 in regex blocks are handled means we can't just delegate
# block to gsub to get block, we have to do it this way, with a block that gets a MatchData,
# from which captures and other match data can be retrieved.
# https://ruby-doc.org/3.3.7/MatchData.html
def substitute_with_block(match, &block)
@text_nodes.each do |text_node|
text_node.content = text_node.content.gsub(match) { |_unused_matched_string|
block.call(Regexp.last_match)
}
end
reset_text
end

def parse_formatting
{
italic: [email protected]('.//w:i').empty?,
Expand Down
13 changes: 13 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@

expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1same paragraph line 2yet the same paragraph line3 ')
end

it "should replace placeholder in any line of paragraph using substitute_with_block" do
expect(@doc.paragraphs[0].text).to eq('Page title')
expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1_placeholder2_ line 2_placeholder3_ line3 ')

@doc.paragraphs[1].each_text_run do |text_run|
text_run.substitute_with_block(/_placeholder(\d)_/) { |match_data|
"_replacement_#{match_data[1]}"
}
end

expect(@doc.paragraphs[1].text).to eq('Multi-line paragraph line 1_replacement_2 line 2_replacement_3 line3 ')
end
end

describe 'read formatting' do
Expand Down