Mercurial > libervia-backend
comparison sat_frontends/tools/strings.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 559a625a236b |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
26 | 26 |
27 | 27 |
28 # 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) |
29 | 29 |
30 | 30 |
31 def getURLParams(url): | 31 def get_url_params(url): |
32 """This comes from pyjamas.Location.makeUrlDict with a small change | 32 """This comes from pyjamas.Location.makeUrlDict with a small change |
33 to also parse full URLs, and parameters with no value specified | 33 to also parse full URLs, and parameters with no value specified |
34 (in that case the default value "" is used). | 34 (in that case the default value "" is used). |
35 @param url: any URL with or without parameters | 35 @param url: any URL with or without parameters |
36 @return: a dictionary of the parameters, if any was given, or {} | 36 @return: a dictionary of the parameters, if any was given, or {} |
49 kv = pair.split("=", 1) | 49 kv = pair.split("=", 1) |
50 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" | 50 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" |
51 return dict_ | 51 return dict_ |
52 | 52 |
53 | 53 |
54 def addURLToText(string, new_target=True): | 54 def add_url_to_text(string, new_target=True): |
55 """Check a text for what looks like an URL and make it clickable. | 55 """Check a text for what looks like an URL and make it clickable. |
56 | 56 |
57 @param string (unicode): text to process | 57 @param string (unicode): text to process |
58 @param new_target (bool): if True, make the link open in a new window | 58 @param new_target (bool): if True, make the link open in a new window |
59 """ | 59 """ |
60 # XXX: report any change to libervia.browser.strings.addURLToText | 60 # XXX: report any change to libervia.browser.strings.add_url_to_text |
61 def repl(match): | 61 def repl(match): |
62 url = match.group(0) | 62 url = match.group(0) |
63 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): | 63 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): |
64 url = "http://" + url | 64 url = "http://" + url |
65 target = ' target="_blank"' if new_target else "" | 65 target = ' target="_blank"' if new_target else "" |
66 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 | 67 |
68 return RE_URL.sub(repl, string) | 68 return RE_URL.sub(repl, string) |
69 | 69 |
70 | 70 |
71 def addURLToImage(string): | 71 def add_url_to_image(string): |
72 """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. |
73 | 73 |
74 @param string (unicode): text to process | 74 @param string (unicode): text to process |
75 """ | 75 """ |
76 # XXX: report any change to libervia.browser.strings.addURLToImage | 76 # XXX: report any change to libervia.browser.strings.add_url_to_image |
77 def repl(match): | 77 def repl(match): |
78 url = match.group(1) | 78 url = match.group(1) |
79 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 | 80 |
81 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" | 81 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" |
82 return re.sub(pattern, repl, string) | 82 return re.sub(pattern, repl, string) |
83 | 83 |
84 | 84 |
85 def fixXHTMLLinks(xhtml): | 85 def fix_xhtml_links(xhtml): |
86 """Add http:// if the scheme is missing and force opening in a new window. | 86 """Add http:// if the scheme is missing and force opening in a new window. |
87 | 87 |
88 @param string (unicode): XHTML Content | 88 @param string (unicode): XHTML Content |
89 """ | 89 """ |
90 subs = [] | 90 subs = [] |