From 66e393336bad355894dd575b471ebcb7d5a322f2 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 1 Jan 2017 16:39:26 +0100 Subject: [PATCH] frontends(tools/strings): put URL regex outside of getURLParams and precompile it --- frontends/src/tools/strings.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frontends/src/tools/strings.py b/frontends/src/tools/strings.py index 3baba574..8f293f87 100644 --- a/frontends/src/tools/strings.py +++ b/frontends/src/tools/strings.py @@ -19,6 +19,12 @@ import re +# Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls +RE_URL = re.compile(r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""") + + +# TODO: merge this class with an other module or at least rename it (strings is not a good name) + def getURLParams(url): """This comes from pyjamas.Location.makeUrlDict with a small change @@ -50,15 +56,13 @@ def addURLToText(string, new_target=True): @param new_target (bool): if True, make the link open in a new window """ # XXX: report any change to libervia.browser.strings.addURLToText - # Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls def repl(match): url = match.group(0) if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): url = "http://" + url target = ' target="_blank"' if new_target else '' return '%s' % (url, target, match.group(0)) - pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""" - return re.sub(pattern, repl, string) + return RE_URL.sub(repl, string) def addURLToImage(string):