From a997542cfedf911e737d24fd83f2645db764cf02 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 10 Mar 2019 23:27:30 +0100 Subject: [PATCH] Handle image/gif (inline) Inline images are very similar to attachments: We just want to store them somewhere and refer to them. But we want to use a different element ( instead of ) or more generally, a different template. So we pass the disposition as an additional argument to save_part and use it to construct the template name - which gives as a flurry of new templates. --- mbox2web | 17 ++++++++++++----- .../attachment_application_octet-stream.html | 9 +++++++++ templates/attachment_application_x-gzip.html | 9 +++++++++ templates/attachment_text_html.html | 9 +++++++++ templates/attachment_text_plain.html | 9 +++++++++ templates/attachment_text_x-vcard.html | 9 +++++++++ templates/body_message_news.html | 11 +++++++++++ templates/inline_image_gif.html | 8 ++++++++ 8 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 templates/attachment_application_octet-stream.html create mode 100644 templates/attachment_application_x-gzip.html create mode 100644 templates/attachment_text_html.html create mode 100644 templates/attachment_text_plain.html create mode 100644 templates/attachment_text_x-vcard.html create mode 100644 templates/body_message_news.html create mode 100644 templates/inline_image_gif.html diff --git a/mbox2web b/mbox2web index 7b7d7c1..692a160 100755 --- a/mbox2web +++ b/mbox2web @@ -51,7 +51,7 @@ def render_message(msg): return jinja2.Markup(msghtml) -def save_part(msg): +def save_part(msg, disposition): content_type = msg.get_content_type() extension = { "application/octet-stream": ".bin", @@ -59,6 +59,7 @@ def save_part(msg): "text/x-vcard": ".vcf", "text/plain": ".txt", "application/x-gzip": ".gz", # more likely tar.gz, but we can't know without looking into it which we ain't + "image/gif": ".gif", }[content_type] name = msg.get_param("name") or "(data)" @@ -69,7 +70,8 @@ def save_part(msg): os.makedirs("parts", exist_ok=True) with open("parts/" + filename, "wb") as fh: fh.write(payload) - bodytmpl = jenv.get_template("body_application_octet_stream.html") + template_name = disposition + "_" + content_type.replace("/", "_") + ".html" + bodytmpl = jenv.get_template(template_name) context = { "name": name, "url": "../../parts/" + filename, @@ -88,7 +90,7 @@ def render_body(msg): # We probably should clean up html. Alternatively we could just store # all of them application/octet-stream, which browsers should download # and not try to display. - bodyhtml = save_part(msg) + bodyhtml = save_part(msg, content_disposition) elif content_type == "text/plain": bodytmpl = jenv.get_template("body_text_plain.html") @@ -165,7 +167,7 @@ def render_body(msg): html.escape(whole_msg_id)) elif content_type == "application/octet-stream": - bodyhtml = save_part(msg) + bodyhtml = save_part(msg, "attachment") elif content_type == "multipart/signed": content, signature = msg.get_payload() @@ -242,7 +244,7 @@ def render_body(msg): bodyhtml = bodytmpl.render(context) elif content_type == "application/x-gzip": - bodyhtml = save_part(msg) + bodyhtml = save_part(msg, "attachment") elif content_type == "message/news": partshtml = [] @@ -254,6 +256,10 @@ def render_body(msg): "parts": partshtml, } bodyhtml = bodytmpl.render(context) + + elif content_type == "image/gif": + bodyhtml = save_part(msg, "inline") + else: raise RuntimeError("Content-type " + content_type + " not implemented yet") @@ -286,6 +292,7 @@ class HTMLPart(html.parser.HTMLParser): "h2", "a", "wbr", "hr", "pre", "img", "font", "i", "br", "table", "tr", "th", "td", "b", "select", "option", "input", "sup", "address", "center", "p", "h1", "dl", "h3", "ul", "li", "ol", "u", "blockquote", + "h4", ] hide_tags = [ "title" ] ignore_tags = [ "html", "head", "body", "marquee", "meta", "form", ] diff --git a/templates/attachment_application_octet-stream.html b/templates/attachment_application_octet-stream.html new file mode 100644 index 0000000..0e1d0b6 --- /dev/null +++ b/templates/attachment_application_octet-stream.html @@ -0,0 +1,9 @@ +
+
+ application/octet-stream +
+
+
+ diff --git a/templates/attachment_application_x-gzip.html b/templates/attachment_application_x-gzip.html new file mode 100644 index 0000000..912dcd2 --- /dev/null +++ b/templates/attachment_application_x-gzip.html @@ -0,0 +1,9 @@ +
+
+ application/x-gzip +
+
+ {{name}} +
+
+ diff --git a/templates/attachment_text_html.html b/templates/attachment_text_html.html new file mode 100644 index 0000000..8ee778e --- /dev/null +++ b/templates/attachment_text_html.html @@ -0,0 +1,9 @@ +
+
+ text/html (attachment) +
+
+ {{name}} +
+
+ diff --git a/templates/attachment_text_plain.html b/templates/attachment_text_plain.html new file mode 100644 index 0000000..00339fa --- /dev/null +++ b/templates/attachment_text_plain.html @@ -0,0 +1,9 @@ +
+
+ text/plain (attachment) +
+
+ {{name}} +
+
+ diff --git a/templates/attachment_text_x-vcard.html b/templates/attachment_text_x-vcard.html new file mode 100644 index 0000000..6e502bd --- /dev/null +++ b/templates/attachment_text_x-vcard.html @@ -0,0 +1,9 @@ +
+
+ text/x-vcard +
+
+ {{name}} +
+
+ diff --git a/templates/body_message_news.html b/templates/body_message_news.html new file mode 100644 index 0000000..78edb90 --- /dev/null +++ b/templates/body_message_news.html @@ -0,0 +1,11 @@ +
+
+ message/news +
+
+ {% for p in parts %} + {{p}} + {% endfor %} + +
+
diff --git a/templates/inline_image_gif.html b/templates/inline_image_gif.html new file mode 100644 index 0000000..951a404 --- /dev/null +++ b/templates/inline_image_gif.html @@ -0,0 +1,8 @@ +
+
+ image/gif +
+
+ {{name}} +
+