From 52541db07e34db6507416537150e4250b0617d5f Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 8 Jan 2022 15:26:40 +0700 Subject: Handle multipart emails --- pyproject.toml | 4 ++-- src/formbox.py | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2cdba01..d69bee3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,10 +4,10 @@ build-backend = "flit_core.buildapi" [project] name = "formbox" -version = "0.1.0" +version = "0.2.0" description = "Format mbox as HTML/XML" readme = "README.md" -requires-python = ">=3.7" +requires-python = ">=3.6" license = { file = "COPYING" } authors = [ { name = "Nguyễn Gia Phong", email = "mcsinyx@disroot.org" } ] maintainers = [ { name = "Nguyễn Gia Phong", email = "mcsinyx@disroot.org" } ] diff --git a/src/formbox.py b/src/formbox.py index 84c778a..46bad22 100644 --- a/src/formbox.py +++ b/src/formbox.py @@ -33,6 +33,17 @@ sanitise = partial(clean, tags=('a', 'code', 'em', 'strong', 'sub', 'sup', 'irc', 'ircs', 'mailto', 'matrix', 'xmpp')) +def get_body(message): + """Return the Markdown message body converted to HTML.""" + if message.is_multipart(): + for payload in map(get_body, message.get_payload()): + if payload is not None: return payload + elif message.get_content_type() in ('text/markdown', 'text/plain'): + return sanitise(linkify(markdown(message.get_payload(decode=True), + output_format='html5'))) + return None + + def decode(header): """Return the decoded email header.""" for string, charset in decode_header(header): @@ -60,14 +71,13 @@ def date(message): def render(template, archive, parent): """Render the thread recursively based on given template.""" for self in archive[parent]: + body = get_body(self) + if body is None: continue message_id = self['Message-Id'] try: author, address = decode(self['From']) except ValueError: author = self['From'] - # TODO: handle multipart - body = sanitise(linkify(markdown(self.get_payload(), - output_format='html5'))) rendered_children = render(template, archive, message_id) yield template.format(message_id=quote(message_id), mailto_params=urlencode(dict(reply_to(self))), -- cgit 1.4.1