annotate src/browser/sat_browser/strings.py @ 848:7dafa5ee809a

browser: replace re module usage by pure javascript
author souliane <souliane@mailoo.org>
date Fri, 15 Jan 2016 16:33:08 +0100
parents 73cc4658f431
children d32b754265a0
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 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
21
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 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
24 """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
25 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
26 (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
27 @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
28 @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
29 """
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 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
31 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
32 # 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
33 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
34 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
35 # 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44
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
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
46 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
47 """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
48
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
49 @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
50 @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
51 """
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
52 # FIXME: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
53 # 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
54 # 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
55 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript
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
56 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g;
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
57 var target = new_target ? ' target="_blank"' : '';
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 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
59 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
60 })""")
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
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
63 def addURLToImage(text):
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
64 """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
65
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
66 @param text (unicode): text to process
840
73cc4658f431 browser (strings): add a parameter "new_target" to addURLToText + fix a wrong import
souliane <souliane@mailoo.org>
parents: 818
diff changeset
67 """
848
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
68 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
69 JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g;
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
70 return text.replace(imgRegex, function(img, src) {
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
71 return '<a href="' + src + '" target="_blank">' + img + '</a>';
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
72 })""")
7dafa5ee809a browser: replace re module usage by pure javascript
souliane <souliane@mailoo.org>
parents: 840
diff changeset
73