From 954a02126eb21ca230319cd8ff62adf04d962de9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Thu, 2 Nov 2023 21:59:34 -0300 Subject: [PATCH] format_links: parse punctuation inside brackets Also, consolidate all punctuation tests into a single table-driven one. --- bookwyrm/tests/views/test_status.py | 37 ++++++++++++++--------------- bookwyrm/views/status.py | 6 +++++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 67e1f6d76..424698130 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -421,25 +421,24 @@ http://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/.', - ) - self.assertEqual( - views.status.format_links(f"{url}!?!"), - f'www.fish.com/!?!', - ) - - def test_format_links_punctuation_parens(self, *_): - """ignore trailing punctuation and brackets combined""" - # Period at the end, wrapped in brackets. - url = "http://www.fish.com" - self.assertEqual( - views.status.format_links(f"({url})."), - f'(www.fish.com).', - ) + """test many combinations of brackets, URLs, and punctuation""" + url = "https://bookwyrm.social" + html = f'bookwyrm.social' + test_table = [ + ("punct", f"text and {url}.", f"text and {html}."), + ("multi_punct", f"text, then {url}?...", f"text, then {html}?..."), + ("bracket_punct", f"here ({url}).", f"here ({html})."), + ("punct_bracket", f"there [{url}?]", f"there [{html}?]"), + ("punct_bracket_punct", f"not here? ({url}!).", f"not here? ({html}!)."), + ( + "multi_punct_bracket", + f"not there ({url}...);", + f"not there ({html}...);", + ), + ] + for desc, text, output in test_table: + with self.subTest(desc=desc): + self.assertEqual(views.status.format_links(text), output) def test_format_links_special_chars(self, *_): """find and format urls into a tags""" diff --git a/bookwyrm/views/status.py b/bookwyrm/views/status.py index 4c1d049df..8dab11a27 100644 --- a/bookwyrm/views/status.py +++ b/bookwyrm/views/status.py @@ -333,6 +333,12 @@ def _unwrap(text): # Split out wrapping chars. suffix = text[-1] + suffix prefix, text = text[:1], text[1:-1] + break # Nested wrappers not supported atm. + + if punct.search(text): + # Move inner punctuation to suffix segment. + text, inner_punct, _ = punct.split(text) + suffix = inner_punct + suffix return prefix, text, suffix