-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdist.py
93 lines (74 loc) · 2.78 KB
/
dist.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import re
import io
from pathlib import Path
from xml.etree import ElementTree
import asyncio
_dir = Path(__file__).parent
def icon_svg():
src_dir = _dir.joinpath('src', 'icon')
dist_dir = _dir.joinpath('dist')
dist_icon_path = dist_dir.joinpath('icon.tmp.svg')
svg_dir = dist_dir.joinpath('svg')
svg_dir.mkdir(parents=True, exist_ok=True)
with open(dist_icon_path, 'w') as f:
f.write('<svg></svg>')
ElementTree.register_namespace('', 'http://www.w3.org/2000/svg')
svg_symbol = ElementTree.parse(dist_icon_path)
def sort_by_name_caseless(path):
return str.casefold(str(path))
for svg_path in sorted(
src_dir.glob('**/*.svg'), key=sort_by_name_caseless):
with open(svg_path) as f:
svg_path = Path(svg_path)
svg = f.read()
svg = svg.replace('fill="#474747"', '')
svg = svg.replace('fill="#2e3436"', '')
svg = svg.replace('fill="#2e3434"', '')
svg = svg.replace('fill="#222222"', '')
svg = svg.replace('fill="#212121"', '')
svg = ElementTree.parse(io.StringIO(svg))
svg_root = svg.getroot()
# Fix node attrib
for node in svg_root.iter('*'):
keys = []
for key in node.attrib:
if re.match('^font-.*', key):
keys.append(key)
if re.match('style', key):
keys.append(key)
if re.match('color', key):
keys.append(key)
for key in keys:
del node.attrib[key]
filename = svg_path.name.replace('-symbolic.svg', '.svg')
id_ = filename.replace('.svg', '')
svg.write(svg_dir.joinpath(filename))
symbol = ElementTree.fromstring('<symbol></symbol>')
symbol.set('viewBox', '0 0 16 16')
symbol.set('id', id_)
title = ElementTree.fromstring('<title></title>')
title.text = id_
for t in svg_root.iter('{http://www.w3.org/2000/svg}title'):
svg_root.remove(t)
symbol.append(title)
for node in svg_root:
symbol.append(node)
for metadata in symbol.iter('{http://www.w3.org/2000/svg}metadata'):
symbol.remove(metadata)
svg_symbol.getroot().append(symbol)
for metadata in svg_symbol.getroot().iter('metadata'):
symbol.remove(metadata)
svg_symbol.write(dist_icon_path)
async def svg_optimize():
src = _dir.joinpath('dist/icon.tmp.svg')
dest = _dir.joinpath('dist/icon.svg')
cmd = f"scour {src} {dest}"
print(cmd)
proc = await asyncio.subprocess.create_subprocess_shell(cmd)
await proc.communicate()
Path.unlink(src)
async def main():
icon_svg()
await svg_optimize()
if __name__ == '__main__':
asyncio.run(main())