Check for punctuation before checking for wrapping parenthesis

This allows to parse `(URL).` correctly, which was not detected
as URL before.
This commit is contained in:
Adeodato Simó 2023-10-09 20:54:06 -03:00
parent 3d123bc2f2
commit c3c22022f6
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
2 changed files with 16 additions and 5 deletions

View file

@ -436,6 +436,15 @@ http://www.fish.com/"""
f'<a href="{url}">www.fish.com/</a>.',
)
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'(<a href="{url}">www.fish.com</a>).',
)
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"

View file

@ -304,17 +304,19 @@ def format_links(content):
for potential_link in split_content:
if not potential_link:
continue
# FIXME: allow for multiple punctuation characters, e.g. `...` and `!?`.
ends_with_punctuation = _ends_with_punctuation(potential_link)
if ends_with_punctuation:
punctuation_glyph = potential_link[-1]
potential_link = potential_link[0:-1]
wrapped = _wrapped(potential_link)
if wrapped:
wrapper_close = potential_link[-1]
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)