Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple JSON to HTML thingy
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