From c3b999663d8da7a1bf0535d057adda4e0dc0b959 Mon Sep 17 00:00:00 2001 From: Marek Pietrucha Date: Tue, 16 Jun 2015 09:37:19 +0200 Subject: [PATCH 1/4] Changes to be committed: modified: lib/docx_replace.rb modified: lib/docx_replace/version.rb By default method `matches` return the exactly same output as `unique_matches`. Now matches returns a hash of matched values with counted repetitions: { "matched_value_1": 1, "matched_value_2": 4 } --- lib/docx_replace.rb | 10 +++++++--- lib/docx_replace/version.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 42368bb..f9a629e 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -23,12 +23,16 @@ def replace(pattern, replacement, multiple_occurrences=false) end end - def matches(pattern) - @document_content.scan(pattern).map{|match| match.first} + def matches(pattern, unique = false) + if unique + @document_content.scan(pattern).map{|match| match.first} + else + @document_content.scan(pattern).flatten.inject(Hash.new(0)) { |hash, key| hash[key] += 1; hash } + end end def unique_matches(pattern) - matches(pattern) + matches(pattern, true) end alias_method :uniq_matches, :unique_matches diff --git a/lib/docx_replace/version.rb b/lib/docx_replace/version.rb index 992ad3c..8f1a022 100644 --- a/lib/docx_replace/version.rb +++ b/lib/docx_replace/version.rb @@ -1,3 +1,3 @@ module DocxReplace - VERSION = "1.0.3" + VERSION = "1.0.4" end From 8a2bf21a542bb12ff5fcc67d2a212700cc2241ed Mon Sep 17 00:00:00 2001 From: Marek Pietrucha Date: Wed, 19 Jul 2017 11:32:27 +0200 Subject: [PATCH 2/4] enabled replacing variables also in headers and footers --- lib/docx_replace.rb | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index f9a629e..31a1949 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -6,7 +6,9 @@ module DocxReplace class Doc - attr_reader :document_content + DOCUMENT_FILE_PATH = 'word/document.xml' + + attr_accessor :document_contents def initialize(path, temp_dir=nil) @zip_file = Zip::File.new(path) @@ -17,17 +19,21 @@ def initialize(path, temp_dir=nil) def replace(pattern, replacement, multiple_occurrences=false) replace = replacement.to_s if multiple_occurrences - @document_content.force_encoding("UTF-8").gsub!(pattern, replace) + @document_contents.each do |content| + content.force_encoding("UTF-8").gsub!(pattern, replace) + end else - @document_content.force_encoding("UTF-8").sub!(pattern, replace) + @document_contents.each do |content| + content.force_encoding("UTF-8").sub!(pattern, replace) + end end end def matches(pattern, unique = false) if unique - @document_content.scan(pattern).map{|match| match.first} + @document_contents[DOCUMENT_FILE_PATH].scan(pattern).map{|match| match.first} else - @document_content.scan(pattern).flatten.inject(Hash.new(0)) { |hash, key| hash[key] += 1; hash } + @document_contents[DOCUMENT_FILE_PATH].scan(pattern).flatten.inject(Hash.new(0)) { |hash, key| hash[key] += 1; hash } end end @@ -43,10 +49,16 @@ def commit(new_path=nil) end private - DOCUMENT_FILE_PATH = 'word/document.xml' def read_docx_file - @document_content = @zip_file.read(DOCUMENT_FILE_PATH) + @document_contents = {} + + @zip_file.entries.each do |entry| + if entry.name.match? /^word\/(document|header[0-9]+|footer[0-9]+).xml/ + @document_contents[entry.name] = @zip_file.read(entry.name) + end + end + end def write_back_to_file(new_path=nil) @@ -57,14 +69,16 @@ def write_back_to_file(new_path=nil) end Zip::OutputStream.open(temp_file.path) do |zos| @zip_file.entries.each do |e| - unless e.name == DOCUMENT_FILE_PATH + unless @document_contents.keys.include? e.name zos.put_next_entry(e.name) zos.print e.get_input_stream.read end end - zos.put_next_entry(DOCUMENT_FILE_PATH) - zos.print @document_content + @document_contents.each do |name, content| + zos.put_next_entry(name) + zos.print content + end end if new_path.nil? From f7a90ec3af0be4f4807165c7b6735dbbd00fca9a Mon Sep 17 00:00:00 2001 From: Marek Pietrucha Date: Wed, 19 Jul 2017 11:39:00 +0200 Subject: [PATCH 3/4] corrected how looping to replace --- lib/docx_replace.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 31a1949..37119f9 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -19,12 +19,12 @@ def initialize(path, temp_dir=nil) def replace(pattern, replacement, multiple_occurrences=false) replace = replacement.to_s if multiple_occurrences - @document_contents.each do |content| - content.force_encoding("UTF-8").gsub!(pattern, replace) + @document_contents.keys.each do |name| + @document_contents[name].force_encoding("UTF-8").gsub!(pattern, replace) end else - @document_contents.each do |content| - content.force_encoding("UTF-8").sub!(pattern, replace) + @document_contents.keys.each do |name| + @document_contents[name].force_encoding("UTF-8").sub!(pattern, replace) end end end From c874e8ad349532600efb5d26bf08dca84078cae1 Mon Sep 17 00:00:00 2001 From: Marek Pietrucha Date: Wed, 19 Jul 2017 12:03:57 +0200 Subject: [PATCH 4/4] added .encode(xml: :text) to replacement string --- lib/docx_replace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 37119f9..b8c6a26 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -17,7 +17,7 @@ def initialize(path, temp_dir=nil) end def replace(pattern, replacement, multiple_occurrences=false) - replace = replacement.to_s + replace = replacement.to_s.encode(xml: :text) if multiple_occurrences @document_contents.keys.each do |name| @document_contents[name].force_encoding("UTF-8").gsub!(pattern, replace)