forked from mirrors/bookwyrm
Improves link detecting regex
This commit is contained in:
parent
a25bc2383b
commit
9e07f094ad
2 changed files with 15 additions and 1 deletions
|
@ -294,8 +294,9 @@ def find_mentions(content):
|
|||
|
||||
|
||||
def format_links(content):
|
||||
''' detect and format links '''
|
||||
return re.sub(
|
||||
r'([^(href=")]|^)(https?:\/\/([\w\.\-_]+\.[a-z]{2,}(\/[\w\.\-_\/]+)?))',
|
||||
r'([^(href=")]|^)(https?:\/\/(%s([\w\.\-_\/])*))' % regex.domain,
|
||||
r'\g<1><a href="\g<2>">\g<3></a>',
|
||||
content)
|
||||
|
||||
|
|
|
@ -490,3 +490,16 @@ class Outgoing(TestCase):
|
|||
list(outgoing.find_mentions('@nutria@%s' % DOMAIN))[0],
|
||||
('@nutria@%s' % DOMAIN, user)
|
||||
)
|
||||
|
||||
def test_format_links(self):
|
||||
''' find and format urls into a tags '''
|
||||
url = 'http://www.fish.com/'
|
||||
self.assertEqual(
|
||||
outgoing.format_links(url),
|
||||
'<a href="%s">www.fish.com/</a>' % url)
|
||||
url = 'https://archive.org/details/dli.granth.72113/page/n25/mode/2up'
|
||||
self.assertEqual(
|
||||
outgoing.format_links(url),
|
||||
'<a href="%s">' \
|
||||
'archive.org/details/dli.granth.72113/page/n25/mode/2up</a>' \
|
||||
% url)
|
||||
|
|
Loading…
Reference in a new issue