comparison libervia/backend/tools/common/template.py @ 4089:e7ee611fc860

tools (common/template): allow use of extra attributes in `icon` function
author Goffi <goffi@goffi.org>
date Thu, 08 Jun 2023 21:28:26 +0200
parents f742f28b3934
children 810921c33a47
comparison
equal deleted inserted replaced
4088:4325a0f13b0f 4089:e7ee611fc860
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 """Template generation""" 19 """Template generation"""
20 20
21 from collections import namedtuple
22 from datetime import datetime
23 import html
24 import json
21 import os.path 25 import os.path
26 from pathlib import Path
27 import re
22 import time 28 import time
23 import re 29 from typing import List, Optional, Tuple, Union
24 import json
25 from datetime import datetime
26 from pathlib import Path
27 from collections import namedtuple
28 from typing import Optional, List, Tuple, Union
29 from xml.sax.saxutils import quoteattr 30 from xml.sax.saxutils import quoteattr
31
30 from babel import support 32 from babel import support
31 from babel import Locale 33 from babel import Locale
32 from babel.core import UnknownLocaleError 34 from babel.core import UnknownLocaleError
35 from jinja2 import is_undefined
36 from jinja2 import utils
37 from jinja2 import TemplateNotFound
38 from jinja2 import pass_context
39 from jinja2.loaders import split_template_path
40 from lxml import etree
41 from markupsafe import Markup as safe
33 import pygments 42 import pygments
34 from pygments import lexers 43 from pygments import lexers
35 from pygments import formatters 44 from pygments import formatters
45
46 from libervia.backend.core import exceptions
36 from libervia.backend.core.constants import Const as C 47 from libervia.backend.core.constants import Const as C
37 from libervia.backend.core.i18n import _ 48 from libervia.backend.core.i18n import _
38 from libervia.backend.core import exceptions 49 from libervia.backend.core.log import getLogger
39 from libervia.backend.tools import config 50 from libervia.backend.tools import config
40 from libervia.backend.tools.common import date_utils 51 from libervia.backend.tools.common import date_utils
41 from libervia.backend.core.log import getLogger
42 52
43 log = getLogger(__name__) 53 log = getLogger(__name__)
44 54
45 try: 55 try:
46 import sat_templates 56 import sat_templates
58 raise exceptions.MissingModule( 68 raise exceptions.MissingModule(
59 "Missing module jinja2, please install it from http://jinja.pocoo.org or with " 69 "Missing module jinja2, please install it from http://jinja.pocoo.org or with "
60 "pip install jinja2" 70 "pip install jinja2"
61 ) 71 )
62 72
63 from lxml import etree 73
64 from markupsafe import Markup as safe
65 from jinja2 import is_undefined
66 from jinja2 import utils
67 from jinja2 import TemplateNotFound
68 from jinja2 import pass_context
69 from jinja2.loaders import split_template_path
70 74
71 HTML_EXT = ("html", "xhtml") 75 HTML_EXT = ("html", "xhtml")
72 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]") 76 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
73 SITE_RESERVED_NAMES = ("sat",) 77 SITE_RESERVED_NAMES = ("sat",)
74 TPL_RESERVED_CHARS = r"()/." 78 TPL_RESERVED_CHARS = r"()/."
953 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg": 957 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg":
954 raise exceptions.DataError("invalid SVG element") 958 raise exceptions.DataError("invalid SVG element")
955 defs_elt.append(icon_svg_elt) 959 defs_elt.append(icon_svg_elt)
956 return safe(etree.tostring(svg_elt, encoding="unicode")) 960 return safe(etree.tostring(svg_elt, encoding="unicode"))
957 961
958 def _icon_use(self, name, cls="", id=""): 962 def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe:
959 if id: 963 """Insert a icon previously defined with [_icon_defs]"""
960 id = id.replace('"', '_') 964 extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items())
961 return safe('<svg class="svg-icon{cls}"{id} xmlns="http://www.w3.org/2000/svg" ' 965 return safe(
962 'viewBox="0 0 100 100">\n' 966 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" '
963 ' <use href="#{name}"/>' 967 'viewBox="0 0 100 100">\n'
964 '</svg>\n'.format( 968 ' <use href="#{name}"/>'
965 name=name, 969 '</svg>\n'.format(
966 cls=(" " + cls) if cls else "", 970 name=name,
967 id=f' id="{id}"' if id else "" 971 cls=(" " + cls) if cls else "",
968 ) 972 extra_attrs=" " + extra_attrs if extra_attrs else ""
969 ) 973 )
974 )
970 975
971 def _icon_from_client(self, client): 976 def _icon_from_client(self, client):
972 """Get icon name to represent a disco client""" 977 """Get icon name to represent a disco client"""
973 if client is None: 978 if client is None:
974 return 'desktop' 979 return 'desktop'