Files
MDHelper/mdhelper/mhtml_archive.py
T

119 lines
5.3 KiB
Python

"""Save a rendered, self-contained browser archive without a visible browser."""
from __future__ import annotations
import base64
import hashlib
import mimetypes
import re
from email import encoders, policy
from email.parser import BytesParser
from html import escape, unescape
from html.parser import HTMLParser
from pathlib import Path
from urllib.parse import urljoin
from .document_resources import resolve_local_reference
from urllib.parse import urlsplit
def portable_archive(data: bytes) -> bytes:
"""Use archive IDs instead of file URLs, and embed CSS font dependencies.
Chromium can block file: subresources even when present in an MHTML archive.
Native page saving also omits local fonts that were not painted on the page.
"""
import tinycss2
message = BytesParser(policy=policy.default).parsebytes(data)
parts = [p for p in message.walk() if not p.is_multipart()]
aliases = {}
for part in parts:
location = part.get("Content-Location", "")
if location:
identifier = "mdhelper-" + hashlib.sha256(location.encode()).hexdigest()[:24]
if "Content-ID" in part:
identifier = part["Content-ID"].strip("<>")
else:
part["Content-ID"] = f"<{identifier}>"
aliases[location] = "cid:" + identifier
def css_resource(url, base):
if url.startswith(("#", "data:", "cid:")):
return url
absolute = urljoin(base, url)
if absolute in aliases:
return aliases[absolute]
scheme = urlsplit(absolute).scheme
if scheme == "file":
path = resolve_local_reference(Path.cwd(), absolute)
# Bundled styles list legacy font fallbacks not shipped with the app.
if not path.is_file():
return "data:,"
mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
return f"data:{mime};base64," + base64.b64encode(path.read_bytes()).decode("ascii")
if scheme in {"http", "https"}:
raise ValueError("以下样式资源未能包含在离线归档中,请改用本地资源后重试:" + absolute)
return absolute
def css_text(text, base):
tokens = tinycss2.parse_stylesheet(text)
def visit(items):
for token in items:
if token.type == "url":
value = css_resource(token.value, base)
token.value = value
token.representation = 'url("' + value.replace('"', '%22') + '")'
elif token.type == "function" and token.lower_name == "url":
arguments = [item for item in token.arguments if item.type not in {"whitespace", "comment"}]
value = arguments[0].value if len(arguments) == 1 and arguments[0].type == "string" else tinycss2.serialize(token.arguments).strip()
token.arguments = tinycss2.parse_component_value_list('"' + css_resource(value, base).replace('"', '%22') + '"')
else:
if token.type == "at-rule" and token.lower_at_keyword == "import":
for value in token.prelude:
if value.type == "string":
value.value = css_resource(value.value, base)
value.representation = '"' + value.value + '"'
for name in ("content", "prelude", "arguments"):
if getattr(token, name, None):
visit(getattr(token, name))
visit(tokens)
return tinycss2.serialize(tokens)
class HtmlResources(HTMLParser):
def __init__(self, text):
super().__init__(convert_charrefs=False)
self.changes = []
self.offsets = [0] + [m.end() for m in re.finditer("\n", text)]
self.feed(text)
def handle_starttag(self, tag, attrs):
raw = self.get_starttag_text()
line, column = self.getpos()
offset = self.offsets[line - 1] + column
for match in re.finditer(r'''\b(?:src|href|poster)\s*=\s*(["'])(.*?)\1''', raw, re.S):
value = unescape(match[2])
if value in aliases:
self.changes.append((offset + match.start(2), offset + match.end(2), escape(aliases[value], quote=True)))
for part in parts:
kind = part.get_content_type()
if kind not in {"text/html", "text/css"}:
continue
text = part.get_payload(decode=True).decode(part.get_content_charset() or "utf-8")
if kind == "text/css":
text = css_text(text, part.get("Content-Location", ""))
else:
chunks, offset = [], 0
for start, end, replacement in HtmlResources(text).changes:
chunks.extend((text[offset:start], replacement)); offset = end
chunks.append(text[offset:])
text = "".join(chunks)
if "Content-Transfer-Encoding" in part:
del part["Content-Transfer-Encoding"]
part.set_param("charset", "utf-8")
part.set_payload(text.encode("utf-8"))
encoders.encode_base64(part)
# Chromium expects literal Content-Location values, not RFC 2047 folded URLs.
return message.as_bytes(policy=policy.SMTP.clone(max_line_length=0, refold_source="none", utf8=True))