about summary refs log tree commit diff
path: root/src
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 /src
parent9710c6acdf63d866e44e1f77d3bd5c578f329ecb (diff)
downloadformbox-52541db07e34db6507416537150e4250b0617d5f.tar.gz
Handle multipart emails 0.2.0
Diffstat (limited to 'src')
-rw-r--r--src/formbox.py16
1 files changed, 13 insertions, 3 deletions
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))),