-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_js.py
executable file
·65 lines (49 loc) · 1.57 KB
/
generate_js.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import json
import os
from glob import glob
from string import Template
from xml.dom.minidom import Document, parse
SVG = "svg"
OUTFILE = os.path.join("dist", "thermal_comfort_icons.js")
def get_path(dom: Document) -> str:
"""Get the path of the svg file."""
return dom.getElementsByTagName("path")[0].getAttribute("d")
def get_id(dom: Document) -> str:
"""Get the id of the svg file."""
return dom.getElementsByTagName(SVG)[0].getAttribute("id")
def get_keywords(dom: Document) -> str:
"""Get the keywords of the svg file."""
desc_tags = dom.getElementsByTagName("desc")
if len(desc_tags) > 0 and desc_tags[0].firstChild is not None:
return desc_tags[0].firstChild.nodeValue.split(" ")
else:
return []
doms = [parse(file) for file in glob(os.path.join(SVG, f"*.{SVG}"))]
icons = {
get_id(dom): {
"path": get_path(dom),
"keywords": get_keywords(dom),
}
for dom in doms
}
template = Template(
"""const TC_ICONS_MAP = $icons;
async function getIcon(name) {
return {path: TC_ICONS_MAP[name]?.path};
}
async function getIconList() {
return Object.entries(TC_ICONS_MAP).map(([icon, content]) => ({
name: icon,
keywords: content.keywords,
}));
}
window.customIcons = window.customIcons || {};
window.customIcons["tc"] = { getIcon, getIconList };
window.customIconsets = window.customIconsets || {};
window.customIconsets["tc"] = getIcon;
"""
)
js = template.substitute(icons=json.dumps(icons, sort_keys=True, indent=2))
with open(OUTFILE, "w") as outfile:
outfile.write(js)