Remove links trailing punctuation

This commit is contained in:
Joachim 2023-07-31 22:12:37 +02:00
parent c29ca5ad32
commit e37ed8ea5e
2 changed files with 25 additions and 0 deletions

View file

@ -428,6 +428,14 @@ http://www.fish.com/"""
f'(<a href="{url}">www.fish.com/</a>)', f'(<a href="{url}">www.fish.com/</a>)',
) )
def test_format_links_punctuation(self, *_):
"""dont take trailing punctuation into account pls"""
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, *_): def test_format_links_special_chars(self, *_):
"""find and format urls into a tags""" """find and format urls into a tags"""
url = "https://archive.org/details/dli.granth.72113/page/n25/mode/2up" url = "https://archive.org/details/dli.granth.72113/page/n25/mode/2up"

View file

@ -305,6 +305,11 @@ def format_links(content):
formatted_content += potential_link[0] formatted_content += potential_link[0]
potential_link = potential_link[1:-1] 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: try:
# raises an error on anything that's not a valid link # raises an error on anything that's not a valid link
validator(potential_link) validator(potential_link)
@ -324,6 +329,9 @@ def format_links(content):
if wrapped: if wrapped:
formatted_content += wrapper_close formatted_content += wrapper_close
if ends_with_punctuation:
formatted_content += punctuation_glyph
return formatted_content return formatted_content
@ -336,6 +344,15 @@ def _wrapped(text):
return False 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): def to_markdown(content):
"""catch links and convert to markdown""" """catch links and convert to markdown"""
content = format_links(content) content = format_links(content)