Skip to content

Instantly share code, notes, and snippets.

@EddieRingle
Created March 24, 2012 23:49
Show Gist options
  • Save EddieRingle/2189280 to your computer and use it in GitHub Desktop.
Save EddieRingle/2189280 to your computer and use it in GitHub Desktop.
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