diff --git a/content.py b/content.py index 113c359fc..58fd6eeea 100644 --- a/content.py +++ b/content.py @@ -1474,6 +1474,22 @@ def save_media_in_form_post(media_bytes, debug: bool, return filename, attachment_media_type +def combine_textarea_lines(text: str) -> str: + """Combines separate lines + """ + result = '' + ctr = 0 + paragraphs = text.split('\n\n') + for para in paragraphs: + para = para.replace('\n', ' ') + para = para.replace(' ', ' ') + if ctr > 0: + result += '\n\n' + result += para + ctr += 1 + return result + + def extract_text_fields_in_post(post_bytes, boundary: str, debug: bool, unit_test_data: str = None) -> {}: """Returns a dictionary containing the text fields of a http form POST @@ -1546,6 +1562,8 @@ def extract_text_fields_in_post(post_bytes, boundary: str, debug: bool, post_value += '\n' post_value += post_lines[line] fields[post_key] = urllib.parse.unquote(post_value) + if boundary == '--LYNX' and post_key in ('message', 'bio'): + fields[post_key] = combine_textarea_lines(fields[post_key]) return fields diff --git a/tests.py b/tests.py index b56e03bc7..1570202c1 100644 --- a/tests.py +++ b/tests.py @@ -133,6 +133,7 @@ from inbox import valid_inbox from inbox import valid_inbox_filenames from inbox import cache_svg_images from categories import guess_hashtag_category +from content import combine_textarea_lines from content import detect_dogwhistles from content import remove_script from content import create_edits_html @@ -7412,6 +7413,42 @@ def _test_text_standardize(): assert result == expected +def _test_combine_lines(): + print('combine_lines') + text = 'This is a test' + expected = text + result = combine_textarea_lines(text) + if result != expected: + print('expected: ' + expected) + print('result: ' + result) + assert result == expected + + text = 'First line.\n\nSecond line.' + expected = text + result = combine_textarea_lines(text) + if result != expected: + print('expected: ' + expected) + print('result: ' + result) + assert result == expected + + text = 'First\nline.\n\nSecond\nline.' + expected = 'First line.\n\nSecond line.' + result = combine_textarea_lines(text) + if result != expected: + print('expected: ' + expected) + print('result: ' + result) + assert result == expected + + # with extra space + text = 'First\nline.\n\nSecond \nline.' + expected = 'First line.\n\nSecond line.' + result = combine_textarea_lines(text) + if result != expected: + print('expected: ' + expected) + print('result: ' + result) + assert result == expected + + def run_all_tests(): base_dir = os.getcwd() print('Running tests...') @@ -7429,6 +7466,7 @@ def run_all_tests(): _test_checkbox_names() _test_thread_functions() _test_functions() + _test_combine_lines() _test_text_standardize() _test_dogwhistles() _test_remove_end_of_line()