From b80e4566f6bf37a78a715dd04ede018c04b9a093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 22 Jun 2016 16:52:56 +0200 Subject: [PATCH] Compress diffs to show only the changes and some context --- CHANGELOG.md | 1 + taiga/mdrender/service.py | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76ed1d66..293cb3d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add gravatar url to Users API endpoint. - ProjectTemplates now are sorted by the attribute 'order'. - Create enpty wiki pages (if not exist) when a new link is created. +- Diff messages in history entries now show only the relevant changes (with some context). - Comments: - Now comment owners and project admins can edit existing comments with the history Entry endpoint. - Add a new permissions to allow add comments instead of use the existent modify permission for this purpose. diff --git a/taiga/mdrender/service.py b/taiga/mdrender/service.py index cc87e25b..701ed0d4 100644 --- a/taiga/mdrender/service.py +++ b/taiga/mdrender/service.py @@ -126,16 +126,42 @@ def render_and_extract(project, text): class DiffMatchPatch(diff_match_patch.diff_match_patch): def diff_pretty_html(self, diffs): + def _sanitize_text(text): + return (text.replace("&", "&").replace("<", "<") + .replace(">", ">").replace("\n", "
")) + + def _split_long_text(text, idx, size): + splited_text = text.split() + + if len(splited_text) > 25: + if idx == 0: + # The first is (...)text + first = "" + else: + first = " ".join(splited_text[:10]) + + if idx != 0 and idx == size - 1: + # The last is text(...) + last = "" + else: + last = " ".join(splited_text[-10:]) + + return "{}(...){}".format(first, last) + return text + + size = len(diffs) html = [] - for (op, data) in diffs: - text = (data.replace("&", "&").replace("<", "<") - .replace(">", ">").replace("\n", "
")) + for idx, (op, data) in enumerate(diffs): if op == self.DIFF_INSERT: - html.append("%s" % text) + text = _sanitize_text(data) + html.append("{}".format(text)) elif op == self.DIFF_DELETE: - html.append("%s" % text) + text = _sanitize_text(data) + html.append("{}".format(text)) elif op == self.DIFF_EQUAL: - html.append("%s" % text) + text = _split_long_text(_sanitize_text(data), idx, size) + html.append("{}".format(text)) + return "".join(html)