tools (strings): add a parameter "new_target" to addURLToText

This commit is contained in:
souliane 2016-01-13 13:08:31 +01:00
commit fab073076c

View file

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