annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT helpers methods for plugins
818
f8a7a046ff9c copyright update
Goffi <goffi@goffi.org>
parents: 784
diff changeset
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
6
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
11
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
16
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
19
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import re
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from __pyjamas__ import JS
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
22
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
23
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
24 def getURLParams(url):
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
25 """This comes from pyjamas.Location.makeUrlDict with a small change
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
26 to also parse full URLs, and parameters with no value specified
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
27 (in that case the default value "" is used).
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
28 @param url: any URL with or without parameters
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
29 @return: a dictionary of the parameters, if any was given, or {}
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
31 dict_ = {}
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
32 if "/" in url:
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
33 # keep the part after the last "/"
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
34 url = url[url.rindex("/") + 1:]
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
35 if url.startswith("?"):
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
36 # remove the first "?"
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
37 url = url[1:]
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
38 pairs = url.split("&")
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
39 for pair in pairs:
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
40 if len(pair) < 3:
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
41 continue
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
42 kv = pair.split("=", 1)
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
43 dict_[kv[0]] = kv[1] if len(kv) > 1 else ""
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return dict_
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
45
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
46
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
47 def addURLToText(text, new_target=True):
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
48 """Check a text for what looks like an URL and make it clickable.
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
49
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
50 @param string (unicode): text to process
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
51 @param new_target (bool): if True, make the link open in a new window
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
53 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
54 # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium).
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
55 # we use javascript as a workaround
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
56 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
57 target = ' target="_blank"' if new_target else ''
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
58 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g;
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
59 return text.replace(urlRegex, function(url) {
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
60 return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>';
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
61 })""")
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
62
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
63
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def addURLToImage(string):
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
65 """Check a XHTML text for what looks like an imageURL and make it clickable.
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
66
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
67 @param string (unicode): text to process
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
68 """
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
69 # XXX: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
784
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def repl(match):
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
71 url = match.group(1)
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
73 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
f3cd261ea12f browser side: workaround for a pyjamas bug which freeze the browser in some case with addURLToText regex
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return re.sub(pattern, repl, string)