Created
October 11, 2016 04:52
-
-
Save kosamari/82209aadb7f5b974a9637c438b19bdad to your computer and use it in GitHub Desktop.
generator function for sbn compiler
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
function generator (svg_ast) { | |
// create attributes string out of attr object | |
// { "width": 100, "height": 100 } becomes 'width="100" height="100"' | |
function createAttrString (attr) { | |
return Object.keys(attr).map(function (key){ | |
return key + '="' + attr[key] + '"' | |
}).join(' ') | |
} | |
// top node is always <svg>. Create attributes string for svg tag | |
var svg_attr = createAttrString(svg_ast.attr) | |
// for each elements in the body of svg_ast, generate svg tag | |
var elements = svg_ast.body.map(function (node) { | |
return '<' + node.tag + ' ' + createAttrString(node.attr) + '></' + node.tag + '>' | |
}).join('\n\t') | |
// wrap with open and close svg tag to complete SVG code | |
return '<svg '+ svg_attr +'>\n' + elements + '\n</svg>' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment