Return to Snippet

Revision: 40723
at February 6, 2011 04:23 by pablo_PXL


Updated Code
/*
	All glory to the ml()
	@subp Page-like structured array
*/
function ml(subp,ctx){
	var out = '', ctx = ctx||{}, force = ['script'],
		clean = function(t){return t.replace(/"/g,'"')},
		fill = function(t){return t.replace(/{{[^}]*}}/g,
			function(hld){with(ctx){return (eval(hld))}})},
		render_tags = arguments.callee,
		render_props = function(props){
			var out = '';
			if (props) for(var p in props){
				if(props.hasOwnProperty(p)) 
				out += ' '+p+'="'+clean(props[p])+'"';  
			} return out;
		};
		
	for(var i=0,l=subp.length;i<l;i++){
		var len = subp[i].length;
		var name = subp[i][0],
			props = len>1&&!subp[i][1].pop?subp[i][1]:null,
			tags = subp[i][len-1].pop?subp[i][len-1]:null;
		var	rtags = (!tags?null:tags.length==1?tags:render_tags(tags,ctx)),
			rprops = props?render_props(props):'';
			
		out += rtags||(force.indexOf(name)>-1)?
				'<'+name+(rprops?rprops:'')+'>'+(rtags||'')+'</'+name+'>':
				'<'+name+(rprops?rprops:'')+'/>';
	} return fill(out);
}

// Some testing
var ctx = { a:1,b:2 };
var page = [
	['html',[
		['head',[['script',{ type: 'text/javascript', src: 'http://www.pablovidal.org/js/jquery-underscore'}],
				 ['script',{type: 'text/javascript'},['console.log($);']]]
		],
		['body',[['h1',['ml 0.1']],
				['span',{style:'font-style:italic'},['round {{ a }}... maybe {{ b }}... fight']],['br']]
		]]
	]
];

document.write(ml(page,ctx));

Revision: 40722
at February 6, 2011 03:55 by pablo_PXL


Updated Code
/*
	All glory to the ml()
	@subp Page-like structured array
*/
function ml(subp){
	var out = '', force = ['script'],
		clean = function(t){ return t.replace(/"/g,'&quot;'); },
		render_tags = arguments.callee,
		render_props = function(props){
			var out = '';
			if (props) for(var p in props){
				if(props.hasOwnProperty(p)) 
				out += ' '+p+'="'+clean(props[p])+'"';  
			} return out;
		};
		
	for(var i=0,l=subp.length;i<l;i++){
		var len = subp[i].length;
		var name = subp[i][0],
			props = len>1&&!subp[i][1].pop?subp[i][1]:null,
			tags = subp[i][len-1].pop?subp[i][len-1]:null;
		var	rtags = (!tags?null:tags.length==1?tags:render_tags(tags)),
			rprops = props?render_props(props):'';
			
		out += rtags||(force.indexOf(name)>-1)?
				'<'+name+(rprops?rprops:'')+'>'+(rtags||'')+'</'+name+'>':
				'<'+name+(rprops?rprops:'')+'/>';
	} return out;
}

var page = [
	['html',[
		['head',[['script',{ type: 'text/javascript', src: 'http://www.pablovidal.org/js/jquery-underscore'}],
				 ['script',{type: 'text/javascript'},['console.log($);']]]
		],
		['body',[['h1',['ml 0.1']],
				['span',{style:'font-style:italic'},['round 1... fight']],['br']]
		]]
	]
];

document.write(ml(page));

Revision: 40721
at February 6, 2011 03:39 by pablo_PXL


Updated Code
/*
	All glory to the ml()
	@subp Page-like structured array
*/
function ml(subp){
	var out = '', force = ['script'],
		clean = function(t){ return t.replace(/"/g,'&quot;'); }
		render_tags = arguments.callee,
		render_props = function(props){
			var out = '';
			if (props) for(var p in props){
				if(props.hasOwnProperty(p)) 
				out += ' '+p+'="'+clean(props[p])+'"';  
			} return out;
		};
		
	for(var i=0,l=subp.length;i<l;i++){
		var len = subp[i].length;
		var name = subp[i][0],
			props = len>1&&!subp[i][1].pop?subp[i][1]:null,
			tags = subp[i][len-1].pop?subp[i][len-1]:null;
		var	rtags = (!tags?null:tags.length==1?tags:render_tags(tags)),
			rprops = props?render_props(props):'';
			
		out += rtags||(force.indexOf(name)>-1)?
				'<'+name+(rprops?rprops:'')+'>'+(rtags||'')+'</'+name+'>':
				'<'+name+(rprops?rprops:'')+'/>';
	} return out;
}

var page = [
	['html',[
		['head',[['script',{ type: 'text/javascript', src: 'http://www.pablovidal.org/js/jquery-underscore'}],
				 ['script',{type: 'text/javascript'},['console.log($);']]]
		],
		['body',[['h1',['ml 0.1']],
				['span',{style:'font-style:italic'},['round 1... fight']],['br']]
		]]
	]
];

document.write(ml(page));

Revision: 40720
at February 6, 2011 03:36 by pablo_PXL


Initial Code
/*
	All glory to the ml()
	@subp Page-like structured array
*/
function ml(subp){
	var out = '', force = ['script'],
		clean = function(t){ return t.replace(/"/g,'&quot;'); }
		render_tags = arguments.callee,
		render_props = function(props){
			var out = '';
			if (props) for(var p in props){
				if(props.hasOwnProperty(p)) 
				out += ' '+p+'="'+clean(props[p])+'"';  
			} return out;
		};
		
	for(var i=0,l=subp.length;i<l;i++){
		var len = subp[i].length;
		var name = subp[i][0],
			props = len>1&&!subp[i][1].pop?subp[i][1]:null,
			tags = subp[i][len-1].pop?subp[i][len-1]:null;
		var	rtags = (!tags?null:tags.length==1?tags:render_tags(tags)),
			rprops = props?render_props(props):'';
			
		out += rtags||(force.indexOf(name)>-1)?
				'<'+name+(rprops?rprops:'')+'>'+(rtags||'')+'</'+name+'>':
				'<'+name+(rprops?rprops:'')+'/>';
	} return out;
}

Initial URL
http://www.twitter.com/pablo_PXL

Initial Description
All glory to the ml function.

Initial Title
Page-like structured array to HTML

Initial Tags
template, html, array

Initial Language
JavaScript