Skip to content

Instantly share code, notes, and snippets.

@jbr
Created November 28, 2010 10:17
Show Gist options
  • Save jbr/718790 to your computer and use it in GitHub Desktop.
Save jbr/718790 to your computer and use it in GitHub Desktop.
Just a little builder.
; this depends on node-proxy. npm install node-proxy.
; https://github.com/brickysam26/node-proxy
(defvar builder (hash))
(defun builder.add (type &rest remainder)
(defvar tagname type
attributes ""
contents "")
(each (item) remainder
(if (function? item) (incr-by contents (item))
(if (string? item) (incr-by contents item)
(each-key key item (incr-by attributes (concat " " key "=\"" (get item key) "\""))))))
(concat "\n<"tagname attributes">" (contents.replace /\n/g "\n ") "\n</"tagname">"))
(defvar proxy (require "node-proxy")
proxy-hash (hash
get (lambda (object method)
(lambda (&rest args)
(args.unshift method)
(apply builder.add args)))
enumerate (thunk (list))
delete (thunk false)
fix (thunk))
build (proxy.create proxy-hash))
(defvar html
(build.html (build.head
(build.title "this is my title")
(build.script (hash src "/javascripts/jquery.js"
type "text/javascript"))
(build.link (hash rel 'stylesheet
href "/stylesheets/screen.css")))
(build.body
(build.h1 "builders and doozers!")
(build.p
(build.a (hash href "http://google.com/")
(build.img (hash src "http://www.google.com/images/logos/logo.gif"))
"\nvisit google")))))
(console.log html)
<html>
<head>
<title>this is my title
</title>
<script src="/javascripts/jquery.js" type="text/javascript">
</script>
<link rel="stylesheet" href="/stylesheets/screen.css">
</link>
</head>
<body>
<h1>builders and doozers!
</h1>
<p>
<a href="http://google.com/">
<img src="http://www.google.com/images/logos/logo.gif">
</img>
visit google
</a>
</p>
</body>
</html>
var builder = { };
builder.add = (function(type, remainder) {
// type:required remainder:rest
var remainder = Array.prototype.slice.call(arguments, 1);
var tagname = type,
attributes = "",
contents = "";;
remainder.forEach((function(item) {
// item:required
return (function() {
if (typeof(item) === 'function') {
return contents += item();
} else {
return (function() {
if (typeof(item) === "string") {
return contents += item;
} else {
return (function() {
for (var key in item) (function() {
return attributes += (" " + key + "=\"" + (item)[key] + "\"");
})();
})();;
};
})();
};
})();
}));
return ("\n<" + tagname + attributes + ">" + contents.replace(/\n/g, "\n ") + "\n</" + tagname + ">");
});
var proxy = require("node-proxy"),
proxyHash = {
get: (function(object, method) {
// object:required method:required
return (function(args) {
// args:rest
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(method);
return builder.add.apply(undefined, args);
});
}),
enumerate: (function() {
return [ ];
}),
delete: (function() {
return false;
}),
fix: (function() {
})
},
build = proxy.create(proxyHash);
// I reformatted below here, sibilant didn't make it pretty
var html = build.html(
build.head(
build.title("this is my title"),
build.script({
src: "/javascripts/jquery.js",
type: "text/javascript"
}),
build.link({
rel: "stylesheet",
href: "/stylesheets/screen.css"
})
),
build.body(
build.h1("builders and doozers!"),
build.p(
build.a({ href: "http://google.com/" },
build.img({ src: "http://www.google.com/images/logos/logo.gif" }),
"\nvisit google"
)
)
)
);
console.log(html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment