forked from rsmmr/git-notifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
htmlify.py
36 lines (28 loc) · 1 KB
/
htmlify.py
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
import cgi
import re
_separator = '>' + ('-'*63) + '\n'
_pattern = re.compile('( +)')
def _mangle_line(line):
content = cgi.escape(line)
m = _pattern.finditer(content)
for i in reversed(list(m)):
(start, end) = i.span()
content = content[:start] + (' ' * (end-start)) + content[end:]
if content.startswith('-'):
return '<tt style="color:#800">%s</tt>' % (content)
if content.startswith('+'):
return '<tt style="color:#008">%s</tt>' % (content)
return content
def htmlify(text):
blocks = text.split(_separator)
if len(blocks) == 3:
caption, commitinfo, changes = blocks
output = [_mangle_line(l) for l in caption.splitlines()]
output.append('<hr>')
output.extend([_mangle_line(l) for l in commitinfo.splitlines()])
output.append('<hr>')
output.extend([_mangle_line(l) for l in changes.splitlines()])
else:
lines = text.splitlines()
output = [_mangle_line(l) for l in lines]
return '<html><body><tt>%s</tt></body></html>' % "<br>".join(output)