# HG changeset patch # User Goffi # Date 1483285166 -3600 # Node ID 2c31ddf633e56fb61d8862de26cd0b8b8e61a526 # Parent 5874da3811b77363067f44ecdcfc3dbd1114494d frontends(tools/strings): put URL regex outside of getURLParams and precompile it diff -r 5874da3811b7 -r 2c31ddf633e5 frontends/src/tools/strings.py --- a/frontends/src/tools/strings.py Sun Jan 01 16:38:32 2017 +0100 +++ b/frontends/src/tools/strings.py Sun Jan 01 16:39:26 2017 +0100 @@ -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 @@ @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):