Mercurial > libervia-web
comparison browser/sat_browser/strings.py @ 1124:28e3eb3bb217
files reorganisation and installation rework:
- files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory)
- VERSION file is now used, as for other SàT projects
- replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly
- removed check for data_dir if it's empty
- installation tested working in virtual env
- libervia launching script is now in bin/libervia
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 25 Aug 2018 17:59:48 +0200 |
parents | src/browser/sat_browser/strings.py@2aaac0605ae2 |
children |
comparison
equal
deleted
inserted
replaced
1123:63a4b8fe9782 | 1124:28e3eb3bb217 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT helpers methods for plugins | |
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from __pyjamas__ import JS | |
21 | |
22 | |
23 def getURLParams(url): | |
24 """This comes from pyjamas.Location.makeUrlDict with a small change | |
25 to also parse full URLs, and parameters with no value specified | |
26 (in that case the default value "" is used). | |
27 @param url: any URL with or without parameters | |
28 @return: a dictionary of the parameters, if any was given, or {} | |
29 """ | |
30 dict_ = {} | |
31 if "/" in url: | |
32 # keep the part after the last "/" | |
33 url = url[url.rindex("/") + 1:] | |
34 if url.startswith("?"): | |
35 # remove the first "?" | |
36 url = url[1:] | |
37 pairs = url.split("&") | |
38 for pair in pairs: | |
39 if len(pair) < 3: | |
40 continue | |
41 kv = pair.split("=", 1) | |
42 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" | |
43 return dict_ | |
44 | |
45 | |
46 def addURLToText(text, new_target=True): | |
47 """Check a text for what looks like an URL and make it clickable. | |
48 | |
49 @param string (unicode): text to process | |
50 @param new_target (bool): if True, make the link open in a new window | |
51 """ | |
52 # FIXME: Workaround for a pyjamas bug with regex, base method in sat.frontends.tools.strings | |
53 # In some case, Pyjamas' re module get crazy and freeze browsers (tested with Iceweasel and Chromium). | |
54 # we use javascript as a workaround | |
55 # This method is inspired from https://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript | |
56 JS("""var urlRegex = /(https?:\/\/[^\s]+)/g; | |
57 var target = new_target ? ' target="_blank"' : ''; | |
58 return text.replace(urlRegex, function(url) { | |
59 return '<a href="' + url + '"' + target + ' class="url">' + url + '</a>'; | |
60 })""") | |
61 | |
62 | |
63 def addURLToImage(text): | |
64 """Check a XHTML text for what looks like an imageURL and make it clickable. | |
65 | |
66 @param text (unicode): text to process | |
67 """ | |
68 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings | |
69 JS("""var imgRegex = /<img[^>]* src="([^"]+)"[^>]*>/g; | |
70 return text.replace(imgRegex, function(img, src) { | |
71 return '<a href="' + src + '" target="_blank">' + img + '</a>'; | |
72 })""") | |
73 | |
74 def fixXHTMLLinks(xhtml): | |
75 """Add http:// if the scheme is missing and force opening in a new window. | |
76 | |
77 @param string (unicode): XHTML Content | |
78 """ | |
79 # FIXME: Pyjamas re module is not stable so we use pure JS instead, base method in sat.frontends.tools.strings | |
80 JS("""var subs = []; | |
81 var tag_re = /<a(?: \w+="[^"]*")* ?\/?>/g; | |
82 var result; | |
83 while ((result = tag_re.exec(xhtml)) !== null) { | |
84 tag = result[0]; | |
85 var link_result = /href="([^"]*)"/.exec(tag); | |
86 if (link_result && !(link_result[1].startsWith("#"))) { // found a link which is not an internal anchor | |
87 var link = link_result[0]; | |
88 var url = link_result[1]; | |
89 if (! /target="([^"]*)"/.test(tag)) { // no target | |
90 subs.push([tag, '<a target="_blank"' + tag.slice(2, tag.length)]); | |
91 } | |
92 if (! /^\w+:\/\//.test(url)) { // no scheme | |
93 subs.push([link, 'href="http://' + url + '"']); | |
94 } | |
95 } | |
96 } | |
97 for (i in subs) { | |
98 xhtml = xhtml.replace(subs[i][0], subs[i][1]); | |
99 } | |
100 """) | |
101 return xhtml |