Skip to content

Commit 89d6d9c

Browse files
committed
File replacement within document
1 parent 26523fb commit 89d6d9c

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

lib/docx/document.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
module Docx
55
# The Document class wraps around a docx file and provides methods to
66
# interface with it.
7-
#
7+
#
88
# # get a Docx::Document for a docx file in the local directory
99
# doc = Docx::Document.open("test.docx")
10-
#
10+
#
1111
# # get the text from the document
1212
# puts doc.text
13-
#
13+
#
1414
# # do the same thing in a block
1515
# Docx::Document.open("test.docx") do |d|
1616
# puts d.text
@@ -26,7 +26,7 @@ def initialize(path, &block)
2626
@parser = Parser.new(File.expand_path(path))
2727
end
2828
end
29-
29+
3030
# With no associated block, Docx::Document.open is a synonym for Docx::Document.new. If the optional code block is given, it will be passed the opened +docx+ file as an argument and the Docx::Document oject will automatically be closed when the block terminates. The values of the block will be returned from Docx::Document.open.
3131
# call-seq:
3232
# open(filepath) => file
@@ -37,14 +37,14 @@ def self.open(path, &block)
3737

3838
##
3939
# *Deprecated*
40-
#
40+
#
4141
# Iterates over paragraphs within document
4242
# call-seq:
4343
# each_paragraph => Enumerator
4444
def each_paragraph
4545
paragraphs.each { |p| yield(p) }
4646
end
47-
47+
4848
# call-seq:
4949
# to_s -> string
5050
def to_s
@@ -69,18 +69,22 @@ def save(path)
6969
end
7070
zip.close
7171
end
72-
72+
7373
alias_method :text, :to_s
7474

75+
def replace_entry(entry_path, file_contents)
76+
@replace[entry_path] = file_contents
77+
end
78+
7579
private
7680

7781
#--
7882
# TODO: Flesh this out to be compatible with other files
79-
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
83+
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
8084
# end of methods that make edits?
8185
#++
8286
def update
83-
@replace["word/document.xml"] = doc.serialize :save_with => 0
87+
replace_entry "word/document.xml", doc.serialize(:save_with => 0)
8488
end
8589

8690
end

spec/docx/document_spec.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@
214214
@doc.paragraphs[5].text_runs[0].italicized?.should be_false
215215
@doc.paragraphs[5].text_runs[0].bolded?.should be_false
216216
@doc.paragraphs[5].text_runs[0].underlined?.should be_false
217-
217+
218218
@formatting[5][1].should eq @all_formatted
219219
@doc.paragraphs[5].text_runs[1].italicized?.should be_true
220220
@doc.paragraphs[5].text_runs[1].bolded?.should be_true
221221
@doc.paragraphs[5].text_runs[1].underlined?.should be_true
222-
222+
223223
@formatting[5][2].should eq @default_formatting
224224
@doc.paragraphs[5].text_runs[2].italicized?.should be_false
225225
@doc.paragraphs[5].text_runs[2].bolded?.should be_false
@@ -258,4 +258,29 @@
258258
end
259259
end
260260
end
261-
end
261+
262+
describe 'replacing contents' do
263+
let(:replacement_file_path) { @fixtures_path + '/replacement.png' }
264+
let(:temp_file_path){ Tempfile.new(['docx_gem', '.docx']).path }
265+
let(:entry_path){ 'word/media/image1.png' }
266+
let(:doc){ Docx::Document.open(@fixtures_path + '/replacement.docx') }
267+
268+
it 'should replace existing file within the document' do
269+
File.open replacement_file_path, "rb" do |io|
270+
doc.replace_entry entry_path, io.read
271+
end
272+
273+
doc.save(temp_file_path)
274+
275+
File.open replacement_file_path, "rb" do |io|
276+
expect(Zip::File.open(temp_file_path).read entry_path).to eq io.read
277+
end
278+
end
279+
280+
after do
281+
if File.exists?(temp_file_path)
282+
File.delete(temp_file_path)
283+
end
284+
end
285+
end
286+
end

spec/fixtures/replacement.docx

20.4 KB
Binary file not shown.

spec/fixtures/replacement.png

87.7 KB
Loading

0 commit comments

Comments
 (0)