1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from collections import deque
8 from django.template.loader import render_to_string
9 from django.utils.html import escape as html_escape
11 DIFF_RE = re.compile(r"""\x00([+^-])""", re.UNICODE)
12 NAMES = {'+': 'added', '-': 'removed', '^': 'changed'}
15 def diff_replace(match):
16 return """<span class="diff_mark diff_mark_%s">""" % NAMES[match.group(1)]
19 def filter_line(line):
20 return DIFF_RE.sub(diff_replace, html_escape(line)).replace('\x01', '</span>')
23 def format_changeset(a, b, change):
24 return (a[0], filter_line(a[1]), b[0], filter_line(b[1]), change)
27 def html_diff_table(la, lb, context=None):
28 all_changes = difflib._mdiff(la, lb)
31 changes = (format_changeset(*c) for c in all_changes)
37 for changeset in all_changes:
43 changes.append((0, '-----', 0, '-----', False))
44 changes.extend(format_changeset(*c) for c in q)
47 if len(q) == context and after_change:
48 changes.extend(format_changeset(*c) for c in q)
51 elif len(q) > context:
54 return render_to_string("wiki/diff_table.html", {
59 __all__ = ['html_diff_table']