Mercurial > libervia-web
annotate src/browser/sat_browser/strings.py @ 918:96a56856d357
pages (blog_new/articles): first concert Libervia page, blog articles:
This page is for now called blog_new to avoid conflict with former blog static page, but it will replace the older one when ready.
The page get target profile from URL, the request 10 last blog from it, and display it using blog/articles template.
Errors are not correctly handled for now (specially blog retrieval errors).
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 06 Mar 2017 00:05:31 +0100 |
parents | 2aaac0605ae2 |
children |
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 | 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 |
849
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
74 def fixXHTMLLinks(xhtml): |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
75 """Add http:// if the scheme is missing and force opening in a new window. |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
76 |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
77 @param string (unicode): XHTML Content |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
78 """ |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
79 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
80 JS("""var subs = []; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
81 var tag_re = /<a(?: \w+="[^"]*")* ?\/?>/g; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
82 var result; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
83 while ((result = tag_re.exec(xhtml)) !== null) { |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
84 tag = result[0]; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
85 var link_result = /href="([^"]*)"/.exec(tag); |
878
2aaac0605ae2
browser (strings): fixXHTMLLinks skips internal anchors (links starting with "#")
souliane <souliane@mailoo.org>
parents:
849
diff
changeset
|
86 if (link_result && !(link_result[1].startsWith("#"))) { // found a link which is not an internal anchor |
849
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
87 var link = link_result[0]; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
88 var url = link_result[1]; |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
89 if (! /target="([^"]*)"/.test(tag)) { // no target |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
90 subs.push([tag, '<a target="_blank"' + tag.slice(2, tag.length)]); |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
91 } |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
92 if (! /^\w+:\/\//.test(url)) { // no scheme |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
93 subs.push([link, 'href="http://' + url + '"']); |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
94 } |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
95 } |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
96 } |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
97 for (i in subs) { |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
98 xhtml = xhtml.replace(subs[i][0], subs[i][1]); |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
99 } |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
100 """) |
d32b754265a0
browser (strings, editor): add javascript version of fixXHTMLLinks and use it for displaying the rich blog messages (add links' missing scheme and open them in new tab)
souliane <souliane@mailoo.org>
parents:
848
diff
changeset
|
101 return xhtml |