mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
format_links: parse punctuation inside brackets
Also, consolidate all punctuation tests into a single table-driven one.
This commit is contained in:
parent
7d13cbb10b
commit
954a02126e
2 changed files with 24 additions and 19 deletions
|
@ -421,25 +421,24 @@ http://www.fish.com/"""
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_format_links_punctuation(self, *_):
|
def test_format_links_punctuation(self, *_):
|
||||||
"""don’t take trailing punctuation into account pls"""
|
"""test many combinations of brackets, URLs, and punctuation"""
|
||||||
url = "http://www.fish.com/"
|
url = "https://bookwyrm.social"
|
||||||
self.assertEqual(
|
html = f'<a href="{url}">bookwyrm.social</a>'
|
||||||
views.status.format_links(f"{url}."),
|
test_table = [
|
||||||
f'<a href="{url}">www.fish.com/</a>.',
|
("punct", f"text and {url}.", f"text and {html}."),
|
||||||
)
|
("multi_punct", f"text, then {url}?...", f"text, then {html}?..."),
|
||||||
self.assertEqual(
|
("bracket_punct", f"here ({url}).", f"here ({html})."),
|
||||||
views.status.format_links(f"{url}!?!"),
|
("punct_bracket", f"there [{url}?]", f"there [{html}?]"),
|
||||||
f'<a href="{url}">www.fish.com/</a>!?!',
|
("punct_bracket_punct", f"not here? ({url}!).", f"not here? ({html}!)."),
|
||||||
)
|
(
|
||||||
|
"multi_punct_bracket",
|
||||||
def test_format_links_punctuation_parens(self, *_):
|
f"not there ({url}...);",
|
||||||
"""ignore trailing punctuation and brackets combined"""
|
f"not there ({html}...);",
|
||||||
# Period at the end, wrapped in brackets.
|
),
|
||||||
url = "http://www.fish.com"
|
]
|
||||||
self.assertEqual(
|
for desc, text, output in test_table:
|
||||||
views.status.format_links(f"({url})."),
|
with self.subTest(desc=desc):
|
||||||
f'(<a href="{url}">www.fish.com</a>).',
|
self.assertEqual(views.status.format_links(text), output)
|
||||||
)
|
|
||||||
|
|
||||||
def test_format_links_special_chars(self, *_):
|
def test_format_links_special_chars(self, *_):
|
||||||
"""find and format urls into a tags"""
|
"""find and format urls into a tags"""
|
||||||
|
|
|
@ -333,6 +333,12 @@ def _unwrap(text):
|
||||||
# Split out wrapping chars.
|
# Split out wrapping chars.
|
||||||
suffix = text[-1] + suffix
|
suffix = text[-1] + suffix
|
||||||
prefix, text = text[:1], text[1:-1]
|
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
|
return prefix, text, suffix
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue