comparison src/browser/sat_browser/strings.py @ 840:73cc4658f431

browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 13:12:58 +0100
parents f8a7a046ff9c
children 7dafa5ee809a
comparison
equal deleted inserted replaced
839:09ace5cbcb9b 840:73cc4658f431
42 kv = pair.split("=", 1) 42 kv = pair.split("=", 1)
43 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" 43 dict_[kv[0]] = kv[1] if len(kv) > 1 else ""
44 return dict_ 44 return dict_
45 45
46 46
47 def addURLToText(text): 47 def addURLToText(text, new_target=True):
48 """Workaround for a pyjamas bug with regex 48 """Check a text for what looks like an URL and make it clickable.
49 49
50 In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium). 50 @param string (unicode): text to process
51 we use javascript as a workaround 51 @param new_target (bool): if True, make the link open in a new window
52 This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
53 """ 52 """
53 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
54 # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium).
55 # we use javascript as a workaround
56 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
57 target = ' target="_blank"' if new_target else ''
54 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g; 58 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g;
55 return text.replace(urlRegex, function(url) { 59 return text.replace(urlRegex, function(url) {
56 return '<a href="' + url + '">' + url + '</a>'; 60 return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>';
57 })""") 61 })""")
58 62
59 63
60 def addURLToImage(string): 64 def addURLToImage(string):
61 """Check a XHTML text for what looks like an imageURL and make it clickable""" 65 """Check a XHTML text for what looks like an imageURL and make it clickable.
66
67 @param string (unicode): text to process
68 """
69 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
62 def repl(match): 70 def repl(match):
63 url = match.group(1) 71 url = match.group(1)
64 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) 72 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
65 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" 73 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
66 return re.sub(pattern, repl, string) 74 return re.sub(pattern, repl, string)