Retain whitespace in split for detecting urls

This commit is contained in:
Mouse Reeve 2021-09-02 13:29:05 -07:00
parent 5bfa09e411
commit ccc41deb36
3 changed files with 27 additions and 6 deletions

View file

@ -56,7 +56,7 @@ def truncatepath(value, arg):
@register.simple_tag(takes_context=False)
def get_book_cover_thumbnail(book, size="medium", ext="jpg"):
"""Returns a book thumbnail at the specified size and extension,
with fallback if needed"""
with fallback if needed"""
if size == "":
size = "medium"
try:

View file

@ -281,7 +281,7 @@ class StatusViews(TestCase):
("@nutria@%s" % DOMAIN, user),
)
def test_format_links(self, *_):
def test_format_links_simple_url(self, *_):
"""find and format urls into a tags"""
url = "http://www.fish.com/"
self.assertEqual(
@ -291,6 +291,27 @@ class StatusViews(TestCase):
views.status.format_links("(%s)" % url),
'(<a href="%s">www.fish.com/</a>)' % url,
)
def test_format_links_paragraph_break(self, *_):
"""find and format urls into a tags"""
url = """okay
http://www.fish.com/"""
self.assertEqual(
views.status.format_links(url),
'okay\n\n<a href="http://www.fish.com/">www.fish.com/</a>',
)
def test_format_links_parens(self, *_):
"""find and format urls into a tags"""
url = "http://www.fish.com/"
self.assertEqual(
views.status.format_links("(%s)" % url),
'(<a href="%s">www.fish.com/</a>)' % url,
)
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"
self.assertEqual(
views.status.format_links(url),

View file

@ -155,9 +155,11 @@ def format_links(content):
"""detect and format links"""
validator = URLValidator()
formatted_content = ""
split_content = content.split(" ")
split_content = re.split(r"(\s+)", content)
for index, potential_link in enumerate(split_content):
for potential_link in split_content:
if not potential_link:
continue
wrapped = _wrapped(potential_link)
if wrapped:
wrapper_close = potential_link[-1]
@ -182,8 +184,6 @@ def format_links(content):
if wrapped:
formatted_content += wrapper_close
if index < len(split_content) - 1:
formatted_content += " "
return formatted_content