Skip to content

Instantly share code, notes, and snippets.

@lujjjh
Last active Apr 8, 2021
Embed
What would you like to do?
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