From d7bb5dc1c7b40cef1f1737d39f8498b8dc14d590 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 11 Apr 2022 13:13:04 +0100 Subject: [PATCH] Fixing edit diffs --- content.py | 74 +++++++++++++++++++++++------------------------------- daemon.py | 2 +- tests.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 100 insertions(+), 48 deletions(-) diff --git a/content.py b/content.py index 81c85631f..be1a07503 100644 --- a/content.py +++ b/content.py @@ -1426,51 +1426,39 @@ def content_diff(content: str, prev_content: str) -> str: """ d = difflib.Differ() text1_lines = content.splitlines() - text2_lines = prev_content.splitlines() - diff = d.compare(text1_lines, text2_lines) + text1_sentences = [] + for line in text1_lines: + sentences = line.split('.') + for sentence in sentences: + text1_sentences.append(sentence.strip()) + + text2_lines = prev_content.splitlines() + text2_sentences = [] + for line in text2_lines: + sentences = line.split('.') + for sentence in sentences: + text2_sentences.append(sentence.strip()) + + diff = d.compare(text1_sentences, text2_sentences) - 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 += \ - '' - - while ctr < len(reference_text): - diff_text += reference_text[ctr] - ctr += 1 - diff_content = diff_content.replace(reference_text, diff_text) - return diff_content + if not diff_text: + diff_text = '

' + else: + diff_text += '
' + diff_text += '' + elif line.startswith('+ '): + if not diff_text: + diff_text = '

' + else: + diff_text += '
' + diff_text += \ + '' + if diff_text: + diff_text += '

' + return diff_text def create_edits_html(edits_json: {}, post_json_object: {}, @@ -1507,11 +1495,11 @@ def create_edits_html(edits_json: {}, post_json_object: {}, datetime_object = \ convert_published_to_local_timezone(datetime_object, timezone) modified_str = datetime_object.strftime("%a %b %d, %H:%M") - diff = '

' + modified_str + '

' + diff + '

' + diff = '

' + modified_str + '

' + diff edits_str += diff content = prev_content if not edits_str: return '' return '
' + \ translate['SHOW EDITS'] + '' + \ - edits_str + '
\n' + edits_str + '' diff --git a/daemon.py b/daemon.py index 3246da238..c9a4aa6f8 100644 --- a/daemon.py +++ b/daemon.py @@ -20792,7 +20792,7 @@ def run_daemon(check_actor_timeout: int, check_actor_timeout = 2 httpd.check_actor_timeout = check_actor_timeout - # how many hours after a post was publushed can a reply be made + # how many hours after a post was published can a reply be made default_reply_interval_hrs = 9999999 httpd.default_reply_interval_hrs = default_reply_interval_hrs diff --git a/tests.py b/tests.py index 2bc945046..6eacaa61f 100644 --- a/tests.py +++ b/tests.py @@ -129,6 +129,7 @@ from inbox import json_post_allows_comments from inbox import valid_inbox from inbox import valid_inbox_filenames from categories import guess_hashtag_category +from content import create_edits_html from content import content_diff from content import bold_reading_string from content import safe_web_text @@ -6921,15 +6922,78 @@ def _test_bold_reading() -> None: def _test_diff_content() -> None: print('diff_content') prev_content = \ - 'Some text before.\nThis is some content.\nThis is another line.' + 'Some text before.\n' + \ + 'Starting sentence. This is some content.\nThis is another line.' content = \ 'Some text before.\nThis is some more content.\nThis is another line.' result = content_diff(content, prev_content) - expected = 'Some text before.\n' + \ - 'This is some content.\nThis is another line.' + expected = \ + '



' + \ + '

' assert result == expected + content = \ + 'Some text before.\nThis is content.\nThis line.' + result = content_diff(content, prev_content) + expected = \ + '


' + \ + '
' + \ + '
' + \ + '
' + \ + '

' + assert result == expected + + translate = { + "SHOW EDITS": "SHOW EDITS" + } + timezone = 'Europe/Berlin' + content1 = \ + "

This is some content.

" + \ + "

Some other content.

" + content2 = \ + "

This is some previous content.

" + \ + "

Some other previous content.

" + content3 = \ + "

This is some more previous content.

" + \ + "

Some other previous content.

" + post_json_object = { + "object": { + "content": content1, + "published": "2020-12-14T00:08:06Z" + } + } + edits_json = { + "2020-12-14T00:05:19Z": { + "object": { + "content": content3, + "published": "2020-12-14T00:05:19Z" + } + }, + "2020-12-14T00:07:34Z": { + "object": { + "content": content2, + "published": "2020-12-14T00:07:34Z" + } + } + } + html_str = \ + create_edits_html(edits_json, post_json_object, translate, timezone) + assert html_str + expected = \ + '
SHOW EDITS' + \ + '

Mon Dec 14, 01:07



' + \ + '
' + \ + '

Mon Dec 14, 01:05

' + \ + '

' + assert html_str == expected + def run_all_tests(): base_dir = os.getcwd()