mirror of https://gitlab.com/bashrc2/epicyon
Fixing edit diffs
parent
8f6980ccab
commit
d7bb5dc1c7
74
content.py
74
content.py
|
@ -1426,51 +1426,39 @@ def content_diff(content: str, prev_content: str) -> str:
|
||||||
"""
|
"""
|
||||||
d = difflib.Differ()
|
d = difflib.Differ()
|
||||||
text1_lines = content.splitlines()
|
text1_lines = content.splitlines()
|
||||||
text2_lines = prev_content.splitlines()
|
text1_sentences = []
|
||||||
diff = d.compare(text1_lines, text2_lines)
|
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 = ''
|
diff_text = ''
|
||||||
reference_text = ''
|
|
||||||
change_positions = ''
|
|
||||||
for line in diff:
|
for line in diff:
|
||||||
if line.startswith('- '):
|
if line.startswith('- '):
|
||||||
reference_text = line[2:]
|
if not diff_text:
|
||||||
elif line.startswith('? '):
|
diff_text = '<p>'
|
||||||
change_positions = line[2:]
|
else:
|
||||||
state = 0
|
diff_text += '<br>'
|
||||||
ctr = 0
|
diff_text += '<label class="diff_add">+ ' + line[2:] + '</label>'
|
||||||
for changed_char in change_positions:
|
elif line.startswith('+ '):
|
||||||
if state == 0:
|
if not diff_text:
|
||||||
if changed_char == '-':
|
diff_text = '<p>'
|
||||||
diff_text += \
|
else:
|
||||||
'<label class="diff_add">' + reference_text[ctr]
|
diff_text += '<br>'
|
||||||
ctr += 1
|
diff_text += \
|
||||||
state = 1
|
'<label class="diff_remove">- ' + line[2:] + '</label>'
|
||||||
continue
|
if diff_text:
|
||||||
elif changed_char == '+':
|
diff_text += '</p>'
|
||||||
diff_text += \
|
return diff_text
|
||||||
'<label class="diff_remove">' + reference_text[ctr]
|
|
||||||
ctr += 1
|
|
||||||
state = 1
|
|
||||||
continue
|
|
||||||
elif state == 1:
|
|
||||||
if changed_char != '-' and changed_char != '+':
|
|
||||||
diff_text += '</label>' + reference_text[ctr]
|
|
||||||
ctr += 1
|
|
||||||
state = 0
|
|
||||||
continue
|
|
||||||
diff_text += reference_text[ctr]
|
|
||||||
ctr += 1
|
|
||||||
|
|
||||||
if state == 1:
|
|
||||||
diff_text += '</label>'
|
|
||||||
|
|
||||||
while ctr < len(reference_text):
|
|
||||||
diff_text += reference_text[ctr]
|
|
||||||
ctr += 1
|
|
||||||
diff_content = diff_content.replace(reference_text, diff_text)
|
|
||||||
return diff_content
|
|
||||||
|
|
||||||
|
|
||||||
def create_edits_html(edits_json: {}, post_json_object: {},
|
def create_edits_html(edits_json: {}, post_json_object: {},
|
||||||
|
@ -1507,11 +1495,11 @@ def create_edits_html(edits_json: {}, post_json_object: {},
|
||||||
datetime_object = \
|
datetime_object = \
|
||||||
convert_published_to_local_timezone(datetime_object, timezone)
|
convert_published_to_local_timezone(datetime_object, timezone)
|
||||||
modified_str = datetime_object.strftime("%a %b %d, %H:%M")
|
modified_str = datetime_object.strftime("%a %b %d, %H:%M")
|
||||||
diff = '<p><b>' + modified_str + '</b></p><p>' + diff + '</p>'
|
diff = '<p><b>' + modified_str + '</b></p>' + diff
|
||||||
edits_str += diff
|
edits_str += diff
|
||||||
content = prev_content
|
content = prev_content
|
||||||
if not edits_str:
|
if not edits_str:
|
||||||
return ''
|
return ''
|
||||||
return '<br><details><summary class="cw">' + \
|
return '<br><details><summary class="cw">' + \
|
||||||
translate['SHOW EDITS'] + '</summary>' + \
|
translate['SHOW EDITS'] + '</summary>' + \
|
||||||
edits_str + '</details>\n'
|
edits_str + '</details>'
|
||||||
|
|
|
@ -20792,7 +20792,7 @@ def run_daemon(check_actor_timeout: int,
|
||||||
check_actor_timeout = 2
|
check_actor_timeout = 2
|
||||||
httpd.check_actor_timeout = check_actor_timeout
|
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
|
default_reply_interval_hrs = 9999999
|
||||||
httpd.default_reply_interval_hrs = default_reply_interval_hrs
|
httpd.default_reply_interval_hrs = default_reply_interval_hrs
|
||||||
|
|
||||||
|
|
72
tests.py
72
tests.py
|
@ -129,6 +129,7 @@ from inbox import json_post_allows_comments
|
||||||
from inbox import valid_inbox
|
from inbox import valid_inbox
|
||||||
from inbox import valid_inbox_filenames
|
from inbox import valid_inbox_filenames
|
||||||
from categories import guess_hashtag_category
|
from categories import guess_hashtag_category
|
||||||
|
from content import create_edits_html
|
||||||
from content import content_diff
|
from content import content_diff
|
||||||
from content import bold_reading_string
|
from content import bold_reading_string
|
||||||
from content import safe_web_text
|
from content import safe_web_text
|
||||||
|
@ -6921,15 +6922,78 @@ def _test_bold_reading() -> None:
|
||||||
def _test_diff_content() -> None:
|
def _test_diff_content() -> None:
|
||||||
print('diff_content')
|
print('diff_content')
|
||||||
prev_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 = \
|
content = \
|
||||||
'Some text before.\nThis is some more content.\nThis is another line.'
|
'Some text before.\nThis is some more content.\nThis is another line.'
|
||||||
result = content_diff(content, prev_content)
|
result = content_diff(content, prev_content)
|
||||||
expected = 'Some text before.\n' + \
|
expected = \
|
||||||
'This is some <label class="diff_add">' + \
|
'<p><label class="diff_remove">' + \
|
||||||
'more </label>content.\nThis is another line.'
|
'- Starting sentence</label><br><label class="diff_add">' + \
|
||||||
|
'+ This is some more content</label><br>' + \
|
||||||
|
'<label class="diff_remove">- This is some content</label></p>'
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
content = \
|
||||||
|
'Some text before.\nThis is content.\nThis line.'
|
||||||
|
result = content_diff(content, prev_content)
|
||||||
|
expected = \
|
||||||
|
'<p><label class="diff_remove">- Starting sentence</label><br>' + \
|
||||||
|
'<label class="diff_add">+ This is content</label><br>' + \
|
||||||
|
'<label class="diff_remove">- This is some content</label><br>' + \
|
||||||
|
'<label class="diff_add">+ This line</label><br>' + \
|
||||||
|
'<label class="diff_remove">- This is another line</label></p>'
|
||||||
|
assert result == expected
|
||||||
|
|
||||||
|
translate = {
|
||||||
|
"SHOW EDITS": "SHOW EDITS"
|
||||||
|
}
|
||||||
|
timezone = 'Europe/Berlin'
|
||||||
|
content1 = \
|
||||||
|
"<p>This is some content.</p>" + \
|
||||||
|
"<p>Some other content.</p>"
|
||||||
|
content2 = \
|
||||||
|
"<p>This is some previous content.</p>" + \
|
||||||
|
"<p>Some other previous content.</p>"
|
||||||
|
content3 = \
|
||||||
|
"<p>This is some more previous content.</p>" + \
|
||||||
|
"<p>Some other previous content.</p>"
|
||||||
|
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 = \
|
||||||
|
'<br><details><summary class="cw">SHOW EDITS</summary>' + \
|
||||||
|
'<p><b>Mon Dec 14, 01:07</b></p><p><label class="diff_add">' + \
|
||||||
|
'+ This is some content</label><br><label class="diff_remove">' + \
|
||||||
|
'- This is some previous content</label><br>' + \
|
||||||
|
'<label class="diff_add">+ Some other content</label><br>' + \
|
||||||
|
'<label class="diff_remove">- Some other previous content' + \
|
||||||
|
'</label></p><p><b>Mon Dec 14, 01:05</b></p><p>' + \
|
||||||
|
'<label class="diff_add">+ This is some previous content' + \
|
||||||
|
'</label><br><label class="diff_remove">' + \
|
||||||
|
'- This is some more previous content</label></p></details>'
|
||||||
|
assert html_str == expected
|
||||||
|
|
||||||
|
|
||||||
def run_all_tests():
|
def run_all_tests():
|
||||||
base_dir = os.getcwd()
|
base_dir = os.getcwd()
|
||||||
|
|
Loading…
Reference in New Issue