1 # -*- coding: utf-8 -*-
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.
8 from collections import deque
10 from django.template.loader import render_to_string
11 from django.utils.html import escape as html_escape
13 DIFF_RE = re.compile(r"""\x00([+^-])""", re.UNICODE)
14 NAMES = {'+': 'added', '-': 'removed', '^': 'changed'}
17 def diff_replace(match):
18 return """<span class="diff_mark diff_mark_%s">""" % NAMES[match.group(1)]
20 def filter_line(line):
21 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)
26 def html_diff_table(la, lb, context=None):
27 all_changes = difflib._mdiff(la, lb)
30 changes = (format_changeset(*c) for c in all_changes)
36 for changeset in all_changes:
42 changes.append((0, '-----', 0, '-----', False))
43 changes.extend(format_changeset(*c) for c in q)
46 if len(q) == context and after_change:
47 changes.extend(format_changeset(*c) for c in q)
50 elif len(q) > context:
53 return render_to_string("wiki/diff_table.html", {
58 __all__ = ['html_diff_table']