From fab073076c3c8916652c69ff4e92e0ec7d09d200 Mon Sep 17 00:00:00 2001 From: souliane Date: Wed, 13 Jan 2016 13:08:31 +0100 Subject: [PATCH] tools (strings): add a parameter "new_target" to addURLToText --- frontends/src/tools/strings.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/frontends/src/tools/strings.py b/frontends/src/tools/strings.py index 5510b1a8..400f855b 100644 --- a/frontends/src/tools/strings.py +++ b/frontends/src/tools/strings.py @@ -43,21 +43,30 @@ def getURLParams(url): return dict_ -def addURLToText(string): - """Check a text for what looks like an URL and make it clickable. Regexp - from http://daringfireball.net/2010/07/improved_regex_for_matching_urls""" +def addURLToText(string, new_target=True): + """Check a text for what looks like an URL and make it clickable. + @param string (unicode): text to process + @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 - return '%s' % (url, match.group(0)) + 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) def addURLToImage(string): - """Check a XHTML text for what looks like an imageURL and make it clickable""" + """Check a XHTML text for what looks like an imageURL and make it clickable. + + @param string (unicode): text to process + """ + # XXX: report any change to libervia.browser.strings.addURLToImage def repl(match): url = match.group(1) return '%s' % (url, match.group(0))