Mercurial > libervia-backend
annotate src/tools/common/template.py @ 2351:3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 06 Sep 2017 07:39:10 +0200 |
parents | 2dae79990122 |
children | d14c1a3e3244 |
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 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
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 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
29 import time |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
30 from babel import support |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
31 from babel import Locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
32 from babel.core import UnknownLocaleError |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 import sat_templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 except ImportError: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 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
|
37 else: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 sat_templates # to avoid pyflakes warning |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 import jinja2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 except: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 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
|
44 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
45 from jinja2 import Markup as safe |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
46 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
47 HTML_EXT = ('html', 'xhtml') |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
48 DEFAULT_LOCALE = u'en' |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
49 # 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
|
50 # 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
|
51 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
52 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
53 class TemplateLoader(jinja2.FileSystemLoader): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
54 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
55 def __init__(self): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
56 searchpath = os.path.dirname(sat_templates.__file__) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
57 super(TemplateLoader, self).__init__(searchpath, followlinks=True) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
58 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
59 def parse_template(self, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
60 """parse template path and return theme and relative URL |
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 @param template_path(unicode): path to template with parenthesis syntax |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
63 @return (tuple[(unicode,None),unicode]): theme and template_path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
64 theme can be None if relative path is used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
65 relative path is the path from search path with theme specified |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
66 e.g. default/blog/articles.html |
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 if template.startswith(u'('): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
69 try: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
70 theme_end = template.index(u')') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
71 except IndexError: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
72 raise ValueError(u"incorrect theme in template") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
73 theme = template[1:theme_end] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
74 template = template[theme_end+1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
75 if not template or template.startswith(u'/'): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
76 raise ValueError(u"incorrect path after template name") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
77 template = os.path.join(theme, template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
78 elif template.startswith(u'/'): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
79 # absolute path means no template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
80 theme = None |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
81 raise NotImplementedError(u'absolute path is not implemented yet') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
82 else: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
83 theme = C.TEMPLATE_THEME_DEFAULT |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
84 template = os.path.join(theme, template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
85 return theme, template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
86 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
87 def get_default_template(self, theme, template_path): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
88 """return default template path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
89 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
90 @param theme(unicode): theme used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
91 @param template_path(unicode): path to the not found template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
92 @return (unicode, None): default path or None if there is not |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
93 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
94 ext = os.path.splitext(template_path)[1][1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
95 path_elems = template_path.split(u'/') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
96 if ext in HTML_EXT: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
97 if path_elems[1] == u'error': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
98 # if an inexisting error page is requested, we return base page |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
99 default_path = os.path.join(theme, u'error/base.html') |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
100 return default_path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
101 if theme != C.TEMPLATE_THEME_DEFAULT: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
102 # 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
|
103 return os.path.join(C.TEMPLATE_THEME_DEFAULT, path_elems[1:]) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
104 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
105 def get_source(self, environment, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
106 """relative path to template dir, with special theme handling |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
107 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
108 if the path is just relative, "default" theme is used. |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
109 The theme can be specified in parenthesis just before the path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
110 e.g.: (some_theme)path/to/template.html |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
111 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
112 theme, template_path = self.parse_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
113 try: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
114 return super(TemplateLoader, self).get_source(environment, template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
115 except jinja2.exceptions.TemplateNotFound as e: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
116 # 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
|
117 if theme is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
118 default_path = self.get_default_template(theme, template_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
119 if default_path is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
120 return super(TemplateLoader, self).get_source(environment, default_path) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
121 # if no default template is found, we re-raise the error |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
122 raise e |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
123 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
125 class Indexer(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
126 """Index global to a page""" |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
127 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
128 def __init__(self): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
129 self._idx = 0 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
130 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
131 def next(self): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
132 self._idx+=1 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
133 return self._idx |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
134 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
135 def current(self): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
136 return self._idx |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
137 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
138 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
139 class ScriptsHandler(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
140 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
141 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
|
142 self.renderer = renderer |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
143 self.template_root_dir = template_root_dir |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
144 self.root_path = root_path |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
145 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
|
146 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
|
147 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
148 def include(self, library_name): |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
149 """Mark that a script need to be imported. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
150 |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
151 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
|
152 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
|
153 @param library_name(unicode): name of the library to import |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
154 """ |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
155 if library_name.endswith('.js'): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
156 library_name = library_name[:-3] |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
157 if library_name not in self.scripts: |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
158 self.scripts.append(library_name) |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
159 return u'' |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
160 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
161 def generate_scripts(self): |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
162 """Generate the <script> elements |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
163 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
164 @return (unicode): <scripts> HTML tags |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
165 """ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
166 scripts = [] |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
167 tpl = u'<script src={src}></script>' |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
168 for library in self.scripts: |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
169 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
|
170 if path is None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
171 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
|
172 continue |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
173 path = os.path.join(self.root_path, path) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
174 scripts.append(tpl.format(src=quoteattr(path))) |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
175 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
|
176 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
177 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 class Renderer(object): |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
180 def __init__(self, host): |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 self.host = host |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
182 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
|
183 self.env = jinja2.Environment( |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
184 loader=TemplateLoader(), |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 autoescape=jinja2.select_autoescape(['html', 'xhtml', 'xml']), |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 trim_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 lstrip_blocks=True, |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
188 extensions=['jinja2.ext.i18n'], |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 ) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
190 self._locale_str = DEFAULT_LOCALE |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
191 self._locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
192 self.installTranslations() |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 # 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
|
194 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
|
195 # custom filters |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
196 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
|
197 self.env.filters['cur_gidx'] = self._cur_gidx |
2266
084a75b8aa7a
tools (common/template): changed blog_date filter to date_days
Goffi <goffi@goffi.org>
parents:
2265
diff
changeset
|
198 self.env.filters['date_days'] = self._date_days |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
200 def installTranslations(self): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
201 i18n_dir = os.path.join(self.base_dir, 'i18n') |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
202 self.translations = {} |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
203 for lang_dir in os.listdir(i18n_dir): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
204 lang_path = os.path.join(i18n_dir, lang_dir) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
205 if not os.path.isdir(lang_path): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
206 continue |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
207 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
|
208 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
209 with open(po_path, 'rb') as f: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
210 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
|
211 except EnvironmentError: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
212 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
|
213 except UnknownLocaleError as e: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
214 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
|
215 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
216 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
|
217 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
218 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
219 def setLocale(self, locale_str): |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
220 if locale_str == self._locale_str: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
221 return |
2323
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
222 try: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
223 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
|
224 except ValueError as e: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
229 locale_str = unicode(locale) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
230 if locale_str != DEFAULT_LOCALE: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
231 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
232 translations = self.translations[locale] |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
233 except KeyError: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
234 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
|
235 locale_str = DEFAULT_LOCALE |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
236 locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
237 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
238 self.env.install_gettext_translations(translations, True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
239 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
|
240 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
241 if locale_str == DEFAULT_LOCALE: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
242 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
243 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
244 self._locale = locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
245 self._locale_str = locale_str |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
246 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
247 def getThemeAndRoot(self, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
248 """retrieve theme and root dir of a given tempalte |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
249 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
250 @param template(unicode): template to parse |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
251 @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
|
252 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
253 theme, dummy = self.env.loader.parse_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
254 return theme, os.path.join(self.base_dir, theme) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
255 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
256 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
|
257 """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
|
258 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
259 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
|
260 if not found. |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
261 @param name(unicode): name of the file to look for |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
262 @param template_root_dir(unicode): absolute path to template root used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
263 @param theme(unicode): name of the template theme used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
264 @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
|
265 @return (unicode, None): relative path if found, else None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
266 """ |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
267 file_ = None |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
268 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
|
269 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
|
270 file_ = path |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
271 elif not is_default: |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
272 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
|
273 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
|
274 file_.append(path) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
275 return file_ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
276 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
277 def getThemeData(self, template_path): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
278 """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
|
279 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
280 @return tuple(unicde, unicode, bool): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
281 path_elems: elements of the path |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
282 theme: theme of the page |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
283 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
|
284 """ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
285 path_elems = template_path.split(u'/') |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
286 theme = path_elems.pop(0) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
287 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
|
288 return (path_elems, theme, is_default) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
289 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
290 def getCSSFiles(self, template_path, template_root_dir): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
291 """retrieve CSS files to use according to theme and template path |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
292 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
293 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
|
294 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
|
295 For instance, if template_path is some_theme/blog/articles.html: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
296 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
|
297 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
|
298 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
|
299 @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
|
300 @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
|
301 @return list[unicode]: relative path to CSS files to use |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
302 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
303 # TODO: some caching would be nice |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
304 css_files = [] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
305 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
|
306 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
|
307 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
|
308 if css_path is not None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
309 css_files.append(css_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
310 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
311 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
|
312 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
|
313 if css_path is not None: |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
314 css_files.append(css_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
315 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
316 return css_files |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
317 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
318 @jinja2.contextfilter |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
319 def _next_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
320 """Use next current global index as suffix""" |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
321 return u"{}_{}".format(value, ctx['gidx'].next()) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
322 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
323 @jinja2.contextfilter |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
324 def _cur_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
325 """Use current current global index as suffix""" |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
326 return u"{}_{}".format(value, ctx['gidx'].current()) |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
327 |
2266
084a75b8aa7a
tools (common/template): changed blog_date filter to date_days
Goffi <goffi@goffi.org>
parents:
2265
diff
changeset
|
328 def _date_days(self, timestamp): |
084a75b8aa7a
tools (common/template): changed blog_date filter to date_days
Goffi <goffi@goffi.org>
parents:
2265
diff
changeset
|
329 return int(time.time() - int(timestamp))/(3600*24) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
330 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
331 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_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
|
332 """render a template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
334 @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
|
335 @param theme(unicode): template theme |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
336 @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
|
337 must end with a u'/' |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
338 @param css_files(list[unicode],None): CSS files to used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
339 CSS files must be in static dir of the template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
340 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
|
341 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
|
342 @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
|
343 @param **kwargs: variable to transmit to the template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 """ |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
345 if not template: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
346 raise ValueError(u"template can't be empty") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
347 if theme is not None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
348 # 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
|
349 if template[0] == u'(': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
350 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
|
351 elif template[0] == u'/': |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
352 raise ValueError(u"you can't specify theme with absolute paths") |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
353 template= u'(' + theme + u')' + template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
354 else: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
355 theme, dummy = self.env.loader.parse_template(template) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
357 template_source = self.env.get_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
358 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
|
359 # 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
|
360 template_path = template_source.filename[len(template_root_dir)+1:] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
361 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
362 if css_files is None: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
363 css_files = self.getCSSFiles(template_path, template_root_dir) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
364 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 if css_inline: |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
366 css_contents = [] |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
367 for css_file in css_files: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
368 css_file_path = os.path.join(template_root_dir, css_file) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
369 with open(css_file_path) as f: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
370 css_contents.append(f.read()) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
371 if css_contents: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
372 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
|
373 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
374 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
|
375 self.setLocale(locale) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
376 # 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
|
377 # if the template doesn't exist in the requested theme. |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
378 return template_source.render(theme=theme, root_path=root_path, css_files=css_files, locale=self._locale, gidx=Indexer(), script=scripts_handler, **kwargs) |