612fa22a6af30be488b9d314bd531da063054029
[redakcja.git] / apps / wiki / nice_diff.py
1 #!/usr/bin/env python
2
3 import difflib
4 import re
5
6 from django.template.loader import render_to_string
7 from django.utils.html import escape as html_escape
8
9 DIFF_RE = re.compile(r"""\x00([+^-])""" ,re.UNICODE)
10 NAMES = { '+': 'added', '-': 'removed', '^': 'changed' }
11
12 def diff_replace(match):
13         return """<span class="diff_mark diff_mark_%s">""" %  NAMES[match.group(1)]
14
15 def filter_line(line):
16         return  DIFF_RE.sub(diff_replace, html_escape(line)).replace('\x01', '</span>')
17
18 def html_diff_table(la, lb):
19         return render_to_string("wiki/diff_table.html", {
20                 "changes": [(a[0],filter_line(a[1]),
21                                          b[0],filter_line(b[1]),
22                                          change) for a, b, change in difflib._mdiff(la, lb)]
23         }); 
24                                                                                 
25         
26 __all__ = ['html_diff_table']