-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.py
50 lines (45 loc) · 1.66 KB
/
diff.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import difflib, string, re
import web
def isTag(x): return x[0] == "<" and x[-1] == ">"
def better_diff(a, b):
out = []
a, b = html2list(a), html2list(b)
s = difflib.SequenceMatcher(None, a, b)
for e in s.get_opcodes():
if e[0] == "replace":
out.append('<span class="delete">'+ web.htmlquote(''.join(a[e[1]:e[2]])) + "</span>")
out.append('<span class="insert">'+ web.htmlquote(''.join(b[e[3]:e[4]])) + "</span>")
elif e[0] == "delete":
out.append('<span class="delete">'+ web.htmlquote(''.join(a[e[1]:e[2]])) + "</span>")
elif e[0] == "insert":
out.append('<span class="insert">'+ web.htmlquote(''.join(b[e[3]:e[4]])) + "</span>")
elif e[0] == "equal":
out.append(web.htmlquote(''.join(b[e[3]:e[4]])))
out = ''.join(out)
return re.sub(r'(\r\n|\n|\r)', '<br />\n', out)
def html2list(x, b=0):
mode = 'char'
cur = ''
out = []
x = re.sub(r'(\r\n|\r)', '\n', x)
for c in x:
if mode == 'tag':
if c == '>':
if b: cur += '>'
else: cur += c
out.append(cur); cur = ''; mode = 'char'
else: cur += c
elif mode == 'char':
if c == '<':
out.append(cur)
if b: cur = '<'
else: cur = c
mode = 'tag'
elif c in string.whitespace[:-1]+string.punctuation:
out.append(cur); out.append(c); cur = ''
elif c == ' ':
out.append(cur); cur = c
else:
cur += c
out.append(cur)
return filter(lambda x: x is not '', out)