2 """HTML Diff: http://www.aaronsw.com/2002/diff
3 Rough code, badly documented. Send me comments and patches."""
5 __author__ = 'Aaron Swartz <me@aaronsw.com>'
6 __copyright__ = '(C) 2003 Aaron Swartz. GNU GPL 2.'
11 def isTag(x): return x[0] == "<" and x[-1] == ">"
14 """Takes in strings a and b and returns a human-readable HTML diff."""
17 a, b = html2list(a), html2list(b)
18 s = difflib.SequenceMatcher(None, a, b)
19 for e in s.get_opcodes():
21 # @@ need to do something more complicated here
22 # call textDiff but not for html, but for some html... ugh
23 # gonna cop-out for now
24 out.append('<del>'+''.join(a[e[1]:e[2]]) + '</del><ins>'+''.join(b[e[3]:e[4]])+"</ins>")
25 elif e[0] == "delete":
26 out.append('<del >'+ ''.join(a[e[1]:e[2]]) + "</del>")
27 elif e[0] == "insert":
28 out.append('<ins >'+''.join(b[e[3]:e[4]]) + "</ins>")
30 out.append(''.join(b[e[3]:e[4]]))
32 raise "Um, something's broken. I didn't expect a '" + `e[0]` + "'."
35 def html2list(x, b=0):
44 out.append(cur); cur = ''; mode = 'char'
52 elif c in string.whitespace: out.append(cur+c); cur = ''
55 return filter(lambda x: x is not '', out)
57 if __name__ == '__main__':
62 print "htmldiff: highlight the differences between two html files"
63 print "usage: " + sys.argv[0] + " a b"
65 print textDiff(open(a).read(), open(b).read())