File indexing completed on 2024-04-21 14:53:10

0001 #!/usr/bin/env python3
0002 """
0003 SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
0004 SPDX-FileCopyrightText: 2020 Noah Davis <noahadvs@gmail.com>
0005 SPDX-FileCopyrightText: 2020 Niccolò Venerandi <niccolo@venerandi.com>
0006 """
0007 import sys
0008 from lxml import etree
0009 """
0010 This is a template for making scripts that modify SVGs by parsing XML.
0011 """
0012 
0013 # These are needed to prevent nonsense namespaces like ns0 from being
0014 # added to otherwise perfectly fine svg elements and attributes
0015 etree.register_namespace("w3c", "http://www.w3.org/2000/svg")
0016 etree.register_namespace("xlink", "http://www.w3.org/1999/xlink")
0017 etree.register_namespace("inkscape", "http://www.inkscape.org/namespaces/inkscape")
0018 etree.register_namespace("dc", "http://purl.org/dc/elements/1.1/")
0019 etree.register_namespace("cc", "http://creativecommons.org/ns#")
0020 etree.register_namespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
0021 etree.register_namespace("sodipodi", "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd")
0022 
0023 # Get filenames as arguments. Combine with your favorite CLI tools.
0024 # My favorites are rg (aka ripgrep) and fd (aka fd-find).
0025 # Remember to filter out files that are not SVGs!
0026 # Example: ./this-script.py $(rg -t svg -l 'height="0"')
0027 for f in sys.argv[1:]:
0028     tree = etree.parse(f)
0029     root = tree.getroot()
0030     wasEdited = False
0031 
0032     # BEGIN section
0033     # Reimplement this section as needed
0034     # {http://www.w3.org/2000/svg} is needed to find SVG elements
0035     # Example: find all rect elements
0036     for elem in root.iterfind(".//{http://www.w3.org/2000/svg}rect"):
0037         # Example: find rect elements where height="0"
0038         if (elem.get("height") == "0"):
0039             # Example: remove rect elements that have height="0"
0040             elem.getparent().remove(elem)
0041             wasEdited = True  # Remember to keep this
0042     # END section
0043 
0044     print(f + ": " + ("edited" if wasEdited else "ignored"))
0045     if wasEdited:
0046         tree.write(f, encoding="utf-8", xml_declaration=False, method="xml")