You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.1 KiB
Python

#! /bin/python
import os,re
# Delete the spritesheet.svg file if it exists
if os.path.exists("spritesheet.svg"):
os.remove("spritesheet.svg")
# Open the output file and write the opening <svg> tag
with open("spritesheet.svg", "w") as f:
f.write('<svg style="display:none;">\n')
# List all the SVG files in the current directory
for filename in os.listdir():
if filename.endswith(".svg") and filename != 'spritesheet.svg':
# Read the SVG file
with open(filename, "r") as f:
svg = f.read()
# Extract the viewbox attribute from the SVG file
viewbox = re.search(r'viewBox="(.*?)"', svg).group(1)
# Extract the content within the <svg> tag
start = svg.find('<svg')+4
end = svg.find('</svg>', start) + len('</svg>')
svg = svg[start:end]
# Remove stroke and fill attributes from the children
svg = re.sub(r'height="(.*?)"', '', svg,1)
svg = re.sub(r'width="(.*?)"', '', svg,1)
svg = svg.replace("fill:#000000;","")
svg = svg.replace("stroke:#000000;","")
svg = svg.replace("fill:none","fill:none !important;")
svg = re.sub(r'showgrid="(.*?)"','',svg)
svg = re.sub(r'id="(.*?)"','',svg)
svg = re.sub(r'inkscape:.*?=".*?"', '', svg)
svg = re.sub(r'<sodipodi:namedview.*?\/\>','',svg,flags=re.S)
svg = re.sub(r'<defs.*?\/\>','',svg,flags=re.S)
svg = re.sub(r'sodipodi:.*?=".*?"', '', svg)
svg = re.sub(r'xmlns.*?=".*?"', '', svg)
svg = re.sub(r'version="(.*?)"', '', svg)
# Replace the closing </svg> tag with the closing </symbol> tag
svg = svg.replace('</svg>', '</symbol>', 1)
# Insert the id and viewbox attributes in the opening <svg> tag
id = os.path.splitext(filename)[0]
svg = f'<symbol id="{id}" {svg}'
# Write the modified SVG to the output file
with open("spritesheet.svg", "a") as f:
f.write(svg.replace("\n\n",''))
# Write the closing </svg> tag to the output file
with open("spritesheet.svg", "a") as f:
f.write('</svg>\n')