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
 from email.utils import parseaddr, parsedate_tz, mktime_tz
11
 from email.utils import parseaddr, parsedate_tz, mktime_tz
12
 from email import message_from_bytes
12
 from email import message_from_bytes
13
 
13
 
14
+import markdown
14
 import requests
15
 import requests
15
 from bs4 import BeautifulSoup
16
 from bs4 import BeautifulSoup
17
+from email_reply_parser import EmailReplyParser
16
 
18
 
17
 from tracim.controllers.events import VALID_TOKEN_VALUE
19
 from tracim.controllers.events import VALID_TOKEN_VALUE
18
 
20
 
61
             charset = body_part.get_content_charset('iso-8859-1')
63
             charset = body_part.get_content_charset('iso-8859-1')
62
             ctype = body_part.get_content_type()
64
             ctype = body_part.get_content_type()
63
             if ctype == "text/plain":
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
                     charset)
67
                     charset)
68
+                body = DecodedMail._parse_txt_body(txt_body)
66
 
69
 
67
             elif ctype == "text/html":
70
             elif ctype == "text/html":
68
                 html_body = body_part.get_payload(decode=True).decode(
71
                 html_body = body_part.get_payload(decode=True).decode(
72
         return body
75
         return body
73
 
76
 
74
     @staticmethod
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
     def _parse_html_body(html_body:str):
85
     def _parse_html_body(html_body:str):
76
 
86
 
77
         soup = BeautifulSoup(html_body)
87
         soup = BeautifulSoup(html_body)