Skip to content

Commit

Permalink
Don't raise errors on saving when contents include directories (#30)
Browse files Browse the repository at this point in the history
Fixes #29.
  • Loading branch information
qelphybox authored and chrahunt committed Mar 3, 2019
1 parent 13ed900 commit 8c40b4c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Docx
# end
class Document
attr_reader :xml, :doc, :zip, :styles

def initialize(path, &block)
@replace = {}
@zip = Zip::File.open(path)
Expand Down Expand Up @@ -102,6 +102,7 @@ def save(path)
update
Zip::OutputStream.open(path) do |out|
zip.each do |entry|
next unless entry.file?
out.put_next_entry(entry.name)

if @replace[entry.name]
Expand Down
18 changes: 14 additions & 4 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@
expect(@doc.paragraphs[5].text_runs[0].italicized?).to eq(false)
expect(@doc.paragraphs[5].text_runs[0].bolded?).to eq(false)
expect(@doc.paragraphs[5].text_runs[0].underlined?).to eq(false)

expect(@formatting[5][1]).to eq(@all_formatted)
expect(@doc.paragraphs[5].text_runs[1].italicized?).to eq(true)
expect(@doc.paragraphs[5].text_runs[1].bolded?).to eq(true)
expect(@doc.paragraphs[5].text_runs[1].underlined?).to eq(true)

expect(@formatting[5][2]).to eq(@default_formatting)
expect(@doc.paragraphs[5].text_runs[2].italicized?).to eq(false)
expect(@doc.paragraphs[5].text_runs[2].bolded?).to eq(false)
Expand Down Expand Up @@ -309,6 +309,16 @@
File.delete(@new_doc_path)
end
end

context 'wps modified docx file' do
before { @doc = Docx::Document.open(@fixtures_path + '/saving_wps.docx') }
it 'should save to a normal file path' do
@new_doc_path = @fixtures_path + '/new_save.docx'
@doc.save(@new_doc_path)
@new_doc = Docx::Document.open(@new_doc_path)
expect(@new_doc.paragraphs.size).to eq(@doc.paragraphs.size)
end
end
end

describe 'outputting html' do
Expand All @@ -327,7 +337,7 @@
expect(scan.last).to eq('</p>')
expect(scan[1]).to eq('Normal')
end

it 'should emphasize italicized text' do
scan = @doc.paragraphs[1].to_html.scan(@em_regex).flatten
expect(scan.first).to eq('<em')
Expand Down Expand Up @@ -355,7 +365,7 @@
end

it "should set font size on styled paragraphs" do
regex = /(\<p{1})[^\>]+style\=\"([^\"]+).+(<\/p>)/
regex = /(\<p{1})[^\>]+style\=\"([^\"]+).+(<\/p>)/
scan = @doc.paragraphs[9].to_html.scan(regex).flatten
expect(scan.first).to eq '<p'
expect(scan.last).to eq '</p>'
Expand Down
Binary file added spec/fixtures/saving_wps.docx
Binary file not shown.

0 comments on commit 8c40b4c

Please sign in to comment.