Mercurial > libervia-backend
annotate sat/tools/common/template.py @ 2671:0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
- site can now be specified in template header before theme, for instance: (some_site/some_theme)path/to/template.ext
- absolute template paths are now implemented, but Renderer must be instanciated with trusted to True for security reason (it's the case for jp)
- a new "front_url_filter" callable can be given to Renderer, which will convert template path to URL seen by end-user (default to real path).
- the "front_url_filter" can be used in templates with… "front_url" filter
- template_data is a new named tuple available in templates, which give site, theme and template relative URL
- search order is site/theme, site/default_theme, and default/default_theme where default link to sat_pubsub templates
- when loading CSS files, files with _noscript suffixes are now loaded, and used when javascript is not available
- "styles_extra.css" is also loaded before "styles.css", useful when a theme want to reuse default style, and just override some rules
- new site can be specified in sat.conf [DEFAULT] section, using sites_path_public_dict or sites_path_private_dict (where sites_path_private_dict won't be used in public frontends, like Libervia)
- "private" argument of Renderer tells the renderer to load private sites or not
- templates are now loaded from "templates" subdirectory, to differenciate them from other data like i18n
- jp template output has been updated to handle those changes, and to manage absolute templates
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 10 Sep 2018 08:58:18 +0200 |
parents | 56f94936df1e |
children | 39d187f3698d |
rev | line source |
---|---|
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
20 """Template generation""" |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
22 import os.path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
23 from collections import namedtuple |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 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
|
25 from sat.core.i18n import _ |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from sat.core import exceptions |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
27 from sat.tools import config |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
28 from sat.tools.common import date_utils |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from sat.core.log import getLogger |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
30 from xml.sax.saxutils import quoteattr |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
31 import time |
2401
221478058d8a
template: improved attribute escaping, and added it to filters under the name "attr_escape"
Goffi <goffi@goffi.org>
parents:
2394
diff
changeset
|
32 import re |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
33 from babel import support |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
34 from babel import Locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
35 from babel.core import UnknownLocaleError |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
36 import pygments |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
37 from pygments import lexers |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
38 from pygments import formatters |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
39 |
2159
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 sat_templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
43 raise exceptions.MissingModule( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
44 u"sat_templates module is not available, please install it or check your path to " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
45 u"use template engine" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
46 ) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 else: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 sat_templates # to avoid pyflakes warning |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 import jinja2 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 except: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
53 raise exceptions.MissingModule( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
54 u"Missing module jinja2, please install it from http://jinja.pocoo.org or with " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
55 u"pip install jinja2" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
56 ) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
58 from jinja2 import Markup as safe |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
59 from jinja2 import is_undefined |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
60 from jinja2 import utils |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
61 from jinja2 import TemplateNotFound |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
62 from jinja2.loaders import split_template_path |
2515 | 63 from lxml import etree |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
64 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
65 log = getLogger(__name__) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
66 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
67 HTML_EXT = ("html", "xhtml") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
68 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
69 SITE_RESERVED_NAMES = (u"sat",) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
70 TPL_RESERVED_CHARS = ur"()/." |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
71 RE_TPL_RESERVED_CHARS = re.compile(u"[" + TPL_RESERVED_CHARS + u"]") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
72 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
73 TemplateData = namedtuple("TemplateData", ['site', 'theme', 'path']) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
74 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
75 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
76 class TemplateLoader(jinja2.BaseLoader): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
77 """A template loader which handle site, theme and absolute paths""" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
78 # TODO: list_templates should be implemented |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
79 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
80 def __init__(self, sites_paths, trusted=False): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
81 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
82 @param trusted(bool): if True, absolue template paths will be allowed |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
83 be careful when using this option and sure that you can trust the template, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
84 as this allow the template to open any file on the system that the |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
85 launching user can access. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
86 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
87 if not sites_paths or not u"" in sites_paths: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
88 raise exceptions.InternalError(u"Invalid sites_paths") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
89 super(jinja2.BaseLoader, self).__init__() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
90 self.sites_paths = sites_paths |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
91 self.trusted = trusted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
92 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
93 @staticmethod |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
94 def parse_template(template): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
95 """Parse template path and return site, theme and path |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
96 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
97 @param template_path(unicode): path to template with parenthesis syntax |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
98 The site and/or theme can be specified in parenthesis just before the path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
99 e.g.: (some_theme)path/to/template.html |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
100 (/some_theme)path/to/template.html (equivalent to previous one) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
101 (other_site/other_theme)path/to/template.html |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
102 (other_site/)path/to/template.html (defaut theme for other_site) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
103 /absolute/path/to/template.html (in trusted environment only) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
104 @return (TemplateData): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
105 site, theme and template_path. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
106 if site is empty, SàT Templates are used |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
107 site and theme can be both None if absolute path is used |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
108 Relative path is the path from theme root dir e.g. blog/articles.html |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
109 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
110 if template.startswith(u"("): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
111 # site and/or theme are specified |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
112 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
113 theme_end = template.index(u")") |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
114 except IndexError: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
115 raise ValueError(u"incorrect site/theme in template") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
116 theme_data = template[1:theme_end] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
117 theme_splitted = theme_data.split(u'/') |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
118 if len(theme_splitted) == 1: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
119 site, theme = u"", theme_splitted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
120 elif len(theme_splitted) == 2: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
121 site, theme = theme_splitted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
122 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
123 raise ValueError(u"incorrect site/theme in template") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
124 template_path = template[theme_end+1:] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
125 if not template_path or template_path.startswith(u"/"): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
126 raise ValueError(u"incorrect template path") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
127 elif template.startswith(u"/"): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
128 # this is an absolute path, so we have no site and no theme |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
129 site = None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
130 theme = None |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
131 template_path = template |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
132 else: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
133 # a default template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
134 site = u"" |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
135 theme = C.TEMPLATE_THEME_DEFAULT |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
136 template_path = template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
137 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
138 if site is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
139 site = site.strip() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
140 if not site: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
141 site = u"" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
142 elif site in SITE_RESERVED_NAMES: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
143 raise ValueError(_(u"{site} can't be used as site name, " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
144 u"it's reserved.").format(site=site)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
145 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
146 if theme is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
147 theme = theme.strip() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
148 if not theme: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
149 theme = C.TEMPLATE_THEME_DEFAULT |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
150 if RE_TPL_RESERVED_CHARS.search(theme): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
151 raise ValueError(_(u"{theme} contain forbidden char. Following chars " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
152 u"are forbidden: {reserved}").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
153 theme=theme, reserved=TPL_RESERVED_CHARS)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
154 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
155 return TemplateData(site, theme, template_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
156 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
157 @staticmethod |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
158 def getSitesAndThemes(site, theme): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
159 """Get sites and themes to check for template/file |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
160 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
161 Will add default theme and default site in search list when suitable |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
162 @param site(unicode): site requested |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
163 @param theme(unicode): theme requested |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
164 @return (list[tuple[unicode, unicode]]): site and theme couples to check |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
165 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
166 sites_and_themes = [[site, theme]] |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
167 if theme != C.TEMPLATE_THEME_DEFAULT: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
168 sites_and_themes.append([site, C.TEMPLATE_THEME_DEFAULT]) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
169 if site: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
170 # the site is not the default one, so we add default at the end |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
171 sites_and_themes.append([u'', C.TEMPLATE_THEME_DEFAULT]) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
172 return sites_and_themes |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
173 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
174 def _get_template_f(self, site, theme, path_elts): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
175 """Look for template and return opened file if found |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
176 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
177 @param site(unicode): names of site to check |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
178 (default site will also checked) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
179 @param theme(unicode): theme to check (default theme will also be checked) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
180 @param path_elts(iterable[str]): elements of template path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
181 @return (tuple[(File, None), (str, None)]): a tuple with: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
182 - opened template, or None if not found |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
183 - absolute file path, or None if not found |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
184 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
185 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
186 raise exceptions.InternalError( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
187 u"_get_template_f must not be used with absolute path") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
188 for site, theme in self.getSitesAndThemes(site, theme): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
189 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
190 base_path = self.sites_paths[site] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
191 except KeyError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
192 log.warning(_(u"Unregistered site requested: {site}").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
193 site=site)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
194 filepath = os.path.join(base_path, C.TEMPLATE_TPL_DIR, theme, *path_elts) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
195 f = utils.open_if_exists(filepath) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
196 if f is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
197 return f, filepath |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
198 return None, None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
199 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
200 def get_source(self, environment, template): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
201 """Retrieve source handling site and themes |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
202 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
203 If the path is absolute it is used directly if in trusted environment |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
204 else and exception is raised. |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
205 if the path is just relative, "default" theme is used. |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
206 @raise PermissionError: absolute path used in untrusted environment |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
207 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
208 site, theme, template_path = self.parse_template(template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
209 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
210 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
211 # we have an abolute template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
212 if theme is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
213 raise exceptions.InternalError(u"We can't have a theme with absolute " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
214 u"template.") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
215 if not self.trusted: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
216 log.error(_(u"Absolute template used while unsecure is disabled, hack " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
217 u"attempt? Template: {template}").format(template=template)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
218 raise exceptions.PermissionError(u"absolute template is not allowed") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
219 filepath = template_path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
220 f = utils.open_if_exists(filepath) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
221 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
222 # relative path, we have to deal with site and theme |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
223 assert theme and template_path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
224 path_elts = split_template_path(template_path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
225 # if we have non default site, we check it first, else we only check default |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
226 f, filepath = self._get_template_f(site, theme, path_elts) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
227 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
228 if f is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
229 if (site is not None and path_elts[0] == u"error" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
230 and os.path.splitext(template_path)[1][1:] in HTML_EXT): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
231 # if an HTML error is requested but doesn't exist, we try again |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
232 # with base error. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
233 f, filepath = self._get_template_f( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
234 site, theme, ("error", "base.html")) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
235 if f is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
236 raise exceptions.InternalError(u"error/base.html should exist") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
237 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
238 raise TemplateNotFound(template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
239 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
240 try: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
241 contents = f.read().decode('utf-8') |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
242 finally: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
243 f.close() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
244 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
245 mtime = os.path.getmtime(filepath) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
246 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
247 def uptodate(): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
248 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
249 return os.path.getmtime(filepath) == mtime |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
250 except OSError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
251 return False |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
252 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
253 return contents, filepath, uptodate |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
254 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
256 class Indexer(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
257 """Index global to a page""" |
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 def __init__(self): |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
260 self._indexes = {} |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
261 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
262 def next(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
263 if value not in self._indexes: |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
264 self._indexes[value] = 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
265 return 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
266 self._indexes[value] += 1 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
267 return self._indexes[value] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
268 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
269 def current(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
270 return self._indexes.get(value) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
271 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
272 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
273 class ScriptsHandler(object): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
274 def __init__(self, renderer, template_data): |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
275 self.renderer = renderer |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
276 self.template_data = template_data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
277 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
|
278 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
279 def include(self, library_name, attribute="defer"): |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
280 """Mark that a script need to be imported. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
281 |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
282 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
|
283 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
|
284 @param library_name(unicode): name of the library to import |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
285 @param loading: |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
286 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
287 if attribute not in (u"defer", u"async", u""): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
288 raise exceptions.DataError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
289 _(u'Invalid attribute, please use one of "defer", "async" or ""') |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
290 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
291 if not library_name.endswith(u".js"): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
292 library_name = library_name + u".js" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
293 if (library_name, attribute) not in self.scripts: |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
294 self.scripts.append((library_name, attribute)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
295 return u"" |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
296 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
297 def generate_scripts(self): |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
298 """Generate the <script> elements |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
299 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
300 @return (unicode): <scripts> HTML tags |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
301 """ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
302 scripts = [] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
303 tpl = u"<script src={src} {attribute}></script>" |
2465
bb0bbcc80fc8
template: boolean attribute can now be specified when importing a script, and default to "defer"
Goffi <goffi@goffi.org>
parents:
2454
diff
changeset
|
304 for library, attribute in self.scripts: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
305 library_path = self.renderer.getStaticPath(self.template_data, library) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
306 if library_path is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
307 log.warning(_(u"Can't find {libary} javascript library").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
308 library=library)) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
309 continue |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
310 path = self.renderer.getFrontURL(library_path) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
311 scripts.append(tpl.format(src=quoteattr(path), attribute=attribute)) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
312 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
|
313 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
314 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 class Renderer(object): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
316 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
317 def __init__(self, host, front_url_filter=None, trusted=False, private=False): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
318 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
319 @param front_url_filter(callable): filter to retrieve real url of a directory/file |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
320 The callable will get a two arguments: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
321 - a dict with a "template_data" key containing TemplateData instance of |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
322 current template. Only site and theme should be necessary. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
323 - the relative URL of the file to retrieve, relative from theme root |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
324 None to use default filter which return real path on file |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
325 Need to be specified for web rendering, to reflect URL seen by end user |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
326 @param trusted(bool): if True, allow to access absolute path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
327 Only set to True if environment is safe (e.g. command line tool) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
328 @param private(bool): if True, also load sites from sites_path_private_dict |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
329 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
330 # TODO: |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 self.host = host |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
332 self.trusted = trusted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
333 self.sites_paths = { |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
334 u"": os.path.dirname(sat_templates.__file__), |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
335 } |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
336 conf = config.parseMainConf() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
337 public_sites = config.getConfig(conf, None, u"sites_path_public_dict", {}) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
338 sites_data = [public_sites] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
339 if private: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
340 private_sites = config.getConfig(conf, None, u"sites_path_private_dict", {}) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
341 sites_data.append(private_sites) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
342 for sites in sites_data: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
343 normalised = {} |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
344 for name, path in sites.iteritems(): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
345 if RE_TPL_RESERVED_CHARS.search(name): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
346 log.warning(_(u"Can't add \"{name}\" site, it contains forbidden " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
347 u"characters. Forbidden characters are {forbidden}.") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
348 .format(name=name, forbidden=TPL_RESERVED_CHARS)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
349 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
350 path = os.path.expanduser(os.path.normpath(path)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
351 if not path or not path.startswith(u"/"): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
352 log.warning(_(u"Can't add \"{name}\" site, it should map to an " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
353 u"absolute path").format(name=name)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
354 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
355 normalised[name] = path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
356 self.sites_paths.update(normalised) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
357 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 self.env = jinja2.Environment( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
359 loader=TemplateLoader(sites_paths=self.sites_paths, trusted=trusted), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
360 autoescape=jinja2.select_autoescape(["html", "xhtml", "xml"]), |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 trim_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 lstrip_blocks=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
363 extensions=["jinja2.ext.i18n"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
364 ) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
365 self._locale_str = C.DEFAULT_LOCALE |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
366 self._locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
367 self.installTranslations() |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 # we want to have access to SàT constants in templates |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
369 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
|
370 # custom filters |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
371 self.env.filters[u"next_gidx"] = self._next_gidx |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
372 self.env.filters[u"cur_gidx"] = self._cur_gidx |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
373 self.env.filters[u"date_fmt"] = self._date_fmt |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
374 self.env.filters[u"xmlui_class"] = self._xmlui_class |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
375 self.env.filters[u"attr_escape"] = self.attr_escape |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
376 self.env.filters[u"item_filter"] = self._item_filter |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
377 self.env.filters[u"adv_format"] = self._adv_format |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
378 self.env.filters[u"dict_ext"] = self._dict_ext |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
379 self.env.filters[u"highlight"] = self.highlight |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
380 self.env.filters[u"front_url"] = (self._front_url if front_url_filter is None |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
381 else front_url_filter) |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
382 # custom tests |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
383 self.env.tests[u"in_the_past"] = self._in_the_past |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
384 self.icons_path = os.path.join(host.media_dir, u"fonts/fontello/svg") |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
386 def getFrontURL(self, template_data, path=None): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
387 """Give front URL (i.e. URL seen by end-user) of a path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
388 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
389 @param template_data[TemplateData]: data of current template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
390 @param path(unicode, None): relative path of file to get, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
391 if set, will remplate template_data.path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
392 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
393 return self.env.filters[u"front_url"]({u"template_data": template_data}, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
394 path or template_data.path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
395 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
396 def installTranslations(self): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
397 # TODO: support multi translation |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
398 # for now, only translations in sat_templates are handled |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
399 for site_key, site_path in self.sites_paths.iteritems(): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
400 site_prefix = u"[{}] ".format(site_key) if site_key else u'' |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
401 i18n_dir = os.path.join(site_path, "i18n") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
402 self.translations = {} |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
403 for lang_dir in os.listdir(i18n_dir): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
404 lang_path = os.path.join(i18n_dir, lang_dir) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
405 if not os.path.isdir(lang_path): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
406 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
407 po_path = os.path.join(lang_path, "LC_MESSAGES/sat.mo") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
408 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
409 locale = Locale.parse(lang_dir) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
410 with open(po_path, "rb") as f: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
411 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
412 translations = self.translations[locale] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
413 except KeyError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
414 self.translations[locale] = support.Translations(f, "sat") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
415 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
416 translations.merge(support.Translations(f, "sat")) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
417 except EnvironmentError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
418 log.error( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
419 _(u"Can't find template translation at {path}").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
420 path=po_path)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
421 except UnknownLocaleError as e: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
422 log.error(_(u"{site}Invalid locale name: {msg}").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
423 site=site_prefix, msg=e)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
424 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
425 log.info(_(u"{site}loaded {lang} templates translations").format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
426 site = site_prefix, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
427 lang=lang_dir)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
428 self.env.install_null_translations(True) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
429 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
430 def setLocale(self, locale_str): |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
431 """set current locale |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
432 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
433 change current translation locale and self self._locale and self._locale_str |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
434 """ |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
435 if locale_str == self._locale_str: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
436 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
437 if locale_str == "en": |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
438 # we default to GB English when it's not specified |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
439 # one of the main reason is to avoid the nonsense U.S. short date format |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
440 locale_str = "en_GB" |
2323
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
441 try: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
442 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
|
443 except ValueError as e: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
444 log.warning(_(u"invalid locale value: {msg}").format(msg=e)) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
445 locale_str = self._locale_str = C.DEFAULT_LOCALE |
2323
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
446 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
|
447 |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
448 locale_str = unicode(locale) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
449 if locale_str != C.DEFAULT_LOCALE: |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
450 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
451 translations = self.translations[locale] |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
452 except KeyError: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
453 log.warning(_(u"Can't find locale {locale}".format(locale=locale))) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
454 locale_str = C.DEFAULT_LOCALE |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
455 locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
456 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
457 self.env.install_gettext_translations(translations, True) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
458 log.debug(_(u"Switched to {lang}").format(lang=locale.english_name)) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
459 |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
460 if locale_str == C.DEFAULT_LOCALE: |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
461 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
462 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
463 self._locale = locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
464 self._locale_str = locale_str |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
465 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
466 def getThemeAndRoot(self, template): |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
467 """retrieve theme and root dir of a given tempalte |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
468 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
469 @param template(unicode): template to parse |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
470 @return (tuple[unicode, unicode]): theme and absolute path to theme's root dir |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
471 @raise NotFound: requested site has not been found |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
472 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
473 # FIXME: check use in jp, and include site |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
474 site, theme, __ = self.env.loader.parse_template(template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
475 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
476 # absolute template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
477 return u"", os.path.dirname(template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
478 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
479 site_root_dir = self.sites_paths[site] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
480 except KeyError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
481 raise exceptions.NotFound |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
482 return theme, os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, theme) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
483 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
484 def getStaticPath(self, template_data, filename): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
485 """Retrieve path of a static file if it exists with current theme or default |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
486 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
487 File will be looked at <site_root_dir>/<theme_dir>/<static_dir>/filename, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
488 then <site_root_dir>/<default_theme_dir>/<static_dir>/filename anf finally |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
489 <default_site>/<default_theme_dir>/<static_dir> (i.e. sat_templates). |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
490 In case of absolue URL, base dir of template is used as base. For instance if |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
491 template is an absolute template to "/some/path/template.html", file will be |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
492 checked at "/some/path/<filename>" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
493 @param template_data(TemplateData): data of current template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
494 @return (TemplateData, None): built template data instance where .path is |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
495 the relative path to the file, from theme root dir. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
496 None if not found. |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
497 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
498 if template_data.site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
499 # we have and absolue path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
500 if (not template_data.theme is None |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
501 or not template_data.path.startswith(u'/')): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
502 raise exceptions.InternalError( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
503 u"invalid template data, was expecting absolute URL") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
504 static_dir = os.path.dirname(template_data.path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
505 file_path = os.path.join(static_dir, filename) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
506 if os.path.exists(file_path): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
507 return TemplateData(site=None, theme=None, path=file_path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
508 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
509 return None |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
510 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
511 sites_and_themes = TemplateLoader.getSitesAndThemes(template_data.site, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
512 template_data.theme) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
513 for site, theme in sites_and_themes: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
514 site_root_dir = self.sites_paths[site] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
515 relative_path = os.path.join(C.TEMPLATE_STATIC_DIR, filename) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
516 absolute_path = os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
517 theme, relative_path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
518 if os.path.exists(absolute_path): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
519 return TemplateData(site=site, theme=theme, path=relative_path) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
520 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
521 return None |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
522 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
523 def _appendCSSPaths(self, template_data, css_files, css_files_noscript, name_root): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
524 """Append found css to css_files and css_files_noscript |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
525 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
526 @param css_files(list): list to fill of relative path to found css file |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
527 @param css_files_noscript(list): list to fill of relative path to found css file |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
528 with "_noscript" suffix |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
529 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
530 name = name_root + u".css" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
531 css_path = self.getStaticPath(template_data, name) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
532 if css_path is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
533 css_files.append(self.getFrontURL(css_path)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
534 noscript_name = name_root + u"_noscript.css" |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
535 noscript_path = self.getStaticPath(template_data, noscript_name) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
536 if noscript_path is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
537 css_files_noscript.append(self.getFrontURL(noscript_path)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
538 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
539 def getCSSFiles(self, template_data): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
540 """Retrieve CSS files to use according template_data |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
541 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
542 For each element of the path, a .css file is looked for in /static, and returned |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
543 if it exists. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
544 Previous element are kept by replacing '/' with '_'. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
545 styles_extra.css, styles.css and fonts.css are always used if they exist. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
546 For each found file, if a file with the same name and "_noscript" suffix exists, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
547 it will be return is second part of resulting tuple. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
548 For instance, if template_data is (some_site, some_theme, blog/articles.html), |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
549 following files are returned, earch time trying [some_site root] first, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
550 then default site (i.e. sat_templates) root: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
551 - some_theme/static/styles.css is returned if it exists |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
552 else default/static/styles.css |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
553 - some_theme/static/blog.css is returned if it exists |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
554 else default/static/blog.css (if it exists too) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
555 - some_theme/static/blog_articles.css is returned if it exists |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
556 else default/static/blog_articles.css (if it exists too) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
557 and for each found files, if same file with _noscript suffix exists, it is put |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
558 in noscript list (for instance (some_theme/static/styles_noscript.css)). |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
559 @param template_data(TemplateData): data of the current template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
560 @return (tuple[list[unicode], list[unicode]]): a tuple with: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
561 - front URLs of CSS files to use |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
562 - front URLs of CSS files to use when scripts are not enabled |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
563 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
564 # TODO: some caching would be nice |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
565 css_files = [] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
566 css_files_noscript = [] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
567 path_elems = template_data.path.split(u'/') |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
568 path_elems[-1] = os.path.splitext(path_elems[-1])[0] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
569 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
570 for name_root in (u'styles_extra', u'styles'): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
571 self._appendCSSPaths(template_data, css_files, css_files_noscript, name_root) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
572 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
573 css_path = self.getStaticPath(template_data, u'fonts.css') |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
574 if css_path is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
575 css_files.append(self.getFrontURL(css_path)) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
576 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
577 for idx in xrange(len(path_elems)): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
578 name_root = u"_".join(path_elems[:idx+1]) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
579 self._appendCSSPaths(template_data, css_files, css_files_noscript, name_root) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
580 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
581 return css_files, css_files_noscript |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
582 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
583 ## custom filters ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
584 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
585 @jinja2.contextfilter |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
586 def _front_url(self, ctx, relative_url): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
587 """Get front URL (URL seen by end-user) from a relative URL |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
588 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
589 This default method return absolute full path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
590 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
591 template_data = ctx[u'template_data'] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
592 if template_data.site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
593 assert template_data.theme is None |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
594 assert template_data.path.startswith(u"/") |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
595 return os.path.join(os.path.dirname(template_data.path, relative_url)) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
596 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
597 site_root_dir = self.sites_paths[template_data.site] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
598 return os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
599 relative_url) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
600 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
601 @jinja2.contextfilter |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
602 def _next_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
603 """Use next current global index as suffix""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
604 next_ = ctx["gidx"].next(value) |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
605 return value if next_ == 0 else u"{}_{}".format(value, next_) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
606 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
607 @jinja2.contextfilter |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
608 def _cur_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
609 """Use current current global index as suffix""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
610 current = ctx["gidx"].current(value) |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
611 return value if not current else u"{}_{}".format(value, current) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
612 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
613 def _date_fmt( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
614 self, timestamp, fmt="short", date_only=False, auto_limit=None, auto_old_fmt=None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
615 ): |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
616 if is_undefined(fmt): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
617 fmt = u"short" |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
618 |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
619 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
620 return date_utils.date_fmt( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
621 timestamp, fmt, date_only, auto_limit, auto_old_fmt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
622 ) |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
623 except Exception as e: |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
624 log.warning(_(u"Can't parse date: {msg}").format(msg=e)) |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
625 return timestamp |
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
626 |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
627 def attr_escape(self, text): |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
628 """escape a text to a value usable as an attribute |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
629 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
630 remove spaces, and put in lower case |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
631 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
632 return RE_ATTR_ESCAPE.sub(u"_", text.strip().lower())[:50] |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
633 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
634 def _xmlui_class(self, xmlui_item, fields): |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
635 """return classes computed from XMLUI fields name |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
636 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
637 will return a string with a series of escaped {name}_{value} separated by spaces. |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
638 @param xmlui_item(xmlui.XMLUIPanel): XMLUI containing the widgets to use |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
639 @param fields(iterable(unicode)): names of the widgets to use |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
640 @return (unicode, None): computer string to use as class attribute value |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
641 None if no field was specified |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
642 """ |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
643 classes = [] |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
644 for name in fields: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
645 escaped_name = self.attr_escape(name) |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
646 try: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
647 for value in xmlui_item.widgets[name].values: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
648 classes.append(escaped_name + "_" + self.attr_escape(value)) |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
649 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
650 log.debug( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
651 _(u'ignoring field "{name}": it doesn\'t exists').format(name=name) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
652 ) |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
653 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
654 return u" ".join(classes) or None |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
655 |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
656 @jinja2.contextfilter |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
657 def _item_filter(self, ctx, item, filters): |
2402 | 658 """return item's value, filtered if suitable |
659 | |
660 @param item(object): item to filter | |
661 value must have name and value attributes, | |
662 mostly used for XMLUI items | |
663 @param filters(dict[unicode, (callable, dict, None)]): map of name => filter | |
664 if filter is None, return the value unchanged | |
665 if filter is a callable, apply it | |
666 if filter is a dict, it can have following keys: | |
667 - filters: iterable of filters to apply | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
668 - filters_args: kwargs of filters in the same order as filters (use empty |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
669 dict if needed) |
2402 | 670 - template: template to format where {value} is the filtered value |
671 """ | |
672 value = item.value | |
673 filter_ = filters.get(item.name, None) | |
674 if filter_ is None: | |
675 return value | |
676 elif isinstance(filter_, dict): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
677 filters_args = filter_.get(u"filters_args") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
678 for idx, f_name in enumerate(filter_.get(u"filters", [])): |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
679 kwargs = filters_args[idx] if filters_args is not None else {} |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
680 filter_func = self.env.filters[f_name] |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
681 try: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
682 eval_context_filter = filter_func.evalcontextfilter |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
683 except AttributeError: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
684 eval_context_filter = False |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
685 |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
686 if eval_context_filter: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
687 value = filter_func(ctx.eval_ctx, value, **kwargs) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
688 else: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
689 value = filter_func(value, **kwargs) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
690 template = filter_.get(u"template") |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
691 if template: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
692 # format will return a string, so we need to check first |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
693 # if the value is safe or not, and re-mark it after formatting |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
694 is_safe = isinstance(value, safe) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
695 value = template.format(value=value) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
696 if is_safe: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
697 value = safe(value) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
698 return value |
2402 | 699 |
2391
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
700 def _adv_format(self, value, template, **kwargs): |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
701 """Advancer formatter |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
702 |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
703 like format() method, but take care or special values like None |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
704 @param value(unicode): value to format |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
705 @param template(None, unicode): template to use with format() method. |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
706 It will be formatted using value=value and **kwargs |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
707 None to return value unchanged |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
708 @return (unicode): formatted value |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
709 """ |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
710 if template is None: |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
711 return value |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
712 # jinja use string when no special char is used, so we have to convert to unicode |
2391
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
713 return unicode(template).format(value=value, **kwargs) |
07e1543d6992
template: new "adv_format" filter which use Python's format and return whole value if template is None
Goffi <goffi@goffi.org>
parents:
2385
diff
changeset
|
714 |
2425
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
715 def _dict_ext(self, source_dict, extra_dict, key=None): |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
716 """extend source_dict with extra dict and return the result |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
717 |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
718 @param source_dict(dict): dictionary to extend |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
719 @param extra_dict(dict, None): dictionary to use to extend first one |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
720 None to return source_dict unmodified |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
721 @param key(unicode, None): if specified extra_dict[key] will be used |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
722 if it doesn't exists, a copy of unmodified source_dict is returned |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
723 @return (dict): resulting dictionary |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
724 """ |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
725 if extra_dict is None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
726 return source_dict |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
727 if key is not None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
728 extra_dict = extra_dict.get(key, {}) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
729 ret = source_dict.copy() |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
730 ret.update(extra_dict) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
731 return ret |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
732 |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
733 def highlight(self, code, lexer_name=None, lexer_opts=None, html_fmt_opts=None): |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
734 """Do syntax highlighting on code |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
735 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
736 Under the hood, pygments is used, check its documentation for options possible |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
737 values. |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
738 @param code(unicode): code or markup to highlight |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
739 @param lexer_name(unicode, None): name of the lexer to use |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
740 None to autodetect it |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
741 @param html_fmt_opts(dict, None): kword arguments to use for HtmlFormatter |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
742 @return (unicode): HTML markup with highlight classes |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
743 """ |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
744 if lexer_opts is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
745 lexer_opts = {} |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
746 if html_fmt_opts is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
747 html_fmt_opts = {} |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
748 if lexer_name is None: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
749 lexer = lexers.guess_lexer(code, **lexer_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
750 else: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
751 lexer = lexers.get_lexer_by_name(lexer_name, **lexer_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
752 formatter = formatters.HtmlFormatter(**html_fmt_opts) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
753 return safe(pygments.highlight(code, lexer, formatter)) |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
754 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
755 ## custom tests ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
756 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
757 def _in_the_past(self, timestamp): |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
758 """check if a date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
759 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
760 @param timestamp(unicode, int): unix time |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
761 @return (bool): True if date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
762 """ |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
763 return time.time() > int(timestamp) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
764 |
2515 | 765 ## template methods ## |
766 | |
767 def _icon_defs(self, *names): | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
768 """Define svg icons which will be used in the template. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
769 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
770 Their name is used as id |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
771 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
772 svg_elt = etree.Element( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
773 "svg", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
774 nsmap={None: "http://www.w3.org/2000/svg"}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
775 width="0", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
776 height="0", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
777 style="display: block", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
778 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
779 defs_elt = etree.SubElement(svg_elt, "defs") |
2515 | 780 for name in names: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
781 path = os.path.join(self.icons_path, name + u".svg") |
2515 | 782 icon_svg_elt = etree.parse(path).getroot() |
783 # we use icon name as id, so we can retrieve them easily | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
784 icon_svg_elt.set("id", name) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
785 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
786 raise exceptions.DataError(u"invalid SVG element") |
2515 | 787 defs_elt.append(icon_svg_elt) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
788 return safe(etree.tostring(svg_elt, encoding="unicode")) |
2515 | 789 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
790 def _icon_use(self, name, cls=""): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
791 return safe(u'<svg class="svg-icon{cls}" xmlns="http://www.w3.org/2000/svg" ' |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
792 u'viewBox="0 0 100 100">\n' |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
793 u' <use href="#{name}"/>' |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
794 u'</svg>\n'.format(name=name, cls=(" " + cls) if cls else "")) |
2515 | 795 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
796 def render(self, template, site=None, theme=None, locale=C.DEFAULT_LOCALE, |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
797 media_path=u"", css_files=None, css_inline=False, **kwargs): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
798 """Render a template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
799 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
800 @param template(unicode): template to render (e.g. blog/articles.html) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
801 @param site(unicide): site name |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
802 None or empty string for defaut site (i.e. SàT templates) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
803 @param theme(unicode): template theme |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
804 @param media_path(unicode): prefix of the SàT media path/URL to use for |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
805 template root. Must end with a u'/' |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
806 @param css_files(list[unicode],None): CSS files to used |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
807 CSS files must be in static dir of the template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
808 use None for automatic selection of CSS files based on template category |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
809 None is recommended. General static/style.css and theme file name will be |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
810 used. |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
811 @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
|
812 @param **kwargs: variable to transmit to the template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
813 """ |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
814 if not template: |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
815 raise ValueError(u"template can't be empty") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
816 if site is not None or theme is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
817 # user wants to set site and/or theme, so we add it to the template path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
818 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
819 site = u'' |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
820 if theme is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
821 theme = C.TEMPLATE_THEME_DEFAULT |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
822 if template[0] == u"(": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
823 raise ValueError( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
824 u"you can't specify site or theme in template path and in argument " |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
825 u"at the same time" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
826 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
827 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
828 template_data = TemplateData(site, theme, template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
829 template = u"({site}/{theme}){template}".format( |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
830 site=site, theme=theme, template=template) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
831 else: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
832 template_data = self.env.loader.parse_template(template) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
833 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
834 template_source = self.env.get_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
835 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
836 if css_files is None: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
837 css_files, css_files_noscript = self.getCSSFiles(template_data) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
838 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
839 kwargs["icon_defs"] = self._icon_defs |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
840 kwargs["icon"] = self._icon_use |
2515 | 841 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
842 if css_inline: |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
843 css_contents = [] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
844 for files, suffix in ((css_files, u""), |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
845 (css_files_noscript, u"_noscript")): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
846 site_root_dir = self.sites_paths[template_data.site] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
847 for css_file in files: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
848 css_file_path = os.path.join(site_root_dir, css_file) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
849 with open(css_file_path) as f: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
850 css_contents.append(f.read()) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
851 if css_contents: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
852 kwargs[u"css_content" + suffix] = u"\n".join(css_contents) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
853 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
854 scripts_handler = ScriptsHandler(self, template_data) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
855 self.setLocale(locale) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
856 # XXX: theme used in template arguments is the requested theme, which may differ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
857 # from actual theme if the template doesn't exist in the requested theme. |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
858 return template_source.render( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
859 template_data=template_data, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
860 media_path=media_path, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
861 css_files=css_files, |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
862 css_files_noscript=css_files, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
863 locale=self._locale, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
864 gidx=Indexer(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
865 script=scripts_handler, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
866 **kwargs |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
867 ) |