html2doc 1.12.1 → 1.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/html2doc/mime.rb +31 -23
- data/lib/html2doc/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d4f4911207749ae30b4772abc1dbea2ddec7d45043845b29ec3bb2d3ced3befa
|
|
4
|
+
data.tar.gz: 26cabd55b18619f41b193f347011faa19825cfb19ae12640563d34eabf74c57f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2b99cb99a65ab6fab6df6ae059db429252d22e188b348873c305f1407f84634b4590f3785806dfda15820bf6387dbcf82d12f38f3ef024d93f5d3cb6cb4564ac
|
|
7
|
+
data.tar.gz: b398ba68a236f55cdd02b7199234a24e4dde95e6d167823e5ef5a722fd4a74434dd65cee8c26d8a9e85eb026b6c5cd8cac17c372d1190f6a50fdbdb765d3f4e2
|
data/lib/html2doc/mime.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "digest"
|
|
2
2
|
require "base64"
|
|
3
3
|
require "marcel"
|
|
4
4
|
require "fileutils"
|
|
@@ -63,22 +63,20 @@ class Html2Doc
|
|
|
63
63
|
type
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
-
# Deterministic MIME boundary: minted from the content of this
|
|
67
|
-
# instead of random entropy, so identical input yields
|
|
68
|
-
# .doc MHTML output. Boundaries only need uniqueness
|
|
69
|
-
# not across the universe.
|
|
66
|
+
# Deterministic MIME boundary: minted from the content address of this
|
|
67
|
+
# package instead of random entropy, so identical input yields
|
|
68
|
+
# byte-identical .doc MHTML output. Boundaries only need uniqueness
|
|
69
|
+
# within a document, not across the universe.
|
|
70
70
|
def mime_boundary(seed)
|
|
71
|
-
|
|
72
|
-
"----=_NextPart_#{salt}"
|
|
71
|
+
"----=_NextPart_#{Digest::SHA256.hexdigest(seed)[0, 18]}"
|
|
73
72
|
end
|
|
74
73
|
|
|
75
74
|
def mime_package(result, filename, dir)
|
|
76
|
-
boundary = mime_boundary(
|
|
75
|
+
boundary = mime_boundary(package_seed(result, filename, dir))
|
|
77
76
|
mhtml = mime_preamble(boundary, "#{filename}.htm", result)
|
|
78
77
|
mhtml += mime_attachment(boundary, "#{filename}.htm", "filelist.xml", dir)
|
|
79
|
-
Dir.
|
|
80
|
-
next if item
|
|
81
|
-
item == "filelist.xml"
|
|
78
|
+
Dir.children(dir).sort.each do |item|
|
|
79
|
+
next if /^\./.match?(item) || item == "filelist.xml"
|
|
82
80
|
|
|
83
81
|
mhtml += mime_attachment(boundary, "#{filename}.htm", item, dir)
|
|
84
82
|
end
|
|
@@ -86,6 +84,18 @@ class Html2Doc
|
|
|
86
84
|
File.open("#{filename}.doc", "w:UTF-8") { |f| f.write contentid(mhtml) }
|
|
87
85
|
end
|
|
88
86
|
|
|
87
|
+
# The seed is the package's content address: the main document name, the
|
|
88
|
+
# main HTML with its references reduced to content addresses, and every
|
|
89
|
+
# attachment as name:SHA-256 pairs in sorted order. Deriving it from
|
|
90
|
+
# content alone - never from raw paths or scratch state - keeps the
|
|
91
|
+
# boundary immune to upstream working-directory volatility.
|
|
92
|
+
def package_seed(result, filename, dir)
|
|
93
|
+
parts = Dir.children(dir).sort.map do |item|
|
|
94
|
+
"#{item}:#{content_address(File.join(dir, item))}"
|
|
95
|
+
end
|
|
96
|
+
"#{File.basename(filename)}.htm:#{contentid(result)}:#{parts.join(',')}"
|
|
97
|
+
end
|
|
98
|
+
|
|
89
99
|
def contentid(mhtml)
|
|
90
100
|
mhtml.gsub %r{(<img[^<>]*?src=")([^"'<]+)(['"])}m do |m|
|
|
91
101
|
repl = "#{$1}cid:#{File.basename($2)}#{$3}"
|
|
@@ -98,16 +108,12 @@ class Html2Doc
|
|
|
98
108
|
|
|
99
109
|
IMAGE_PATH = "//*[local-name() = 'img' or local-name() = 'imagedata']".freeze
|
|
100
110
|
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def image_uuid(filename)
|
|
110
|
-
uuid5(IO.binread(filename))
|
|
111
|
+
# Content-addressed part name seed, mirroring IsoDoc::AssetStore's
|
|
112
|
+
# addressing: the SHA-256 of the part bytes. Word/Outlook treat
|
|
113
|
+
# Content-IDs as opaque; identical content collapses to a single part,
|
|
114
|
+
# and emitted names carry no entropy.
|
|
115
|
+
def content_address(filename)
|
|
116
|
+
Digest::SHA256.file(filename).hexdigest
|
|
111
117
|
end
|
|
112
118
|
|
|
113
119
|
def warnsvg(src)
|
|
@@ -158,7 +164,8 @@ class Html2Doc
|
|
|
158
164
|
|
|
159
165
|
def rename_image(img, dir, localdir)
|
|
160
166
|
local_filename = localname(img["src"], localdir)
|
|
161
|
-
new_filename =
|
|
167
|
+
new_filename =
|
|
168
|
+
"#{content_address(local_filename)}#{File.extname(img['src'])}"
|
|
162
169
|
FileUtils.cp local_filename, File.join(dir, new_filename)
|
|
163
170
|
img["src"] = File.join(File.basename(dir), new_filename)
|
|
164
171
|
local_filename
|
|
@@ -240,7 +247,8 @@ class Html2Doc
|
|
|
240
247
|
!(%r{ src="data:(image|application)/[^;]+;base64}.match a[1])
|
|
241
248
|
m = / src=['"](?<src>[^"']+)['"]/.match a[1]
|
|
242
249
|
m2 = /\.(?<suffix>[a-zA-Z_0-9]+)$/.match m[:src]
|
|
243
|
-
new_filename =
|
|
250
|
+
new_filename =
|
|
251
|
+
"#{content_address(localname(m[:src], localdir))}.#{m2[:suffix]}"
|
|
244
252
|
FileUtils.cp localname(m[:src], localdir), File.join(dir, new_filename)
|
|
245
253
|
a[1].sub!(%r{ src=['"](?<src>[^"']+)['"]}, " src='cid:#{new_filename}'")
|
|
246
254
|
end
|
data/lib/html2doc/version.rb
CHANGED