Skip to content

Instantly share code, notes, and snippets.

@lujjjh
Last active April 8, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lujjjh/1f10ed514191cd4d13e0057ed23ad6ed to your computer and use it in GitHub Desktop.
Save lujjjh/1f10ed514191cd4d13e0057ed23ad6ed to your computer and use it in GitHub Desktop.
An HTML DSL in CoffeeScript
scope = []
enter = ->
scope.push([])
scope[scope.length - 1]
leave = ->
scope.pop()
scope[scope.length - 1]
T = (tag) -> (attributes, init) ->
children = enter()
init?()
parent = leave()
element = { tag, attributes, children }
parent?.push element
element
html = T 'html'
body = T 'body'
p = T 'p'
root =
html { lang: 'en' }, ->
body {}, ->
p {}
p {}
document.querySelector('#result').textContent = JSON.stringify root, null, 2
@lujjjh
Copy link
Author

lujjjh commented Apr 8, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment