-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperltemplates.min.js
11 lines (9 loc) · 5.25 KB
/
perltemplates.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
* PerlTemplates, a JavaScript templating engine which follows HTML::Template syntax
* @author: David McLaughlin <http://www.dmclaughlin.com>
*/
PerlTemplates=function(options){if(options.template){this.template=this.clean_template(options.template);}else if(options.url){this.template=this.clean_template(PerlTemplates.doRequest(options.url));}if(!this.template){throw new Error('No template supplied.');}this.target=options.target;if(options.data){this.data=options.data;}this.parse();};PerlTemplates.prototype.clean_template=function(template){template=template.replace(/\r\n/g,"\n");template=template.replace(/\r/g,"\n");return template;};PerlTemplates.clean=function(content){content=content.replace(/\\/g,'\\\\');content=content.replace(/\n/g,'\\n');content=content.replace(/\"/g,'\\"');return content;};PerlTemplates.prototype.parse=function(){var lexer=new PerlTemplates.Lexer(this.template,this.data);var raw_process_function=' this.process = function() { var data = this.data; '+lexer.create_output()+' }; ';eval(raw_process_function);};PerlTemplates.prototype.get_content=function(data){if(data){this.data=data;}return this.process();};PerlTemplates.prototype.render=function(data){if(data){this.data=data;}if(this.target){document.getElementById(this.target).innerHTML=this.process();}};PerlTemplates.Request=function(){var factories=[function(){return new ActiveXObject("Msxml2.XMLHTTP");},function(){return new XMLHttpRequest();},function(){return new ActiveXObject("Microsoft.XMLHTTP");}];for(var i=0;i<factories.length;i++){try{var request=factories[i]();if(request!=null)return request;}catch(e){continue;}}};PerlTemplates.doRequest=function(template){var request=new PerlTemplates.Request();request.open("GET",template,false);try{request.send(null);}catch(e){return null;}if(request.status==404||request.status==2||(request.status==0&&request.responseText==''))return null;return request.responseText;};PerlTemplates.Lexer=function(template,data){this.template=template;this.data=data;this.tokenreg=new RegExp("<tmpl_([a-z]+)[\\s]+(?:name=)?[\"]?([a-zA-Z0-9_\\-\\.]+)[\"]?[\\s]*(?:escape=[\"]?(url|html)[\"]?)?[\\s]*>|<(\/)tmpl_([a-zA-Z]+)>|<tmpl_(else)>|<tmpl_(unless)>","im");this.loop_depth=0;this.scope=["this","data"];};PerlTemplates.Lexer.prototype.create_output=function(){this.output_func=' var __templateOUT = ""; ';this.analyse();this.output_func+=' return __templateOUT; ';return this.output_func;};PerlTemplates.Lexer.prototype.analyse=function(){var lines=this.tokenize(this.template,/\n/);for(var i=0;i<lines.length;i++){var tokens=this.tokenize(lines[i],this.tokenreg);for(var j=0;j<tokens.length;j++){this.parse_token(tokens[j]);}}};PerlTemplates.Lexer.prototype.tokenize=function(item,regex){var result=regex.exec(item);var tag_type=1;var tag_name=2;var escaped=3;var closing_tag=4;var closing_tag_type=5;var else_tag=6;var unless_tag=7;var tokens=new Array();while(result!=null){var start=result.index;if((start)!=0){tokens.push(item.substring(0,start));item=item.slice(start);}if(result[tag_type]&&result[tag_name]){var escape=result[escaped]?result[escaped]:false;tokens.push({type:result[tag_type],value:result[tag_name],escape:escape});}else if(result[closing_tag]&&result[closing_tag_type]){tokens.push({close:result[closing_tag_type]});}else if(result[else_tag]){tokens.push({type:'else'});}else if(result[unless_tag]){tokens.push({type:'unless'});}else
{tokens.push(result[0]);}item=item.slice(result[0].length);result=regex.exec(item);}if(!item==''){tokens.push(item);}return tokens;};PerlTemplates.Lexer.prototype.parse_token=function(token){if(typeof token=='string'){this.output_func+=' __templateOUT += "'+PerlTemplates.clean(token)+'";';}else
{if(token.type){token.type=token.type.toLowerCase();}if(token.escape){token.escape=token.escape.toLowerCase();}if(token.close){token.close=token.close.toLowerCase();}if(token.type=='var'){if(token.escape){if(token.escape=='html'){this.output_func+=' __templateOUT += escape('+this.get_scope()+token.value+');';}else
{this.output_func+=' __templateOUT += encodeURI('+this.get_scope()+token.value+');';}}else
{this.output_func+=' __templateOUT += '+this.get_scope()+token.value+';';}}else if(token.type=='include'){var template_url=token.value;this.output_func+=' __templateOUT += "'+new PerlTemplates({url:token.value,data:this.data}).get_content()+'";';}else if(token.type=='if'){var v=this.get_scope()+token.value;this.output_func+=' if( ('+v+' instanceof Array && '+v+'.length > 0) || (!('+v+' instanceof Array) && '+v+')) { ';}else if(token.type=='unless'){this.output_func+=' if(!'+this.get_scope()+token.value+') { ';}else if(token.type=='else'){this.output_func+=' } else { ';}else if(token.type=='loop'){this.loop_depth++;this.output_func+=this.create_loop_str(token);this.scope.push(token.value);}else if(token.close){if(token.close=='loop'){this.loop_depth--;this.scope.pop();}this.output_func+=' } ';}}};PerlTemplates.Lexer.prototype.get_scope=function(){var final_scope=this.scope.slice(0,2).join('.')+'.';if(this.scope.length>2){var loops=this.scope.slice(2,this.scope.length);for(var i=0;i<loops.length;i++){final_scope+=loops[i]+'[i'+(i+1)+'].';}}return final_scope;};PerlTemplates.Lexer.prototype.create_loop_str=function(token){return' for(i'+this.loop_depth+' = 0; i'+this.loop_depth+' < '+this.get_scope()+token.value+'.length; i'+this.loop_depth+'++) { ';};