comparison libervia/frontends/tools/strings.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/tools/strings.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
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 import re
21
22 # Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
23 RE_URL = re.compile(
24 r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:|geo:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))"""
25 )
26
27
28 # TODO: merge this class with an other module or at least rename it (strings is not a good name)
29
30
31 def get_url_params(url):
32 """This comes from pyjamas.Location.makeUrlDict with a small change
33 to also parse full URLs, and parameters with no value specified
34 (in that case the default value "" is used).
35 @param url: any URL with or without parameters
36 @return: a dictionary of the parameters, if any was given, or {}
37 """
38 dict_ = {}
39 if "/" in url:
40 # keep the part after the last "/"
41 url = url[url.rindex("/") + 1 :]
42 if url.startswith("?"):
43 # remove the first "?"
44 url = url[1:]
45 pairs = url.split("&")
46 for pair in pairs:
47 if len(pair) < 3:
48 continue
49 kv = pair.split("=", 1)
50 dict_[kv[0]] = kv[1] if len(kv) > 1 else ""
51 return dict_
52
53
54 def add_url_to_text(string, new_target=True):
55 """Check a text for what looks like an URL and make it clickable.
56
57 @param string (unicode): text to process
58 @param new_target (bool): if True, make the link open in a new window
59 """
60 # XXX: report any change to libervia.browser.strings.add_url_to_text
61 def repl(match):
62 url = match.group(0)
63 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url):
64 url = "http://" + url
65 target = ' target="_blank"' if new_target else ""
66 return '<a href="%s"%s class="url">%s</a>' % (url, target, match.group(0))
67
68 return RE_URL.sub(repl, string)
69
70
71 def add_url_to_image(string):
72 """Check a XHTML text for what looks like an imageURL and make it clickable.
73
74 @param string (unicode): text to process
75 """
76 # XXX: report any change to libervia.browser.strings.add_url_to_image
77 def repl(match):
78 url = match.group(1)
79 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0))
80
81 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>"""
82 return re.sub(pattern, repl, string)
83
84
85 def fix_xhtml_links(xhtml):
86 """Add http:// if the scheme is missing and force opening in a new window.
87
88 @param string (unicode): XHTML Content
89 """
90 subs = []
91 for match in re.finditer(r'<a( \w+="[^"]*")* ?/?>', xhtml):
92 tag = match.group(0)
93 url = re.search(r'href="([^"]*)"', tag)
94 if url and not url.group(1).startswith("#"): # skip internal anchor
95 if not re.search(r'target="([^"]*)"', tag): # no target
96 subs.append((tag, '<a target="_blank"%s' % tag[2:]))
97 if not re.match(r"^\w+://", url.group(1)): # no scheme
98 subs.append((url.group(0), 'href="http://%s"' % url.group(1)))
99
100 for url, new_url in subs:
101 xhtml = xhtml.replace(url, new_url)
102 return xhtml