Skip to content

Instantly share code, notes, and snippets.

@thomasfoster96
Created May 16, 2015 06:20
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 thomasfoster96/deeb7a297051ca674257 to your computer and use it in GitHub Desktop.
Save thomasfoster96/deeb7a297051ca674257 to your computer and use it in GitHub Desktop.
HTMLScript
(function(){
function exec(script){
return eval(transpile(new DOMParser().parseFromString(, "text/html").documentElement));
}
function run(){
return Array.from(document.querySelectorAll('')).forEach(function(script){
exec(script);
});
}
function transpile(element){
var inner = Array.from(element.children).map(function(element){
return transpile(element);
});
switch(element.nodeName){
case "HTML":
return children;
case "HEAD":
return "";
case "BODY":
return "(function(){" + inner.join('') + "})()";
case "SPAN":
return "(" + inner.join('') + ")";
case "DIV":
return "{" + inner.join('') + "}";
case "P":
return "'" + inner.join('') + "'";
case "PRE":
return "`" + inner.join('') + "`";
}
}
return {
exec : exec,
run : run,
transpile : transpile
}
})(window, document)

HTML5cript Spec

HTML5cript (yes, that's a 5, not a S) transpiles HTML (the latest WHATWG spec) to JavaScript (ES5 for now).

API Reference

exec(script)

Executes (so, evals) script, where script is a string of HTML.

run()

Finds all <script> elements with the "text/html" type and executes them.

transpile()

Transpiles an individual element from HTML to JavaScript.

Element to JavaScript translation reference

<html></

${contents}

<head>{{contents}}</body>

``

<body>{{contents}}</body>
(function(){
  ${contents}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment