diff --git a/content.py b/content.py index 081d9e6b1..800510cbe 100644 --- a/content.py +++ b/content.py @@ -189,6 +189,27 @@ def html_replace_email_quote(content: str) -> str: return _remove_quotes_within_quotes(new_content) +def html_replace_inline_quotes(content: str) -> str: + """Replaces inline quotes within content with quotation marks + """ + if '

' not in content: + return content + sections = content.split('

') + ctr = 0 + new_content = '' + for section in sections: + if ctr == 0: + new_content = section + ctr += 1 + continue + if '

' in section: + new_content += '

"' + section.replace('

', '"

', 1) + else: + new_content += section + ctr += 1 + return new_content + + def html_replace_quote_marks(content: str) -> str: """Replaces quotes with html formatting "hello" becomes hello @@ -1384,6 +1405,7 @@ def add_html_tags(base_dir: str, http_prefix: str, by matching against known following accounts """ if content.startswith('

'): + content = html_replace_inline_quotes(content) content = html_replace_email_quote(content) return html_replace_quote_marks(content) max_word_length = 40 @@ -1529,6 +1551,7 @@ def add_html_tags(base_dir: str, http_prefix: str, content = remove_long_words(content, max_word_length, long_words_list) content = limit_repeated_words(content, 6) content = html_replace_email_quote(content) + content = html_replace_inline_quotes(content) content = html_replace_quote_marks(content) # undo replacements diff --git a/speaker.py b/speaker.py index 2736cd3b5..6f1fdf9ba 100644 --- a/speaker.py +++ b/speaker.py @@ -31,6 +31,7 @@ from utils import acct_dir from utils import local_actor_url from utils import get_actor_from_post from content import html_replace_quote_marks +from content import html_replace_inline_quotes SPEAKER_REMOVE_CHARS = ('.\n', '. ', ',', ';', '?', '!') @@ -418,7 +419,8 @@ def speakable_text(http_prefix: str, # replace some emoji before removing html if ' <3' in content: content = content.replace(' <3', ' ' + translate['heart']) - content = remove_html(html_replace_quote_marks(content)) + content = remove_html(html_replace_inline_quotes(content)) + content = html_replace_quote_marks(content) detected_links: list[str] = [] content = speaker_replace_links(http_prefix, nickname, domain, domain_full, @@ -459,7 +461,8 @@ def _post_to_speaker_json(base_dir: str, http_prefix: str, # replace some emoji before removing html if ' <3' in content: content = content.replace(' <3', ' ' + translate['heart']) - content = remove_html(html_replace_quote_marks(content)) + content = remove_html(html_replace_inline_quotes(content)) + content = html_replace_quote_marks(content) content = speaker_replace_links(http_prefix, nickname, domain, domain_full, content, translate, detected_links) diff --git a/tests.py b/tests.py index 5eeef3643..c04c3002b 100644 --- a/tests.py +++ b/tests.py @@ -171,6 +171,7 @@ from content import limit_repeated_words from content import switch_words from content import extract_text_fields_in_post from content import html_replace_email_quote +from content import html_replace_inline_quotes from content import html_replace_quote_marks from content import dangerous_css from content import add_web_links @@ -4673,7 +4674,7 @@ def _test_danger_markup(): assert not dangerous_markup(content, allow_local_network_access, []) -def _run_html_replace_quote_marks(): +def _test_html_replace_quote_marks(): print('html_replace_quote_marks') test_str = 'The "cat" "sat" on the mat' result = html_replace_quote_marks(test_str) @@ -4692,6 +4693,22 @@ def _run_html_replace_quote_marks(): assert result == '“hello” “test” html' +def _test_html_replace_inline_quotes(): + print('html_replace_inline_quotes') + test_str = 'The

cat

' + \ + '

sat

on the mat' + result = html_replace_inline_quotes(test_str) + assert result == 'The

"cat"

"sat"

on the mat' + + test_str = 'The cat sat on the mat' + result = html_replace_inline_quotes(test_str) + assert result == 'The cat sat on the mat' + + test_str = '

hello

' + result = html_replace_inline_quotes(test_str) + assert result == '

"hello"

' + + def _test_json_post_allows_comment(): print('test_json_post_allows_comments') post_json_object = { @@ -9662,7 +9679,8 @@ def run_all_tests(): _test_valid_content_warning() _test_remove_id_ending() _test_json_post_allows_comment() - _run_html_replace_quote_marks() + _test_html_replace_inline_quotes() + _test_html_replace_quote_marks() _test_danger_css(base_dir) _test_danger_markup() _test_strip_html() diff --git a/webapp_post.py b/webapp_post.py index 3d873e05a..ec6ba5cbd 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -94,6 +94,7 @@ from content import create_edits_html from content import bold_reading_string from content import limit_repeated_words from content import replace_emoji_from_tags +from content import html_replace_inline_quotes from content import html_replace_quote_marks from content import html_replace_email_quote from content import remove_text_formatting @@ -3132,6 +3133,7 @@ def individual_post_as_html(signing_priv_key_pem: str, object_content = \ switch_words(base_dir, nickname, domain, object_content) object_content = html_replace_email_quote(object_content) + object_content = html_replace_inline_quotes(object_content) object_content = html_replace_quote_marks(object_content) object_content = \ format_mixed_right_to_left(object_content, system_language)