From 82b88f920a85487372cc6e0b46633e4aa328eb69 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 26 Sep 2019 14:27:19 +0200 Subject: Modify for pogojig infrastructure --- support/inkscape_svg_filter_layers.py | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 support/inkscape_svg_filter_layers.py (limited to 'support') diff --git a/support/inkscape_svg_filter_layers.py b/support/inkscape_svg_filter_layers.py new file mode 100755 index 0000000..9301e1e --- /dev/null +++ b/support/inkscape_svg_filter_layers.py @@ -0,0 +1,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 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)) -- cgit