Debian Codegraph @

imgsrc/srv/generate.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
4
5 import os
6 import re
7 import sys
8
9 from lxml import etree
10
11 SVG_NS = 'http://www.w3.org/2000/svg'
12 XLINK_NS = 'http://www.w3.org/1999/xlink'
13
14
15 def clone_node(node, parent):
16 ans = parent.makeelement(node.tag)
17 for k in node.keys():
18 ans.set(k, node.get(k))
19 ans.text, ans.tail = node.text, node.tail
20 for child in node.iterchildren('*'):
21 clone_node(child, ans)
22 parent.append(ans)
23 return ans
24
25
26 def merge():
27 base = os.path.dirname(os.path.abspath(__file__))
28 ans = etree.fromstring(f'<svg xmlns="{SVG_NS}" xmlns:xlink="{XLINK_NS}"/>', parser=etree.XMLParser(recover=True, no_network=True, resolve_entities=False))
29 for f in sorted(os.listdir(base)):
30 if not f.endswith('.svg'):
31 continue
32 with open(os.path.join(base, f), 'rb') as ff:
33 raw = ff.read()
34 svg = etree.fromstring(raw, parser=etree.XMLParser(recover=True, no_network=True, resolve_entities=False))
35 symbol = ans.makeelement('{%s}symbol' % SVG_NS) # noqa: UP031
36 symbol.set('viewBox', svg.get('viewBox') or '')
37 symbol.set('id', 'icon-' + f.rpartition('.')[0])
38 for child in svg.iterchildren('*'):
39 clone_node(child, symbol)
40 ans.append(symbol)
41 ans = etree.tostring(ans, encoding='unicode', pretty_print=True, with_tail=False)
42 ans = re.sub(r'<svg[^>]+>', '<svg style="display:none">', ans, count=1)
43 return ans
44
45
46 if __name__ == '__main__':
47 sys.stdout.write(merge().encode('utf-8'))