Merge pull request #2933 from joachimesque/remove-links-trailing-punctuation

Remove links trailing punctuation
This commit is contained in:
Mouse Reeve 2023-07-31 18:58:39 -07:00 committed by GitHub
commit 2f8cf941af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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>)',
)
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, *_):
"""find and format urls into a tags"""
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]
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 ends with a punctuation glyph"""
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)