comparison libervia/backend/tools/common/template.py @ 4353:e22a5d80be02

template: fix name handling for icons.
author Goffi <goffi@goffi.org>
date Fri, 11 Apr 2025 18:18:50 +0200
parents 00837fa13e5a
children 4a8bb49b9d0f
comparison
equal deleted inserted replaced
4352:382dc6e62b6e 4353:e22a5d80be02
1010 defs_elt = etree.SubElement(svg_elt, "defs") 1010 defs_elt = etree.SubElement(svg_elt, "defs")
1011 for name in names: 1011 for name in names:
1012 if "/" in name: 1012 if "/" in name:
1013 raise ValueError("SVG name must not have a `/` in it.") 1013 raise ValueError("SVG name must not have a `/` in it.")
1014 if name.startswith("regular "): 1014 if name.startswith("regular "):
1015 name = name[8:] 1015 filename = name[8:]
1016 path = self.fa_svgs_path / f"regular/{name}.svg" 1016 path = self.fa_svgs_path / f"regular/{filename}.svg"
1017 else: 1017 else:
1018 if name.startswith("solid "): 1018 if name.startswith("solid "):
1019 # "solid" can be explicitely specified, and it's the default value. 1019 # "solid" can be explicitely specified, and it's the default value.
1020 name = name[6:] 1020 filename = name[6:]
1021 path = self.fa_svgs_path / f"solid/{name}.svg" 1021 else:
1022 filename = name
1023 path = self.fa_svgs_path / f"solid/{filename}.svg"
1022 1024
1023 # Parse the source SVG 1025 # Parse the source SVG
1024 try: 1026 try:
1025 icon_svg_elt = etree.parse(path).getroot() 1027 icon_svg_elt = etree.parse(path).getroot()
1026 except OSError: 1028 except OSError:
1036 1038
1037 # Create a new symbol element 1039 # Create a new symbol element
1038 symbol = etree.SubElement( 1040 symbol = etree.SubElement(
1039 defs_elt, 1041 defs_elt,
1040 "symbol", 1042 "symbol",
1041 id=name, 1043 id=name.replace(" ", "_"),
1042 viewBox=viewbox, 1044 viewBox=viewbox,
1043 ) 1045 )
1044 1046
1045 # Add the path to the symbol 1047 # Add the path to the symbol
1046 if path_element is not None: 1048 if path_element is not None:
1058 return safe( 1060 return safe(
1059 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" ' 1061 '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" '
1060 'width="1em" height="1em" fill="currentColor" >\n' 1062 'width="1em" height="1em" fill="currentColor" >\n'
1061 ' <use href="#{name}"/>' 1063 ' <use href="#{name}"/>'
1062 "</svg>\n".format( 1064 "</svg>\n".format(
1063 name=name, 1065 name=name.replace(" ", "_"),
1064 cls=(" " + cls) if cls else "", 1066 cls=(" " + cls) if cls else "",
1065 extra_attrs=" " + extra_attrs if extra_attrs else "", 1067 extra_attrs=" " + extra_attrs if extra_attrs else "",
1066 ) 1068 )
1067 ) 1069 )
1068 1070