razor-tmpl
Version:
razor style template engine for JavaScript
1 lines • 10.8 kB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.razor=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var Tokens=require("./parser").Tokens;var format=require("./util").format;var escapeInNewFunction=require("./util").escapeInNewFunction;exports.toCodes=function(tokens){var codes=['var $result = "";'];tokens.forEach(function(token){var data=token.val;switch(token.type){case Tokens.TK_CODE_BLOCK:codes.push(data);break;case Tokens.TK_VAR:var inner=format("try{$result+={0};}catch(e){ $result+= 'undefined';}",data);codes.push(inner);break;case Tokens.TK_STRING:var inner=format("$result+='{0}';",escapeInNewFunction(data));codes.push(inner);break;default:break}});codes.push("return $result");return codes};exports.compile=function(tokens,localsName){localsName=localsName||"locals";var codes=exports.toCodes(tokens);var code=codes.join("\n");return new Function(localsName,code)}},{"./parser":5,"./util":6}],2:[function(require,module,exports){var Parser=require("./parser");var Compiler=require("./compiler");var $=require("./util");exports=module.exports=Engine;function Engine(options){options=options||{};this.symbol=options.symbol||"@";this.localsName=options.localsName||"locals";this.easyLocals=options.easyLocals||true}Engine.prototype.set=function(name,val){this[name]=val;return this};Engine.prototype.reset=function(){return this.set("symbol","@").set("localsName","locals").set("easyLocals",true)};Engine.prototype.addVarDef=function(tmpl,locals){if(this.easyLocals&&locals){var varDefs="@{ ";var keys=Object.keys(locals);keys.forEach(function(key){varDefs+=$.format('var {0} = locals["{0}"];',key)});varDefs+="}";tmpl=varDefs+tmpl}return tmpl};Engine.prototype.compile=function(tmpl,locals){tmpl=this.addVarDef(tmpl,locals);var tokens=Parser.parse(this.symbol,tmpl);var fn=Compiler.compile(tokens,this.localsName);return fn};Engine.prototype.render=function(tmpl,locals){return this.compile(tmpl,locals)(locals)}},{"./compiler":1,"./parser":5,"./util":6}],3:[function(require,module,exports){var requires=[Array.prototype.forEach,Array.prototype.sort,String.prototype.trim,Object.keys];for(var idx=0;idx<requires.length;idx++){var req=requires[idx];if(!req){console.error("ES5 requireed. See es5-shim");throw new Error("requirements@"+idx+" not satisified.")}}var Engine=require("./engine");var Parser=require("./parser");var Compiler=require("./compiler");var util=require("./util");var razor=exports=module.exports=new Engine;exports.Engine=Engine;exports.Parser=Parser;exports.Compiler=Compiler;exports.util=util;exports.version=require("../package.json").version;require("./jqueryExt")},{"../package.json":7,"./compiler":1,"./engine":2,"./jqueryExt":4,"./parser":5,"./util":6}],4:[function(require,module,exports){var $;if(typeof jQuery!=="undefined"&&jQuery){$=jQuery}if(!$)return;var razor=require("./index");var format=require("./util").format;var unescape=require("./util").unescape;$(function(){$("[razor-template]").hide()});function getLoopHeader($el){var attr=$el.attr("razor-for")||$el.attr("data-razor-for");if(attr){return format("for({0}){",attr.trim())}attr=$el.attr("razor-if")||$el.attr("data-razor-if");if(attr){return format("if({0}){",attr.trim())}attr=$el.attr("razor-while")||$el.attr("data-razor-while");if(attr){return format("while({0}){",attr.trim())}attr=$el.attr("razor-each")||$el.attr("data-razor-each");if(attr){return format("each({0}){",attr.trim())}return""}function getTemplate($el){var el=$el.get(0);if(!el)return"";var ret;if(el.tagName==="SCRIPT"){return ret=$el.html().trim()}else{ret=$el.attr("razor-template");if(ret)return ret;var ret=$el.html().trim();var header=getLoopHeader($el);if(header){ret=razor.symbol+header+ret+"}"}ret=unescape(ret);$el.attr("razor-template",ret);return ret}}$.prototype.compile=function(){return razor.compile(getTemplate(this))};$.prototype.render=function(locals){var el=this.get(0);var tmpl=getTemplate(this);var result=razor.render(tmpl,locals);if(el.tagName!=="SCRIPT"){this.html(result);this.show()}return result}},{"./index":3,"./util":6}],5:[function(require,module,exports){var $=require("./util");exports=module.exports=Parser;function Parser(symbol,input){this.symbol=symbol;this.input=String(input);this.consumed=-1;this.tokens=[]}Parser.parse=parse;function parse(symbol,input){var parser=new Parser(symbol,input);return parser.parse()}Parser.Tokens={TK_VAR:0,TK_CODE_BLOCK:1,TK_STRING:2};Parser.prototype.tok=function(type,val){this.tokens.push({type:type,val:val})};Parser.prototype.parse=function(){for(var index=0;index<this.input.length;index++){var cur=this.input[index];var next="";if(cur==this.symbol){this.handleString(index);next=this.input[index+1];if(next==this.symbol){index=this.handleEscapeSymbol(index);continue}else if(next=="*"){index=this.handleComment(index);continue}else{var tokenIndex=index+1;while(next==" "){tokenIndex++;next=this.input[tokenIndex]}switch(next){case"{":index=this.handleCodeBlock(index,tokenIndex);continue;case"(":index=this.handleExplicitVariable(index,tokenIndex);continue;default:var remain=this.input.substring(tokenIndex);if(/^each\s*\([\s\S]*\)\s*\{/.test(remain)){index=this.handleEach(index,tokenIndex);continue}else if(/^if\s*\([\s\S]*\)\s*\{/.test(remain)){index=this.handleIfElse(index,tokenIndex);continue}else if(/^(for|while)\s*\([\s\S]*\)\s*\{/.test(remain)){index=this.handleControlFlow(index,tokenIndex);continue}break}var match=/^([\w\._\[\]])+/.exec(this.input.substring(index+1));if(match&&match[0]){index=this.handleImplicitVariable(index,match[0]);continue}}}}this.handleString(this.input.length);return this.tokens};Parser.prototype.handleString=function(i){var content=this.input.substring(this.consumed+1,i);if(content){this.tok(Parser.Tokens.TK_STRING,content)}this.consumed=i-1};Parser.prototype.handleComment=function(i){var remain=this.input.substr(i);var star_index=remain.indexOf("*"+this.symbol);if(star_index>-1){var commentEnd=star_index+1+i;return this.consumed=commentEnd}else{return i}};Parser.prototype.handleEscapeSymbol=function(i){this.tok(Parser.Tokens.TK_STRING,this.symbol);return this.consumed=i+1};Parser.prototype.handleCodeBlock=function(i,fi){var sec=$.getRight(this.input,fi);var content=this.input.substring(fi+1,sec);content=content.trim();if(content){this.tok(Parser.Tokens.TK_CODE_BLOCK,content)}return this.consumed=sec};Parser.prototype.handleExplicitVariable=function(i,fi){var sec=$.getRight(this.input,fi);var content=this.input.substring(fi+1,sec);if(content){content=$.unescape(content);if(content[0]==="-"){content=content.substring(1).trim();content+=".replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>')"}this.tok(Parser.Tokens.TK_VAR,content)}return this.consumed=sec};Parser.prototype.handleImplicitVariable=function(i,variable){this.tok(Parser.Tokens.TK_VAR,variable);return this.consumed=i+variable.length};Parser.prototype.handleEach=function(i,fi){var remain=this.input.substring(i);var fi_small=remain.indexOf("(")+i;var sec_small=$.getRight(this.input,fi_small);remain=this.input.substring(sec_small);var fi_big=remain.indexOf("{")+sec_small;var sec_big=$.getRight(this.input,fi_big);var loop=this.input.substring(fi_small+1,sec_small);var inIndex=loop.indexOf("in");var item=loop.substring(0,inIndex).trim();var items=loop.substring(inIndex+2).trim();var loop_head=$.format("for(var $index = 0,$length = {1}.length;$index<$length;$index++){var {0}={1}[$index];",item,items);this.tok(Parser.Tokens.TK_CODE_BLOCK,loop_head);var loop_body=this.input.substring(fi_big+1,sec_big).trim()+"\n";var inner_tokens=parse(this.symbol,loop_body);this.tokens=this.tokens.concat(inner_tokens);this.tok(Parser.Tokens.TK_CODE_BLOCK,"}");return this.consumed=sec_big};Parser.prototype.handleIfElse=function(i,fi){var lastRightIndex,remain;do{lastRightIndex=this.handleControlFlow(i,fi);remain=this.input.substring(lastRightIndex+1);fi=remain.indexOf("else")+lastRightIndex+1}while(/^\s*else/.test(remain));return this.consumed=lastRightIndex};Parser.prototype.handleControlFlow=function(_,fi){var remain=this.input.substring(fi);var fi_big=remain.indexOf("{")+fi;var sec_big=$.getRight(this.input,fi_big);var part1=this.input.substring(fi,fi_big+1);var part2=this.input.substring(fi_big+1,sec_big);var part3="}";this.tok(Parser.Tokens.TK_CODE_BLOCK,part1);part2=part2.trim()+"\n";var inner_tokens=parse(this.symbol,part2);this.tokens=this.tokens.concat(inner_tokens);this.tok(Parser.Tokens.TK_CODE_BLOCK,part3);return this.consumed=sec_big}},{"./util":6}],6:[function(require,module,exports){exports.format=function(s){var paras=[].slice.call(arguments,1);paras.forEach(function(para,index){s=s.replace(new RegExp("\\{"+index+"\\}","gi"),para.toString())});return s};exports.getRight=function(input,fi){var pair={"{":"}","(":")","[":"]"};var left=input[fi];var right=pair[left];var count=1;for(var i=fi+1;i<input.length;i++){var cur=input[i];if(cur==right){count--;if(count==0){return i}}else if(cur==left){count++}}return-1};exports.unescape=function(encoded){return encoded.replace(/</gi,"<").replace(/>/gi,">").replace(/&/gi,"&")};exports.escapeInNewFunction=function(s){if(!s)return s;return s.replace(/'/g,"\\'").replace(/"/g,'\\"').replace(/(\r?\n)/g,"\\n")};exports.getDate=function(d){d=d||new Date;var year=d.getFullYear();var mon=d.getMonth()+1+"";mon.length===1&&(mon="0"+mon);var day=d.getDate()+"";day.length===1&&(day="0"+day);return exports.format("{0}-{1}-{2}",year,mon,day)}},{}],7:[function(require,module,exports){module.exports={name:"razor-tmpl",version:"1.3.0",description:"razor style template engine for JavaScript",main:"./lib/node/index.js",browser:"./lib/index.js",bin:{razor:"./lib/node/bin/razor"},scripts:{test:"./node_modules/.bin/mocha --recursive",browser:"browserify lib/index.js --standalone razor > browser/razor-tmpl.js",min:"uglifyjs browser/razor-tmpl.js > browser/razor-tmpl.min.js"},repository:{type:"git",url:"https://github.com/magicdawn/razor-tmpl"},keywords:["razor","template"],author:"magicdawn",license:"ISC",devDependencies:{browserify:"^9.0.8",mocha:"^2.1.0",uglifyjs:"^2.4.10"}}},{}]},{},[3])(3)});