comparison libervia/backend/tools/common/template.py @ 4322:00837fa13e5a default tip @

tools (common/template), cli (call/gui): use font-awesome instead of fontello: following change in Libervia Media, code has been updated to use font-awesome now instead of fontello.
author Goffi <goffi@goffi.org>
date Sat, 26 Oct 2024 22:42:17 +0200
parents 0d7bb4df2343
children
comparison
equal deleted inserted replaced
4321:2246eeeccc74 4322:00837fa13e5a
514 ) 514 )
515 self.env.filters["media_type_main"] = self.media_type_main 515 self.env.filters["media_type_main"] = self.media_type_main
516 self.env.filters["media_type_sub"] = self.media_type_sub 516 self.env.filters["media_type_sub"] = self.media_type_sub
517 # custom tests 517 # custom tests
518 self.env.tests["in_the_past"] = self._in_the_past 518 self.env.tests["in_the_past"] = self._in_the_past
519 self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg") 519 self.fa_svgs_path = Path(host.media_dir, "fonts/fontawesome/svgs")
520 520
521 # policies 521 # policies
522 self.env.policies["ext.i18n.trimmed"] = True 522 self.env.policies["ext.i18n.trimmed"] = True
523 self.env.policies["json.dumps_kwargs"] = { 523 self.env.policies["json.dumps_kwargs"] = {
524 "sort_keys": True, 524 "sort_keys": True,
996 996
997 ## template methods ## 997 ## template methods ##
998 998
999 def _icon_defs(self, *names): 999 def _icon_defs(self, *names):
1000 """Define svg icons which will be used in the template. 1000 """Define svg icons which will be used in the template.
1001
1002 Their name is used as id 1001 Their name is used as id
1003 """ 1002 """
1004 svg_elt = etree.Element( 1003 svg_elt = etree.Element(
1005 "svg", 1004 "svg",
1006 nsmap={None: "http://www.w3.org/2000/svg"}, 1005 nsmap={None: "http://www.w3.org/2000/svg"},
1008 height="0", 1007 height="0",
1009 style="display: block", 1008 style="display: block",
1010 ) 1009 )
1011 defs_elt = etree.SubElement(svg_elt, "defs") 1010 defs_elt = etree.SubElement(svg_elt, "defs")
1012 for name in names: 1011 for name in names:
1013 path = os.path.join(self.icons_path, name + ".svg") 1012 if "/" in name:
1014 icon_svg_elt = etree.parse(path).getroot() 1013 raise ValueError("SVG name must not have a `/` in it.")
1015 # we use icon name as id, so we can retrieve them easily 1014 if name.startswith("regular "):
1016 icon_svg_elt.set("id", name) 1015 name = name[8:]
1016 path = self.fa_svgs_path / f"regular/{name}.svg"
1017 else:
1018 if name.startswith("solid "):
1019 # "solid" can be explicitely specified, and it's the default value.
1020 name = name[6:]
1021 path = self.fa_svgs_path / f"solid/{name}.svg"
1022
1023 # Parse the source SVG
1024 try:
1025 icon_svg_elt = etree.parse(path).getroot()
1026 except OSError:
1027 log.exception(f"Can't load icon at {path}.")
1028 return safe("")
1029
1017 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg": 1030 if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg":
1018 raise exceptions.DataError("invalid SVG element") 1031 raise exceptions.DataError("invalid SVG element")
1019 defs_elt.append(icon_svg_elt) 1032
1033 # Extract the viewBox and path data
1034 viewbox = icon_svg_elt.get("viewBox")
1035 path_element = icon_svg_elt.find(".//{http://www.w3.org/2000/svg}path")
1036
1037 # Create a new symbol element
1038 symbol = etree.SubElement(
1039 defs_elt,
1040 "symbol",
1041 id=name,
1042 viewBox=viewbox,
1043 )
1044
1045 # Add the path to the symbol
1046 if path_element is not None:
1047 path = etree.SubElement(
1048 symbol,
1049 "path",
1050 d=path_element.get("d"),
1051 )
1052
1020 return safe(etree.tostring(svg_elt, encoding="unicode")) 1053 return safe(etree.tostring(svg_elt, encoding="unicode"))
1021 1054
1022 def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe: 1055 def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe:
1023 """Insert a icon previously defined with [_icon_defs]""" 1056 """Insert a icon previously defined with [_icon_defs]"""
1024 extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items()) 1057 extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items())
1025 return safe( 1058 return safe(
1026 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" ' 1059 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" '
1027 'viewBox="0 0 100 100">\n' 1060 'width="1em" height="1em" fill="currentColor" >\n'
1028 ' <use href="#{name}"/>' 1061 ' <use href="#{name}"/>'
1029 "</svg>\n".format( 1062 "</svg>\n".format(
1030 name=name, 1063 name=name,
1031 cls=(" " + cls) if cls else "", 1064 cls=(" " + cls) if cls else "",
1032 extra_attrs=" " + extra_attrs if extra_attrs else "", 1065 extra_attrs=" " + extra_attrs if extra_attrs else "",