69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/ferrum_common.rb', line 69
def self.process_mhtml
scanner = ::StringScanner.new(mht = ARGF.read)
fail scanner.peek(400).inspect unless scanner.scan(/\AFrom: <Saved by Blink>\r
Snapshot-Content-Location: \S+\r
Subject:(?: \S.*\r\n)+Date: [A-Z][a-z][a-z], \d\d? [A-Z][a-z][a-z] 20\d\d \d\d:\d\d:\d\d -0000\r
MIME-Version: 1\.0\r
Content-Type: multipart\/related;\r
\ttype="text\/html";\r
\tboundary="(----MultipartBoundary--[a-zA-Z0-9]{42}----)"\r\n\r\n\r\n--\1/)
delimeter = scanner[1]
fail unless scanner.charpos == prev = scanner.pos
reps = []
while s = scanner.search_full(::Regexp.new(delimeter), true, true)
doc = s[0...-delimeter.size-4]
case doc
when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: chrome-error:\/\/chromewebdata\/\r\n\r\n/,
/\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r\n\r\n/
STDERR.puts "trash #{$'.size}"
reps.push [prev-delimeter.size-2, scanner.pos-delimeter.size-4, "", ""]
when /\A\r\nContent-Type: text\/html\r
Content-ID: <frame-[A-Z0-9]{32}@mhtml\.blink>\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
STDERR.puts "html #{$'.size}"
= $&
t = $'.gsub(/=([0-9A-F][0-9A-F])/){ fail $1 unless "3D" == $1 || "20" == $1 || "0A" == $1 unless "80" <= $1; $1.hex.chr }.gsub("=\r\n", "")
STDERR.puts "unpacked #{t.size}"
html = ::Nokogiri::HTML t
STDERR.puts ".to_s.size #{html.to_s.size}"
html.xpath("//*[not(*)]").group_by(&:name).
map{ |_, g| [_, g.map(&:to_s).map(&:size).reduce(:+)] }.
sort_by(&:last).reverse.take(5).each &method(:p)
if block_given?
yield html
STDERR.puts "yielded"
STDERR.puts "yield #{html.to_s.size}"
end
reps.push [prev, scanner.pos-delimeter.size-4, , html.to_s, true, :html]
when /\A\r\nContent-Type: text\/css\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
STDERR.puts "css > #{$'.size}"
= $&
css = $'.gsub(/=([0-9A-F][0-9A-F])/){ fail $1 unless "3D" == $1 || "20" == $1 || "0A" == $1 unless "80" <= $1; $1.hex.chr }.gsub("=\r\n", "")
css.gsub!(/[\r\n]+/, "\n")
STDERR.puts "css < #{css.size}"
reps.push [prev, scanner.pos-delimeter.size-4, , css, true, :css]
when /\A\r\nContent-Type: image\/(webp|png|gif|jpeg)\r
Content-Transfer-Encoding: base64\r
Content-Location: \S+\r\n\r\n/
STDERR.puts "#{$1} #{$'.size}"
when /\A\r\nContent-Type: image\/svg\+xml\r
Content-Transfer-Encoding: quoted-printable\r
Content-Location: \S+\r\n\r\n/
STDERR.puts "svg #{$'.size}"
else
STDERR.puts doc[0..300]
fail
end
fail unless scanner.charpos == prev = scanner.pos
end
is = reps.map.with_index{ |(_, _, _, _, _, type), i| i if :html == type }.compact
STDERR.puts is.inspect
cs = reps.map.with_index{ |(_, _, _, _, _, type), i| i if :css == type }.compact
STDERR.puts cs.inspect
cs.each_cons(2){ |i,j| fail unless i+1==j }
fail unless is == [cs[0]-1]
File.write "temp.htm", reps[is[0]][3]
puts "css > #{File.size "temp.css"}"
File.open("temp.css", "w"){ |f| cs.each{ |i| f.puts reps[i][3] } }
system "uncss temp.htm -s temp.css -o out.css"
STDERR.puts "css < #{File.size "out.css"}"
reps[cs[0]][1] = reps[cs[-1]][1]
reps[cs[0]+1..cs[-1]] = []
reps[cs[0]][3] = File.read "out.css"
reps.reverse_each do |from, to, , str, qp|
str = qp ?
+ str.gsub("=", "=3D").
b.gsub(/[\x80-\xFF]/n){ |_| "=%02X" % _.ord }.
gsub(/.{73}[^=][^=](?=.)/, "\\0=\r\n") :
+ str.gsub("\n", "\r\n")
STDERR.puts [str.size, "to - from = #{to - from}"].inspect
mht[from...to] = str
end
puts mht
STDERR.puts "OK"
end
|