changeset 2107:2c31ddf633e5

frontends(tools/strings): put URL regex outside of getURLParams and precompile it
author Goffi <goffi@goffi.org>
date Sun, 01 Jan 2017 16:39:26 +0100
parents 5874da3811b7
children 70f23bc7859b
files frontends/src/tools/strings.py
diffstat 1 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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 '<a href="%s"%s class="url">%s</a>' % (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):