diff --git a/src/paperless_mail/parsers.py b/src/paperless_mail/parsers.py index b325b79d5..c9c4e7e90 100644 --- a/src/paperless_mail/parsers.py +++ b/src/paperless_mail/parsers.py @@ -229,21 +229,14 @@ class MailDocumentParser(DocumentParser): data["date"] = clean_html(mail.date.astimezone().strftime("%Y-%m-%d %H:%M")) data["content"] = clean_html(mail.text.strip()) - html_file = os.path.join(os.path.dirname(__file__), "mail_template/index.html") - placeholder_pattern = re.compile(r"{{(.+)}}") - html = StringIO() - with open(html_file) as html_template_handle: - for line in html_template_handle.readlines(): - for placeholder in placeholder_pattern.findall(line): - line = re.sub( - "{{" + placeholder + "}}", - data.get(placeholder.strip(), ""), - line, - ) - html.write(line) - html.seek(0) + from django.template.loader import render_to_string + + rendered = render_to_string("email_msg_template.html", context=data) + + html.write(rendered) + html.seek(0) return html @@ -252,12 +245,12 @@ class MailDocumentParser(DocumentParser): url = self.gotenberg_server + "/forms/chromium/convert/html" self.log("info", "Converting mail to PDF") - css_file = os.path.join(os.path.dirname(__file__), "mail_template/output.css") + css_file = os.path.join(os.path.dirname(__file__), "templates/output.css") with open(css_file, "rb") as css_handle: files = { - "html": ("index.html", self.mail_to_html(mail)), + "html": ("email_msg_template.html", self.mail_to_html(mail)), "css": ("output.css", css_handle), } headers = {} @@ -302,7 +295,7 @@ class MailDocumentParser(DocumentParser): files.append((name_clean, BytesIO(a.payload))) html_clean = html_clean.replace(name_cid, name_clean) - files.append(("index.html", StringIO(html_clean))) + files.append(("email_msg_template.html", StringIO(html_clean))) return files diff --git a/src/paperless_mail/mail_template/index.html b/src/paperless_mail/templates/email_msg_template.html similarity index 97% rename from src/paperless_mail/mail_template/index.html rename to src/paperless_mail/templates/email_msg_template.html index d801f57ef..a22666957 100644 --- a/src/paperless_mail/mail_template/index.html +++ b/src/paperless_mail/templates/email_msg_template.html @@ -1,3 +1,4 @@ +{% autoescape off %} @@ -43,3 +44,5 @@ + +{% endautoescape %} diff --git a/src/paperless_mail/mail_template/input.css b/src/paperless_mail/templates/input.css similarity index 100% rename from src/paperless_mail/mail_template/input.css rename to src/paperless_mail/templates/input.css diff --git a/src/paperless_mail/mail_template/output.css b/src/paperless_mail/templates/output.css similarity index 100% rename from src/paperless_mail/mail_template/output.css rename to src/paperless_mail/templates/output.css diff --git a/src/paperless_mail/mail_template/package-lock.json b/src/paperless_mail/templates/package-lock.json similarity index 100% rename from src/paperless_mail/mail_template/package-lock.json rename to src/paperless_mail/templates/package-lock.json diff --git a/src/paperless_mail/mail_template/package.json b/src/paperless_mail/templates/package.json similarity index 100% rename from src/paperless_mail/mail_template/package.json rename to src/paperless_mail/templates/package.json diff --git a/src/paperless_mail/mail_template/tailwind.config.js b/src/paperless_mail/templates/tailwind.config.js similarity index 100% rename from src/paperless_mail/mail_template/tailwind.config.js rename to src/paperless_mail/templates/tailwind.config.js diff --git a/src/paperless_mail/tests/test_parsers.py b/src/paperless_mail/tests/test_parsers.py index 892d1feb7..6de97b30e 100644 --- a/src/paperless_mail/tests/test_parsers.py +++ b/src/paperless_mail/tests/test_parsers.py @@ -369,6 +369,7 @@ class TestParser(TestCase): os.path.join(self.SAMPLE_FILES, "html.eml.html"), ) as html_expected_handle: html_expected = html_expected_handle.read() + self.assertHTMLEqual(html_expected, html_received) @mock.patch("paperless_mail.parsers.requests.post") @@ -436,7 +437,7 @@ class TestParser(TestCase): result = self.parser.transform_inline_html(html, attachments) resulting_html = result[-1][1].read() - self.assertTrue(result[-1][0] == "index.html") + self.assertTrue(result[-1][0] == "email_msg_template.html") self.assertTrue(result[0][0] in resulting_html) self.assertFalse("