X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/662a8a111721df714e9c95f94db91d2d03d7b19d..97487ee6a55f54bd483c20557fb554fea4f28844:/apps/wiki/nice_diff.py?ds=inline diff --git a/apps/wiki/nice_diff.py b/apps/wiki/nice_diff.py old mode 100755 new mode 100644 index 612fa22a..19ad602c --- a/apps/wiki/nice_diff.py +++ b/apps/wiki/nice_diff.py @@ -1,26 +1,61 @@ -#!/usr/bin/env python - +# -*- coding: utf-8 -*- +# +# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# import difflib import re +from collections import deque from django.template.loader import render_to_string from django.utils.html import escape as html_escape -DIFF_RE = re.compile(r"""\x00([+^-])""" ,re.UNICODE) -NAMES = { '+': 'added', '-': 'removed', '^': 'changed' } +DIFF_RE = re.compile(r"""\x00([+^-])""", re.UNICODE) +NAMES = {'+': 'added', '-': 'removed', '^': 'changed'} + def diff_replace(match): - return """""" % NAMES[match.group(1)] + return """""" % NAMES[match.group(1)] + def filter_line(line): - return DIFF_RE.sub(diff_replace, html_escape(line)).replace('\x01', '') - -def html_diff_table(la, lb): - return render_to_string("wiki/diff_table.html", { - "changes": [(a[0],filter_line(a[1]), - b[0],filter_line(b[1]), - change) for a, b, change in difflib._mdiff(la, lb)] - }); - - -__all__ = ['html_diff_table'] \ No newline at end of file + return DIFF_RE.sub(diff_replace, html_escape(line)).replace('\x01', '') + + +def format_changeset(a, b, change): + return a[0], filter_line(a[1]), b[0], filter_line(b[1]), change + + +def html_diff_table(la, lb, context=None): + all_changes = difflib._mdiff(la, lb) + + if context is None: + changes = (format_changeset(*c) for c in all_changes) + else: + changes = [] + q = deque() + after_change = False + + for changeset in all_changes: + q.append(changeset) + + if changeset[2]: + after_change = True + if not after_change: + changes.append((0, '-----', 0, '-----', False)) + changes.extend(format_changeset(*c) for c in q) + q.clear() + else: + if len(q) == context and after_change: + changes.extend(format_changeset(*c) for c in q) + q.clear() + after_change = False + elif len(q) > context: + q.popleft() + + return render_to_string("wiki/diff_table.html", { + "changes": changes, + }) + + +__all__ = ['html_diff_table']