Skip to content

Instantly share code, notes, and snippets.

@brapse
Created January 24, 2010 04:11
Show Gist options
  • Save brapse/285001 to your computer and use it in GitHub Desktop.
Save brapse/285001 to your computer and use it in GitHub Desktop.
var html = function(t_func){
var t = {};
var tag_function = function(tag){
return function(attrs, contents){
var tag_attributes = [];
if(typeof(attrs) == 'object'){
for(var prop in attrs){
if (attrs.hasOwnProperty(prop)){
tag_attributes.push(prop + '="' + attrs[prop] + '"');
}
}
}
return '<' + tag + ' ' + tag_attributes.join(' ') + '>' + content + '</' +tag + '>'
}
};
var body = t_func.toString().replace(/\n/g, "").match(/{(.*)}/)[1];
var supported_tags = ['div', 'p', 'span', 'a', 'ul', 'li', 'table', 'tr', 'td'];
var wrapper = new Function('div', 'p', 'span', 'a', 'ul', 'li', 'table', 'tr', 'td', body);
var tag_functions = supported.tags.map(function(tag){
return tag_function(tag);
});
var stack = wrapper.apply(null, tag_functions);
var render = function(node){
}
return render.call(stack);
};
var example4 = html(function(){
return [ul,
[li, "ichi"],
[li, "ni"],
[li, "san"],
];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment