docx 0.13.1 → 0.13.2

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: d639e88e5950b94157fc4f81b14f0f8bf355abb4fdedd24b7f40dc00eafb72d7
4
- data.tar.gz: e64400d25d919b4cd51f4b876d7f35c3433841523055866dff2c300f7c15aca7
3
+ metadata.gz: db1fd167dc6ddfecc81518f27cd78fc203d74738ffe52b2877251b2ed291c297
4
+ data.tar.gz: 7c2f230a44f18ceffffcbbfa55d212ebdd2a58651823aa78d303cc64493aa461
5
5
  SHA512:
6
- metadata.gz: d2ad2931111374349dd03ca04bef655a9eaa797b84d688198ebeac84cdf93af147d4f8fd5a561c4e506d7f823c9b22884c698a315d7e2bb505357c7c6da9316a
7
- data.tar.gz: 25cb0bc5f3b67b4af1874b902fbdfe3061205cee0c8b288187924b80cb50a6bd5a7076ea497be702a07241841da146055b3be0dbffeb90c20922f851ad31588c
6
+ metadata.gz: 6e1af62546207b59b6b2bcd193c83aaafc11258dcb50d7c52bcd2251b77552704b39d5667a709cdc9fdd005c37e00e3530acd9fb88df565ba10db361d6032d98
7
+ data.tar.gz: 7d753f33adc0795e12cea763d32e01d4cf76777d1cf7f5c8d815991efccfd0ede2b513a7a99c5558824e972e17ffd86460f661d87f7003dd916cca2cdbc06498
@@ -70,9 +70,13 @@ module Docx
70
70
  # such as "{{fi", "rst_na", "me}}"). The per-run TextRun#substitute cannot
71
71
  # match those, but this can, because it joins the runs first.
72
72
  #
73
- # The matched region is collapsed into the first run it touches, so that
74
- # run's formatting is kept while the other spanned runs are emptied; runs
75
- # outside the match are left untouched.
73
+ # The substitution works on the paragraph's w:t nodes, so it also handles
74
+ # runs holding several w:t (LibreOffice writes "text<w:br/>text" in one
75
+ # run) and placeholders that start inside a hyperlink. The replacement
76
+ # goes into the w:t holding the first matched character, so that run's
77
+ # formatting is used; the rest of the match is cut from the following
78
+ # w:t, and line breaks and tabs inside the match are dropped. Text,
79
+ # breaks and tabs outside the match stay where they are.
76
80
  #
77
81
  # +pattern+ may be a String or a Regexp; +replacement+ follows String#sub
78
82
  # semantics, so capture-group backreferences (e.g. '\1') work with a Regexp.
@@ -81,20 +85,19 @@ module Docx
81
85
  # paragraph.substitute('{{first_name}}', 'Jane') # => "Hello Jane!"
82
86
  # paragraph.substitute(/\{\{(\w+)\}\}/, 'value of \1')
83
87
  #
84
- # See https://github.com/ruby-docx/docx/issues/147
88
+ # See https://github.com/ruby-docx/docx/issues/147 and #187
85
89
  def substitute(pattern, replacement)
86
90
  search_from = 0
87
91
  loop do
88
- runs = text_runs
89
- break if runs.empty?
92
+ texts = @node.xpath(TEXT_XPATH).map { |t_node| Elements::Text.new(t_node) }
90
93
 
91
94
  offsets = []
92
95
  cursor = 0
93
- runs.each do |run|
96
+ texts.each do |text|
94
97
  offsets << cursor
95
- cursor += run.text.length
98
+ cursor += text.content.length
96
99
  end
97
- full_text = runs.map(&:text).join
100
+ full_text = texts.map(&:content).join
98
101
 
99
102
  match = full_text.match(pattern, search_from)
100
103
  break unless match
@@ -102,15 +105,21 @@ module Docx
102
105
 
103
106
  match_start = match.begin(0)
104
107
  match_end = match.end(0) # exclusive
105
- first = offsets.rindex { |offset| offset <= match_start }
106
- last = offsets.rindex { |offset| offset < match_end }
107
-
108
- combined = runs[first..last].map(&:text).join
109
- local_start = match_start - offsets[first]
110
- local_end = match_end - offsets[first]
111
- replaced = combined[local_start...local_end].sub(pattern, replacement)
112
- runs[first].text = combined[0...local_start] + replaced + combined[local_end..-1]
113
- ((first + 1)..last).each { |index| runs[index].text = '' }
108
+ # the w:t holding the first and the last matched character
109
+ first = texts.each_index.find { |i| offsets[i] + texts[i].content.length > match_start }
110
+ last = texts.each_index.find { |i| offsets[i] + texts[i].content.length >= match_end }
111
+
112
+ replaced = match[0].sub(pattern, replacement)
113
+ head = texts[first].content[0...(match_start - offsets[first])]
114
+ tail = texts[last].content[(match_end - offsets[last])..]
115
+ if first == last
116
+ texts[first].content = head + replaced + tail
117
+ else
118
+ texts[first].content = head + replaced
119
+ texts[(first + 1)...last].each { |text| text.content = '' }
120
+ texts[last].content = tail
121
+ remove_separators_between(texts[first].node, texts[last].node)
122
+ end
114
123
 
115
124
  # advance past the inserted replacement so it is not re-matched
116
125
  search_from = match_start + replaced.length
@@ -165,6 +174,22 @@ module Docx
165
174
 
166
175
  private
167
176
 
177
+ # The w:t read by #text (those of the runs and hyperlink runs), in
178
+ # document order.
179
+ TEXT_XPATH = 'w:r/w:t|w:hyperlink/w:r/w:t'
180
+ # Line breaks and tabs that sit between those w:t.
181
+ SEPARATOR_XPATH = %w[br tab cr].map { |tag| "w:r/w:#{tag}|w:hyperlink/w:r/w:#{tag}" }.join('|')
182
+ private_constant :TEXT_XPATH, :SEPARATOR_XPATH
183
+
184
+ # Remove the line breaks and tabs lying between two w:t of a match.
185
+ def remove_separators_between(first_t, last_t)
186
+ separators = @node.xpath(SEPARATOR_XPATH).to_a
187
+ nodes = @node.xpath("#{TEXT_XPATH}|#{SEPARATOR_XPATH}").to_a
188
+ from = nodes.index(first_t)
189
+ to = nodes.index(last_t)
190
+ nodes[(from + 1)...to].each { |node| node.remove if separators.include?(node) }
191
+ end
192
+
168
193
  def style_property
169
194
  properties&.at_xpath('w:pStyle') || properties&.add_child('<w:pStyle/>').first
170
195
  end
@@ -13,6 +13,8 @@ module Docx
13
13
 
14
14
  def content=(args)
15
15
  @node.content = args
16
+ # Without xml:space="preserve", Word drops leading and trailing spaces.
17
+ @node['xml:space'] = 'preserve' if args.to_s.match?(/\A\s|\s\z/)
16
18
  end
17
19
 
18
20
  def initialize(node)
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.13.1'
4
+ VERSION = '0.13.2'
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.13.1
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Hunt