From 502b472c0796fcb5bd5a5f57673bf27314497a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondr=CC=8Cej=20Ny=CC=81vlt?= Date: Fri, 25 Aug 2023 22:41:25 +0200 Subject: [PATCH] Customize diff to html rendering --- processor/app.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/processor/app.py b/processor/app.py index 292b110..fae924c 100644 --- a/processor/app.py +++ b/processor/app.py @@ -11,6 +11,28 @@ import hashlib from diff_match_patch import diff_match_patch +class DiffThing(diff_match_patch): + def diff_html(self, diffs): + """Like diff_prettyHtml, but without inline style attributes + (makes it easier to style it with stylesheets). + """ + html = [] + for op, data in diffs: + text = ( + data.replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace("\n", "¶
") + ) + if op == self.DIFF_INSERT: + html.append("%s" % text) + elif op == self.DIFF_DELETE: + html.append('%s' % text) + elif op == self.DIFF_EQUAL: + html.append("%s" % text) + return "".join(html) + + # # Idea block: # @@ -25,7 +47,7 @@ REDIS_ARTICLE_EXPIRE_SEC = 604800 config = confuse.Configuration("headline", __name__) config.set_file(CONFIG_FILE) -dmp = diff_match_patch() +dmp = DiffThing() rc = redis.Redis(host="redis", port=6379, db=0) @@ -88,7 +110,7 @@ def write_article(article, rc): def process_diff(old, new, rss_id): diff = dmp.diff_main(old["title"], new["title"]) dmp.diff_cleanupSemantic(diff) - html_diff = dmp.diff_prettyHtml(diff) + html_diff = dmp.diff_html(diff) # print(old['link']) # print(diff)