From 9e07f094ada98aec9c1932bf3c3013c71d7da684 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 6 Jan 2021 10:08:43 -0800 Subject: [PATCH] Improves link detecting regex --- bookwyrm/outgoing.py | 3 ++- bookwyrm/tests/test_outgoing.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 7838b335..b438f5d1 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -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>\g<3>', content) diff --git a/bookwyrm/tests/test_outgoing.py b/bookwyrm/tests/test_outgoing.py index c99b89c0..498b43d9 100644 --- a/bookwyrm/tests/test_outgoing.py +++ b/bookwyrm/tests/test_outgoing.py @@ -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), + 'www.fish.com/' % url) + url = 'https://archive.org/details/dli.granth.72113/page/n25/mode/2up' + self.assertEqual( + outgoing.format_links(url), + '' \ + 'archive.org/details/dli.granth.72113/page/n25/mode/2up' \ + % url)