-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdownmail.py
More file actions
executable file
·75 lines (57 loc) · 2.68 KB
/
markdownmail.py
File metadata and controls
executable file
·75 lines (57 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
File: markdownmail.py
Author: alps2006
Email: guoxiangxun@gmail.com
Github: https://github.com/alps2006
Description: convert markdown file to multipart/related mail for neomutt.
"""
import fileinput
import sys
import os
import uuid
import re
def write_txt(md_filename, out_filename):
os.system('pandoc -f markdown-blank_before_blockquote-blank_before_header -t plain -o {0} {1}'.format(out_filename, md_filename))
def write_html(md_filename, out_filename):
os.system('pandoc -s -f markdown-blank_before_blockquote-blank_before_header --resource-path ~/.local/share/templates/ --template email -o {0} {1}'.format(out_filename, md_filename))
def convert_to_inline_html(html_filename, out_filename):
""" rewrite html img src url to mail cid """
related_cid = {}
with open(html_filename, 'r') as f_in:
lines = f_in.read()
# findall <img> src list and set key-value(uuid) in each item
src_list = re.findall(r'<img\n?\s*src="(.*?)"', lines)
for item in set(src_list):
related_cid[item] = uuid.uuid4().hex
lines = re.sub(item, 'cid:' + related_cid[item], lines)
with open(out_filename, 'w') as f_out :
f_out.write(lines)
return related_cid
def write_macro_instructions(plain_filename, html_filename, related_cid, out_filename):
instructions = 'push ';
if len(related_cid) == 0:
instructions += '<attach-file>{0}<enter><toggle-disposition><tag-entry><attach-file>{1}<enter><toggle-disposition><tag-entry><group-alternatives>'.format(plain_filename, html_filename)
else:
instructions += '<attach-file>{0}<enter><toggle-disposition><tag-entry><attach-file>{1}<enter><toggle-disposition><tag-entry><group-alternatives><tag-entry>'.format(plain_filename, html_filename)
for k,v in related_cid.items():
# clear existed content-id by '^u' before setting a new
instructions += '<attach-file>{0}<enter><toggle-disposition><edit-content-id>^u{1}<enter><tag-entry>'.format(k, v)
else:
instructions += '<group-related>'
with open(out_filename, 'w') as f:
f.write(instructions)
return instructions
def main():
TXT_FILENAME = '/tmp/msg.txt'
HTML_FILENAME = '/tmp/msg.html'
HTML_INLINE_FILENAME = '/tmp/msg_inline.html'
MACRO_FILENAME = '/tmp/msg.macro'
with open(TXT_FILENAME, 'w') as f, sys.stdin as f2:
f.write(f2.read())
write_html(TXT_FILENAME, HTML_FILENAME)
write_txt(TXT_FILENAME, TXT_FILENAME)
related_cid = convert_to_inline_html(HTML_FILENAME, HTML_INLINE_FILENAME)
return write_macro_instructions(TXT_FILENAME, HTML_INLINE_FILENAME, related_cid, MACRO_FILENAME)
if __name__ == "__main__":
main()