Customize diff to html rendering

This commit is contained in:
Ondřej 2023-08-25 22:41:25 +02:00
parent e48eacf506
commit 502b472c07

View file

@ -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("<", "&lt;")
.replace(">", "&gt;")
.replace("\n", "&para;<br>")
)
if op == self.DIFF_INSERT:
html.append("<ins>%s</ins>" % text)
elif op == self.DIFF_DELETE:
html.append('<del">%s</del>' % text)
elif op == self.DIFF_EQUAL:
html.append("<span>%s</span>" % 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)