Last active
April 8, 2021 08:31
-
-
Save lujjjh/1f10ed514191cd4d13e0057ed23ad6ed to your computer and use it in GitHub Desktop.
An HTML DSL in CoffeeScript
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JS Bin: https://jsbin.com/zarenil/edit?js,output