forked from KDE/breeze-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg-xml-script-template.py
executable file
·46 lines (42 loc) · 1.93 KB
/
svg-xml-script-template.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
#!/usr/bin/env python3
"""
SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
SPDX-FileCopyrightText: 2020 Noah Davis <[email protected]>
SPDX-FileCopyrightText: 2020 Niccolò Venerandi <[email protected]>
"""
import sys
from lxml import etree
"""
This is a template for making scripts that modify SVGs by parsing XML.
"""
# These are needed to prevent nonsense namespaces like ns0 from being
# added to otherwise perfectly fine svg elements and attributes
etree.register_namespace("w3c", "http://www.w3.org/2000/svg")
etree.register_namespace("xlink", "http://www.w3.org/1999/xlink")
etree.register_namespace("inkscape", "http://www.inkscape.org/namespaces/inkscape")
etree.register_namespace("dc", "http://purl.org/dc/elements/1.1/")
etree.register_namespace("cc", "http://creativecommons.org/ns#")
etree.register_namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
etree.register_namespace("sodipodi", "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd")
# Get filenames as arguments. Combine with your favorite CLI tools.
# My favorites are rg (aka ripgrep) and fd (aka fd-find).
# Remember to filter out files that are not SVGs!
# Example: ./this-script.py $(rg -t svg -l 'height="0"')
for f in sys.argv[1:]:
tree = etree.parse(f)
root = tree.getroot()
wasEdited = False
# BEGIN section
# Reimplement this section as needed
# {http://www.w3.org/2000/svg} is needed to find SVG elements
# Example: find all rect elements
for elem in root.iterfind(".//{http://www.w3.org/2000/svg}rect"):
# Example: find rect elements where height="0"
if (elem.get("height") == "0"):
# Example: remove rect elements that have height="0"
elem.getparent().remove(elem)
wasEdited = True # Remember to keep this
# END section
print(f + ": " + ("edited" if wasEdited else "ignored"))
if wasEdited:
tree.write(f, encoding="utf-8", xml_declaration=False, method="xml")