# HG changeset patch # User souliane # Date 1452686911 -3600 # Node ID bf7e468fe4403fe0dba1a575e175922af3d93536 # Parent 8e2c831073a6ef341a7aa61192b188ecc174235f tools (strings): add a parameter "new_target" to addURLToText diff -r 8e2c831073a6 -r bf7e468fe440 frontends/src/tools/strings.py --- a/frontends/src/tools/strings.py Wed Jan 13 13:07:36 2016 +0100 +++ b/frontends/src/tools/strings.py Wed Jan 13 13:08:31 2016 +0100 @@ -43,21 +43,30 @@ 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))