Created
March 24, 2012 23:49
-
-
Save EddieRingle/2189280 to your computer and use it in GitHub Desktop.
Simple JSON to HTML thingy
This file contains 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
shapeOptions = [ | |
{ | |
node: "select" | |
children: [ | |
{ | |
node: "option" | |
attrs: { | |
value: "rectangle" | |
} | |
content: "Rectangle" | |
} | |
{ | |
node: "option" | |
attrs: { | |
value: "oval" | |
} | |
content: "Oval" | |
} | |
{ | |
node: "option" | |
attrs: { | |
value: "line" | |
} | |
content: "Line" | |
} | |
{ | |
node: "option" | |
attrs: { | |
value: "ring" | |
} | |
content: "Ring" | |
} | |
] | |
} | |
] | |
buildElement = (opts, parent = null) -> | |
html = if parent? then parent else "" | |
for e in opts | |
unless e.node is "text" | |
html += "<#{e.node}" | |
for name,value of e.attrs | |
html += " #{name}=\"#{value}\"" | |
html += ">\n" | |
if e.children? and e.children.length > 0 | |
html = buildElement e.children, html | |
if e.content? | |
html += e.content + "\n" | |
html += "</#{e.node}>\n" | |
else | |
html += e.content + "\n" | |
return html | |
### | |
Output: | |
<select> | |
<option value="rectangle"> | |
Rectangle | |
</option> | |
<option value="oval"> | |
Oval | |
</option> | |
<option value="line"> | |
Line | |
</option> | |
<option value="ring"> | |
Ring | |
</option> | |
</select> | |
### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment