From 5b664efd071b706fedb53c8ddb416cf49256fcca Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Sun, 31 Dec 2023 02:08:06 +1100 Subject: [PATCH] refactor image size to metanorma-utils: https://github.com/metanorma/html2doc/issues/44 --- Gemfile.devel | 1 + html2doc.gemspec | 1 - lib/html2doc/mime.rb | 25 ++--------------------- spec/html2doc_spec.rb | 47 ------------------------------------------- 4 files changed, 3 insertions(+), 71 deletions(-) create mode 100644 Gemfile.devel diff --git a/Gemfile.devel b/Gemfile.devel new file mode 100644 index 0000000..aba18a3 --- /dev/null +++ b/Gemfile.devel @@ -0,0 +1 @@ +gem "metanorma-utils", git: "https://github.com/metanorma/metanorma-utils", branch: "features/image-size" diff --git a/html2doc.gemspec b/html2doc.gemspec index 94b70c0..2d08cab 100644 --- a/html2doc.gemspec +++ b/html2doc.gemspec @@ -27,7 +27,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0") spec.add_dependency "htmlentities", "~> 4.3.4" - spec.add_dependency "image_size", ">= 3.2.0" spec.add_dependency "metanorma-utils" spec.add_dependency "mime-types" spec.add_dependency "nokogiri", "~> 1.15" diff --git a/lib/html2doc/mime.rb b/lib/html2doc/mime.rb index e0bc4b9..16c2a27 100644 --- a/lib/html2doc/mime.rb +++ b/lib/html2doc/mime.rb @@ -76,27 +76,6 @@ def contentid(mhtml) end end - def image_resize(img, path, maxheight, maxwidth) - s, realsize = get_image_size(img, path) - s[0] == nil && s[1] == nil and return s - if img.name == "svg" && !img["viewBox"] - img["viewBox"] = "0 0 #{s[0]} #{s[1]}" - end - s[1] = s[0] * realsize[1] / realsize[0] if s[1].zero? && !s[0].zero? - s[0] = s[1] * realsize[0] / realsize[1] if s[0].zero? && !s[1].zero? - s = [(s[0] * maxheight / s[1]).ceil, maxheight] if s[1] > maxheight - s = [maxwidth, (s[1] * maxwidth / s[0]).ceil] if s[0] > maxwidth - s - end - - def get_image_size(img, path) - realsize = ImageSize.path(path).size - s = [img["width"].to_i, img["height"].to_i] - s = realsize if s[0].zero? && s[1].zero? - s = [nil, nil] if realsize.nil? || realsize[0].nil? || realsize[1].nil? - [s, realsize] - end - IMAGE_PATH = "//*[local-name() = 'img' or local-name() = 'imagedata']".freeze def mkuuid @@ -119,9 +98,9 @@ def image_cleanup(docxml, dir, localdir) local_filename = rename_image(i, dir, localdir) i["width"], i["height"] = if landscape?(i) - image_resize(i, local_filename, maxwidth, maxheight) + Metanorma::Utils.image_resize(i, local_filename, maxwidth, maxheight) else - image_resize(i, local_filename, maxheight, maxwidth) + Metanorma::Utils.image_resize(i, local_filename, maxheight, maxwidth) end end docxml diff --git a/spec/html2doc_spec.rb b/spec/html2doc_spec.rb index ed32852..07e2468 100644 --- a/spec/html2doc_spec.rb +++ b/spec/html2doc_spec.rb @@ -816,53 +816,6 @@ def image_clean(xml) OUTPUT end - it "resizes images with missing or auto sizes" do - image = Nokogiri::XML("").root - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - image["width"] = "20" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [20, 65] - image.delete("width") - image["height"] = "50" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [15, 50] - image.delete("height") - image["width"] = "500" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - image.delete("width") - image["height"] = "500" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - image["width"] = "20" - image["height"] = "auto" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [20, 65] - image["width"] = "auto" - image["height"] = "50" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [15, 50] - image["width"] = "500" - image["height"] = "auto" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - image["width"] = "auto" - image["height"] = "500" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - image["width"] = "auto" - image["height"] = "auto" - expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100)) - .to eq [30, 100] - end - - it "resizes SVG with missing or auto sizes" do - image = Nokogiri::XML(File.read("spec/odf.svg")).root - Html2Doc.new({}).image_resize(image, "spec/odf.svg", 100, 100) - expect(image.to_xml).to match_fuzzy '' - end - it "does not move images if they are external URLs" do simple_body = '' Html2Doc.new(filename: "test", imagedir: ".")