lib/html2doc/mime.rb in html2doc-1.5.4 vs lib/html2doc/mime.rb in html2doc-1.5.5

- old
+ new

@@ -76,12 +76,11 @@ end end def image_resize(img, path, maxheight, maxwidth) s, realsize = get_image_size(img, path) - return s if s[0] == nil && s[1] == nil - + 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? @@ -116,30 +115,41 @@ def image_cleanup(docxml, dir, localdir) maxheight, maxwidth = page_dimensions(docxml) docxml.traverse do |i| skip_image_cleanup?(i) and next local_filename = rename_image(i, dir, localdir) - i["width"], i["height"] = image_resize(i, local_filename, maxheight, - maxwidth) + i["width"], i["height"] = + if landscape?(i) + image_resize(i, local_filename, maxwidth, maxheight) + else + image_resize(i, local_filename, maxheight, maxwidth) + end end docxml end + def landscape?(img) + img.ancestors.each do |a| + a.name == "div" or next + @landscape.include?(a["class"]) and return true + end + false + end + def rename_image(img, dir, localdir) local_filename = localname(img["src"], localdir) new_filename = "#{mkuuid}#{File.extname(img['src'])}" FileUtils.cp local_filename, File.join(dir, new_filename) img["src"] = File.join(File.basename(dir), new_filename) local_filename end def skip_image_cleanup?(img) src = img["src"] - return true unless img.element? && %w(img v:imagedata).include?(img.name) - return true if src.nil? || src.empty? || /^http/.match?(src) || - %r{^data:(image|application)/[^;]+;base64}.match?(src) - + (img.element? && %w(img v:imagedata).include?(img.name)) or return true + (src.nil? || src.empty? || /^http/.match?(src) || + %r{^data:(image|application)/[^;]+;base64}.match?(src)) and return true false end # we are going to use the 2nd instance of @page in the Word CSS, # skipping the cover page. Currently doesn't deal with Landscape. @@ -220,11 +230,10 @@ def generate_filelist(filename, dir) File.open(File.join(dir, "filelist.xml"), "w") do |f| f.write %{<xml xmlns:o="urn:schemas-microsoft-com:office:office"> <o:MainFile HRef="../#{filename}.htm"/>} Dir.entries(dir).sort.each do |item| - next if item == "." || item == ".." || /^\./.match(item) - + (item == "." || item == ".." || /^\./.match(item)) and next f.write %{ <o:File HRef="#{item}"/>\n} end f.write("</xml>\n") end end