Mercurial > libervia-web
annotate libervia/web/pages/_browser/template.py @ 1577:9ba532041a8e
browser (chat): implement message reactions.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 22 Nov 2023 16:31:36 +0100 |
parents | 8d61654cb34f |
children | 9865013da86c |
rev | line source |
---|---|
1298 | 1 """Integrate templating system using nunjucks""" |
2 | |
3 from js_modules.nunjucks import nunjucks | |
4 from browser import window, document | |
5 import javascript | |
6 | |
7 | |
8 safe = nunjucks.runtime.SafeString.new | |
9 env = nunjucks.configure( | |
10 window.templates_root_url, | |
11 { | |
12 'autoescape': True, | |
13 'trimBlocks': True, | |
14 'lstripBlocks': True, | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1333
diff
changeset
|
15 'web': {'use_cache': True}, |
1298 | 16 }) |
17 | |
18 nunjucks.installJinjaCompat() | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
19 env.addGlobal("profile", window.profile) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
20 env.addGlobal("csrf_token", window.csrf_token) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
21 # FIXME: integrate gettext or equivalent here |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
22 env.addGlobal("_", lambda txt: txt) |
1298 | 23 |
24 | |
25 class Indexer: | |
26 """Index global to a page""" | |
27 | |
28 def __init__(self): | |
29 self._indexes = {} | |
30 | |
31 def next(self, value): | |
32 if value not in self._indexes: | |
33 self._indexes[value] = 0 | |
34 return 0 | |
35 self._indexes[value] += 1 | |
36 return self._indexes[value] | |
37 | |
38 def current(self, value): | |
39 return self._indexes.get(value) | |
40 | |
41 | |
42 gidx = Indexer() | |
1529
de7e468e2d44
browser (template): fix use of `auto*` in `date_fmt`
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
43 # suffix used to avoid collision with IDs generated in static page |
1298 | 44 SCRIPT_SUFF = "__script__" |
45 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
46 def escape_html(txt): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
47 return ( |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
48 txt |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
49 .replace('&', '&') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
50 .replace('<', '<') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
51 .replace('>', '>') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
52 .replace('"', '"') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
53 ) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
54 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
55 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
56 def get_args(n_args, *sig_args, **sig_kwargs): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
57 """Retrieve function args when they are transmitted using nunjucks convention |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
58 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
59 cf. https://mozilla.github.io/nunjucks/templating.html#keyword-arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
60 @param n_args: argument from nunjucks call |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
61 @param sig_args: expected positional arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
62 @param sig_kwargs: expected keyword arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
63 @return: all expected arguments, with default value if not specified in nunjucks |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
64 """ |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
65 # nunjucks set kwargs in last argument |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
66 given_args = list(n_args) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
67 try: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
68 given_kwargs = given_args.pop().to_dict() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
69 except (AttributeError, IndexError): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
70 # we don't have a dict as last argument |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
71 # that happens when there is no keyword argument |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
72 given_args = list(n_args) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
73 given_kwargs = {} |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
74 ret = given_args[:len(sig_args)] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
75 # we check if we have remaining positional arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
76 # in which case they may be specified in keyword arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
77 for name in sig_args[len(given_args):]: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
78 try: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
79 value = given_kwargs.pop(name) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
80 except KeyError: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
81 raise ValueError(f"missing positional arguments {name!r}") |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
82 ret.append(value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
83 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
84 extra_pos_args = given_args[len(sig_args):] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
85 # and now the keyword arguments |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
86 for name, default in sig_kwargs.items(): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
87 if extra_pos_args: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
88 # kw args has been specified with a positional argument |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
89 ret.append(extra_pos_args.pop(0)) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
90 continue |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
91 value = given_kwargs.get(name, default) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
92 ret.append(value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
93 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
94 return ret |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
95 |
1298 | 96 |
97 def _next_gidx(value): | |
98 """Use next current global index as suffix""" | |
99 next_ = gidx.next(value) | |
100 return f"{value}{SCRIPT_SUFF}" if next_ == 0 else f"{value}_{SCRIPT_SUFF}{next_}" | |
101 | |
102 env.addFilter("next_gidx", _next_gidx) | |
103 | |
104 | |
105 def _cur_gidx(value): | |
106 """Use current current global index as suffix""" | |
107 current = gidx.current(value) | |
108 return f"{value}{SCRIPT_SUFF}" if not current else f"{value}_{SCRIPT_SUFF}{current}" | |
109 | |
110 env.addFilter("cur_gidx", _cur_gidx) | |
111 | |
112 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
113 def _xmlattr(d, autospace=True): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
114 if not d: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
115 return |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
116 d = d.to_dict() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
117 ret = [''] if autospace else [] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
118 for key, value in d.items(): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
119 if value is not None: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
120 ret.append(f'{escape_html(key)}="{escape_html(str(value))}"') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
121 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
122 return safe(' '.join(ret)) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
123 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
124 env.addFilter("xmlattr", _xmlattr) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
125 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
126 |
1298 | 127 def _tojson(value): |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
128 return safe(escape_html(window.JSON.stringify(value))) |
1298 | 129 |
130 env.addFilter("tojson", _tojson) | |
131 | |
132 | |
1533
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
133 def _media_type_main(value: str|None) -> str|None: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
134 """Return main type of a media type""" |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
135 if not value: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
136 return None |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
137 return value.partition("/")[0] |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
138 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
139 env.addFilter("media_type_main", _media_type_main) |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
140 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
141 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
142 def _media_type_sub(value: str|None) -> str|None: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
143 """Return main type of a media type""" |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
144 if not value: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
145 return None |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
146 return value.partition("/")[1] |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
147 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
148 env.addFilter("media_type_sub", _media_type_sub) |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
149 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
150 |
1298 | 151 def _icon_use(name, cls=""): |
1538
8d61654cb34f
browser (template): fix `icon_use` when no class is specified
Goffi <goffi@goffi.org>
parents:
1533
diff
changeset
|
152 if cls: |
8d61654cb34f
browser (template): fix `icon_use` when no class is specified
Goffi <goffi@goffi.org>
parents:
1533
diff
changeset
|
153 kwargs = cls.to_dict() |
8d61654cb34f
browser (template): fix `icon_use` when no class is specified
Goffi <goffi@goffi.org>
parents:
1533
diff
changeset
|
154 cls = kwargs.get('cls') |
1298 | 155 return safe( |
156 '<svg class="svg-icon{cls}" xmlns="http://www.w3.org/2000/svg" ' | |
157 'viewBox="0 0 100 100">\n' | |
158 ' <use href="#{name}"/>' | |
159 '</svg>\n'.format(name=name, cls=(" " + cls) if cls else "") | |
160 ) | |
161 | |
162 env.addGlobal("icon", _icon_use) | |
163 | |
164 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
165 def _date_fmt( |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
166 timestamp, *args |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
167 ): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
168 """Date formatting |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
169 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
170 cf. libervia.backend.tools.common.date_utils for arguments details |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
171 """ |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
172 fmt, date_only, auto_limit, auto_old_fmt, auto_new_fmt = get_args( |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
173 args, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short", |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
174 auto_new_fmt="relative", |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
175 ) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
176 from js_modules.moment import moment |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
177 date = moment.unix(timestamp) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
178 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
179 if fmt == "auto_day": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
180 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
181 if fmt == "auto": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
182 limit = moment().startOf('day').subtract(auto_limit, 'days') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
183 m_fmt = auto_old_fmt if date < limit else auto_new_fmt |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
184 |
1529
de7e468e2d44
browser (template): fix use of `auto*` in `date_fmt`
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
185 elif fmt == "short": |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
186 m_fmt = "DD/MM/YY" if date_only else "DD/MM/YY HH:mm" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
187 elif fmt == "medium": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
188 m_fmt = "ll" if date_only else "lll" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
189 elif fmt == "long": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
190 m_fmt = "LL" if date_only else "LLL" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
191 elif fmt == "full": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
192 m_fmt = "dddd, LL" if date_only else "LLLL" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
193 elif fmt == "relative": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
194 return date.fromNow() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
195 elif fmt == "iso": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
196 if date_only: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
197 m_fmt == "YYYY-MM-DD" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
198 else: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
199 return date.toISOString() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
200 else: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
201 raise NotImplementedError("free format is not implemented yet") |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
202 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
203 return date.format(m_fmt) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
204 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
205 env.addFilter("date_fmt", _date_fmt) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
206 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
207 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
208 class I18nExtension: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
209 """Extension to handle the {% trans %}{% endtrans %} statement""" |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
210 # FIXME: for now there is no translation, this extension only returns the string |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
211 # unmodified |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
212 tags = ['trans'] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
213 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
214 def parse(self, parser, nodes, lexer): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
215 tok = parser.nextToken() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
216 args = parser.parseSignature(None, True) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
217 parser.advanceAfterBlockEnd(tok.value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
218 body = parser.parseUntilBlocks('endtrans') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
219 parser.advanceAfterBlockEnd() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
220 return nodes.CallExtension.new(self._js_ext, 'run', args, [body]) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
221 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
222 def run(self, context, *args): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
223 body = args[-1] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
224 return body() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
225 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
226 @classmethod |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
227 def install(cls, env): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
228 ext = cls() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
229 ext_dict = { |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
230 "tags": ext.tags, |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
231 "parse": ext.parse, |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
232 "run": ext.run |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
233 } |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
234 ext._js_ext = javascript.pyobj2jsobj(ext_dict) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
235 env.addExtension(cls.__name__, ext._js_ext) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
236 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
237 I18nExtension.install(env) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
238 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
239 |
1298 | 240 class Template: |
241 | |
242 def __init__(self, tpl_name): | |
243 self._tpl = env.getTemplate(tpl_name, True) | |
244 | |
245 def render(self, context): | |
246 return self._tpl.render(context) | |
247 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
248 def get_elt(self, context=None): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
249 if context is None: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
250 context = {} |
1298 | 251 raw_html = self.render(context) |
252 template_elt = document.createElement('template') | |
253 template_elt.innerHTML = raw_html | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
254 return template_elt.content.firstElementChild |