diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 5874d9f2f..750d1ea5f 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -428,6 +428,14 @@ http://www.fish.com/""" f'(www.fish.com/)', ) + def test_format_links_punctuation(self, *_): + """don’t take trailing punctuation into account pls""" + url = "http://www.fish.com/" + self.assertEqual( + views.status.format_links(f"{url}."), + f'www.fish.com/.', + ) + def test_format_links_special_chars(self, *_): """find and format urls into a tags""" url = "https://archive.org/details/dli.granth.72113/page/n25/mode/2up" diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index e3a7481f8..7670c2e8f 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -305,6 +305,11 @@ def format_links(content): formatted_content += potential_link[0] potential_link = potential_link[1:-1] + ends_with_punctuation = _ends_with_punctuation(potential_link) + if ends_with_punctuation: + punctuation_glyph = potential_link[-1] + potential_link = potential_link[0:-1] + try: # raises an error on anything that's not a valid link validator(potential_link) @@ -324,6 +329,9 @@ def format_links(content): if wrapped: formatted_content += wrapper_close + if ends_with_punctuation: + formatted_content += punctuation_glyph + return formatted_content @@ -336,6 +344,15 @@ def _wrapped(text): return False +def _ends_with_punctuation(text): + """check if a line of text is wrapped""" + glyphs = [".", ",", ";", ":", "!", "?", "”", "’", '"', "»"] + for glyph in glyphs: + if text[-1] == glyph: + return True + return False + + def to_markdown(content): """catch links and convert to markdown""" content = format_links(content)