changeset 1790:bf7e468fe440

tools (strings): add a parameter "new_target" to addURLToText
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 13:08:31 +0100 (2016-01-13)
parents 8e2c831073a6
children 1359ad0b37c2
files frontends/src/tools/strings.py
diffstat 1 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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 '<a href="%s" target="_blank" class="url">%s</a>' % (url, match.group(0))
+        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)
 
 
 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 '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))