lib/html2doc/mime.rb in html2doc-0.8.2 vs lib/html2doc/mime.rb in html2doc-0.8.3

- old
+ new

@@ -1,9 +1,10 @@ require "uuidtools" require "base64" require "mime/types" require "image_size" +require "fileutils" module Html2Doc def self.mime_preamble(boundary, filename, result) <<~"PREAMBLE" MIME-Version: 1.0 @@ -18,11 +19,11 @@ PREAMBLE end def self.mime_attachment(boundary, filename, item, dir) encoded_file = Base64.strict_encode64( - File.read("#{dir}/#{item}"), + File.read("#{dir}/#{item}", encoding: "utf-8"), ).gsub(/(.{76})/, "\\1\n") <<~"FILE" --#{boundary} Content-Location: file:///C:/Doc/#{filename}_files/#{item} Content-Transfer-Encoding: base64 @@ -53,11 +54,11 @@ next if item == "." || item == ".." || /^\./.match(item) || item == "filelist.xml" mhtml += mime_attachment(boundary, filename, item, dir) end mhtml += "--#{boundary}--" - File.open("#{filename}.doc", "w") { |f| f.write mhtml } + File.open("#{filename}.doc", "w:UTF-8") { |f| f.write mhtml } end # max height for Word document is 400, max width is 680 def self.image_resize(i, maxheight, maxwidth) realSize = ImageSize.path(i["src"]).size @@ -70,17 +71,18 @@ s end 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 end docxml end @@ -98,10 +100,11 @@ matched = / src=['"](?<src>[^"']+)['"]/.match a[1] matched2 = /\.(?<suffix>\S+)$/.match matched[:src] uuid = UUIDTools::UUID.random_create.to_s new_full_filename = "file:///C:/Doc/#{filename}_files/#{uuid}.#{matched2[:suffix]}" dest_filename = File.join(dir, "#{uuid}.#{matched2[:suffix]}") - system "cp #{matched[:src]} #{dest_filename}" + #system "cp #{matched[:src]} #{dest_filename}" + FileUtils.cp matched[:src], dest_filename a[1].sub!(%r{ src=['"](?<src>[^"']+)['"]}, " src='#{new_full_filename}'") end a.join end