From f826b6f8bafb93e880ed13b223ae112eaa419083 Mon Sep 17 00:00:00 2001 From: Jony Date: Sun, 28 Sep 2014 16:19:43 +0800 Subject: [PATCH] Optimized code --- index.js | 18 +++++++++--------- tpl.debug.js | 37 ++++++++++++++++++------------------- tpl.js | 4 ++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 58757e5..b77c15c 100644 --- a/index.js +++ b/index.js @@ -3,17 +3,17 @@ var fs = require("fs"); function compiler(html) { html = html || ''; if (/\.(?=tpl|html)$/.test(html)) html = fs.readFileSync(html); - var begin = '<#', - end = '#>', - ecp = function(str){ + + var ecp = function(str){ return str.replace(/('|\\)/g, '\\$1').replace(/\r\n/g, '\\r\\n').replace(/\n/g, '\\n'); }, - str = "var __='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){", - blen = begin.length, - elen = end.length, - b = html.indexOf(begin), - e, + begin = '<#', + end = '#>', + blen = begin.length, elen = end.length, + b = html.indexOf(begin), e, + str = "var __='',___,echo=function(s){__+=s},include=function(t,d){__+=tpl(t,d||_$)};with($||{}){", tmp; + while(b != -1) { e = html.indexOf(end); if(e < b) break; //出错后不再编译 @@ -21,7 +21,7 @@ function compiler(html) { tmp = html.substring(b+blen, e).trim(); if( tmp.indexOf('=') === 0 ) { //模板变量 tmp = tmp.substring(1); - str += "typeof (" + tmp + ")!='undefined'&&(__+=" + tmp + ");"; + str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);"; } else { //js代码 str += tmp + ";"; } diff --git a/tpl.debug.js b/tpl.debug.js index 8967ffa..61d7167 100644 --- a/tpl.debug.js +++ b/tpl.debug.js @@ -1,9 +1,11 @@ -/*! tpl.js 0.3.0, github.com/niceue/tpl.js */ +/*! tpl.js 0.3.1, github.com/niceue/tpl.js */ /* 类似于PHP的嵌入方式, 可以嵌入js语句 模板语法: 嵌入传入的变量: <#=xxx#> , 注意,xxx变量不能为javascript关键字 嵌入任意js语句:<#if(xxx){#> foo <#}else{#> bar <#}#> + 内置 echo() 和 include() 两个方法 + <###>之间的内容会被跳过编译<###> 调用: 方式一(直接输出结果):tpl(template, data) 方式二(预编译,二次调用传入data):tpl(template)(data) @@ -21,29 +23,28 @@ function compiler(html) { html = html || ''; - if (/^#\w+$/.test(html)) html = document.getElementById(html.substring(1)).innerHTML; - var begin = tpl.begin, - end = tpl.end, - v = tpl.variable, - arg1 = v || "$", - trim = function(str) { + if (html.charAt(0)==='#') html = document.getElementById(html.substring(1)).innerHTML; + var trim = function(str) { return str.trim ? str.trim() : str.replace(/^\s*|\s*$/g, ''); }, ecp = function(str){ return str.replace(/('|\\|\r?\n)/g, '\\$1'); }, - str = "var "+ arg1 +"="+ arg1 +"||this,__='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t).call(d||"+ arg1 +")};"+ (v?"":"with($||{}){"), - blen = begin.length, - elen = end.length, - b = html.indexOf(begin), - e, + begin = tpl.begin, + end = tpl.end, + v = tpl.variable, + arg1 = v || "$", + str = "var "+ arg1 +"="+ arg1 +"||this,__='',___,\ + echo=function(s){__+=s},\ + include=function(t,d){__+=tpl(t).call(d||"+ arg1 +")};"+ (v?"":"with($||{}){"), + blen = begin.length, elen = end.length, + b = html.indexOf(begin), e, skip, tmp; while(b != -1) { e = skip ? b + blen : html.indexOf(end); if(e < b) break; //出错后不再编译 - str += "__+='" + ecp(html.substring(0, b)) + "';"; if (skip) { @@ -53,12 +54,11 @@ tmp = trim(html.substring(b+blen, e)); if ('#'===tmp) { skip = 1; - } - else if( tmp.indexOf('=') === 0 ) { //模板变量 + } else if( tmp.indexOf('=') === 0 ) { //模板变量 tmp = tmp.substring(1); - str += "typeof (" + tmp + ")!=='undefined'&&(__+=" + tmp + ");"; + str += "___=" + tmp + ";typeof ___!=='undefined'&&(__+=___);"; } else { //js代码 - str += tmp + ";"; + str += "\n" + tmp + "\n"; } } @@ -66,7 +66,6 @@ b = html.indexOf( begin + (skip ? '#'+end : '') ); } str += "__+='" + ecp(html) + "'"+ (v?";":"}") +"return __"; - console.log(str) return new Function(arg1, str); } @@ -74,4 +73,4 @@ window.tpl = tpl; //AMD and CMD (RequireJS, OzJS, curl, SeaJS ...) typeof define === 'function' && define('tpl',[],function(){return tpl}); -})(window); \ No newline at end of file +})(this); \ No newline at end of file diff --git a/tpl.js b/tpl.js index 10a4bab..92a87b3 100644 --- a/tpl.js +++ b/tpl.js @@ -1,2 +1,2 @@ -/*! tpl.js 0.3.0, github.com/niceue/tpl.js */ -!function(n){function t(n,t){var i=e(n);return t?i(t):i}function e(n){n=n||"",/^#\w+$/.test(n)&&(n=document.getElementById(n.substring(1)).innerHTML);for(var e,i,r,u=t.begin,f=t.end,o=t.variable,s=o||"$",c=function(n){return n.trim?n.trim():n.replace(/^\s*|\s*$/g,"")},d=function(n){return n.replace(/('|\\|\r?\n)/g,"\\$1")},g="var "+s+"="+s+"||this,__='',echo=function(s){__+=s},include=function(t,d){__+=tpl(t).call(d||"+s+")};"+(o?"":"with($||{}){"),l=u.length,_=f.length,b=n.indexOf(u);-1!=b&&(e=i?b+l:n.indexOf(f),!(b>e));)g+="__+='"+d(n.substring(0,b))+"';",i?(n=n.substring(l+_+1),i--):(r=c(n.substring(b+l,e)),"#"===r?i=1:0===r.indexOf("=")?(r=r.substring(1),g+="typeof ("+r+")!=='undefined'&&(__+="+r+");"):g+=r+";"),n=n.substring(e+_),b=n.indexOf(u+(i?"#"+f:""));return g+="__+='"+d(n)+"'"+(o?";":"}")+"return __",console.log(g),new Function(s,g)}t.begin="<#",t.end="#>",n.tpl=t,"function"==typeof define&&define("tpl",[],function(){return t})}(window); \ No newline at end of file +/*! tpl.js 0.3.1, github.com/niceue/tpl.js */ +!function(n){function t(n,t){var i=e(n);return t?i(t):i}function e(n){n=n||"","#"===n.charAt(0)&&(n=document.getElementById(n.substring(1)).innerHTML);for(var e,i,r,_=function(n){return n.trim?n.trim():n.replace(/^\s*|\s*$/g,"")},u=function(n){return n.replace(/('|\\|\r?\n)/g,"\\$1")},f=t.begin,s=t.end,c=t.variable,d=c||"$",o="var "+d+"="+d+"||this,__='',___, echo=function(s){__+=s}, include=function(t,d){__+=tpl(t).call(d||"+d+")};"+(c?"":"with($||{}){"),g=f.length,l=s.length,a=n.indexOf(f);-1!=a&&(e=i?a+g:n.indexOf(s),!(a>e));)o+="__+='"+u(n.substring(0,a))+"';",i?(n=n.substring(g+l+1),i--):(r=_(n.substring(a+g,e)),"#"===r?i=1:0===r.indexOf("=")?(r=r.substring(1),o+="___="+r+";typeof ___!=='undefined'&&(__+=___);"):o+="\n"+r+"\n"),n=n.substring(e+l),a=n.indexOf(f+(i?"#"+s:""));return o+="__+='"+u(n)+"'"+(c?";":"}")+"return __",new Function(d,o)}t.begin="<#",t.end="#>",n.tpl=t,"function"==typeof define&&define("tpl",[],function(){return t})}(this); \ No newline at end of file