summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-01-08 15:26:40 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-01-08 15:36:44 +0700
commit52541db07e34db6507416537150e4250b0617d5f (patch)
tree1aa21df30f7bc2ce1a3c8648db8a90e339501bed
parent9710c6acdf63d866e44e1f77d3bd5c578f329ecb (diff)
downloadformbox-52541db07e34db6507416537150e4250b0617d5f.tar.gz
Handle multipart emails 0.2.0
-rw-r--r--pyproject.toml4
-rw-r--r--src/formbox.py16
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))),