Mercurial > libervia-backend
annotate sat/tools/common/template.py @ 2579:787b15d16347
bridge: added generated pb files to repository (needed for Cagou on Android)
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 13 Apr 2018 18:45:43 +0200 |
parents | 26edcf3a30eb |
children | 5b26033c49a8 |
rev | line source |
---|---|
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ template generation """ |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
23 from sat.core.i18n import _ |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core import exceptions |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.core.log import getLogger |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 log = getLogger(__name__) |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import os.path |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
28 from xml.sax.saxutils import quoteattr |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
29 import datetime |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
30 import time |
2401
221478058d8a
template: improved attribute escaping, and added it to filters under the name "attr_escape"
Goffi <goffi@goffi.org>
parents:
2394
diff
changeset
|
31 import re |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
32 from babel import support |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
33 from babel import Locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
34 from babel.core import UnknownLocaleError |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
35 from babel import dates |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
36 import pygments |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
37 from pygments import lexers |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
38 from pygments import formatters |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 import sat_templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 except ImportError: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 raise exceptions.MissingModule(u'sat_templates module is not available, please install it or check your path to use template engine') |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 else: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 sat_templates # to avoid pyflakes warning |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 import jinja2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 except: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 raise exceptions.MissingModule(u'Missing module jinja2, please install it from http://jinja.pocoo.org or with pip install jinja2') |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
51 from jinja2 import Markup as safe |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
52 from jinja2 import is_undefined |
2515 | 53 from lxml import etree |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
54 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
55 HTML_EXT = ('html', 'xhtml') |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
56 DEFAULT_LOCALE = u'en_GB' |
2401
221478058d8a
template: improved attribute escaping, and added it to filters under the name "attr_escape"
Goffi <goffi@goffi.org>
parents:
2394
diff
changeset
|
57 RE_ATTR_ESCAPE = re.compile(r'[^a-z_-]') |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
58 # TODO: handle external path (an additional search path for templates should be settable by user |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
59 # TODO: handle absolute URL (should be used for trusted use cases) only (e.g. jp) for security reason |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
60 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
61 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
62 class TemplateLoader(jinja2.FileSystemLoader): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
63 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
64 def __init__(self): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
65 searchpath = os.path.dirname(sat_templates.__file__) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
66 super(TemplateLoader, self).__init__(searchpath, followlinks=True) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
67 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
68 def parse_template(self, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
69 """parse template path and return theme and relative URL |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
70 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
71 @param template_path(unicode): path to template with parenthesis syntax |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
72 @return (tuple[(unicode,None),unicode]): theme and template_path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
73 theme can be None if relative path is used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
74 relative path is the path from search path with theme specified |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
75 e.g. default/blog/articles.html |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
76 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
77 if template.startswith(u'('): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
78 try: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
79 theme_end = template.index(u')') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
80 except IndexError: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
81 raise ValueError(u"incorrect theme in template") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
82 theme = template[1:theme_end] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
83 template = template[theme_end+1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
84 if not template or template.startswith(u'/'): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
85 raise ValueError(u"incorrect path after template name") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
86 template = os.path.join(theme, template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
87 elif template.startswith(u'/'): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
88 # absolute path means no template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
89 theme = None |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
90 raise NotImplementedError(u'absolute path is not implemented yet') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
91 else: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
92 theme = C.TEMPLATE_THEME_DEFAULT |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
93 template = os.path.join(theme, template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
94 return theme, template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
95 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
96 def get_default_template(self, theme, template_path): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
97 """return default template path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
98 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
99 @param theme(unicode): theme used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
100 @param template_path(unicode): path to the not found template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
101 @return (unicode, None): default path or None if there is not |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
102 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
103 ext = os.path.splitext(template_path)[1][1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
104 path_elems = template_path.split(u'/') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
105 if ext in HTML_EXT: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
106 if path_elems[1] == u'error': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
107 # if an inexisting error page is requested, we return base page |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
108 default_path = os.path.join(theme, u'error/base.html') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
109 return default_path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
110 if theme != C.TEMPLATE_THEME_DEFAULT: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
111 # if template doesn't exists for this theme, we try with default |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
112 return os.path.join(C.TEMPLATE_THEME_DEFAULT, path_elems[1:]) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
113 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
114 def get_source(self, environment, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
115 """relative path to template dir, with special theme handling |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
116 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
117 if the path is just relative, "default" theme is used. |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
118 The theme can be specified in parenthesis just before the path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
119 e.g.: (some_theme)path/to/template.html |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
120 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
121 theme, template_path = self.parse_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
122 try: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
123 return super(TemplateLoader, self).get_source(environment, template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
124 except jinja2.exceptions.TemplateNotFound as e: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
125 # in some special cases, a defaut template is returned if nothing is found |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
126 if theme is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
127 default_path = self.get_default_template(theme, template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
128 if default_path is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
129 return super(TemplateLoader, self).get_source(environment, default_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
130 # if no default template is found, we re-raise the error |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
131 raise e |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
132 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
134 class Indexer(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
135 """Index global to a page""" |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
136 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
137 def __init__(self): |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
138 self._indexes = {} |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
139 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
140 def next(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
141 if value not in self._indexes: |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
142 self._indexes[value] = 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
143 return 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
144 self._indexes[value] += 1 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
145 return self._indexes[value] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
146 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
147 def current(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
148 return self._indexes.get(value) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
149 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
150 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
151 class ScriptsHandler(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
152 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
153 def __init__(self, renderer, template_path, template_root_dir, root_path): |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
154 self.renderer = renderer |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
155 self.template_root_dir = template_root_dir |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
156 self.root_path = root_path |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
157 self.scripts = [] # we don't use a set because order may be important |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
158 dummy, self.theme, self.is_default_theme = renderer.getThemeData(template_path) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
159 |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
160 def include(self, library_name, attribute='defer'): |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
161 """Mark that a script need to be imported. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
162 |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
163 Must be used before base.html is extended, as <script> are generated there. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
164 If called several time with the same library, it will be imported once. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
165 @param library_name(unicode): name of the library to import |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
166 @param loading: |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
167 """ |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
168 if attribute not in ('defer', 'async', ''): |
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
169 raise exceptions.DataError(_(u'Invalid attribute, please use one of "defer", "async" or ""')) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
170 if library_name.endswith('.js'): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
171 library_name = library_name[:-3] |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
172 if library_name not in self.scripts: |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
173 self.scripts.append((library_name, attribute)) |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
174 return u'' |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
175 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
176 def generate_scripts(self): |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
177 """Generate the <script> elements |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
178 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
179 @return (unicode): <scripts> HTML tags |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
180 """ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
181 scripts = [] |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
182 tpl = u'<script src={src} {attribute}></script>' |
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
183 for library, attribute in self.scripts: |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
184 path = self.renderer.getStaticPath(library, self.template_root_dir, self.theme, self.is_default_theme, '.js') |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
185 if path is None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
186 log.warning(_(u"Can't find {}.js javascript library").format(library)) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
187 continue |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
188 path = os.path.join(self.root_path, path) |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
189 scripts.append(tpl.format( |
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
190 src = quoteattr(path), |
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
191 attribute = attribute, |
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
192 )) |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
193 return safe(u'\n'.join(scripts)) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
194 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
195 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 class Renderer(object): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
198 def __init__(self, host): |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 self.host = host |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
200 self.base_dir = os.path.dirname(sat_templates.__file__) # FIXME: should be modified if we handle use extra dirs |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 self.env = jinja2.Environment( |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
202 loader=TemplateLoader(), |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 autoescape=jinja2.select_autoescape(['html', 'xhtml', 'xml']), |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 trim_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 lstrip_blocks=True, |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
206 extensions=['jinja2.ext.i18n'], |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 ) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
208 self._locale_str = DEFAULT_LOCALE |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
209 self._locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
210 self.installTranslations() |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 # we want to have access to SàT constants in templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 self.env.globals[u'C'] = C |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
213 # custom filters |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
214 self.env.filters['next_gidx'] = self._next_gidx |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
215 self.env.filters['cur_gidx'] = self._cur_gidx |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
216 self.env.filters['date_fmt'] = self._date_fmt |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
217 self.env.filters['xmlui_class'] = self._xmlui_class |
2401
221478058d8a
template: improved attribute escaping, and added it to filters under the name "attr_escape"
Goffi <goffi@goffi.org>
parents:
2394
diff
changeset
|
218 self.env.filters['attr_escape'] = self.attr_escape |
2402 | 219 self.env.filters['item_filter'] = self._item_filter |
2391
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
220 self.env.filters['adv_format'] = self._adv_format |
2425
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
221 self.env.filters['dict_ext'] = self._dict_ext |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
222 self.env.filters['highlight'] = self.highlight |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
223 # custom tests |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
224 self.env.tests['in_the_past'] = self._in_the_past |
2515 | 225 self.icons_path = os.path.join(host.media_dir, u'fonts/fontello/svg') |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
227 def installTranslations(self): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
228 i18n_dir = os.path.join(self.base_dir, 'i18n') |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
229 self.translations = {} |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
230 for lang_dir in os.listdir(i18n_dir): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
231 lang_path = os.path.join(i18n_dir, lang_dir) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
232 if not os.path.isdir(lang_path): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
233 continue |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
234 po_path = os.path.join(lang_path, 'LC_MESSAGES/sat.mo') |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
235 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
236 with open(po_path, 'rb') as f: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
237 self.translations[Locale.parse(lang_dir)] = support.Translations(f, 'sat') |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
238 except EnvironmentError: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
239 log.error(_(u"Can't find template translation at {path}").format(path = po_path)) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
240 except UnknownLocaleError as e: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
241 log.error(_(u"Invalid locale name: {msg}").format(msg=e)) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
242 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
243 log.info(_(u'loaded {lang} templates translations').format(lang=lang_dir)) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
244 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
245 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
246 def setLocale(self, locale_str): |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
247 """set current locale |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
248 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
249 change current translation locale and self self._locale and self._locale_str |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
250 """ |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
251 if locale_str == self._locale_str: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
252 return |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
253 if locale_str == 'en': |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
254 # we default to GB English when it's not specified |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
255 # one of the main reason is to avoid the nonsense U.S. short date format |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
256 locale_str = 'en_GB' |
2323
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
257 try: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
258 locale = Locale.parse(locale_str) |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
259 except ValueError as e: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
260 log.warning(_(u"invalid locale value: {msg}").format(msg=e)) |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
261 locale_str = self._locale_str = DEFAULT_LOCALE |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
262 locale = Locale.parse(locale_str) |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
263 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
264 locale_str = unicode(locale) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
265 if locale_str != DEFAULT_LOCALE: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
266 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
267 translations = self.translations[locale] |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
268 except KeyError: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
269 log.warning(_(u"Can't find locale {locale}".format(locale=locale))) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
270 locale_str = DEFAULT_LOCALE |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
271 locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
272 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
273 self.env.install_gettext_translations(translations, True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
274 log.debug(_(u'Switched to {lang}').format(lang=locale.english_name)) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
275 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
276 if locale_str == DEFAULT_LOCALE: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
277 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
278 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
279 self._locale = locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
280 self._locale_str = locale_str |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
281 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
282 def getThemeAndRoot(self, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
283 """retrieve theme and root dir of a given tempalte |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
284 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
285 @param template(unicode): template to parse |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
286 @return (tuple[unicode, unicode]): theme and absolute path to theme's root dir |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
287 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
288 theme, dummy = self.env.loader.parse_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
289 return theme, os.path.join(self.base_dir, theme) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
290 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
291 def getStaticPath(self, name, template_root_dir, theme, is_default, ext='.css'): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
292 """retrieve path of a static file if it exists with current theme or default |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
293 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
294 File will be looked at [theme]/static/[name][ext], and then default |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
295 if not found. |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
296 @param name(unicode): name of the file to look for |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
297 @param template_root_dir(unicode): absolute path to template root used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
298 @param theme(unicode): name of the template theme used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
299 @param is_default(bool): True if theme is the default theme |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
300 @return (unicode, None): relative path if found, else None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
301 """ |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
302 file_ = None |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
303 path = os.path.join(theme, C.TEMPLATE_STATIC_DIR, name + ext) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
304 if os.path.exists(os.path.join(template_root_dir, path)): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
305 file_ = path |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
306 elif not is_default: |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
307 path = os.path.join(C.TEMPLATE_THEME_DEFAULT, C.TEMPLATE_STATIC_DIR, name + ext) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
308 if os.path.exists(os.path.join(template_root_dir, path)): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
309 file_.append(path) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
310 return file_ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
311 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
312 def getThemeData(self, template_path): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
313 """return template data got from template_path |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
314 |
2452
7aa863cbc47f
template: remove extension from elements while looking for a path:
Goffi <goffi@goffi.org>
parents:
2425
diff
changeset
|
315 @return tuple(unicode, unicode, bool): |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
316 path_elems: elements of the path |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
317 theme: theme of the page |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
318 is_default: True if the theme is the default theme |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
319 """ |
2452
7aa863cbc47f
template: remove extension from elements while looking for a path:
Goffi <goffi@goffi.org>
parents:
2425
diff
changeset
|
320 path_elems = [os.path.splitext(p)[0] for p in template_path.split(u'/')] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
321 theme = path_elems.pop(0) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
322 is_default = theme == C.TEMPLATE_THEME_DEFAULT |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
323 return (path_elems, theme, is_default) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
324 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
325 def getCSSFiles(self, template_path, template_root_dir): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
326 """retrieve CSS files to use according to theme and template path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
327 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
328 for each element of the path, a .css file is looked for in /static, and returned if it exists. |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
329 previous element are kept by replacing '/' with '_', and styles.css is always returned. |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
330 For instance, if template_path is some_theme/blog/articles.html: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
331 some_theme/static/styles.css is returned if it exists else default/static/styles.css |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
332 some_theme/static/blog.css is returned if it exists else default/static/blog.css (if it exists too) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
333 some_theme/static/blog_articles.css is returned if it exists else default/static/blog_articles.css (if it exists too) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
334 @param template_path(unicode): relative path to template file (e.g. some_theme/blog/articles.html) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
335 @param template_root_dir(unicode): absolute path of the theme root dir used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
336 @return list[unicode]: relative path to CSS files to use |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
337 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
338 # TODO: some caching would be nice |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
339 css_files = [] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
340 path_elems, theme, is_default = self.getThemeData(template_path) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
341 for css in (u'fonts', u'styles'): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
342 css_path = self.getStaticPath(css, template_root_dir, theme, is_default) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
343 if css_path is not None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
344 css_files.append(css_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
345 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
346 for idx, path in enumerate(path_elems): |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
347 css_path = self.getStaticPath(u'_'.join(path_elems[:idx+1]), template_root_dir, theme, is_default) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
348 if css_path is not None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
349 css_files.append(css_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
350 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
351 return css_files |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
352 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
353 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
354 ## custom filters ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
355 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
356 @jinja2.contextfilter |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
357 def _next_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
358 """Use next current global index as suffix""" |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
359 next_ = ctx['gidx'].next(value) |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
360 return value if next_ == 0 else u"{}_{}".format(value, next_) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
361 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
362 @jinja2.contextfilter |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
363 def _cur_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
364 """Use current current global index as suffix""" |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
365 current = ctx['gidx'].current(value) |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
366 return value if not current else u"{}_{}".format(value, current) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
367 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
368 def _date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=None, auto_old_fmt=None): |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
369 try: |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
370 return self.date_fmt(timestamp, fmt, date_only, auto_limit, auto_old_fmt) |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
371 except Exception as e: |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
372 log.warning(_(u"Can't parse date: {msg}").format(msg=e)) |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
373 return timestamp |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
374 |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
375 def date_fmt(self, timestamp, fmt='short', date_only=False, auto_limit=7, auto_old_fmt='short', auto_new_fmt='relative'): |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
376 """format date according to locale |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
377 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
378 @param timestamp(basestring, int): unix time |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
379 @param fmt(str): one of: |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
380 - short: e.g. u'31/12/17' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
381 - medium: e.g. u'Apr 1, 2007' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
382 - long: e.g. u'April 1, 2007' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
383 - full: e.g. u'Sunday, April 1, 2007' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
384 - relative: format in relative time |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
385 e.g.: 3 hours |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
386 note that this format is not precise |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
387 - iso: ISO 8601 format |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
388 e.g.: u'2007-04-01T19:53:23Z' |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
389 - auto: use auto_old_fmt if date is older than auto_limit |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
390 else use auto_new_fmt |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
391 - auto_day: shorcut to set auto format with change on day |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
392 old format will be short, and new format will be time only |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
393 or a free value which is passed to babel.dates.format_datetime |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
394 @param date_only(bool): if True, only display date (not datetime) |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
395 @param auto_limit (int): limit in days before using auto_old_fmt |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
396 use 0 to have a limit at last midnight (day change) |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
397 @param auto_old_fmt(unicode): format to use when date is older than limit |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
398 @param auto_new_fmt(unicode): format to use when date is equal to or more recent |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
399 than limit |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
400 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
401 """ |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
402 if is_undefined(fmt): |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
403 fmt = u'short' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
404 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
405 if (auto_limit is not None or auto_old_fmt is not None) and fmt != 'auto': |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
406 raise ValueError(u'auto argument can only be used with auto fmt') |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
407 if fmt == 'auto_day': |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
408 fmt, auto_limit, auto_old_fmt, auto_new_fmt = 'auto', 0, 'short', 'HH:mm' |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
409 if fmt == 'auto': |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
410 if auto_limit == 0: |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
411 today = time.mktime(datetime.date.today().timetuple()) |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
412 if int(timestamp) < today: |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
413 fmt = auto_old_fmt |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
414 else: |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
415 fmt = auto_new_fmt |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
416 else: |
2466
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
417 days_delta = (time.time() - int(timestamp)) / 3600 |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
418 if days_delta > (auto_limit or 7): |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
419 fmt = auto_old_fmt |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
420 else: |
d2e16a7466a0
template: date filter improvments:
Goffi <goffi@goffi.org>
parents:
2465
diff
changeset
|
421 fmt = auto_new_fmt |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
422 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
423 if fmt == 'relative': |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
424 delta = int(timestamp) - time.time() |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
425 return dates.format_timedelta(delta, granularity="minute", add_direction=True, locale=self._locale_str) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
426 elif fmt in ('short', 'long'): |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
427 formatter = dates.format_date if date_only else dates.format_datetime |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
428 return formatter(int(timestamp), format=fmt, locale=self._locale_str) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
429 elif fmt == 'iso': |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
430 if date_only: |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
431 fmt = 'yyyy-MM-dd' |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
432 else: |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
433 fmt = "yyyy-MM-ddTHH:mm:ss'Z'" |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
434 return dates.format_datetime(int(timestamp), format=fmt) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
435 else: |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
436 return dates.format_datetime(int(timestamp), format=fmt, locale=self._locale_str) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
437 |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
438 def attr_escape(self, text): |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
439 """escape a text to a value usable as an attribute |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
440 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
441 remove spaces, and put in lower case |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
442 """ |
2401
221478058d8a
template: improved attribute escaping, and added it to filters under the name "attr_escape"
Goffi <goffi@goffi.org>
parents:
2394
diff
changeset
|
443 return RE_ATTR_ESCAPE.sub(u'_', text.strip().lower())[:50] |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
444 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
445 def _xmlui_class(self, xmlui_item, fields): |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
446 """return classes computed from XMLUI fields name |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
447 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
448 will return a string with a series of escaped {name}_{value} separated by spaces. |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
449 @param xmlui_item(xmlui.XMLUIPanel): XMLUI containing the widgets to use |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
450 @param fields(iterable(unicode)): names of the widgets to use |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
451 @return (unicode, None): computer string to use as class attribute value |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
452 None if no field was specified |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
453 """ |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
454 classes = [] |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
455 for name in fields: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
456 escaped_name = self.attr_escape(name) |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
457 try: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
458 for value in xmlui_item.widgets[name].values: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
459 classes.append(escaped_name + '_' + self.attr_escape(value)) |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
460 except KeyError: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
461 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
462 continue |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
463 return u' '.join(classes) or None |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
464 |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
465 @jinja2.contextfilter |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
466 def _item_filter(self, ctx, item, filters): |
2402 | 467 """return item's value, filtered if suitable |
468 | |
469 @param item(object): item to filter | |
470 value must have name and value attributes, | |
471 mostly used for XMLUI items | |
472 @param filters(dict[unicode, (callable, dict, None)]): map of name => filter | |
473 if filter is None, return the value unchanged | |
474 if filter is a callable, apply it | |
475 if filter is a dict, it can have following keys: | |
476 - filters: iterable of filters to apply | |
477 - filters_args: kwargs of filters in the same order as filters (use empty dict if needed) | |
478 - template: template to format where {value} is the filtered value | |
479 """ | |
480 value = item.value | |
481 filter_ = filters.get(item.name, None) | |
482 if filter_ is None: | |
483 return value | |
484 elif isinstance(filter_, dict): | |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
485 filters_args = filter_.get(u'filters_args') |
2402 | 486 for idx, f_name in enumerate(filter_.get(u'filters', [])): |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
487 kwargs = filters_args[idx] if filters_args is not None else {} |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
488 filter_func = self.env.filters[f_name] |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
489 try: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
490 eval_context_filter = filter_func.evalcontextfilter |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
491 except AttributeError: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
492 eval_context_filter = False |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
493 |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
494 if eval_context_filter: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
495 value = filter_func(ctx.eval_ctx, value, **kwargs) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
496 else: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
497 value = filter_func(value, **kwargs) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
498 template = filter_.get(u'template') |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
499 if template: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
500 # format will return a string, so we need to check first |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
501 # if the value is safe or not, and re-mark it after formatting |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
502 is_safe = isinstance(value, safe) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
503 value = template.format(value=value) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
504 if is_safe: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
505 value = safe(value) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
506 return value |
2402 | 507 |
2391
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
508 def _adv_format(self, value, template, **kwargs): |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
509 """Advancer formatter |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
510 |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
511 like format() method, but take care or special values like None |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
512 @param value(unicode): value to format |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
513 @param template(None, unicode): template to use with format() method. |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
514 It will be formatted using value=value and **kwargs |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
515 None to return value unchanged |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
516 @return (unicode): formatted value |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
517 """ |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
518 if template is None: |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
519 return value |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
520 # jinja use string when no special char is used, so we have to convert to unicode |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
521 return unicode(template).format(value=value, **kwargs) |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
522 |
2425
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
523 def _dict_ext(self, source_dict, extra_dict, key=None): |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
524 """extend source_dict with extra dict and return the result |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
525 |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
526 @param source_dict(dict): dictionary to extend |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
527 @param extra_dict(dict, None): dictionary to use to extend first one |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
528 None to return source_dict unmodified |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
529 @param key(unicode, None): if specified extra_dict[key] will be used |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
530 if it doesn't exists, a copy of unmodified source_dict is returned |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
531 @return (dict): resulting dictionary |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
532 """ |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
533 if extra_dict is None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
534 return source_dict |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
535 if key is not None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
536 extra_dict = extra_dict.get(key, {}) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
537 ret = source_dict.copy() |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
538 ret.update(extra_dict) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
539 return ret |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
540 |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
541 def highlight(self, code, lexer_name=None, lexer_opts=None, html_fmt_opts=None): |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
542 """Do syntax highlighting on code |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
543 |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
544 under the hood, pygments is used, check its documentation for options possible values |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
545 @param code(unicode): code or markup to highlight |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
546 @param lexer_name(unicode, None): name of the lexer to use |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
547 None to autodetect it |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
548 @param html_fmt_opts(dict, None): kword arguments to use for HtmlFormatter |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
549 @return (unicode): HTML markup with highlight classes |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
550 """ |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
551 if lexer_opts is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
552 lexer_opts = {} |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
553 if html_fmt_opts is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
554 html_fmt_opts = {} |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
555 if lexer_name is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
556 lexer = lexers.guess_lexer(code, **lexer_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
557 else: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
558 lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
559 formatter = formatters.HtmlFormatter(**html_fmt_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
560 return safe(pygments.highlight(code, lexer, formatter)) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
561 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
562 ## custom tests ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
563 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
564 def _in_the_past(self, timestamp): |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
565 """check if a date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
566 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
567 @param timestamp(unicode, int): unix time |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
568 @return (bool): True if date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
569 """ |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
570 return time.time() > int(timestamp) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
571 |
2515 | 572 ## template methods ## |
573 | |
574 def _icon_defs(self, *names): | |
575 """Define svg icons which will be used in the template, and use their name as id""" | |
576 svg_elt = etree.Element('svg', nsmap={None: 'http://www.w3.org/2000/svg'}, | |
577 width='0', height='0', style='display: block' | |
578 ) | |
579 defs_elt = etree.SubElement(svg_elt, 'defs') | |
580 for name in names: | |
581 path = os.path.join(self.icons_path, name + u'.svg') | |
582 icon_svg_elt = etree.parse(path).getroot() | |
583 # we use icon name as id, so we can retrieve them easily | |
584 icon_svg_elt.set('id', name) | |
585 if not icon_svg_elt.tag == '{http://www.w3.org/2000/svg}svg': | |
586 raise exceptions.DataError(u'invalid SVG element') | |
587 defs_elt.append(icon_svg_elt) | |
588 return safe(etree.tostring(svg_elt, encoding='unicode')) | |
589 | |
590 def _icon_use(self, name, cls=''): | |
591 return safe(u"""<svg class="svg-icon{cls}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> | |
592 <use href="#{name}"/> | |
593 </svg> | |
594 """.format( | |
595 name=name, | |
596 cls=(' ' + cls) if cls else '')) | |
597 | |
2394
7bfcc431f66d
template: added media_path to template data
Goffi <goffi@goffi.org>
parents:
2391
diff
changeset
|
598 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', media_path=u'', css_files=None, css_inline=False, **kwargs): |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
599 """render a template |
2515 | 600 . |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
601 @param template(unicode): template to render (e.g. blog/articles.html) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
602 @param theme(unicode): template theme |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
603 @param root_path(unicode): prefix of the path/URL to use for template root |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
604 must end with a u'/' |
2394
7bfcc431f66d
template: added media_path to template data
Goffi <goffi@goffi.org>
parents:
2391
diff
changeset
|
605 @param media_path(unicode): prefix of the SàT media path/URL to use for template root |
7bfcc431f66d
template: added media_path to template data
Goffi <goffi@goffi.org>
parents:
2391
diff
changeset
|
606 must end with a u'/' |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
607 @param css_files(list[unicode],None): CSS files to used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
608 CSS files must be in static dir of the template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
609 use None for automatic selection of CSS files based on template category |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
610 None is recommended. General static/style.css and theme file name will be used. |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
611 @param css_inline(bool): if True, CSS will be embedded in the HTML page |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
612 @param **kwargs: variable to transmit to the template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 """ |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
614 if not template: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
615 raise ValueError(u"template can't be empty") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
616 if theme is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
617 # use want to set a theme, we add it to the template path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
618 if template[0] == u'(': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
619 raise ValueError(u"you can't specify theme in template path and in argument at the same time") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
620 elif template[0] == u'/': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
621 raise ValueError(u"you can't specify theme with absolute paths") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
622 template= u'(' + theme + u')' + template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
623 else: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
624 theme, dummy = self.env.loader.parse_template(template) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
625 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
626 template_source = self.env.get_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
627 template_root_dir = os.path.normpath(self.base_dir) # FIXME: should be modified if we handle use extra dirs |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
628 # XXX: template_path may have a different theme as first element than theme if a default page is used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
629 template_path = template_source.filename[len(template_root_dir)+1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
630 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
631 if css_files is None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
632 css_files = self.getCSSFiles(template_path, template_root_dir) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
633 |
2515 | 634 kwargs['icon_defs'] = self._icon_defs |
635 kwargs['icon'] = self._icon_use | |
636 | |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
637 if css_inline: |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
638 css_contents = [] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
639 for css_file in css_files: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
640 css_file_path = os.path.join(template_root_dir, css_file) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
641 with open(css_file_path) as f: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
642 css_contents.append(f.read()) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
643 if css_contents: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
644 kwargs['css_content'] = '\n'.join(css_contents) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
645 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
646 scripts_handler = ScriptsHandler(self, template_path, template_root_dir, root_path) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
647 self.setLocale(locale) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
648 # XXX: theme used in template arguments is the requested theme, which may differ from actual theme |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
649 # if the template doesn't exist in the requested theme. |
2515 | 650 return template_source.render(theme=theme, root_path=root_path, media_path=media_path, |
651 css_files=css_files, locale=self._locale, | |
652 gidx=Indexer(), script=scripts_handler, | |
653 **kwargs) |