annotate src/tools/common/template.py @ 2245:e09048cb7595

core (tools/common/template): helping methods/filters for templates: - Indexer class (used with next_gidx/cur_gidx) allows to get per page indexes, usefull to have unique id to reference - blog_date is a Q&D filter to get relative dates, should be replaced/improved in the future - ScriptsHandler (used with scripts_handler) is intended to be a way to add scripts from template, and group them at a specific location of the page. It is not used yet and may change heavily or disappear in the future.
author Goffi <goffi@goffi.org>
date Fri, 19 May 2017 12:54:31 +0200
parents f472179305a1
children e572482f6cbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 collections import OrderedDict
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
29 from xml.sax.saxutils import quoteattr
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
30 import time
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 try:
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import sat_templates
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 except ImportError:
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 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
35 else:
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 sat_templates # to avoid pyflakes warning
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 try:
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 import jinja2
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 except:
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 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
42
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
43 HTML_EXT = ('html', 'xhtml')
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
44 # 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
45 # 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
46
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
47
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
48 class TemplateLoader(jinja2.FileSystemLoader):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
49
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
50 def __init__(self):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
51 searchpath = os.path.dirname(sat_templates.__file__)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
52 super(TemplateLoader, self).__init__(searchpath, followlinks=True)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
53
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
54 def parse_template(self, template):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
55 """parse template path and return theme and relative URL
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
56
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
57 @param template_path(unicode): path to template with parenthesis syntax
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
58 @return (tuple[(unicode,None),unicode]): theme and template_path
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
59 theme can be None if relative path is used
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
60 relative path is the path from search path with theme specified
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
61 e.g. default/blog/articles.html
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
62 """
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
63 if template.startswith(u'('):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
64 try:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
65 theme_end = template.index(u')')
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
66 except IndexError:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
67 raise ValueError(u"incorrect theme in template")
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
68 theme = template[1:theme_end]
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
69 template = template[theme_end+1:]
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
70 if not template or template.startswith(u'/'):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
71 raise ValueError(u"incorrect path after template name")
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
72 template = os.path.join(theme, template)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
73 elif template.startswith(u'/'):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
74 # absolute path means no template
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
75 theme = None
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
76 raise NotImplementedError(u'absolute path is not implemented yet')
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
77 else:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
78 theme = C.TEMPLATE_THEME_DEFAULT
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
79 template = os.path.join(theme, template)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
80 return theme, template
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
81
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
82 def get_default_template(self, theme, template_path):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
83 """return default template path
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
84
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
85 @param theme(unicode): theme used
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
86 @param template_path(unicode): path to the not found template
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
87 @return (unicode, None): default path or None if there is not
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
88 """
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
89 ext = os.path.splitext(template_path)[1][1:]
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
90 path_elems = template_path.split(u'/')
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
91 if ext in HTML_EXT:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
92 if path_elems[1] == u'error':
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
93 # if an inexisting error page is requested, we return base page
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
94 default_path = os.path.join(theme, u'error/base.html')
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
95 return default_path
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
96 if theme != C.TEMPLATE_THEME_DEFAULT:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
97 # 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
98 return os.path.join(C.TEMPLATE_THEME_DEFAULT, path_elems[1:])
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
99
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
100 def get_source(self, environment, template):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
101 """relative path to template dir, with special theme handling
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 if the path is just relative, "default" theme is used.
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
104 The theme can be specified in parenthesis just before the path
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
105 e.g.: (some_theme)path/to/template.html
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
106 """
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
107 theme, template_path = self.parse_template(template)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
108 try:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
109 return super(TemplateLoader, self).get_source(environment, template_path)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
110 except jinja2.exceptions.TemplateNotFound as e:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
111 # 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
112 if theme is not None:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
113 default_path = self.get_default_template(theme, template_path)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
114 if default_path is not None:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
115 return super(TemplateLoader, self).get_source(environment, default_path)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
116 # if no default template is found, we re-raise the error
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
117 raise e
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
118
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
120 class Indexer(object):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
121 """Index global to a page"""
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
122
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
123 def __init__(self):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
124 self._idx = 0
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
125
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
126 def next(self):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
127 self._idx+=1
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
128 return self._idx
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
129
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
130 def current(self):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
131 return self._idx
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
132
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
133
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
134 class ScriptsHandler(object):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
135 # TODO: this class is not finished/used yet, and add_script is referenced in default/script/base.html
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
136 # but doesn't exist yet here
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 def __init__(self, renderer, template_path, template_root_dir):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
139 self.renderer = renderer
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
140 self.template_root_dir = template_root_dir
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
141 self.scripts = OrderedDict
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
142 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
143
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
144 def import_script(self, library_name):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
145 if library_name.endswith('.js'):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
146 library_name = library_name[:-3]
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
147 if library_name not in self.scripts:
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
148 self.scripts[library_name] = {}
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 def generate(self):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
151 """Generate the <scripts> elements
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
152 @return (unicode): <scripts> HTML tags
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
153 """
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
154 scripts = []
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
155 tpl = u'<script src="{src}"></script>'
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
156 for library,data in self.scripts:
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
157 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
158 if path is None:
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
159 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
160 continue
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
161 scripts.append(tpl.format(src=quoteattr(path)))
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
162 return u'\n'.join(scripts)
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
163
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
164
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 class Renderer(object):
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
167 def __init__(self, host):
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.host = host
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
169 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
170 self.env = jinja2.Environment(
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
171 loader=TemplateLoader(),
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 autoescape=jinja2.select_autoescape(['html', 'xhtml', 'xml']),
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 trim_blocks=True,
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 lstrip_blocks=True,
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 )
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 # 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
177 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
178 # custom filters
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
179 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
180 self.env.filters['cur_gidx'] = self._cur_gidx
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
181 self.env.filters['blog_date'] = self._blog_date
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
183 def getThemeAndRoot(self, template):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
184 """retrieve theme and root dir of a given tempalte
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
185
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
186 @param template(unicode): template to parse
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
187 @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
188 """
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
189 theme, dummy = self.env.loader.parse_template(template)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
190 return theme, os.path.join(self.base_dir, theme)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
191
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
192 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
193 """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
194
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
195 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
196 if not found.
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
197 @param name(unicode): name of the file to look for
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
198 @param template_root_dir(unicode): absolute path to template root used
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
199 @param theme(unicode): name of the template theme used
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
200 @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
201 @return (unicode, None): relative path if found, else None
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
202 """
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
203 file_ = None
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
204 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
205 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
206 file_ = path
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
207 elif not is_default:
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
208 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
209 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
210 file_.append(path)
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
211 return file_
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
212
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
213 def getThemeData(self, template_path):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
214 """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
215
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
216 @return tuple(unicde, unicode, bool):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
217 path_elems: elements of the path
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
218 theme: theme of the page
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
219 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
220 """
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
221 path_elems = template_path.split(u'/')
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
222 theme = path_elems.pop(0)
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
223 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
224 return (path_elems, theme, is_default)
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
225
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
226 def getCSSFiles(self, template_path, template_root_dir):
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
227 """retrieve CSS files to use according to theme and template path
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
228
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
229 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
230 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
231 For instance, if template_path is some_theme/blog/articles.html:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
232 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
233 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
234 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
235 @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
236 @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
237 @return list[unicode]: relative path to CSS files to use
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
238 """
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
239 # TODO: some caching would be nice
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
240 css_files = []
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
241 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
242 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
243 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
244 if css_path is not None:
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
245 css_files.append(css_path)
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
246
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
247 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
248 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
249 if css_path is not None:
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
250 css_files.append(css_path)
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
251
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
252 return css_files
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
253
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
254 @jinja2.contextfilter
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
255 def _next_gidx(self, ctx, value):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
256 """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
257 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
258
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
259 @jinja2.contextfilter
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
260 def _cur_gidx(self, ctx, value):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
261 """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
262 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
263
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
264 def _blog_date(self, timestamp):
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
265 # FIXME: Q&D, need to be done properly
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
266 return unicode(int(time.time() - int(timestamp))/(3600*24)) + u" days ago"
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
267
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
268 def render(self, template, theme=None, 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
269 """render a template
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
271 @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
272 @param theme(unicode): template theme
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
273 @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
274 must end with a u'/'
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
275 @param css_files(list[unicode],None): CSS files to used
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
276 CSS files must be in static dir of the template
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
277 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
278 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
279 @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
280 @param **kwargs: variable to transmit to the template
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 """
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
282 if not template:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
283 raise ValueError(u"template can't be empty")
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
284 if theme is not None:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
285 # 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
286 if template[0] == u'(':
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
287 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
288 elif template[0] == u'/':
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
289 raise ValueError(u"you can't specify theme with absolute paths")
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
290 template= u'(' + theme + u')' + template
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
291 else:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
292 theme, dummy = self.env.loader.parse_template(template)
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
294 template_source = self.env.get_template(template)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
295 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
296 # 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
297 template_path = template_source.filename[len(template_root_dir)+1:]
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
298
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
299 if css_files is None:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
300 css_files = self.getCSSFiles(template_path, template_root_dir)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
301
2159
5734b0994cf0 core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 if css_inline:
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
303 css_contents = []
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
304 for css_file in css_files:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
305 css_file_path = os.path.join(template_root_dir, css_file)
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
306 with open(css_file_path) as f:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
307 css_contents.append(f.read())
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
308 if css_contents:
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
309 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
310
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
311 scripts_handler = ScriptsHandler(self, template_path, template_root_dir)
2169
f472179305a1 tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents: 2159
diff changeset
312 # 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
313 # if the template doesn't exist in the requested theme.
2245
e09048cb7595 core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents: 2169
diff changeset
314 return template_source.render(theme=theme, root_path=root_path, css_files=css_files, gidx=Indexer(), scripts_handler=scripts_handler, **kwargs)