update templates handling folling changes in Libervia Templates

This commit is contained in:
Goffi 2025-12-19 22:43:20 +01:00
commit c4c3b5bd7c

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# SAT: an XMPP client # Libervia: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -50,18 +50,10 @@ from libervia.backend.core.log import getLogger
from libervia.backend.tools import config from libervia.backend.tools import config
from libervia.backend.tools.common import date_utils from libervia.backend.tools.common import date_utils
from libervia.frontends.tools import jid from libervia.frontends.tools import jid
from libervia.templates import get_path as get_templates_path
log = getLogger(__name__) log = getLogger(__name__)
try:
import sat_templates
except ImportError:
raise exceptions.MissingModule(
"sat_templates module is not available, please install it or check your path to "
"use template engine"
)
else:
sat_templates # to avoid pyflakes warning
try: try:
import jinja2 import jinja2
@ -78,7 +70,7 @@ except ImportError:
HTML_EXT = ("html", "xhtml") HTML_EXT = ("html", "xhtml")
RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]") RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
SITE_RESERVED_NAMES = ("sat",) SITE_RESERVED_NAMES = ("libervia",)
TPL_RESERVED_CHARS = r"()/." TPL_RESERVED_CHARS = r"()/."
RE_TPL_RESERVED_CHARS = re.compile("[" + TPL_RESERVED_CHARS + "]") RE_TPL_RESERVED_CHARS = re.compile("[" + TPL_RESERVED_CHARS + "]")
BROWSER_DIR = "_browser" BROWSER_DIR = "_browser"
@ -119,7 +111,7 @@ class TemplateLoader(jinja2.BaseLoader):
/absolute/path/to/template.html (in trusted environment only) /absolute/path/to/template.html (in trusted environment only)
@return (TemplateData): @return (TemplateData):
site, theme and template_path. site, theme and template_path.
if site is empty, SàT Templates are used if site is empty, Libervia Templates are used
site and theme can be both None if absolute path is used site and theme can be both None if absolute path is used
Relative path is the path from theme root dir e.g. blog/articles.html Relative path is the path from theme root dir e.g. blog/articles.html
""" """
@ -399,7 +391,7 @@ class Renderer:
self.host = host self.host = host
self.trusted = trusted self.trusted = trusted
self.sites_paths = { self.sites_paths = {
"": os.path.dirname(sat_templates.__file__), "": str(get_templates_path()),
} }
self.sites_themes = {} self.sites_themes = {}
if WebSettings is None: if WebSettings is None:
@ -502,7 +494,7 @@ class Renderer:
self._locale = Locale.parse(self._locale_str) self._locale = Locale.parse(self._locale_str)
self.install_translations() self.install_translations()
# we want to have access to SàT constants in templates # we want to have access to Libervia constants in templates
self.env.globals["C"] = C self.env.globals["C"] = C
# custom filters # custom filters
@ -549,7 +541,7 @@ class Renderer:
def install_translations(self): def install_translations(self):
# TODO: support multi translation # TODO: support multi translation
# for now, only translations in sat_templates are handled # for now, only translations in libervia_templates are handled
self.translations = {} self.translations = {}
for site_key, site_path in self.sites_paths.items(): for site_key, site_path in self.sites_paths.items():
site_prefix = "[{}] ".format(site_key) if site_key else "" site_prefix = "[{}] ".format(site_key) if site_key else ""
@ -558,16 +550,16 @@ class Renderer:
lang_path = os.path.join(i18n_dir, lang_dir) lang_path = os.path.join(i18n_dir, lang_dir)
if not os.path.isdir(lang_path): if not os.path.isdir(lang_path):
continue continue
po_path = os.path.join(lang_path, "LC_MESSAGES/sat.mo") po_path = os.path.join(lang_path, "LC_MESSAGES/libervia.mo")
try: try:
locale = Locale.parse(lang_dir) locale = Locale.parse(lang_dir)
with open(po_path, "rb") as f: with open(po_path, "rb") as f:
try: try:
translations = self.translations[locale] translations = self.translations[locale]
except KeyError: except KeyError:
self.translations[locale] = support.Translations(f, "sat") self.translations[locale] = support.Translations(f, "libervia")
else: else:
translations.merge(support.Translations(f, "sat")) translations.merge(support.Translations(f, "libervia"))
except EnvironmentError: except EnvironmentError:
log.error( log.error(
_("Can't find template translation at {path}").format( _("Can't find template translation at {path}").format(
@ -667,7 +659,7 @@ class Renderer:
File will be looked at <site_root_dir>/<theme_dir>/<static_dir>/filename, File will be looked at <site_root_dir>/<theme_dir>/<static_dir>/filename,
then <site_root_dir>/<default_theme_dir>/<static_dir>/filename anf finally then <site_root_dir>/<default_theme_dir>/<static_dir>/filename anf finally
<default_site>/<default_theme_dir>/<static_dir> (i.e. sat_templates). <default_site>/<default_theme_dir>/<static_dir> (i.e. libervia_templates).
In case of absolute URL, base dir of template is used as base. For instance if In case of absolute URL, base dir of template is used as base. For instance if
template is an absolute template to "/some/path/template.html", file will be template is an absolute template to "/some/path/template.html", file will be
checked at "/some/path/<filename>" checked at "/some/path/<filename>"
@ -740,7 +732,7 @@ class Renderer:
it will be returned is second part of resulting tuple. it will be returned is second part of resulting tuple.
For instance, if template_data is (some_site, some_theme, blog/articles.html), For instance, if template_data is (some_site, some_theme, blog/articles.html),
following files are returned, each time trying [some_site root] first, following files are returned, each time trying [some_site root] first,
then default site (i.e. sat_templates) root: then default site (i.e. libervia_templates) root:
- some_theme/static/styles.css is returned if it exists - some_theme/static/styles.css is returned if it exists
else default/static/styles.css else default/static/styles.css
- some_theme/static/blog.css is returned if it exists - some_theme/static/blog.css is returned if it exists
@ -1119,9 +1111,9 @@ class Renderer:
@param template(unicode): template to render (e.g. blog/articles.html) @param template(unicode): template to render (e.g. blog/articles.html)
@param site(unicode): site name @param site(unicode): site name
None or empty string for defaut site (i.e. SàT templates) None or empty string for defaut site (i.e. Libervia templates)
@param theme(unicode): template theme @param theme(unicode): template theme
@param media_path(unicode): prefix of the SàT media path/URL to use for @param media_path(unicode): prefix of the Libervia media path/URL to use for
template root. Must end with a u'/' template root. Must end with a u'/'
@param css_files(list[unicode],None): CSS files to use @param css_files(list[unicode],None): CSS files to use
CSS files must be in static dir of the template CSS files must be in static dir of the template