forked from mirrors/bookwyrm
formatting
This commit is contained in:
parent
ac6438147d
commit
6e628fed38
2 changed files with 12 additions and 12 deletions
|
@ -305,18 +305,16 @@ class StatusViews(TestCase):
|
|||
)
|
||||
url = "https://tech.lgbt/@bookwyrm"
|
||||
self.assertEqual(
|
||||
views.status.format_links(url),
|
||||
'<a href="%s">tech.lgbt/@bookwyrm</a>' % url
|
||||
views.status.format_links(url), '<a href="%s">tech.lgbt/@bookwyrm</a>' % url
|
||||
)
|
||||
url = "users.speakeasy.net/~lion/nb/book.pdf"
|
||||
self.assertEqual(
|
||||
views.status.format_links(url),
|
||||
'<a href="%s">users.speakeasy.net/~lion/nb/book.pdf</a>' % url
|
||||
'<a href="%s">users.speakeasy.net/~lion/nb/book.pdf</a>' % url,
|
||||
)
|
||||
url = "pkm.one/#/page/The%20Book%20which%20launched%20a%201000%20Note%20taking%20apps"
|
||||
self.assertEqual(
|
||||
views.status.format_links(url),
|
||||
'<a href="%s">%s</a>' % (url, url)
|
||||
views.status.format_links(url), '<a href="%s">%s</a>' % (url, url)
|
||||
)
|
||||
|
||||
def test_to_markdown(self, *_):
|
||||
|
|
|
@ -157,7 +157,7 @@ def format_links(content):
|
|||
formatted_content = ""
|
||||
for potential_link in content.split():
|
||||
try:
|
||||
# raises an error on anything that's not a valid
|
||||
# raises an error on anything that's not a valid
|
||||
URLValidator(potential_link)
|
||||
except (ValidationError, UnicodeError):
|
||||
formatted_content += potential_link + " "
|
||||
|
@ -165,32 +165,34 @@ def format_links(content):
|
|||
wrapped = _wrapped(potential_link)
|
||||
if wrapped:
|
||||
wrapper_close = potential_link[-1]
|
||||
formatted_content += potential_link[0]
|
||||
formatted_content += potential_link[0]
|
||||
potential_link = potential_link[1:-1]
|
||||
|
||||
# so we can use everything but the scheme in the presentation of the link
|
||||
url = urlparse(potential_link)
|
||||
link = url.netloc + url.path + url.params
|
||||
if url.query != "":
|
||||
link += "?" + url.query
|
||||
link += "?" + url.query
|
||||
if url.fragment != "":
|
||||
link += "#" + url.fragment
|
||||
|
||||
formatted_content += '<a href="%s">%s</a>' % (potential_link, link)
|
||||
|
||||
if wrapped:
|
||||
formatted_content += wrapper_close
|
||||
formatted_content += wrapper_close
|
||||
|
||||
return formatted_content
|
||||
|
||||
|
||||
def _wrapped(text):
|
||||
""" check if a line of text is wrapped in parentheses, square brackets or curly brackets. return wrapped status """
|
||||
wrappers = [("(", ")"), ("[","]"), ("{", "}")]
|
||||
"""check if a line of text is wrapped in parentheses, square brackets or curly brackets. return wrapped status"""
|
||||
wrappers = [("(", ")"), ("[", "]"), ("{", "}")]
|
||||
for w in wrappers:
|
||||
if text[0] == w[0] and text[-1] == w[-1]:
|
||||
return True
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def to_markdown(content):
|
||||
"""catch links and convert to markdown"""
|
||||
content = markdown(content)
|
||||
|
|
Loading…
Reference in a new issue