forked from Anthchirp/git-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlify.py
More file actions
47 lines (37 loc) · 1.25 KB
/
htmlify.py
File metadata and controls
47 lines (37 loc) · 1.25 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
import cgi
import re
# pylint: disable=bad-indentation
# separates the commit message, diff, and tags
_separator = '>' + ('-'*63) + '\n'
# separates multiple emails in a changeset
_emailsep = '>' + ('*'*63)
_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:#080">%s</tt>' % (content)
return content
def _breakemail(text):
def _convert_block(text):
return [_mangle_line(l) for l in text.splitlines()]
output = []
for i, blk in enumerate(text.split(_separator)):
if i > 0:
output.append('<hr>')
output.extend(_convert_block(blk))
return "<br>".join(output)
def _breakbody(text):
return ("<hr style='" +
"height: 3px; background-color: #eee; border: solid 1px; color: #ccc;'" +
"></hr>").join(
[_breakemail(emailtext) for emailtext in text.split(_emailsep)])
def htmlify(text):
return ('<html><body><tt>' +
_breakbody(text) +
'</tt></body></html>')