Mercurial > libervia-backend
comparison sat_frontends/tools/strings.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | a754523fc0f6 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 import re | 20 import re |
21 | 21 |
22 # Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls | 22 # Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls |
23 RE_URL = re.compile(r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""") | 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:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""" | |
25 ) | |
24 | 26 |
25 | 27 |
26 # TODO: merge this class with an other module or at least rename it (strings is not a good name) | 28 # TODO: merge this class with an other module or at least rename it (strings is not a good name) |
27 | 29 |
28 | 30 |
34 @return: a dictionary of the parameters, if any was given, or {} | 36 @return: a dictionary of the parameters, if any was given, or {} |
35 """ | 37 """ |
36 dict_ = {} | 38 dict_ = {} |
37 if "/" in url: | 39 if "/" in url: |
38 # keep the part after the last "/" | 40 # keep the part after the last "/" |
39 url = url[url.rindex("/") + 1:] | 41 url = url[url.rindex("/") + 1 :] |
40 if url.startswith("?"): | 42 if url.startswith("?"): |
41 # remove the first "?" | 43 # remove the first "?" |
42 url = url[1:] | 44 url = url[1:] |
43 pairs = url.split("&") | 45 pairs = url.split("&") |
44 for pair in pairs: | 46 for pair in pairs: |
58 # XXX: report any change to libervia.browser.strings.addURLToText | 60 # XXX: report any change to libervia.browser.strings.addURLToText |
59 def repl(match): | 61 def repl(match): |
60 url = match.group(0) | 62 url = match.group(0) |
61 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): | 63 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): |
62 url = "http://" + url | 64 url = "http://" + url |
63 target = ' target="_blank"' if new_target else '' | 65 target = ' target="_blank"' if new_target else "" |
64 return '<a href="%s"%s class="url">%s</a>' % (url, target, match.group(0)) | 66 return '<a href="%s"%s class="url">%s</a>' % (url, target, match.group(0)) |
67 | |
65 return RE_URL.sub(repl, string) | 68 return RE_URL.sub(repl, string) |
66 | 69 |
67 | 70 |
68 def addURLToImage(string): | 71 def addURLToImage(string): |
69 """Check a XHTML text for what looks like an imageURL and make it clickable. | 72 """Check a XHTML text for what looks like an imageURL and make it clickable. |
72 """ | 75 """ |
73 # XXX: report any change to libervia.browser.strings.addURLToImage | 76 # XXX: report any change to libervia.browser.strings.addURLToImage |
74 def repl(match): | 77 def repl(match): |
75 url = match.group(1) | 78 url = match.group(1) |
76 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) | 79 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) |
80 | |
77 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" | 81 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" |
78 return re.sub(pattern, repl, string) | 82 return re.sub(pattern, repl, string) |
79 | 83 |
80 | 84 |
81 def fixXHTMLLinks(xhtml): | 85 def fixXHTMLLinks(xhtml): |
94 subs.append((url.group(0), 'href="http://%s"' % url.group(1))) | 98 subs.append((url.group(0), 'href="http://%s"' % url.group(1))) |
95 | 99 |
96 for url, new_url in subs: | 100 for url, new_url in subs: |
97 xhtml = xhtml.replace(url, new_url) | 101 xhtml = xhtml.replace(url, new_url) |
98 return xhtml | 102 return xhtml |
99 |