changeset 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 4325a0f13b0f
children e9d800b105c1
files libervia/backend/tools/common/template.py
diffstat 1 files changed, 32 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/tools/common/template.py	Thu Jun 08 21:27:12 2023 +0200
+++ b/libervia/backend/tools/common/template.py	Thu Jun 08 21:28:26 2023 +0200
@@ -18,27 +18,37 @@
 
 """Template generation"""
 
-import os.path
-import time
-import re
-import json
+from collections import namedtuple
 from datetime import datetime
+import html
+import json
+import os.path
 from pathlib import Path
-from collections import namedtuple
-from typing import Optional, List, Tuple, Union
+import re
+import time
+from typing import List, Optional, Tuple, Union
 from xml.sax.saxutils import quoteattr
+
 from babel import support
 from babel import Locale
 from babel.core import UnknownLocaleError
+from jinja2 import is_undefined
+from jinja2 import utils
+from jinja2 import TemplateNotFound
+from jinja2 import pass_context
+from jinja2.loaders import split_template_path
+from lxml import etree
+from markupsafe import Markup as safe
 import pygments
 from pygments import lexers
 from pygments import formatters
+
+from libervia.backend.core import exceptions
 from libervia.backend.core.constants import Const as C
 from libervia.backend.core.i18n import _
-from libervia.backend.core import exceptions
+from libervia.backend.core.log import getLogger
 from libervia.backend.tools import config
 from libervia.backend.tools.common import date_utils
-from libervia.backend.core.log import getLogger
 
 log = getLogger(__name__)
 
@@ -60,13 +70,7 @@
         "pip install jinja2"
     )
 
-from lxml import etree
-from markupsafe import Markup as safe
-from jinja2 import is_undefined
-from jinja2 import utils
-from jinja2 import TemplateNotFound
-from jinja2 import pass_context
-from jinja2.loaders import split_template_path
+
 
 HTML_EXT = ("html", "xhtml")
 RE_ATTR_ESCAPE = re.compile(r"[^a-z_-]")
@@ -955,18 +959,19 @@
             defs_elt.append(icon_svg_elt)
         return safe(etree.tostring(svg_elt, encoding="unicode"))
 
-    def _icon_use(self, name, cls="", id=""):
-        if id:
-            id = id.replace('"', '_')
-        return safe('<svg class="svg-icon{cls}"{id} xmlns="http://www.w3.org/2000/svg" '
-                    'viewBox="0 0 100 100">\n'
-                    '    <use href="#{name}"/>'
-                    '</svg>\n'.format(
-                        name=name,
-                        cls=(" " + cls) if cls else "",
-                        id=f' id="{id}"' if id else ""
-                    )
-                    )
+    def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe:
+        """Insert a icon previously defined with [_icon_defs]"""
+        extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items())
+        return safe(
+            '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" '
+            'viewBox="0 0 100 100">\n'
+            '    <use href="#{name}"/>'
+            '</svg>\n'.format(
+                name=name,
+                cls=(" " + cls) if cls else "",
+                extra_attrs=" " + extra_attrs if extra_attrs else ""
+            )
+        )
 
     def _icon_from_client(self, client):
         """Get icon name to represent a disco client"""