lib/html2doc/mime.rb in html2doc-1.3.1 vs lib/html2doc/mime.rb in html2doc-1.4.0
- old
+ new
@@ -2,12 +2,12 @@
require "base64"
require "mime/types"
require "image_size"
require "fileutils"
-module Html2Doc
- def self.mime_preamble(boundary, filename, result)
+class Html2Doc
+ def mime_preamble(boundary, filename, result)
<<~"PREAMBLE"
MIME-Version: 1.0
Content-Type: multipart/related; boundary="#{boundary}"
--#{boundary}
@@ -18,11 +18,11 @@
#{result}
PREAMBLE
end
- def self.mime_attachment(boundary, _filename, item, dir)
+ def mime_attachment(boundary, _filename, item, dir)
content_type = mime_type(item)
text_mode = %w[text application].any? { |p| content_type.start_with? p }
path = File.join(dir, item)
content = text_mode ? File.read(path, encoding: "utf-8") : IO.binread(path)
@@ -38,23 +38,23 @@
#{encoded_file}
FILE
end
- def self.mime_type(item)
+ def mime_type(item)
types = MIME::Types.type_for(item)
type = types ? types.first.to_s : 'text/plain; charset="utf-8"'
type = %(#{type} charset="utf-8") if /^text/.match(type) && types
type
end
- def self.mime_boundary
+ def mime_boundary
salt = UUIDTools::UUID.random_create.to_s.gsub(/-/, ".")[0..17]
"----=_NextPart_#{salt}"
end
- def self.mime_package(result, filename, dir)
+ def mime_package(result, filename, dir)
boundary = mime_boundary
mhtml = mime_preamble(boundary, "#{filename}.htm", result)
mhtml += mime_attachment(boundary, "#{filename}.htm", "filelist.xml", dir)
Dir.foreach(dir) do |item|
next if item == "." || item == ".." || /^\./.match(item) ||
@@ -64,22 +64,22 @@
end
mhtml += "--#{boundary}--"
File.open("#{filename}.doc", "w:UTF-8") { |f| f.write contentid(mhtml) }
end
- def self.contentid(mhtml)
+ def contentid(mhtml)
mhtml.gsub %r{(<img[^>]*?src=")([^\"']+)(['"])}m do |m|
repl = "#{$1}cid:#{File.basename($2)}#{$3}"
/^data:|^https?:/.match($2) ? m : repl
end.gsub %r{(<v:imagedata[^>]*?src=")([^\"']+)(['"])}m do |m|
repl = "#{$1}cid:#{File.basename($2)}#{$3}"
/^data:|^https?:/.match($2) ? m : repl
end
end
# max width for Word document is 400, max height is 680
- def self.image_resize(img, path, maxheight, maxwidth)
+ def image_resize(img, path, maxheight, maxwidth)
realsize = ImageSize.path(path).size
s = [img["width"].to_i, img["height"].to_i]
s = realsize if s[0].zero? && s[1].zero?
return [nil, nil] if realsize.nil? || realsize[0].nil? || realsize[1].nil?
@@ -90,24 +90,24 @@
s
end
IMAGE_PATH = "//*[local-name() = 'img' or local-name() = 'imagedata']".freeze
- def self.mkuuid
+ def mkuuid
UUIDTools::UUID.random_create.to_s
end
- def self.warnsvg(src)
+ def warnsvg(src)
warn "#{src}: SVG not supported" if /\.svg$/i.match?(src)
end
- def self.localname(src, localdir)
+ def localname(src, localdir)
%r{^([A-Z]:)?/}.match?(src) ? src : File.join(localdir, src)
end
# only processes locally stored images
- def self.image_cleanup(docxml, dir, localdir)
+ def image_cleanup(docxml, dir, localdir)
docxml.traverse do |i|
src = i["src"]
next unless i.element? && %w(img v:imagedata).include?(i.name)
next if src.nil? || src.empty? || /^http/.match?(src)
next if %r{^data:(image|application)/[^;]+;base64}.match? src
@@ -121,17 +121,17 @@
docxml
end
# do not parse the header through Nokogiri, since it will contain
# non-XML like <![if !supportFootnotes]>
- def self.header_image_cleanup(doc, dir, filename, localdir)
+ def header_image_cleanup(doc, dir, filename, localdir)
doc.split(%r{(<img [^>]*>|<v:imagedata [^>]*>)}).each_slice(2).map do |a|
header_image_cleanup1(a, dir, filename, localdir)
end.join
end
- def self.header_image_cleanup1(a, dir, _filename, localdir)
+ def header_image_cleanup1(a, dir, _filename, localdir)
if a.size == 2 && !(/ src="https?:/.match a[1]) &&
!(%r{ src="data:(image|application)/[^;]+;base64}.match a[1])
m = / src=['"](?<src>[^"']+)['"]/.match a[1]
m2 = /\.(?<suffix>[a-zA-Z_0-9]+)$/.match m[:src]
new_filename = "#{mkuuid}.#{m2[:suffix]}"
@@ -139,10 +139,10 @@
a[1].sub!(%r{ src=['"](?<src>[^"']+)['"]}, " src='cid:#{new_filename}'")
end
a.join
end
- def self.generate_filelist(filename, dir)
+ 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)