Coding style overhaul for Python files (PEP8 conformant). Removed buggy csstidy pytho...
[redakcja.git] / apps / wiki / nice_diff.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import difflib
7 import re
8
9 from django.template.loader import render_to_string
10 from django.utils.html import escape as html_escape
11
12 DIFF_RE = re.compile(r"""\x00([+^-])""", re.UNICODE)
13 NAMES = {'+': 'added', '-': 'removed', '^': 'changed'}
14
15
16 def diff_replace(match):
17     return """<span class="diff_mark diff_mark_%s">""" % NAMES[match.group(1)]
18
19
20 def filter_line(line):
21     return  DIFF_RE.sub(diff_replace, html_escape(line)).replace('\x01', '</span>')
22
23
24 def html_diff_table(la, lb):
25     return render_to_string("wiki/diff_table.html", {
26         "changes": [(a[0], filter_line(a[1]), b[0], filter_line(b[1]), change)
27                         for a, b, change in difflib._mdiff(la, lb)],
28     })
29
30
31 __all__ = ['html_diff_table']