-
-
Save fernandogrd/3360781 to your computer and use it in GitHub Desktop.
Extract SVG paths and convert to JSON for use with Raphael.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
from xml.dom import minidom | |
config = { | |
'js_var' : 'map', | |
} | |
def parse(svg_file): | |
svg = minidom.parse(svg_file) | |
paths = svg.getElementsByTagName('path') | |
items = {} | |
for node in paths: | |
if node.getAttributeNode('id'): | |
path_id = str(node.getAttributeNode('id').nodeValue) | |
path = str(node.getAttributeNode('d').nodeValue) | |
items[path_id] = path | |
return 'var {0} = {1}'.format( | |
config['js_var'], json.dumps(items, indent=2), | |
) | |
def main(): | |
if len(sys.argv) < 2: | |
print (u'Usage: python svg-to-json.py some_svg.svg') | |
return | |
sys.stdout.write(parse(sys.argv[1])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment