diff --git a/content.py b/content.py index 5bd414974..4a4f2990d 100644 --- a/content.py +++ b/content.py @@ -7,6 +7,7 @@ __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Core" +import difflib import math import html import os @@ -1415,3 +1416,52 @@ def import_emoji(base_dir: str, import_filename: str, session) -> None: added += 1 save_json(emoji_dict, base_dir + '/emoji/default_emoji.json') print(str(added) + ' custom emoji added') + + +def content_diff(content: str, prev_content: str) -> str: + """Returns a diff for the given content + """ + d = difflib.Differ() + text1_lines = content.splitlines() + text2_lines = prev_content.splitlines() + diff = d.compare(text1_lines, text2_lines) + + diff_content = content + diff_text = '' + reference_text = '' + change_positions = '' + for line in diff: + if line.startswith('- '): + reference_text = line[2:] + elif line.startswith('? '): + change_positions = line[2:] + state = 0 + ctr = 0 + for changed_char in change_positions: + if state == 0: + if changed_char == '-': + diff_text += \ + '