frontends(tools/strings): put URL regex outside of getURLParams and precompile it

This commit is contained in:
Goffi 2017-01-01 16:39:26 +01:00
commit 66e393336b

View file

@ -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 '<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):