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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a7563641963ef0845a507bea481d742d970aa780935c0a6012f9724071f3a7b
4
- data.tar.gz: 88073d037f32936739ce4629ca1ed1c9cb7e1f661ef84c87c8185e0d05146192
3
+ metadata.gz: d4f4911207749ae30b4772abc1dbea2ddec7d45043845b29ec3bb2d3ced3befa
4
+ data.tar.gz: 26cabd55b18619f41b193f347011faa19825cfb19ae12640563d34eabf74c57f
5
5
  SHA512:
6
- metadata.gz: 64e521545d1a41466111d7592abe7164810a8294c1669d5c7dfd7602746da9fc1e905b008ab0d6a17cc6fa4686478b70a8d804eadffb7d5fb2a33d8f5f06a756
7
- data.tar.gz: c64b5d5c51ec9d1fd81d0489a53e169b5d6b55a820d11109b4be0b199e88fb166726402c4cc075c495c8049141222dba4fa837a71a0b6b3746f6a12feca14b18
6
+ metadata.gz: 2b99cb99a65ab6fab6df6ae059db429252d22e188b348873c305f1407f84634b4590f3785806dfda15820bf6387dbcf82d12f38f3ef024d93f5d3cb6cb4564ac
7
+ data.tar.gz: b398ba68a236f55cdd02b7199234a24e4dde95e6d167823e5ef5a722fd4a74434dd65cee8c26d8a9e85eb026b6c5cd8cac17c372d1190f6a50fdbdb765d3f4e2
data/lib/html2doc/mime.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "uuidtools"
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 package
67
- # instead of random entropy, so identical input yields byte-identical
68
- # .doc MHTML output. Boundaries only need uniqueness within a document,
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
- salt = uuid5(seed).tr("-", ".")[0..17]
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("#{filename}.htm:#{result}")
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.entries(dir).sort.each do |item|
80
- next if item == "." || item == ".." || /^\./.match(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
- # RFC 4122 name-based (SHA-1, version 5) UUID minted from seed content.
102
- # Word/Outlook treat Content-IDs as opaque; deriving them from content
103
- # keeps identical documents byte-identical across runs.
104
- def uuid5(seed)
105
- UUIDTools::UUID.sha1_create(UUIDTools::UUID_OID_NAMESPACE,
106
- seed).to_s
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 = "#{image_uuid(local_filename)}#{File.extname(img['src'])}"
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 = "#{image_uuid(localname(m[:src], localdir))}.#{m2[:suffix]}"
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
@@ -1,3 +1,3 @@
1
1
  class Html2Doc
2
- VERSION = "1.12.1".freeze
2
+ VERSION = "1.12.2".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html2doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.1
4
+ version: 1.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.