From 0d04dd662e499ec605635b40e7b127513e3de913 Mon Sep 17 00:00:00 2001 From: jaspervandenberg Date: Wed, 13 Jan 2016 16:14:14 +0100 Subject: [PATCH 1/3] Now include header*.xml files and footer*.xml --- lib/docx_replace.rb | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 42368bb..6cfa594 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -10,21 +10,24 @@ class Doc def initialize(path, temp_dir=nil) @zip_file = Zip::File.new(path) + @document_file_paths = find_query_file_paths() @temp_dir = temp_dir - read_docx_file + read_docx_files end def replace(pattern, replacement, multiple_occurrences=false) replace = replacement.to_s - if multiple_occurrences - @document_content.force_encoding("UTF-8").gsub!(pattern, replace) - else - @document_content.force_encoding("UTF-8").sub!(pattern, replace) + @document_contents.each do |path, document| + if multiple_occurrences + document.force_encoding("UTF-8").gsub!(pattern, replace) + else + document.force_encoding("UTF-8").sub!(pattern, replace) + end end end def matches(pattern) - @document_content.scan(pattern).map{|match| match.first} + @document_contents.values.join.scan(pattern).map{|match| match.first} end def unique_matches(pattern) @@ -39,10 +42,18 @@ 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) + def find_query_file_paths + @zip_file.entries.map(&:name).select do |entry| + !(/^word\/(document|footer[0-9]+|header[0-9]+).xml$/ =~ entry).nil? + end + end + + def read_docx_files + @document_contents = {} + @document_file_paths.each do |path| + @document_contents[path] = @zip_file.read(path) + end end def write_back_to_file(new_path=nil) @@ -51,16 +62,20 @@ def write_back_to_file(new_path=nil) else temp_file = Tempfile.new('docxedit-', @temp_dir) end + Zip::OutputStream.open(temp_file.path) do |zos| @zip_file.entries.each do |e| - unless e.name == DOCUMENT_FILE_PATH + unless @document_file_paths.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 |path, document| + zos.put_next_entry(path) + zos.print document + end + end if new_path.nil? From 16dcf16297791511e1901179993ab0e813653918 Mon Sep 17 00:00:00 2001 From: Peter Gumeson Date: Tue, 20 Dec 2022 10:16:44 -0700 Subject: [PATCH 2/3] Rename document_content attr to be same as upstream --- lib/docx_replace.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 1e15eaa..0ad0325 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -6,7 +6,7 @@ module DocxReplace class Doc - attr_reader :document_contents + attr_reader :document_content def initialize(path, temp_dir=nil) @zip_file = Zip::File.new(path) @@ -17,7 +17,7 @@ def initialize(path, temp_dir=nil) def replace(pattern, replacement, multiple_occurrences=false) replace = replacement.to_s.encode(xml: :text) - @document_contents.each do |path, document| + @document_content.each do |path, document| if multiple_occurrences document.force_encoding("UTF-8").gsub!(pattern, replace) else @@ -27,7 +27,7 @@ def replace(pattern, replacement, multiple_occurrences=false) end def matches(pattern) - @document_contents.values.join.scan(pattern).map{|match| match.first} + @document_content.values.join.scan(pattern).map{|match| match.first} end def unique_matches(pattern) @@ -50,9 +50,9 @@ def find_query_file_paths end def read_docx_files - @document_contents = {} + @document_content = {} @document_file_paths.each do |path| - @document_contents[path] = @zip_file.read(path) + @document_content[path] = @zip_file.read(path) end end @@ -71,7 +71,7 @@ def write_back_to_file(new_path=nil) end end - @document_contents.each do |path, document| + @document_content.each do |path, document| zos.put_next_entry(path) zos.print document end From 7f86c0e687ce7168a185d98265a259b9dea6fb47 Mon Sep 17 00:00:00 2001 From: Peter Gumeson Date: Tue, 20 Dec 2022 10:26:59 -0700 Subject: [PATCH 3/3] Revert "Rename document_content attr to be same as upstream" This reverts commit 16dcf16297791511e1901179993ab0e813653918. --- lib/docx_replace.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/docx_replace.rb b/lib/docx_replace.rb index 0ad0325..1e15eaa 100644 --- a/lib/docx_replace.rb +++ b/lib/docx_replace.rb @@ -6,7 +6,7 @@ module DocxReplace class Doc - attr_reader :document_content + attr_reader :document_contents def initialize(path, temp_dir=nil) @zip_file = Zip::File.new(path) @@ -17,7 +17,7 @@ def initialize(path, temp_dir=nil) def replace(pattern, replacement, multiple_occurrences=false) replace = replacement.to_s.encode(xml: :text) - @document_content.each do |path, document| + @document_contents.each do |path, document| if multiple_occurrences document.force_encoding("UTF-8").gsub!(pattern, replace) else @@ -27,7 +27,7 @@ def replace(pattern, replacement, multiple_occurrences=false) end def matches(pattern) - @document_content.values.join.scan(pattern).map{|match| match.first} + @document_contents.values.join.scan(pattern).map{|match| match.first} end def unique_matches(pattern) @@ -50,9 +50,9 @@ def find_query_file_paths end def read_docx_files - @document_content = {} + @document_contents = {} @document_file_paths.each do |path| - @document_content[path] = @zip_file.read(path) + @document_contents[path] = @zip_file.read(path) end end @@ -71,7 +71,7 @@ def write_back_to_file(new_path=nil) end end - @document_content.each do |path, document| + @document_contents.each do |path, document| zos.put_next_entry(path) zos.print document end