summaryrefslogtreecommitdiff
path: root/renderer/support/inkscape_svg_filter_layers.py
blob: 9301e1e6d717188694a4c6cc245078b377a9b91d (plain)
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
#!/usr/bin/env python3

import xml.etree.ElementTree as xe
import argparse
import re


if __name__ != '__main__':
    raise SystemError('Not running as shell script')


parser = argparse.ArgumentParser()
parser.add_argument('infile', metavar='input.svg', type=argparse.FileType('r'))
parser.add_argument('outfile', metavar='output.svg', type=argparse.FileType('wb'))
parser.add_argument('-n', '--name', nargs='+', default=[], help='Remove layers with this exact name (case-insensitive)')
parser.add_argument('-r', '--regex', nargs='+', default=[], help='Remove layers with names matching this regex')
parser.add_argument('-i', '--invisible', action='store_true', help='Remove hidden (invisible) layers')
parser.add_argument('-o', '--only', action='store_true', help='Invert logic, i.e. keep matched layers and discard others')
parser.add_argument('-d', '--strip-defs', action='store_true', help='Also strip any <defs> tags (off by default)')
args = parser.parse_args()

doc = xe.fromstring(args.infile.read())
ns = {
        'svg': 'http://www.w3.org/2000/svg',
        'inkscape': 'http://www.inkscape.org/namespaces/inkscape',
        'sodipodi': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'
}

if args.strip_defs:
    for elem in doc.findall('svg:defs', ns):
        doc.remove(elem)

for i, g in enumerate(doc.findall('svg:g', ns)):
    if g.attrib.get(f'{{{ns["inkscape"]}}}groupmode') != 'layer':
        continue

    label = g.attrib.get(f'{{{ns["inkscape"]}}}label', '')
    match = (
            any(label == name for name in args.name) or
            any(re.match(regex, label) for regex in args.regex) or
            ('display:none' in g.attrib.get('style', '') and args.hidden)
            )
    print(f'Layer {i} "{label}": {"match" if match else "not found"}', end='')

    if match != args.only:
        print(', removing.')
        doc.remove(g)
    else:
        print()

args.outfile.write(xe.tostring(doc))