Skip to content

Commit

Permalink
ignore external images in image postproc; closes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Aug 26, 2018
1 parent 403781a commit 33c922e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/html2doc/mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def self.image_resize(i, maxheight, maxwidth)

IMAGE_PATH = "//*[local-name() = 'img' or local-name() = 'imagedata']".freeze

# only processes locally stored images
def self.image_cleanup(docxml, dir)
docxml.xpath(IMAGE_PATH).each do |i|
next if /^http/.match i["src"]
matched = /\.(?<suffix>\S+)$/.match i["src"]
uuid = UUIDTools::UUID.random_create.to_s
new_full_filename = File.join(dir, "#{uuid}.#{matched[:suffix]}")
# presupposes that the image source is local
#system "cp #{i['src']} #{new_full_filename}"
FileUtils.cp i["src"], new_full_filename
i["width"], i["height"] = image_resize(i, 400, 680)
i["src"] = new_full_filename
Expand Down
2 changes: 1 addition & 1 deletion lib/html2doc/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Html2Doc
VERSION = "0.8.2".freeze
VERSION = "0.8.3".freeze
end
12 changes: 11 additions & 1 deletion spec/html2doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def image_clean(x)
OUTPUT
end

it "processes a header with an image" do
it "processes a header with an image" do
Html2Doc.process(html_input(""), filename: "test", header_file: "spec/header_img.html")
expect(guid_clean(File.read("test.doc", encoding: "utf-8"))).to match(%r{Content-Type: image/png})
end
Expand Down Expand Up @@ -577,6 +577,16 @@ def image_clean(x)
expect(Html2Doc.image_resize(image, 100, 100)).to eq [30, 100]
end

it "does not move images if they are external URLs" do
simple_body = '<img src="https://example.com/19160-6.png">'
Html2Doc.process(html_input(simple_body), filename: "test")
testdoc = File.read("test.doc", encoding: "utf-8")
expect(image_clean(guid_clean(testdoc))).to match_fuzzy(<<~OUTPUT)
#{WORD_HDR} #{DEFAULT_STYLESHEET} #{WORD_HDR_END}
#{image_clean(word_body('<img src="https://example.com/19160-6.png"></img>', '<div style="mso-element:footnote-list"/>'))}
#{image_clean(WORD_FTR1)}
OUTPUT
end

it "processes epub:type footnotes" do
simple_body = '<div>This is a very simple
Expand Down

0 comments on commit 33c922e

Please sign in to comment.