diff --git a/superdesk/text_utils.py b/superdesk/text_utils.py index 94c3b2f473..8744749769 100644 --- a/superdesk/text_utils.py +++ b/superdesk/text_utils.py @@ -212,7 +212,12 @@ def replacement(match_object): # so return the whole match. return match_object.group(0) else: - return '{1}'.format(html.unescape(groups[1]), groups[1]) + url = html.unescape(groups[1]) + if url.startswith("www."): + url = "https://" + url + elif not url.startswith(("http://", "https://")): + url = "https://" + url + return '{1}'.format(url, groups[1]) value = EMAIL_REGEX.sub(r'\1', text) value = re.sub(URL_REGEX, replacement, value) diff --git a/tests/text_utils_test.py b/tests/text_utils_test.py index 30d591a3a8..0d202b3211 100644 --- a/tests/text_utils_test.py +++ b/tests/text_utils_test.py @@ -141,7 +141,7 @@ def test_convert_plain_text_to_html(self): test_strings = [ ["plain text", "
plain text
"], ["email me at baz@foobar.com", 'email me at baz@foobar.com
'], - ["find me on foobar.com", 'find me on foobar.com
'], + ["find me on foobar.com", 'find me on foobar.com
'], [ "https://www.foobar.com", '', @@ -152,7 +152,7 @@ def test_convert_plain_text_to_html(self): "https://www.foobar.com/test?one=two&three=4", ], ["https://foobar.com", ''], - ["www.foobar.com", ''], + ["www.foobar.com", ''], [ """Foo Bar @@ -164,8 +164,12 @@ def test_convert_plain_text_to_html(self): "Foo Bar PTY LTD
" "Ph +61 23456789
" 'Email: baz@foobar.com ' - 'Website: www.foobar.com
', + 'Website: www.foobar.com', ], ] for tests in test_strings: self.assertEqual(text_utils.plain_text_to_html(tests[0]), tests[1]) + + self.assertEqual( + text_utils.plain_text_to_html("foo.com"), '' + )