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
# SAT: an XMPP client
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# Libervia: an XMPP client
# Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
# 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
@ -50,18 +50,10 @@ from libervia.backend.core.log import getLogger
from libervia.backend.tools import config
from libervia.backend.tools.common import date_utils
from libervia.frontends.tools import jid
from libervia.templates import get_path as get_templates_path
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:
import jinja2
@ -78,7 +70,7 @@ except ImportError:
HTML_EXT = ("html", "xhtml")
RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
SITE_RESERVED_NAMES = ("sat",)
SITE_RESERVED_NAMES = ("libervia",)
TPL_RESERVED_CHARS = r"()/."
RE_TPL_RESERVED_CHARS = re.compile("[" + TPL_RESERVED_CHARS + "]")
BROWSER_DIR = "_browser"
@ -119,7 +111,7 @@ class TemplateLoader(jinja2.BaseLoader):
/absolute/path/to/template.html (in trusted environment only)
@return (TemplateData):
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
Relative path is the path from theme root dir e.g. blog/articles.html
"""
@ -399,7 +391,7 @@ class Renderer:
self.host = host
self.trusted = trusted
self.sites_paths = {
"": os.path.dirname(sat_templates.__file__),
"": str(get_templates_path()),
}
self.sites_themes = {}
if WebSettings is None:
@ -502,7 +494,7 @@ class Renderer:
self._locale = Locale.parse(self._locale_str)
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
# custom filters
@ -549,7 +541,7 @@ class Renderer:
def install_translations(self):
# TODO: support multi translation
# for now, only translations in sat_templates are handled
# for now, only translations in libervia_templates are handled
self.translations = {}
for site_key, site_path in self.sites_paths.items():
site_prefix = "[{}] ".format(site_key) if site_key else ""
@ -558,16 +550,16 @@ class Renderer:
lang_path = os.path.join(i18n_dir, lang_dir)
if not os.path.isdir(lang_path):
continue
po_path = os.path.join(lang_path, "LC_MESSAGES/sat.mo")
po_path = os.path.join(lang_path, "LC_MESSAGES/libervia.mo")
try:
locale = Locale.parse(lang_dir)
with open(po_path, "rb") as f:
try:
translations = self.translations[locale]
except KeyError:
self.translations[locale] = support.Translations(f, "sat")
self.translations[locale] = support.Translations(f, "libervia")
else:
translations.merge(support.Translations(f, "sat"))
translations.merge(support.Translations(f, "libervia"))
except EnvironmentError:
log.error(
_("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,
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
template is an absolute template to "/some/path/template.html", file will be
checked at "/some/path/<filename>"
@ -740,7 +732,7 @@ class Renderer:
it will be returned is second part of resulting tuple.
For instance, if template_data is (some_site, some_theme, blog/articles.html),
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
else default/static/styles.css
- 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 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 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'/'
@param css_files(list[unicode],None): CSS files to use
CSS files must be in static dir of the template