Added svg formatter for diagrams stored in git to minimize diffs
This commit is contained in:
51
util/format_svg.py
Executable file
51
util/format_svg.py
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
##
|
||||
## util/format_svg.py
|
||||
##
|
||||
## Copyright (c) 2021-2022 Bartek Kryza <bkryza@gmail.com>
|
||||
##
|
||||
## Licensed under the Apache License, Version 2.0 (the "License");
|
||||
## you may not use this file except in compliance with the License.
|
||||
## You may obtain a copy of the License at
|
||||
##
|
||||
## http://www.apache.org/licenses/LICENSE-2.0
|
||||
##
|
||||
## Unless required by applicable law or agreed to in writing, software
|
||||
## distributed under the License is distributed on an "AS IS" BASIS,
|
||||
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
## See the License for the specific language governing permissions and
|
||||
## limitations under the License.
|
||||
##
|
||||
|
||||
import sys
|
||||
from lxml import etree
|
||||
|
||||
def main(argv):
|
||||
if len(argv) < 1:
|
||||
print('Usage: \n')
|
||||
print('\t\t ./format_svg.py input_file1.svg input_file2.svg ...\n')
|
||||
sys.exit(1)
|
||||
|
||||
for inputfile in argv:
|
||||
# Read svg file contents
|
||||
with open(inputfile, 'r') as f:
|
||||
xml = f.read()
|
||||
|
||||
# Parse SVG XML
|
||||
tree = etree.fromstring(bytes(xml, encoding='utf8'))
|
||||
|
||||
# Remove comments from SVG, to minimize diff
|
||||
# when updating diagrams in Git
|
||||
comments = tree.xpath('//comment()')
|
||||
|
||||
for c in comments:
|
||||
p = c.getparent()
|
||||
p.remove(c)
|
||||
|
||||
# Overwrite the input svg properly formatted
|
||||
etree.ElementTree(tree).write(inputfile, encoding='utf-8', pretty_print=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
Reference in New Issue
Block a user