Mercurial > libervia-web
annotate libervia/web/pages/_browser/template.py @ 1612:ebd538cb26cb
server, pages (files/view): use new `use_local_shared_tmp` option.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 12 Jun 2024 23:11:08 +0200 |
parents | 9865013da86c |
children |
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 | |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
6 import jid |
1298 | 7 |
8 | |
9 safe = nunjucks.runtime.SafeString.new | |
10 env = nunjucks.configure( | |
11 window.templates_root_url, | |
12 { | |
13 'autoescape': True, | |
14 'trimBlocks': True, | |
15 '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
|
16 'web': {'use_cache': True}, |
1298 | 17 }) |
18 | |
19 nunjucks.installJinjaCompat() | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
20 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
|
21 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
|
22 # 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
|
23 env.addGlobal("_", lambda txt: txt) |
1298 | 24 |
25 | |
26 class Indexer: | |
27 """Index global to a page""" | |
28 | |
29 def __init__(self): | |
30 self._indexes = {} | |
31 | |
32 def next(self, value): | |
33 if value not in self._indexes: | |
34 self._indexes[value] = 0 | |
35 return 0 | |
36 self._indexes[value] += 1 | |
37 return self._indexes[value] | |
38 | |
39 def current(self, value): | |
40 return self._indexes.get(value) | |
41 | |
42 | |
43 gidx = Indexer() | |
1529
de7e468e2d44
browser (template): fix use of `auto*` in `date_fmt`
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
44 # suffix used to avoid collision with IDs generated in static page |
1298 | 45 SCRIPT_SUFF = "__script__" |
46 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
47 def escape_html(txt): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
48 return ( |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
49 txt |
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 .replace('"', '"') |
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 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
57 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
|
58 """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
|
59 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
60 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
|
61 @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
|
62 @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
|
63 @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
|
64 @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
|
65 """ |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
66 # 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
|
67 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
|
68 try: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
69 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
|
70 except (AttributeError, IndexError): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
71 # 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
|
72 # 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
|
73 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
|
74 given_kwargs = {} |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
75 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
|
76 # 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
|
77 # 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
|
78 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
|
79 try: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
80 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
|
81 except KeyError: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
82 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
|
83 ret.append(value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
84 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
85 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
|
86 # 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
|
87 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
|
88 if extra_pos_args: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
89 # 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
|
90 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
|
91 continue |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
92 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
|
93 ret.append(value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
94 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
95 return ret |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
96 |
1298 | 97 |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
98 def _bare_jid(full_jid: str|jid.JID) -> str: |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
99 """Return the bare JID""" |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
100 return str(jid.JID(str(full_jid)).bare) |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
101 |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
102 env.addFilter("bare_jid", _bare_jid) |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
103 |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
104 |
1298 | 105 def _next_gidx(value): |
106 """Use next current global index as suffix""" | |
107 next_ = gidx.next(value) | |
108 return f"{value}{SCRIPT_SUFF}" if next_ == 0 else f"{value}_{SCRIPT_SUFF}{next_}" | |
109 | |
110 env.addFilter("next_gidx", _next_gidx) | |
111 | |
112 | |
113 def _cur_gidx(value): | |
114 """Use current current global index as suffix""" | |
115 current = gidx.current(value) | |
116 return f"{value}{SCRIPT_SUFF}" if not current else f"{value}_{SCRIPT_SUFF}{current}" | |
117 | |
118 env.addFilter("cur_gidx", _cur_gidx) | |
119 | |
120 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
121 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
|
122 if not d: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
123 return |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
124 d = d.to_dict() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
125 ret = [''] if autospace else [] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
126 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
|
127 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
|
128 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
|
129 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
130 return safe(' '.join(ret)) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
131 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
132 env.addFilter("xmlattr", _xmlattr) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
133 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
134 |
1298 | 135 def _tojson(value): |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
136 return safe(escape_html(window.JSON.stringify(value))) |
1298 | 137 |
138 env.addFilter("tojson", _tojson) | |
139 | |
140 | |
1533
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
141 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
|
142 """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
|
143 if not value: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
144 return None |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
145 return value.partition("/")[0] |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
146 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
147 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
|
148 |
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 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
|
151 """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
|
152 if not value: |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
153 return None |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
154 return value.partition("/")[1] |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
155 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
156 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
|
157 |
285c49d7aad3
_browser (template): add `media_type_main` and `media_type_sub` filters:
Goffi <goffi@goffi.org>
parents:
1529
diff
changeset
|
158 |
1298 | 159 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
|
160 if cls: |
8d61654cb34f
browser (template): fix `icon_use` when no class is specified
Goffi <goffi@goffi.org>
parents:
1533
diff
changeset
|
161 kwargs = cls.to_dict() |
8d61654cb34f
browser (template): fix `icon_use` when no class is specified
Goffi <goffi@goffi.org>
parents:
1533
diff
changeset
|
162 cls = kwargs.get('cls') |
1298 | 163 return safe( |
164 '<svg class="svg-icon{cls}" xmlns="http://www.w3.org/2000/svg" ' | |
165 'viewBox="0 0 100 100">\n' | |
166 ' <use href="#{name}"/>' | |
167 '</svg>\n'.format(name=name, cls=(" " + cls) if cls else "") | |
168 ) | |
169 | |
170 env.addGlobal("icon", _icon_use) | |
171 | |
172 | |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
173 def _date_fmt(timestamp, *args): |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
174 """Date formatting |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
175 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
176 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
|
177 """ |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
178 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
|
179 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
|
180 auto_new_fmt="relative", |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
181 ) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
182 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
|
183 date = moment.unix(timestamp) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
184 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
185 if fmt == "auto_day": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
186 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
187 |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
188 if fmt == "auto": |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
189 limit = moment().startOf('day').subtract(auto_limit, 'days') |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
190 fmt = auto_old_fmt if date < limit else auto_new_fmt |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
191 |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
192 format_mapping = { |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
193 "short": "DD/MM/YY" if date_only else "DD/MM/YY HH:mm", |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
194 "medium": "DD MMM YYYY" if date_only else "DD MMM YYYY HH:mm", |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
195 "long": "D MMMM YYYY" if date_only else "D MMMM YYYY HH:mm", |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
196 "full": "dddd, D MMMM YYYY" if date_only else "dddd, D MMMM YYYY HH:mm", |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
197 "relative": date.fromNow(), |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
198 "iso": date.toISOString() if not date_only else "YYYY-MM-DD" |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
199 } |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
200 |
1583
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
201 # if `fmt` doesn't match any mapping, it's a free format |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
202 m_fmt = format_mapping.get(fmt, fmt) |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
203 |
9865013da86c
browser(template): add `bare_jid` and fix `date_fmt`
Goffi <goffi@goffi.org>
parents:
1538
diff
changeset
|
204 return m_fmt if fmt in ["relative", "iso"] else date.format(m_fmt) |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
205 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
206 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
|
207 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
208 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
209 class I18nExtension: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
210 """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
|
211 # 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
|
212 # unmodified |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
213 tags = ['trans'] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
214 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
215 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
|
216 tok = parser.nextToken() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
217 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
|
218 parser.advanceAfterBlockEnd(tok.value) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
219 body = parser.parseUntilBlocks('endtrans') |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
220 parser.advanceAfterBlockEnd() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
221 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
|
222 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
223 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
|
224 body = args[-1] |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
225 return body() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
226 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
227 @classmethod |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
228 def install(cls, env): |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
229 ext = cls() |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
230 ext_dict = { |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
231 "tags": ext.tags, |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
232 "parse": ext.parse, |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
233 "run": ext.run |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
234 } |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
235 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
|
236 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
|
237 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
238 I18nExtension.install(env) |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
239 |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
240 |
1298 | 241 class Template: |
242 | |
243 def __init__(self, tpl_name): | |
244 self._tpl = env.getTemplate(tpl_name, True) | |
245 | |
246 def render(self, context): | |
247 return self._tpl.render(context) | |
248 | |
1306
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
249 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
|
250 if context is None: |
c07112ef01cd
browser (template): adapted filters/global/extensions to manage SàT templates:
Goffi <goffi@goffi.org>
parents:
1298
diff
changeset
|
251 context = {} |
1298 | 252 raw_html = self.render(context) |
253 template_elt = document.createElement('template') | |
254 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
|
255 return template_elt.content.firstElementChild |