Browse Source

Parse txt mail as markdown file to html result

Guénaël Muller 7 years ago
parent
commit
e1f6df8a6f
1 changed files with 11 additions and 1 deletions
  1. 11 1
      tracim/tracim/lib/email_fetcher.py

+ 11 - 1
tracim/tracim/lib/email_fetcher.py View File

@@ -11,8 +11,10 @@ from email.header import Header, decode_header, make_header
11 11
 from email.utils import parseaddr, parsedate_tz, mktime_tz
12 12
 from email import message_from_bytes
13 13
 
14
+import markdown
14 15
 import requests
15 16
 from bs4 import BeautifulSoup
17
+from email_reply_parser import EmailReplyParser
16 18
 
17 19
 from tracim.controllers.events import VALID_TOKEN_VALUE
18 20
 
@@ -61,8 +63,9 @@ class DecodedMail(object):
61 63
             charset = body_part.get_content_charset('iso-8859-1')
62 64
             ctype = body_part.get_content_type()
63 65
             if ctype == "text/plain":
64
-                body = body_part.get_payload(decode=True).decode(
66
+                txt_body = body_part.get_payload(decode=True).decode(
65 67
                     charset)
68
+                body = DecodedMail._parse_txt_body(txt_body)
66 69
 
67 70
             elif ctype == "text/html":
68 71
                 html_body = body_part.get_payload(decode=True).decode(
@@ -72,6 +75,13 @@ class DecodedMail(object):
72 75
         return body
73 76
 
74 77
     @staticmethod
78
+    def _parse_txt_body(txt_body:str):
79
+        txt_body = EmailReplyParser.parse_reply(txt_body)
80
+        html_body = markdown.markdown(txt_body)
81
+        body = DecodedMail._parse_html_body(html_body)
82
+        return body
83
+
84
+    @staticmethod
75 85
     def _parse_html_body(html_body:str):
76 86
 
77 87
         soup = BeautifulSoup(html_body)