Mercurial > libervia-backend
annotate libervia/backend/tools/common/template.py @ 4322:00837fa13e5a default tip @
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
following change in Libervia Media, code has been updated to use font-awesome now instead
of fontello.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Oct 2024 22:42:17 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
3 # SAT: an XMPP client |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # (at your option) any later version. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # 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
|
12 # 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
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # 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
|
17 # 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
|
18 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
19 """Template generation""" |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
21 from collections import namedtuple |
4031
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
22 from datetime import datetime |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
23 import html |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
24 import json |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
25 import os.path |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
26 from pathlib import Path |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
27 import re |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
28 import time |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
29 from typing import List, Optional, Tuple, Union |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
30 from xml.sax.saxutils import quoteattr |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
31 |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
32 from babel import support |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
33 from babel import Locale |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
34 from babel.core import UnknownLocaleError |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
35 from jinja2 import is_undefined |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
36 from jinja2 import utils |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
37 from jinja2 import TemplateNotFound |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
38 from jinja2 import pass_context |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
39 from jinja2.loaders import split_template_path |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
40 from lxml import etree |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
41 from markupsafe import Markup as safe |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
42 import pygments |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
43 from pygments import lexers |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
44 from pygments import formatters |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
45 |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
46 from libervia.backend.core import exceptions |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
47 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
48 from libervia.backend.core.i18n import _ |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
49 from libervia.backend.core.log import getLogger |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
50 from libervia.backend.tools import config |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
51 from libervia.backend.tools.common import date_utils |
4165 | 52 from libervia.frontends.tools import jid |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
53 |
3038
5f3068915686
common (template): suppress jinja2 depreaction warning (to be removed once 2.10.1 is released)
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
54 log = getLogger(__name__) |
5f3068915686
common (template): suppress jinja2 depreaction warning (to be removed once 2.10.1 is released)
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
55 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 try: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 import sat_templates |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
59 raise exceptions.MissingModule( |
3028 | 60 "sat_templates module is not available, please install it or check your path to " |
61 "use template engine" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
62 ) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 else: |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 sat_templates # to avoid pyflakes warning |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 try: |
3061
948833e3b542
tools (common/template): removed warning catches, jinja2>=2.10.3 now handles them correctly
Goffi <goffi@goffi.org>
parents:
3038
diff
changeset
|
67 import jinja2 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 except: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
69 raise exceptions.MissingModule( |
3028 | 70 "Missing module jinja2, please install it from http://jinja.pocoo.org or with " |
71 "pip install jinja2" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
72 ) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
74 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
75 HTML_EXT = ("html", "xhtml") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
76 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]") |
3028 | 77 SITE_RESERVED_NAMES = ("sat",) |
78 TPL_RESERVED_CHARS = r"()/." | |
79 RE_TPL_RESERVED_CHARS = re.compile("[" + TPL_RESERVED_CHARS + "]") | |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
80 BROWSER_DIR = "_browser" |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
81 BROWSER_META_FILE = "browser_meta.json" |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
82 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
83 TemplateData = namedtuple("TemplateData", ["site", "theme", "path"]) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
84 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
85 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
86 class TemplateLoader(jinja2.BaseLoader): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
87 """A template loader which handle site, theme and absolute paths""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
88 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
89 # TODO: list_templates should be implemented |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
90 |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
91 def __init__(self, sites_paths, sites_themes, trusted=False): |
2671
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 @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
|
94 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
|
95 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
|
96 launching user can access. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
97 """ |
3028 | 98 if not sites_paths or not "" in sites_paths: |
99 raise exceptions.InternalError("Invalid sites_paths") | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
100 super(jinja2.BaseLoader, self).__init__() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
101 self.sites_paths = sites_paths |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
102 self.sites_themes = sites_themes |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
103 self.trusted = trusted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
104 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
105 @staticmethod |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
106 def parse_template(template): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
107 """Parse template path and return site, theme and path |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
108 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
109 @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
|
110 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
|
111 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
|
112 (/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
|
113 (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
|
114 (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
|
115 /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
|
116 @return (TemplateData): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
117 site, theme and template_path. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
118 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
|
119 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
|
120 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
|
121 """ |
3028 | 122 if template.startswith("("): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
123 # site and/or theme are specified |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
124 try: |
3028 | 125 theme_end = template.index(")") |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
126 except IndexError: |
3028 | 127 raise ValueError("incorrect site/theme in template") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
128 theme_data = template[1:theme_end] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
129 theme_splitted = theme_data.split("/") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
130 if len(theme_splitted) == 1: |
3028 | 131 site, theme = "", theme_splitted[0] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
132 elif len(theme_splitted) == 2: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
133 site, theme = theme_splitted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
134 else: |
3028 | 135 raise ValueError("incorrect site/theme in template") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
136 template_path = template[theme_end + 1 :] |
3028 | 137 if not template_path or template_path.startswith("/"): |
138 raise ValueError("incorrect template path") | |
139 elif template.startswith("/"): | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
140 # 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
|
141 site = None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
142 theme = None |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
143 template_path = template |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
144 else: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
145 # a default template |
3028 | 146 site = "" |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
147 theme = C.TEMPLATE_THEME_DEFAULT |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
148 template_path = template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
149 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
150 if site is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
151 site = site.strip() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
152 if not site: |
3028 | 153 site = "" |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
154 elif site in SITE_RESERVED_NAMES: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
155 raise ValueError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
156 _("{site} can't be used as site name, " "it's reserved.").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
157 site=site |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
158 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
159 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
160 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
161 if theme is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
162 theme = theme.strip() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
163 if not theme: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
164 theme = C.TEMPLATE_THEME_DEFAULT |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
165 if RE_TPL_RESERVED_CHARS.search(theme): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
166 raise ValueError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
167 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
168 "{theme} contain forbidden char. Following chars " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
169 "are forbidden: {reserved}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
170 ).format(theme=theme, reserved=TPL_RESERVED_CHARS) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
171 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
172 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
173 return TemplateData(site, theme, template_path) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
174 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
175 @staticmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
176 def get_sites_and_themes( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
177 site: str, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
178 theme: str, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
179 settings: Optional[dict] = None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
180 ) -> List[Tuple[str, str]]: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
181 """Get sites and themes to check for template/file |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
182 |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
183 Will add default theme and default site in search list when suitable. Settings' |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
184 `fallback` can be used to modify behaviour: themes in this list will then be used |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
185 instead of default (it can also be empty list or None, in which case no fallback |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
186 is used). |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
187 |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
188 @param site: site requested |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
189 @param theme: theme requested |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
190 @return: site and theme couples to check |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
191 """ |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
192 if settings is None: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
193 settings = {} |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
194 sites_and_themes = [[site, theme]] |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
195 fallback = settings.get("fallback", [C.TEMPLATE_THEME_DEFAULT]) |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
196 for fb_theme in fallback: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
197 if theme != fb_theme: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
198 sites_and_themes.append([site, fb_theme]) |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
199 if site: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
200 for fb_theme in fallback: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
201 sites_and_themes.append(["", fb_theme]) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
202 return sites_and_themes |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
203 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
204 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
|
205 """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
|
206 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
207 @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
|
208 (default site will also checked) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
209 @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
|
210 @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
|
211 @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
|
212 - 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
|
213 - 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
|
214 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
215 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
216 raise exceptions.InternalError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
217 "_get_template_f must not be used with absolute path" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
218 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
219 settings = self.sites_themes[site][theme]["settings"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
220 for site_to_check, theme_to_check in self.get_sites_and_themes( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
221 site, theme, settings |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
222 ): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
223 try: |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
224 base_path = self.sites_paths[site_to_check] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
225 except KeyError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
226 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
227 _("Unregistered site requested: {site_to_check}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
228 site_to_check=site_to_check |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
229 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
230 ) |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
231 filepath = os.path.join( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
232 base_path, C.TEMPLATE_TPL_DIR, theme_to_check, *path_elts |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
233 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
234 f = utils.open_if_exists(filepath, "r") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
235 if f is not None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
236 return f, filepath |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
237 return None, None |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
238 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
239 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
|
240 """Retrieve source handling site and themes |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
241 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
242 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
|
243 else and exception is raised. |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
244 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
|
245 @raise PermissionError: absolute path used in untrusted environment |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
246 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
247 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
|
248 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
249 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
250 # we have an abolute template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
251 if theme is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
252 raise exceptions.InternalError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
253 "We can't have a theme with absolute " "template." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
254 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
255 if not self.trusted: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
256 log.error( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
257 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
258 "Absolute template used while unsecure is disabled, hack " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
259 "attempt? Template: {template}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
260 ).format(template=template) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
261 ) |
3028 | 262 raise exceptions.PermissionError("absolute template is not allowed") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
263 filepath = template_path |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
264 f = utils.open_if_exists(filepath, "r") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
265 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
266 # 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
|
267 assert theme and template_path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
268 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
|
269 # 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
|
270 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
|
271 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
272 if f is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
273 if ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
274 site is not None |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
275 and path_elts[0] == "error" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
276 and os.path.splitext(template_path)[1][1:] in HTML_EXT |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
277 ): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
278 # 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
|
279 # with base error. |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
280 f, filepath = self._get_template_f(site, theme, ("error", "base.html")) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
281 if f is None: |
3028 | 282 raise exceptions.InternalError("error/base.html should exist") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
283 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
284 raise TemplateNotFound(template) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
285 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
286 try: |
3028 | 287 contents = f.read() |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
288 finally: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
289 f.close() |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
290 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
291 mtime = os.path.getmtime(filepath) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
292 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
293 def uptodate(): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
294 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
295 return os.path.getmtime(filepath) == mtime |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
296 except OSError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
297 return False |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
298 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
299 return contents, filepath, uptodate |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
300 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
302 class Indexer(object): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
303 """Index global to a page""" |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
304 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
305 def __init__(self): |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
306 self._indexes = {} |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
307 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
308 def next(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
309 if value not in self._indexes: |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
310 self._indexes[value] = 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
311 return 0 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
312 self._indexes[value] += 1 |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
313 return self._indexes[value] |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
314 |
2385
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
315 def current(self, value): |
39d30cf722cb
template: gidx methods improvment:
Goffi <goffi@goffi.org>
parents:
2384
diff
changeset
|
316 return self._indexes.get(value) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
317 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
318 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
319 class ScriptsHandler(object): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
320 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
|
321 self.renderer = renderer |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
322 self.template_data = template_data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
323 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
|
324 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
325 def include(self, library_name, attribute="defer"): |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
326 """Mark that a script need to be imported. |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
327 |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
328 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
|
329 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
|
330 @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
|
331 @param loading: |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
332 """ |
3028 | 333 if attribute not in ("defer", "async", ""): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
334 raise exceptions.DataError( |
3028 | 335 _('Invalid attribute, please use one of "defer", "async" or ""') |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
336 ) |
3028 | 337 if not library_name.endswith(".js"): |
338 library_name = library_name + ".js" | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
339 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
|
340 self.scripts.append((library_name, attribute)) |
3028 | 341 return "" |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
342 |
2265
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
343 def generate_scripts(self): |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
344 """Generate the <script> elements |
322694543225
tools (common/template): ScriptsHandler fix/improvments:
Goffi <goffi@goffi.org>
parents:
2249
diff
changeset
|
345 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
346 @return (unicode): <scripts> HTML tags |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
347 """ |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
348 scripts = [] |
3028 | 349 tpl = "<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
|
350 for library, attribute in self.scripts: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
351 library_path = self.renderer.get_static_path(self.template_data, library) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
352 if library_path is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
353 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
354 _("Can't find {libary} javascript library").format(library=library) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
355 ) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
356 continue |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
357 path = self.renderer.get_front_url(library_path) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
358 scripts.append(tpl.format(src=quoteattr(path), attribute=attribute)) |
3028 | 359 return safe("\n".join(scripts)) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
360 |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
361 |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
362 class Environment(jinja2.Environment): |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
363 |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
364 def get_template(self, name, parent=None, globals=None): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
365 if name[0] not in ("/", "("): |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
366 # if name is not an absolute path or a full template name (this happen on |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
367 # extend or import during rendering), we convert it to a full template name. |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
368 # This is needed to handle cache correctly when a base template is overriden. |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
369 # Without that, we could not distinguish something like base/base.html if |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
370 # it's launched from some_site/some_theme or from [default]/default |
3028 | 371 name = "({site}/{theme}){template}".format( |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
372 site=self._template_data.site, |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
373 theme=self._template_data.theme, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
374 template=name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
375 ) |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
376 |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
377 return super(Environment, self).get_template(name, parent, globals) |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
378 |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
379 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 class Renderer(object): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
381 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
382 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
|
383 """ |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
384 @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
|
385 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
|
386 - 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
|
387 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
|
388 - 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
|
389 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
|
390 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
|
391 @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
|
392 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
|
393 @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
|
394 """ |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 self.host = host |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
396 self.trusted = trusted |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
397 self.sites_paths = { |
3028 | 398 "": os.path.dirname(sat_templates.__file__), |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
399 } |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
400 self.sites_themes = {} |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
401 conf = config.parse_main_conf() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
402 public_sites = config.config_get(conf, None, "sites_path_public_dict", {}) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
403 sites_data = [public_sites] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
404 if private: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
405 private_sites = config.config_get(conf, None, "sites_path_private_dict", {}) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
406 sites_data.append(private_sites) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
407 for sites in sites_data: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
408 normalised = {} |
3028 | 409 for name, path in sites.items(): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
410 if RE_TPL_RESERVED_CHARS.search(name): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
411 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
412 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
413 'Can\'t add "{name}" site, it contains forbidden ' |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
414 "characters. Forbidden characters are {forbidden}." |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
415 ).format(name=name, forbidden=TPL_RESERVED_CHARS) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
416 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
417 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
418 path = os.path.expanduser(os.path.normpath(path)) |
3028 | 419 if not path or not path.startswith("/"): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
420 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
421 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
422 'Can\'t add "{name}" site, it should map to an ' |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
423 "absolute path" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
424 ).format(name=name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
425 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
426 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
427 normalised[name] = path |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
428 self.sites_paths.update(normalised) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
429 |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
430 for site, site_path in self.sites_paths.items(): |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
431 tpl_path = Path(site_path) / C.TEMPLATE_TPL_DIR |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
432 for p in tpl_path.iterdir(): |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
433 if not p.is_dir(): |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
434 continue |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
435 log.debug(f"theme found for {site or 'default site'}: {p.name}") |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
436 theme_data = self.sites_themes.setdefault(site, {})[p.name] = { |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
437 "path": p, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
438 "settings": {}, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
439 } |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
440 theme_settings = p / "settings.json" |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
441 if theme_settings.is_file: |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
442 try: |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
443 with theme_settings.open() as f: |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
444 settings = json.load(f) |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
445 except Exception as e: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
446 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
447 _("Can't load theme settings at {path}: {e}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
448 path=theme_settings, e=e |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
449 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
450 ) |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
451 else: |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
452 log.debug( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
453 f"found settings for theme {p.name!r} at {theme_settings}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
454 ) |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
455 fallback = settings.get("fallback") |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
456 if fallback is None: |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
457 settings["fallback"] = [] |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
458 elif isinstance(fallback, str): |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
459 settings["fallback"] = [fallback] |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
460 elif not isinstance(fallback, list): |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
461 raise ValueError( |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
462 'incorrect type for "fallback" in settings ' |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
463 f"({type(fallback)}) at {theme_settings}: {fallback}" |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
464 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
465 theme_data["settings"] = settings |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
466 browser_path = p / BROWSER_DIR |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
467 if browser_path.is_dir(): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
468 theme_data["browser_path"] = browser_path |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
469 browser_meta_path = browser_path / BROWSER_META_FILE |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
470 if browser_meta_path.is_file(): |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
471 try: |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
472 with browser_meta_path.open() as f: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
473 theme_data["browser_meta"] = json.load(f) |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
474 except Exception as e: |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
475 log.error( |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
476 f"Can't parse browser metadata at {browser_meta_path}: {e}" |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
477 ) |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
478 continue |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
479 |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
480 self.env = Environment( |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
481 loader=TemplateLoader( |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
482 sites_paths=self.sites_paths, |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
483 sites_themes=self.sites_themes, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
484 trusted=trusted, |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
485 ), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
486 autoescape=jinja2.select_autoescape(["html", "xhtml", "xml"]), |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
487 trim_blocks=True, |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
488 lstrip_blocks=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
489 extensions=["jinja2.ext.i18n"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
490 ) |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
491 self.env._template_data = None |
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
|
492 self._locale_str = C.DEFAULT_LOCALE |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
493 self._locale = Locale.parse(self._locale_str) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
494 self.install_translations() |
2906
7dbdbd132649
template (i18n): activate ext.i18n.trimmed policy, to have clean translation strings. Set minimal jinja2 version to 2.10
Goffi <goffi@goffi.org>
parents:
2903
diff
changeset
|
495 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 # we want to have access to SàT constants in templates |
3028 | 497 self.env.globals["C"] = C |
2906
7dbdbd132649
template (i18n): activate ext.i18n.trimmed policy, to have clean translation strings. Set minimal jinja2 version to 2.10
Goffi <goffi@goffi.org>
parents:
2903
diff
changeset
|
498 |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
499 # custom filters |
4165 | 500 self.env.filters["bare_jid"] = self._bare_jid |
3028 | 501 self.env.filters["next_gidx"] = self._next_gidx |
502 self.env.filters["cur_gidx"] = self._cur_gidx | |
503 self.env.filters["date_fmt"] = self._date_fmt | |
4031
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
504 self.env.filters["timestamp_to_hour"] = self._timestamp_to_hour |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
505 self.env.filters["delta_to_human"] = date_utils.delta2human |
3028 | 506 self.env.filters["xmlui_class"] = self._xmlui_class |
507 self.env.filters["attr_escape"] = self.attr_escape | |
508 self.env.filters["item_filter"] = self._item_filter | |
509 self.env.filters["adv_format"] = self._adv_format | |
510 self.env.filters["dict_ext"] = self._dict_ext | |
511 self.env.filters["highlight"] = self.highlight | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
512 self.env.filters["front_url"] = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
513 self._front_url if front_url_filter is None else front_url_filter |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
514 ) |
4100
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
515 self.env.filters["media_type_main"] = self.media_type_main |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
516 self.env.filters["media_type_sub"] = self.media_type_sub |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
517 # custom tests |
3028 | 518 self.env.tests["in_the_past"] = self._in_the_past |
4322
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
519 self.fa_svgs_path = Path(host.media_dir, "fonts/fontawesome/svgs") |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
520 |
2906
7dbdbd132649
template (i18n): activate ext.i18n.trimmed policy, to have clean translation strings. Set minimal jinja2 version to 2.10
Goffi <goffi@goffi.org>
parents:
2903
diff
changeset
|
521 # policies |
3028 | 522 self.env.policies["ext.i18n.trimmed"] = True |
3306
3af0909629a2
common (template): better json dumping:
Goffi <goffi@goffi.org>
parents:
3274
diff
changeset
|
523 self.env.policies["json.dumps_kwargs"] = { |
3af0909629a2
common (template): better json dumping:
Goffi <goffi@goffi.org>
parents:
3274
diff
changeset
|
524 "sort_keys": True, |
3af0909629a2
common (template): better json dumping:
Goffi <goffi@goffi.org>
parents:
3274
diff
changeset
|
525 # if object can't be serialised, we use None |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
526 "default": lambda o: o.to_json() if hasattr(o, "to_json") else None, |
3306
3af0909629a2
common (template): better json dumping:
Goffi <goffi@goffi.org>
parents:
3274
diff
changeset
|
527 } |
2906
7dbdbd132649
template (i18n): activate ext.i18n.trimmed policy, to have clean translation strings. Set minimal jinja2 version to 2.10
Goffi <goffi@goffi.org>
parents:
2903
diff
changeset
|
528 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
529 def get_front_url(self, template_data, path=None): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
530 """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
|
531 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
532 @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
|
533 @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
|
534 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
|
535 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
536 return self.env.filters["front_url"]( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
537 {"template_data": template_data}, path or template_data.path |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
538 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
539 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
540 def install_translations(self): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
541 # TODO: support multi translation |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
542 # for now, only translations in sat_templates are handled |
2903
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
543 self.translations = {} |
3028 | 544 for site_key, site_path in self.sites_paths.items(): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
545 site_prefix = "[{}] ".format(site_key) if site_key else "" |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
546 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
|
547 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
|
548 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
|
549 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
|
550 continue |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
551 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
|
552 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
553 locale = Locale.parse(lang_dir) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
554 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
|
555 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
556 translations = self.translations[locale] |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
557 except KeyError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
558 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
|
559 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
560 translations.merge(support.Translations(f, "sat")) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
561 except EnvironmentError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
562 log.error( |
3028 | 563 _("Can't find template translation at {path}").format( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
564 path=po_path |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
565 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
566 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
567 except UnknownLocaleError as e: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
568 log.error( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
569 _("{site}Invalid locale name: {msg}").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
570 site=site_prefix, msg=e |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
571 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
572 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
573 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
574 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
575 _("{site}loaded {lang} templates translations").format( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
576 site=site_prefix, lang=lang_dir |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
577 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
578 ) |
2903
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
579 |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
580 default_locale = Locale.parse(self._locale_str) |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
581 if default_locale not in self.translations: |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
582 # default locale disable gettext, |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
583 # so we can use None instead of a Translations instance |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
584 self.translations[default_locale] = None |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
585 |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
586 self.env.install_null_translations(True) |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
587 # we generate a tuple of locales ordered by display name that templates can access |
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
588 # through the "locales" variable |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
589 self.locales = tuple( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
590 sorted(list(self.translations.keys()), key=lambda l: l.language_name.lower()) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
591 ) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
592 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
593 def set_locale(self, locale_str): |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
594 """set current locale |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
595 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
596 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
|
597 """ |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
598 if locale_str == self._locale_str: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
599 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
600 if locale_str == "en": |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
601 # we default to GB English when it's not specified |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
602 # 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
|
603 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
|
604 try: |
2dae79990122
template: print warning message and use DEFAULT_LOCALE on bad locale
Goffi <goffi@goffi.org>
parents:
2266
diff
changeset
|
605 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
|
606 except ValueError as e: |
3028 | 607 log.warning(_("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
|
608 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
|
609 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
|
610 |
3028 | 611 locale_str = str(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
|
612 if locale_str != C.DEFAULT_LOCALE: |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
613 try: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
614 translations = self.translations[locale] |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
615 except KeyError: |
3028 | 616 log.warning(_("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
|
617 locale_str = C.DEFAULT_LOCALE |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
618 locale = Locale.parse(self._locale_str) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
619 else: |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
620 self.env.install_gettext_translations(translations, True) |
3028 | 621 log.debug(_("Switched to {lang}").format(lang=locale.english_name)) |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
622 |
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
|
623 if locale_str == C.DEFAULT_LOCALE: |
2249
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
624 self.env.install_null_translations(True) |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
625 |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
626 self._locale = locale |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
627 self._locale_str = locale_str |
e572482f6cbd
core (tools/common/template): i18n support
Goffi <goffi@goffi.org>
parents:
2245
diff
changeset
|
628 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
629 def get_theme_and_root(self, template): |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
630 """retrieve theme and root dir of a given template |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
631 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
632 @param template(unicode): template to parse |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
633 @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
|
634 @raise NotFound: requested site has not been found |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
635 """ |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
636 # FIXME: check use in CLI frontend, and include site |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
637 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
|
638 if site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
639 # absolute template |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
640 return "", os.path.dirname(template) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
641 try: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
642 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
|
643 except KeyError: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
644 raise exceptions.NotFound |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
645 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
|
646 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
647 def get_themes_data(self, site_name): |
3266
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
648 try: |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
649 return self.sites_themes[site_name] |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
650 except KeyError: |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
651 raise exceptions.NotFound(f"no theme found for {site_name}") |
8ec5ddb4e759
tools (common/template): list themes and parse their browser data, available through new `getThemesData` method
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
652 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
653 def get_static_path( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
654 self, template_data: TemplateData, filename: str, settings: Optional[dict] = None |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
655 ) -> Optional[TemplateData]: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
656 """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
|
657 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
658 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
|
659 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
|
660 <default_site>/<default_theme_dir>/<static_dir> (i.e. sat_templates). |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
661 In case of absolute URL, base dir of template is used as base. For instance if |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
662 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
|
663 checked at "/some/path/<filename>" |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
664 @param template_data: data of current template |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
665 @param filename: name of the file to retrieve |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
666 @param settings: theme settings, can be used to modify behaviour |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
667 @return: built template data instance where .path is |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
668 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
|
669 None if not found. |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
670 """ |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
671 if template_data.site is None: |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
672 # we have an absolue path |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
673 if not template_data.theme is None or not template_data.path.startswith("/"): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
674 raise exceptions.InternalError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
675 "invalid template data, was expecting absolute URL" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
676 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
677 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
|
678 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
|
679 if os.path.exists(file_path): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
680 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
|
681 else: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
682 return None |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
683 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
684 sites_and_themes = TemplateLoader.get_sites_and_themes( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
685 template_data.site, template_data.theme, settings |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
686 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
687 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
|
688 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
|
689 relative_path = os.path.join(C.TEMPLATE_STATIC_DIR, filename) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
690 absolute_path = os.path.join( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
691 site_root_dir, C.TEMPLATE_TPL_DIR, theme, relative_path |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
692 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
693 if os.path.exists(absolute_path): |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
694 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
|
695 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
696 return None |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
697 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
698 def _append_css_paths( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
699 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
700 template_data: TemplateData, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
701 css_files: list, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
702 css_files_noscript: list, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
703 name_root: str, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
704 settings: dict, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
705 ) -> None: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
706 """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
|
707 |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
708 @param css_files: list to fill of relative path to found css file |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
709 @param css_files_noscript: list to fill of relative path to found css file |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
710 with "_noscript" suffix |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
711 """ |
3028 | 712 name = name_root + ".css" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
713 css_path = self.get_static_path(template_data, name, settings) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
714 if css_path is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
715 css_files.append(self.get_front_url(css_path)) |
3028 | 716 noscript_name = name_root + "_noscript.css" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
717 noscript_path = self.get_static_path(template_data, noscript_name, settings) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
718 if noscript_path is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
719 css_files_noscript.append(self.get_front_url(noscript_path)) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
720 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
721 def get_css_files(self, template_data): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
722 """Retrieve CSS files to use according template_data |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
723 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
724 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
|
725 if it exists. |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
726 Previous element are kept by replacing '/' with '_'. |
2877
f8427bf8c072
tools (common/template): always use highlight.css if present.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
727 styles_extra.css, styles.css, highlight.css and fonts.css are always used if they |
f8427bf8c072
tools (common/template): always use highlight.css if present.
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
728 exist. |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
729 For each found file, if a file with the same name and "_noscript" suffix exists, |
3267 | 730 it will be returned is second part of resulting tuple. |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
731 For instance, if template_data is (some_site, some_theme, blog/articles.html), |
3267 | 732 following files are returned, each time trying [some_site root] first, |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
733 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
|
734 - 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
|
735 else default/static/styles.css |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
736 - 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
|
737 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
|
738 - 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
|
739 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
|
740 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
|
741 in noscript list (for instance (some_theme/static/styles_noscript.css)). |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
742 The behaviour may be changed with theme settings: if "fallback" is set, specified |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
743 themes will be checked instead of default. The theme will be checked in given |
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
744 order, and "fallback" may be None or empty list to not check anything. |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
745 @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
|
746 @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
|
747 - 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
|
748 - 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
|
749 """ |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
750 # TODO: some caching would be nice |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
751 css_files = [] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
752 css_files_noscript = [] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
753 path_elems = template_data.path.split("/") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
754 path_elems[-1] = os.path.splitext(path_elems[-1])[0] |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
755 site = template_data.site |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
756 if site is None: |
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
757 # absolute path |
3478
b65175eb7769
tools (common/template): new `fallback` settings:
Goffi <goffi@goffi.org>
parents:
3311
diff
changeset
|
758 settings = {} |
3268
85c9cfcd4f5e
tools (common/template): theme settings with possibility to disable default fallback for CSS:
Goffi <goffi@goffi.org>
parents:
3267
diff
changeset
|
759 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
760 settings = self.sites_themes[site][template_data.theme]["settings"] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
761 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
762 css_path = self.get_static_path(template_data, "fonts.css", settings) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
763 if css_path is not None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
764 css_files.append(self.get_front_url(css_path)) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
765 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
766 for name_root in ("styles", "styles_extra", "highlight"): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
767 self._append_css_paths( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
768 template_data, css_files, css_files_noscript, name_root, settings |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
769 ) |
2683 | 770 |
3028 | 771 for idx in range(len(path_elems)): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
772 name_root = "_".join(path_elems[: idx + 1]) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
773 self._append_css_paths( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
774 template_data, css_files, css_files_noscript, name_root, settings |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
775 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
776 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
777 return css_files, css_files_noscript |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
778 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
779 ## custom filters ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
780 |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
781 @pass_context |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
782 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
|
783 """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
|
784 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
785 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
|
786 """ |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
787 template_data = ctx["template_data"] |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
788 if template_data.site is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
789 assert template_data.theme is None |
3028 | 790 assert template_data.path.startswith("/") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
791 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
|
792 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
793 site_root_dir = self.sites_paths[template_data.site] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
794 return os.path.join( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
795 site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme, relative_url |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
796 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
797 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
798 def _bare_jid(self, full_jid: str | jid.JID) -> str: |
4165 | 799 """Return the bare JID""" |
800 return str(jid.JID(str(full_jid)).bare) | |
801 | |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
802 @pass_context |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
803 def _next_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
804 """Use next current global index as suffix""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
805 next_ = ctx["gidx"].next(value) |
3028 | 806 return value if next_ == 0 else "{}_{}".format(value, next_) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
807 |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
808 @pass_context |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
809 def _cur_gidx(self, ctx, value): |
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
810 """Use current current global index as suffix""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
811 current = ctx["gidx"].current(value) |
3028 | 812 return value if not current else "{}_{}".format(value, current) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
813 |
4030
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
814 def _date_fmt( |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
815 self, |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
816 timestamp: Union[int, float], |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
817 fmt: str = "short", |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
818 date_only: bool = False, |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
819 auto_limit: int = 7, |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
820 auto_old_fmt: str = "short", |
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
821 auto_new_fmt: str = "relative", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
822 tz_name: Optional[str] = None, |
4030
73936abc6838
tools (common/template): let specify timezone name in `date_fmt` filter
Goffi <goffi@goffi.org>
parents:
3747
diff
changeset
|
823 ) -> str: |
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
|
824 if is_undefined(fmt): |
3028 | 825 fmt = "short" |
4031
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
826 |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
827 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
828 return date_utils.date_fmt( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
829 timestamp, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
830 fmt, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
831 date_only, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
832 auto_limit, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
833 auto_old_fmt, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
834 auto_new_fmt, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
835 locale_str=self._locale_str, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
836 tz_info=tz_name or date_utils.TZ_UTC, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
837 ) |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
838 except Exception as e: |
3028 | 839 log.warning(_("Can't parse date: {msg}").format(msg=e)) |
4031
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
840 return str(timestamp) |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
841 |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
842 def _timestamp_to_hour(self, timestamp: float) -> int: |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
843 """Get hour of day corresponding to a timestamp""" |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
844 dt = datetime.fromtimestamp(timestamp) |
a2d4bd1943ba
tools (common/template): add 2 new filters: `timestamp_to_hour` and `delta_to_human`:
Goffi <goffi@goffi.org>
parents:
4030
diff
changeset
|
845 return dt.hour |
2453
84e84a46b014
template (filters): don't crash on invalid date in date_fmt
Goffi <goffi@goffi.org>
parents:
2452
diff
changeset
|
846 |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
847 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
|
848 """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
|
849 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
850 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
|
851 """ |
3028 | 852 return RE_ATTR_ESCAPE.sub("_", 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
|
853 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
854 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
|
855 """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
|
856 |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
857 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
|
858 @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
|
859 @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
|
860 @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
|
861 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
|
862 """ |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
863 classes = [] |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
864 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
|
865 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
|
866 try: |
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
867 for value in xmlui_item.widgets[name].values: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
868 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
|
869 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
870 log.debug( |
3028 | 871 _('ignoring field "{name}": it doesn\'t exists').format(name=name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
872 ) |
2384
d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
Goffi <goffi@goffi.org>
parents:
2323
diff
changeset
|
873 continue |
3028 | 874 return " ".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
|
875 |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
876 @pass_context |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
877 def _item_filter(self, ctx, item, filters): |
2402 | 878 """return item's value, filtered if suitable |
879 | |
880 @param item(object): item to filter | |
881 value must have name and value attributes, | |
882 mostly used for XMLUI items | |
883 @param filters(dict[unicode, (callable, dict, None)]): map of name => filter | |
884 if filter is None, return the value unchanged | |
885 if filter is a callable, apply it | |
886 if filter is a dict, it can have following keys: | |
887 - 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
|
888 - 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
|
889 dict if needed) |
2402 | 890 - template: template to format where {value} is the filtered value |
891 """ | |
892 value = item.value | |
893 filter_ = filters.get(item.name, None) | |
894 if filter_ is None: | |
895 return value | |
896 elif isinstance(filter_, dict): | |
3028 | 897 filters_args = filter_.get("filters_args") |
898 for idx, f_name in enumerate(filter_.get("filters", [])): | |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
899 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
|
900 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
|
901 try: |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
902 eval_context_filter = filter_func.evalpass_context |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
903 except AttributeError: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
904 eval_context_filter = False |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
905 |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
906 if eval_context_filter: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
907 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
|
908 else: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
909 value = filter_func(value, **kwargs) |
3028 | 910 template = filter_.get("template") |
2422
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
911 if template: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
912 # 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
|
913 # 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
|
914 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
|
915 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
|
916 if is_safe: |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
917 value = safe(value) |
5425cf18929b
template: fixed the use of eval_context_filter in item_filter
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
918 return value |
2402 | 919 |
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
|
920 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
|
921 """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
|
922 |
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
|
923 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
|
924 @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
|
925 @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
|
926 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
|
927 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
|
928 @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
|
929 """ |
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
|
930 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
|
931 return value |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
932 # jinja use string when no special char is used, so we have to convert to unicode |
3028 | 933 return str(template).format(value=value, **kwargs) |
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
|
934 |
2425
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
935 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
|
936 """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
|
937 |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
938 @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
|
939 @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
|
940 None to return source_dict unmodified |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
941 @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
|
942 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
|
943 @return (dict): resulting dictionary |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
944 """ |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
945 if extra_dict is None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
946 return source_dict |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
947 if key is not None: |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
948 extra_dict = extra_dict.get(key, {}) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
949 ret = source_dict.copy() |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
950 ret.update(extra_dict) |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
951 return ret |
d294527bd46f
template: added dict_ext filter to extend a dictionary
Goffi <goffi@goffi.org>
parents:
2422
diff
changeset
|
952 |
2454
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
953 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
|
954 """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
|
955 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
956 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
|
957 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
|
958 @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
|
959 @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
|
960 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
|
961 @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
|
962 @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
|
963 """ |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
964 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
|
965 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
|
966 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
|
967 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
|
968 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
|
969 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
|
970 else: |
06ff33052354
core, template (filters): added pygments as a dependency + new highlight filter to use it.
Goffi <goffi@goffi.org>
parents:
2453
diff
changeset
|
971 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
|
972 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
|
973 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
|
974 |
4100
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
975 def media_type_main(self, value: Optional[str]) -> Optional[str]: |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
976 """Return main type of a media type""" |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
977 if not value: |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
978 return None |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
979 return value.partition("/")[0] |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
980 |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
981 def media_type_sub(self, value: Optional[str]) -> Optional[str]: |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
982 """Return main type of a media type""" |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
983 if not value: |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
984 return None |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
985 return value.partition("/")[1] |
810921c33a47
tools (common/template): add filter to get media types:
Goffi <goffi@goffi.org>
parents:
4089
diff
changeset
|
986 |
2403
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
987 ## custom tests ## |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
988 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
989 def _in_the_past(self, timestamp): |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
990 """check if a date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
991 |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
992 @param timestamp(unicode, int): unix time |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
993 @return (bool): True if date is in the past |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
994 """ |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
995 return time.time() > int(timestamp) |
dec31114c402
template: improved date formatter:
Goffi <goffi@goffi.org>
parents:
2402
diff
changeset
|
996 |
2515 | 997 ## template methods ## |
998 | |
999 def _icon_defs(self, *names): | |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1000 """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
|
1001 Their name is used as id |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1002 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1003 svg_elt = etree.Element( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1004 "svg", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1005 nsmap={None: "http://www.w3.org/2000/svg"}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1006 width="0", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1007 height="0", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1008 style="display: block", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1009 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1010 defs_elt = etree.SubElement(svg_elt, "defs") |
2515 | 1011 for name in names: |
4322
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1012 if "/" in name: |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1013 raise ValueError("SVG name must not have a `/` in it.") |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1014 if name.startswith("regular "): |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1015 name = name[8:] |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1016 path = self.fa_svgs_path / f"regular/{name}.svg" |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1017 else: |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1018 if name.startswith("solid "): |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1019 # "solid" can be explicitely specified, and it's the default value. |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1020 name = name[6:] |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1021 path = self.fa_svgs_path / f"solid/{name}.svg" |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1022 |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1023 # Parse the source SVG |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1024 try: |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1025 icon_svg_elt = etree.parse(path).getroot() |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1026 except OSError: |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1027 log.exception(f"Can't load icon at {path}.") |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1028 return safe("") |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1029 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1030 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg": |
3028 | 1031 raise exceptions.DataError("invalid SVG element") |
4322
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1032 |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1033 # Extract the viewBox and path data |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1034 viewbox = icon_svg_elt.get("viewBox") |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1035 path_element = icon_svg_elt.find(".//{http://www.w3.org/2000/svg}path") |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1036 |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1037 # Create a new symbol element |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1038 symbol = etree.SubElement( |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1039 defs_elt, |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1040 "symbol", |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1041 id=name, |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1042 viewBox=viewbox, |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1043 ) |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1044 |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1045 # Add the path to the symbol |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1046 if path_element is not None: |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1047 path = etree.SubElement( |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1048 symbol, |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1049 "path", |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1050 d=path_element.get("d"), |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1051 ) |
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1052 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1053 return safe(etree.tostring(svg_elt, encoding="unicode")) |
2515 | 1054 |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1055 def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe: |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1056 """Insert a icon previously defined with [_icon_defs]""" |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1057 extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items()) |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1058 return safe( |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1059 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" ' |
4322
00837fa13e5a
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
Goffi <goffi@goffi.org>
parents:
4270
diff
changeset
|
1060 'width="1em" height="1em" fill="currentColor" >\n' |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1061 ' <use href="#{name}"/>' |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1062 "</svg>\n".format( |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1063 name=name, |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1064 cls=(" " + cls) if cls else "", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1065 extra_attrs=" " + extra_attrs if extra_attrs else "", |
4089
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1066 ) |
e7ee611fc860
tools (common/template): allow use of extra attributes in `icon` function
Goffi <goffi@goffi.org>
parents:
4084
diff
changeset
|
1067 ) |
2515 | 1068 |
3274
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1069 def _icon_from_client(self, client): |
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1070 """Get icon name to represent a disco client""" |
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1071 if client is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1072 return "desktop" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1073 elif "pc" in client: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1074 return "desktop" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1075 elif "phone" in client: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1076 return "mobile" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1077 elif "web" in client: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1078 return "globe" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1079 elif "console" in client: |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1080 return "terminal" |
3274
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1081 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1082 return "desktop" |
3274
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1083 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1084 def render( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1085 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1086 template, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1087 site=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1088 theme=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1089 locale=C.DEFAULT_LOCALE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1090 media_path="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1091 css_files=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1092 css_inline=False, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1093 **kwargs, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1094 ): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1095 """Render a template |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1096 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1097 @param template(unicode): template to render (e.g. blog/articles.html) |
3267 | 1098 @param site(unicode): site name |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1099 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
|
1100 @param theme(unicode): template theme |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1101 @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
|
1102 template root. Must end with a u'/' |
3267 | 1103 @param css_files(list[unicode],None): CSS files to use |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1104 CSS files must be in static dir of the template |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1105 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
|
1106 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
|
1107 used. |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1108 @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
|
1109 @param **kwargs: variable to transmit to the template |
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1110 """ |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1111 if not template: |
3028 | 1112 raise ValueError("template can't be empty") |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1113 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
|
1114 # 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
|
1115 if site is None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1116 site = "" |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1117 if theme is None: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1118 theme = C.TEMPLATE_THEME_DEFAULT |
3028 | 1119 if template[0] == "(": |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1120 raise ValueError( |
3028 | 1121 "you can't specify site or theme in template path and in argument " |
1122 "at the same time" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1123 ) |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1124 |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1125 template_data = TemplateData(site, theme, template) |
3028 | 1126 template = "({site}/{theme}){template}".format( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1127 site=site, theme=theme, template=template |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1128 ) |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1129 else: |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1130 template_data = self.env.loader.parse_template(template) |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1131 |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1132 # we need to save template_data in environment, to load right templates when they |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1133 # are referenced from other templates (e.g. import) |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1134 # FIXME: this trick will not work anymore if we use async templates (it works |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1135 # here because we know that the rendering will be blocking until we unset |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1136 # _template_data) |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1137 self.env._template_data = template_data |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1138 |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1139 template_source = self.env.get_template(template) |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1140 |
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1141 if css_files is None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
1142 css_files, css_files_noscript = self.get_css_files(template_data) |
3747
dd1d1a582438
tools (common/template): fix setting of `css_files_noscript` when `css_file` is specified
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1143 else: |
dd1d1a582438
tools (common/template): fix setting of `css_files_noscript` when `css_file` is specified
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1144 css_files_noscript = [] |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1145 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1146 kwargs["icon_defs"] = self._icon_defs |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1147 kwargs["icon"] = self._icon_use |
3274
430204a3cc10
tools (common/template): new `icon_from_client` method:
Goffi <goffi@goffi.org>
parents:
3268
diff
changeset
|
1148 kwargs["icon_from_client"] = self._icon_from_client |
2515 | 1149 |
2159
5734b0994cf0
core (tools/common): template renderer first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1150 if css_inline: |
2169
f472179305a1
tools(templates): workflow improvments:
Goffi <goffi@goffi.org>
parents:
2159
diff
changeset
|
1151 css_contents = [] |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1152 for files, suffix in ((css_files, ""), (css_files_noscript, "_noscript")): |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1153 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
|
1154 for css_file in files: |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1155 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
|
1156 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
|
1157 css_contents.append(f.read()) |
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1158 if css_contents: |
3028 | 1159 kwargs["css_content" + suffix] = "\n".join(css_contents) |
2245
e09048cb7595
core (tools/common/template): helping methods/filters for templates:
Goffi <goffi@goffi.org>
parents:
2169
diff
changeset
|
1160 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1161 scripts_handler = ScriptsHandler(self, template_data) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4031
diff
changeset
|
1162 self.set_locale(locale) |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1163 |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1164 # 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
|
1165 # from actual theme if the template doesn't exist in the requested theme. |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1166 rendered = template_source.render( |
2671
0fa217fafabf
tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
1167 template_data=template_data, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1168 media_path=media_path, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1169 css_files=css_files, |
2680
ae5340b57ff8
template: fixed by variable used in css_files_noscript
Goffi <goffi@goffi.org>
parents:
2676
diff
changeset
|
1170 css_files_noscript=css_files_noscript, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1171 locale=self._locale, |
2903
68a7543ebbb3
template: added "locales" variables to templates:
Goffi <goffi@goffi.org>
parents:
2877
diff
changeset
|
1172 locales=self.locales, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1173 gidx=Indexer(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1174 script=scripts_handler, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4165
diff
changeset
|
1175 **kwargs, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
1176 ) |
2676
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1177 self.env._template_data = None |
da8f3ac86845
template: overriden get_template to always have full template name:
Goffi <goffi@goffi.org>
parents:
2675
diff
changeset
|
1178 return rendered |