Skip to content

Instantly share code, notes, and snippets.

@kosamari
Created October 11, 2016 04:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kosamari/82209aadb7f5b974a9637c438b19bdad to your computer and use it in GitHub Desktop.
generator function for sbn compiler
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