tools (common/template): allow use of extra attributes in icon function

This commit is contained in:
Goffi 2023-06-08 21:28:26 +02:00
commit fbf72390da

View file

@ -18,27 +18,37 @@
"""Template generation"""
import os.path
import time
import re
import json
from datetime import datetime
from pathlib import Path
from collections import namedtuple
from typing import Optional, List, Tuple, Union
from datetime import datetime
import html
import json
import os.path
from pathlib import Path
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 @@ except:
"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 @@ class Renderer(object):
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"""