Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better style, more tests for cache functions

  • Loading branch information...
commit a44831637c228bd8e405680c75978b98d08a54c6 1 parent ab3e541
Max Melentiev authored
View
3  Makefile
@@ -1,7 +1,6 @@
BIN := node_modules/.bin
UGLIFY := $(BIN)/uglifyjs
COFFEE := $(BIN)/coffee
-MOCHA := $(BIN)/mocha
TARGETS := doT.js compile.js express.js
UGLIFIED:= doT.min.js
@@ -22,4 +21,4 @@ clean:
rm -f $(TARGETS) $(UGLIFIED)
test: compile
- $(MOCHA)
+ npm test
View
15 compile.js
@@ -1,13 +1,18 @@
// Generated by CoffeeScript 1.4.0
(function() {
+ var child, flow, fs, path;
+
+ fs = require('fs');
+
+ path = require('path');
+
+ flow = require('flow');
+
+ child = require('child_process');
module.exports = function(data, finalcb) {
- var child, doT, flow, fs, path, readFile, readItem, _ref;
- fs = require('fs');
- path = require('path');
- flow = require('flow');
+ var doT, readFile, readItem, _ref;
doT = (_ref = data.doT) != null ? _ref : require('./doT');
- child = require('child_process');
flow.anyError = function(results) {
var r, _i, _len;
for (_i = 0, _len = results.length; _i < _len; _i++) {
View
89 doT.js
@@ -10,7 +10,7 @@
(function() {
- "use strict";
+ 'use strict';
var cache, doT, mangles, resolveDefs, sid, skip, startend, tags, unescape;
@@ -49,6 +49,40 @@
skip = /$^/;
+ if (typeof module !== "undefined" && module !== null ? module.exports : void 0) {
+ module.exports = doT;
+ } else if (typeof define !== "undefined" && define !== null ? define.amd : void 0) {
+ define(function() {
+ return doT;
+ });
+ } else {
+ this.doT = doT;
+ }
+
+ if (!String.prototype.encodeHTML) {
+ (function() {
+ var match, rules;
+ rules = {
+ "&": "&#38;",
+ "<": "&#60;",
+ ">": "&#62;",
+ '"': '&#34;',
+ "'": '&#39;',
+ "/": '&#47;'
+ };
+ match = /&(?!#?\w+;)|<|>|"|'|\//g;
+ return String.prototype.encodeHTML = function() {
+ return this.replace(match, function(m) {
+ return rules[m] || m;
+ });
+ };
+ })();
+ }
+
+ doT.unescape = unescape = function(code) {
+ return code.replace(/\\('|\\)/g, '$1').replace(/[\r\t\n]/g, ' ');
+ };
+
tags = doT.tags;
tags.interpolate = {
@@ -184,7 +218,7 @@
new Function("def", "def['" + code + "'] = " + value)(def);
}
}
- return "";
+ return '';
}).replace(c.use || skip, function(m, code) {
var v;
if (c.useParams) {
@@ -261,42 +295,6 @@
}
};
- if (typeof module !== "undefined" && module !== null ? module.exports : void 0) {
- module.exports = doT;
- } else if (typeof define !== "undefined" && define !== null ? define.amd : void 0) {
- define(function() {
- return doT;
- });
- } else {
- this.doT = doT;
- }
-
- if (!String.prototype.encodeHTML) {
- (function() {
- var match, rules;
- rules = {
- "&": "&#38;",
- "<": "&#60;",
- ">": "&#62;",
- '"': '&#34;',
- "'": '&#39;',
- "/": '&#47;'
- };
- match = /&(?!#?\w+;)|<|>|"|'|\//g;
- return String.prototype.encodeHTML = function() {
- return this.replace(match, function(m) {
- return rules[m] || m;
- });
- };
- })();
- }
-
- unescape = function(code) {
- return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, ' ');
- };
-
- doT.unescape = unescape;
-
doT.compile = function(tmpl, def) {
var compile_params, m_id, m_name, mangles_list;
compile_params = {
@@ -333,7 +331,7 @@
f = cache[id];
str += ",\"" + id + "\": " + (f.toString());
}
- return "{" + (str.substring(1)) + "}";
+ return "{" + str.slice(1) + "}";
};
doT.addCached = function(id, fn) {
@@ -350,9 +348,11 @@
doT.render = function(tmpl) {
var src;
- ('object' !== typeof tmpl) && (tmpl = {
- name: tmpl
- });
+ if ('object' !== typeof tmpl) {
+ tmpl = {
+ name: tmpl
+ };
+ }
if (!cache[tmpl.name]) {
src = doT.autoload(tmpl.name);
if (false === src) {
@@ -367,11 +367,10 @@
return function(name) {
var src;
src = document.getElementById(name);
- if (!src || !src.type || 'text/x-dot-tmpl' !== src.type) {
+ if ((src != null ? src.type : void 0) !== 'text/x-dot-tmpl') {
return false;
- } else {
- return src.innerHTML;
}
+ return src.innerHTML;
};
};
View
2  doT.min.js
@@ -1 +1 @@
-(function(){"use strict";var cache,doT,mangles,resolveDefs,sid,skip,startend,tags,unescape;startend={append:{start:"' + (",end:") + '",endEncode:").encodeHTML() + '"},split:{start:"'; out += (",end:"); out += '",endEncode:").encodeHTML(); out += '"}};doT={version:"0.2.0",templateSettings:{use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,varname:"it",strip:true,"with":true,dynamicList:"it._dynamic",startend:startend.append},startend:startend,mangles:{},tags:{}};cache={};sid=0;skip=/$^/;tags=doT.tags;tags.interpolate={regex:/\{\{\s*=([\s\S]*?)\}\}/g,func:function(m,code){var cse;cse=doT.templateSettings.startend;return cse.start+unescape(code)+cse.end}};tags.encode={regex:/\{\{\s*!([\s\S]*?)\}\}/g,func:function(m,code){var cse;cse=doT.templateSettings.startend;return cse.start+unescape(code)+cse.endEncode}};tags.conditional={regex:/\{\{\s*\?(\?)?\s*([\s\S]*?)\}\}/g,func:function(m,elsecase,code){if(elsecase){if(code){return"' ; } else if ( "+unescape(code)+" ) { out += '"}else{return"'; } else { out += '"}}else{if(code){return"'; if ( "+unescape(code)+" ) { out += '"}else{return"'; } out += '"}}}};tags.iterate={regex:/\{\{\s*~\s*(?:(\S+?)\s*\:\s*([\w$]+)\s*(?:=>\s*([\w$]+))?\s*)?\}\}/g,func:function(m,iterate,iname,vname){var _ref;if(!iterate){return"'; } } out += '"}if(!vname){_ref=[iname,"i"+ ++sid],vname=_ref[0],iname=_ref[1]}iterate=unescape(iterate);return"'; var arr"+sid+" = "+iterate+"; if( arr"+sid+" ) { var "+vname+", "+iname+" = -1, l"+sid+" = arr"+sid+".length-1; while( "+iname+" < l"+sid+" ){ "+vname+" = arr"+sid+"["+iname+" += 1]; out += '"}};tags.iterateFor={regex:/\{\{\s*:\s*(?:(\S+?)\s*\:\s*([\w$]+)\s*(?:=>\s*([\w$]+))?\s*)?\}\}/g,func:function(m,iterate,iname,vname){var inpname,_ref;if(!iterate){return"'; } } out += '"}inpname="i"+ ++sid;if(!vname){_ref=[iname,"i"+ ++sid],vname=_ref[0],iname=_ref[1]}return"'; var "+inpname+" = "+iterate+"; if ( "+inpname+" ) { var "+vname+", "+iname+"; for ("+iname+" in "+inpname+" ) { "+vname+" = "+inpname+"[ "+iname+" ]; out += '"}};tags.content_for={regex:/\{\{\s*>([\s\S]*?)\}\}/g,func:function(m,id){this.multiple_contents=true;if(id){return"'; contents[current_out] = out; out_stack.push(current_out); current_out='"+unescape(id).trim()+"'; out = contents[current_out] = '"}else{return"'; contents[current_out] = out; out = contents[current_out = out_stack.pop()] += '"}}};tags.xx_includeDynamic={regex:/\{\{\s*@@\s*(\S+?)\(([\s\S]*?)\)\s*\}\}/g,func:function(m,tmpl,args){var vname;sid+=1;vname="tmpl"+sid;return"'; var "+vname+" = "+doT.templateSettings.dynamicList+"[ '"+unescape(tmpl)+"' ]; if ('string' === typeof "+vname+") "+vname+" = {name: "+vname+"}; out += doT.render({name: "+vname+".name, args: "+vname+".args || arguments}) + '"}};tags.xy_render={regex:/\{\{\s*@\s*(\S+?)\(([\s\S]*?)\)\s*\}\}/g,func:function(m,tmpl,args){return"' + doT.render( '"+tmpl+"' "+(args?","+unescape(args):"")+" ) + '"}};tags.zz_evaluate={regex:/\{\{([\s\S]*?)\}\}/g,func:function(m,code){return"'; "+unescape(code)+"; out += '"}};mangles=doT.mangles;mangles["05_define"]=resolveDefs=function(block,compileParams){var c,def;if(!(this.use||this.define)){return str}c=this;def=compileParams.def||{};return block.toString().replace(c.define||skip,function(m,code,assign,value){if(code.indexOf("def.")===0){code=code.substring(4)}if(!(code in def)){if(assign===":"){if(c.defineParams){value.replace(c.defineParams,function(m,param,v){return def[code]={arg:param,text:v}})}if(!(code in def)){def[code]=value}}else{new Function("def","def['"+code+"'] = "+value)(def)}}return""}).replace(c.use||skip,function(m,code){var v;if(c.useParams){code=code.replace(c.useParams,function(m,s,d,param){var rw;if(def[d]&&def[d].arg&&param){rw=(d+":"+param).replace(/'|\\/g,"_");def.__exp=def.__exp||{};def.__exp[rw]=def[d].text.replace(new RegExp("(^|[^\\w$])"+def[d].arg+"([^\\w$])","g"),"$1"+param+"$2");return s+("def.__exp['"+rw+"']")}})}v=new Function("def","return "+code)(def);if(v){return resolveDefs.call(c,v,compileParams)}else{return v}})};mangles["10_strip"]=function(str,compileParams){if(!this.strip){return str}return str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,"")};mangles["20_escape_quotes"]=function(str,compileParams){return str.replace(/'|\\/g,"\\$&")};mangles["50_tags"]=function(str,compileParams){var t_id,t_name,taglist;taglist=Object.keys(doT.tags).sort();for(t_id in taglist){t_name=taglist[t_id];str=str.replace(doT.tags[t_name].regex,function(){return doT.tags[t_name].func.apply(compileParams,arguments)})}return str};mangles["70_escape_spaces"]=function(str,compileParams){return str.replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r")};mangles["80_cleanup"]=function(str,compileParams){return str.replace(/(\s|;|}|^|{)out\+='';/g,"$1").replace(/\s*\+\s*''/g,"").replace(/(\s|;|}|^|{)out\+=''\+/g,"$1out+=")};mangles["80_function_basics"]=function(str,compileParams){if(compileParams.multiple_contents){return" var out_stack = [], contents = {}, current_out = '_content'; var out = '"+str+"'; contents[current_out] = out; return contents; "}else{return" var out = '"+str+"'; return out; "}};mangles["80_with"]=function(str,compileParams){if(!this["with"]){return str}return"with("+(true===this["with"]?this.varname:this["with"])+") {"+str+"}"};mangles["95_functionize"]=function(str,compileParams){try{return new Function(this.varname,str)}catch(e){throw new Error(""+e+' in "'+str+'"')}};if(typeof module!=="undefined"&&module!==null?module.exports:void 0){module.exports=doT}else if(typeof define!=="undefined"&&define!==null?define.amd:void 0){define(function(){return doT})}else{this.doT=doT}if(!String.prototype.encodeHTML){(function(){var match,rules;rules={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"};match=/&(?!#?\w+;)|<|>|"|'|\//g;return String.prototype.encodeHTML=function(){return this.replace(match,function(m){return rules[m]||m})}})()}unescape=function(code){return code.replace(/\\('|\\)/g,"$1").replace(/[\r\t\n]/g," ")};doT.unescape=unescape;doT.compile=function(tmpl,def){var compile_params,m_id,m_name,mangles_list;compile_params={def:def};mangles_list=Object.keys(doT.mangles).sort();for(m_id in mangles_list){m_name=mangles_list[m_id];tmpl=doT.mangles[m_name].call(doT.templateSettings,tmpl,compile_params)}return tmpl};doT.template=doT.compile;doT.getCached=function(tmpl){if(!tmpl){return cache}if(!cache[tmpl]){throw new Error("Template not found: "+tmpl)}return cache[tmpl]};doT.setCached=function(fns){return cache=fns};doT.exportCached=function(){var f,id,str;str="";for(id in cache){f=cache[id];str+=',"'+id+'": '+f.toString()}return"{"+str.substring(1)+"}"};doT.addCached=function(id,fn){var f,i;if("object"===typeof id){for(i in id){f=id[i];doT.addCached(i,f)}return}return cache[id]=fn};doT.render=function(tmpl){var src;"object"!==typeof tmpl&&(tmpl={name:tmpl});if(!cache[tmpl.name]){src=doT.autoload(tmpl.name);if(false===src){throw new Error("Template not found: "+tmpl.name)}doT.addCached(tmpl.name,doT.compile(src))}return cache[tmpl.name].apply(this,tmpl.args||Array.prototype.slice.call(arguments,1))};doT.autoloadDOM=function(opts){return function(name){var src;src=document.getElementById(name);if(!src||!src.type||"text/x-dot-tmpl"!==src.type){return false}else{return src.innerHTML}}};doT.autoloadFS=function(opts){return function(name){try{return opts.fs.readFileSync(""+opts.root+"/"+name.replace(".","/")+".tmpl")}catch(e){return false}}};doT.autoloadFail=function(){return false};doT.autoload=doT.autoloadDOM()}).call(this);
+(function(){"use strict";var cache,doT,mangles,resolveDefs,sid,skip,startend,tags,unescape;startend={append:{start:"' + (",end:") + '",endEncode:").encodeHTML() + '"},split:{start:"'; out += (",end:"); out += '",endEncode:").encodeHTML(); out += '"}};doT={version:"0.2.0",templateSettings:{use:/\{\{#([\s\S]+?)\}\}/g,define:/\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,varname:"it",strip:true,"with":true,dynamicList:"it._dynamic",startend:startend.append},startend:startend,mangles:{},tags:{}};cache={};sid=0;skip=/$^/;if(typeof module!=="undefined"&&module!==null?module.exports:void 0){module.exports=doT}else if(typeof define!=="undefined"&&define!==null?define.amd:void 0){define(function(){return doT})}else{this.doT=doT}if(!String.prototype.encodeHTML){(function(){var match,rules;rules={"&":"&#38;","<":"&#60;",">":"&#62;",'"':"&#34;","'":"&#39;","/":"&#47;"};match=/&(?!#?\w+;)|<|>|"|'|\//g;return String.prototype.encodeHTML=function(){return this.replace(match,function(m){return rules[m]||m})}})()}doT.unescape=unescape=function(code){return code.replace(/\\('|\\)/g,"$1").replace(/[\r\t\n]/g," ")};tags=doT.tags;tags.interpolate={regex:/\{\{\s*=([\s\S]*?)\}\}/g,func:function(m,code){var cse;cse=doT.templateSettings.startend;return cse.start+unescape(code)+cse.end}};tags.encode={regex:/\{\{\s*!([\s\S]*?)\}\}/g,func:function(m,code){var cse;cse=doT.templateSettings.startend;return cse.start+unescape(code)+cse.endEncode}};tags.conditional={regex:/\{\{\s*\?(\?)?\s*([\s\S]*?)\}\}/g,func:function(m,elsecase,code){if(elsecase){if(code){return"' ; } else if ( "+unescape(code)+" ) { out += '"}else{return"'; } else { out += '"}}else{if(code){return"'; if ( "+unescape(code)+" ) { out += '"}else{return"'; } out += '"}}}};tags.iterate={regex:/\{\{\s*~\s*(?:(\S+?)\s*\:\s*([\w$]+)\s*(?:=>\s*([\w$]+))?\s*)?\}\}/g,func:function(m,iterate,iname,vname){var _ref;if(!iterate){return"'; } } out += '"}if(!vname){_ref=[iname,"i"+ ++sid],vname=_ref[0],iname=_ref[1]}iterate=unescape(iterate);return"'; var arr"+sid+" = "+iterate+"; if( arr"+sid+" ) { var "+vname+", "+iname+" = -1, l"+sid+" = arr"+sid+".length-1; while( "+iname+" < l"+sid+" ){ "+vname+" = arr"+sid+"["+iname+" += 1]; out += '"}};tags.iterateFor={regex:/\{\{\s*:\s*(?:(\S+?)\s*\:\s*([\w$]+)\s*(?:=>\s*([\w$]+))?\s*)?\}\}/g,func:function(m,iterate,iname,vname){var inpname,_ref;if(!iterate){return"'; } } out += '"}inpname="i"+ ++sid;if(!vname){_ref=[iname,"i"+ ++sid],vname=_ref[0],iname=_ref[1]}return"'; var "+inpname+" = "+iterate+"; if ( "+inpname+" ) { var "+vname+", "+iname+"; for ("+iname+" in "+inpname+" ) { "+vname+" = "+inpname+"[ "+iname+" ]; out += '"}};tags.content_for={regex:/\{\{\s*>([\s\S]*?)\}\}/g,func:function(m,id){this.multiple_contents=true;if(id){return"'; contents[current_out] = out; out_stack.push(current_out); current_out='"+unescape(id).trim()+"'; out = contents[current_out] = '"}else{return"'; contents[current_out] = out; out = contents[current_out = out_stack.pop()] += '"}}};tags.xx_includeDynamic={regex:/\{\{\s*@@\s*(\S+?)\(([\s\S]*?)\)\s*\}\}/g,func:function(m,tmpl,args){var vname;sid+=1;vname="tmpl"+sid;return"'; var "+vname+" = "+doT.templateSettings.dynamicList+"[ '"+unescape(tmpl)+"' ]; if ('string' === typeof "+vname+") "+vname+" = {name: "+vname+"}; out += doT.render({name: "+vname+".name, args: "+vname+".args || arguments}) + '"}};tags.xy_render={regex:/\{\{\s*@\s*(\S+?)\(([\s\S]*?)\)\s*\}\}/g,func:function(m,tmpl,args){return"' + doT.render( '"+tmpl+"' "+(args?","+unescape(args):"")+" ) + '"}};tags.zz_evaluate={regex:/\{\{([\s\S]*?)\}\}/g,func:function(m,code){return"'; "+unescape(code)+"; out += '"}};mangles=doT.mangles;mangles["05_define"]=resolveDefs=function(block,compileParams){var c,def;if(!(this.use||this.define)){return str}c=this;def=compileParams.def||{};return block.toString().replace(c.define||skip,function(m,code,assign,value){if(code.indexOf("def.")===0){code=code.substring(4)}if(!(code in def)){if(assign===":"){if(c.defineParams){value.replace(c.defineParams,function(m,param,v){return def[code]={arg:param,text:v}})}if(!(code in def)){def[code]=value}}else{new Function("def","def['"+code+"'] = "+value)(def)}}return""}).replace(c.use||skip,function(m,code){var v;if(c.useParams){code=code.replace(c.useParams,function(m,s,d,param){var rw;if(def[d]&&def[d].arg&&param){rw=(d+":"+param).replace(/'|\\/g,"_");def.__exp=def.__exp||{};def.__exp[rw]=def[d].text.replace(new RegExp("(^|[^\\w$])"+def[d].arg+"([^\\w$])","g"),"$1"+param+"$2");return s+("def.__exp['"+rw+"']")}})}v=new Function("def","return "+code)(def);if(v){return resolveDefs.call(c,v,compileParams)}else{return v}})};mangles["10_strip"]=function(str,compileParams){if(!this.strip){return str}return str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,"")};mangles["20_escape_quotes"]=function(str,compileParams){return str.replace(/'|\\/g,"\\$&")};mangles["50_tags"]=function(str,compileParams){var t_id,t_name,taglist;taglist=Object.keys(doT.tags).sort();for(t_id in taglist){t_name=taglist[t_id];str=str.replace(doT.tags[t_name].regex,function(){return doT.tags[t_name].func.apply(compileParams,arguments)})}return str};mangles["70_escape_spaces"]=function(str,compileParams){return str.replace(/\n/g,"\\n").replace(/\t/g,"\\t").replace(/\r/g,"\\r")};mangles["80_cleanup"]=function(str,compileParams){return str.replace(/(\s|;|}|^|{)out\+='';/g,"$1").replace(/\s*\+\s*''/g,"").replace(/(\s|;|}|^|{)out\+=''\+/g,"$1out+=")};mangles["80_function_basics"]=function(str,compileParams){if(compileParams.multiple_contents){return" var out_stack = [], contents = {}, current_out = '_content'; var out = '"+str+"'; contents[current_out] = out; return contents; "}else{return" var out = '"+str+"'; return out; "}};mangles["80_with"]=function(str,compileParams){if(!this["with"]){return str}return"with("+(true===this["with"]?this.varname:this["with"])+") {"+str+"}"};mangles["95_functionize"]=function(str,compileParams){try{return new Function(this.varname,str)}catch(e){throw new Error(""+e+' in "'+str+'"')}};doT.compile=function(tmpl,def){var compile_params,m_id,m_name,mangles_list;compile_params={def:def};mangles_list=Object.keys(doT.mangles).sort();for(m_id in mangles_list){m_name=mangles_list[m_id];tmpl=doT.mangles[m_name].call(doT.templateSettings,tmpl,compile_params)}return tmpl};doT.template=doT.compile;doT.getCached=function(tmpl){if(!tmpl){return cache}if(!cache[tmpl]){throw new Error("Template not found: "+tmpl)}return cache[tmpl]};doT.setCached=function(fns){return cache=fns};doT.exportCached=function(){var f,id,str;str="";for(id in cache){f=cache[id];str+=',"'+id+'": '+f.toString()}return"{"+str.slice(1)+"}"};doT.addCached=function(id,fn){var f,i;if("object"===typeof id){for(i in id){f=id[i];doT.addCached(i,f)}return}return cache[id]=fn};doT.render=function(tmpl){var src;if("object"!==typeof tmpl){tmpl={name:tmpl}}if(!cache[tmpl.name]){src=doT.autoload(tmpl.name);if(false===src){throw new Error("Template not found: "+tmpl.name)}doT.addCached(tmpl.name,doT.compile(src))}return cache[tmpl.name].apply(this,tmpl.args||Array.prototype.slice.call(arguments,1))};doT.autoloadDOM=function(opts){return function(name){var src;src=document.getElementById(name);if((src!=null?src.type:void 0)!=="text/x-dot-tmpl"){return false}return src.innerHTML}};doT.autoloadFS=function(opts){return function(name){try{return opts.fs.readFileSync(""+opts.root+"/"+name.replace(".","/")+".tmpl")}catch(e){return false}}};doT.autoloadFail=function(){return false};doT.autoload=doT.autoloadDOM()}).call(this);
View
22 express.js
@@ -1,22 +1,16 @@
// Generated by CoffeeScript 1.4.0
(function() {
- var dot;
-
- dot = require('doT');
-
- module.exports = dot;
(function() {
- this.compile = dot.render;
- return {
- this.__express: function(filename, options, cb) {
- try {
- return cb(null, dot.render);
- } catch (err) {
- return cb(err);
- }
+ var _this = this;
+ this.compile = this.render;
+ return this.__express = function(filename, options, cb) {
+ try {
+ return cb(null, _this.render);
+ } catch (err) {
+ return cb(err);
}
};
- }).call(dot);
+ }).call(module.exports = require('doT'));
}).call(this);
View
2  package.json
@@ -19,7 +19,7 @@
"mocha": "*"
},
"scripts": {
- "test": "test"
+ "test": "mocha"
},
"repository": {
"type": "git",
View
15 src/compile.coffee
@@ -1,9 +1,10 @@
+fs = require 'fs'
+path = require 'path'
+flow = require 'flow'
+child = require 'child_process'
+
module.exports = (data, finalcb) ->
- fs = require 'fs'
- path = require 'path'
- flow = require 'flow'
doT = data.doT ? require './doT'
- child = require 'child_process'
# wait till it apears in release
flow.anyError = (results) ->
@@ -24,8 +25,7 @@ module.exports = (data, finalcb) ->
fs.readdir item, @
(err, files) ->
return @MULTI() err if err
- for file in files
- readItem path.join(item, file), @MULTI()
+ readItem path.join(item, file), @MULTI() for file in files
@MULTI() null
(results) ->
item_cb flow.anyError results
@@ -61,8 +61,7 @@ module.exports = (data, finalcb) ->
flow.exec(
->
- for file in data.files
- readItem file, @MULTI()
+ readItem file, @MULTI() for file in data.files
@MULTI() null
(results) ->
return unless finalcb
View
71 src/doT.coffee
@@ -5,7 +5,7 @@
doT.js is an open source component of http://bebedo.com
Licensed under the MIT license.
###
-"use strict";
+'use strict'
startend =
append:
@@ -35,6 +35,23 @@ cache = {}
sid = 0 # sequental id for variable names
skip = /$^/
+# register in global scope
+if module?.exports
+ module.exports = doT
+else if define?.amd
+ define -> doT
+else
+ @doT = doT
+
+unless String::encodeHTML
+ do ->
+ rules = "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;'
+ match = /&(?!#?\w+;)|<|>|"|'|\//g
+ String::encodeHTML = -> @replace match, (m) -> rules[m] || m
+
+doT.unescape = unescape = (code) ->
+ code.replace(/\\('|\\)/g, '$1').replace /[\r\t\n]/g, ' '
+
# tags definition
tags = doT.tags
tags.interpolate =
@@ -147,7 +164,7 @@ mangles['05_define'] = resolveDefs = (block, compileParams) ->
def[code] = value unless code of def
else
new Function("def", "def['#{code}'] = #{value}") def
- ""
+ ''
.replace c.use or skip, (m, code) ->
if c.useParams
code = code.replace(c.useParams, (m, s, d, param) ->
@@ -178,7 +195,7 @@ mangles['50_tags'] = (str, compileParams) ->
taglist = Object.keys(doT.tags).sort()
for t_id, t_name of taglist
str = str.replace doT.tags[ t_name ].regex, ->
- doT.tags[ t_name ].func.apply compileParams, arguments
+ doT.tags[t_name].func.apply compileParams, arguments
str
mangles['70_escape_spaces'] = (str, compileParams) ->
@@ -216,31 +233,12 @@ mangles['95_functionize'] = (str, compileParams) ->
catch e
throw new Error "#{e} in \"#{str}\""
-# register in global scope
-if module?.exports
- module.exports = doT
-else if define?.amd
- define -> doT
-else
- @doT = doT
-
-unless String::encodeHTML
- (->
- rules = "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;'
- match = /&(?!#?\w+;)|<|>|"|'|\//g
- String::encodeHTML = -> @replace( match, (m) -> rules[m] || m )
- )()
-
-unescape = (code) ->
- code.replace( /\\('|\\)/g, "$1" ).replace( /[\r\t\n]/g, ' ' )
-doT.unescape = unescape
-
# template compilation
doT.compile = (tmpl, def) ->
compile_params = def: def
mangles_list = Object.keys(doT.mangles).sort()
for m_id, m_name of mangles_list
- tmpl = doT.mangles[ m_name ].call doT.templateSettings, tmpl, compile_params
+ tmpl = doT.mangles[m_name].call doT.templateSettings, tmpl, compile_params
tmpl
# backward compability
@@ -254,9 +252,8 @@ doT.getCached = (tmpl) ->
doT.setCached = (fns) -> cache = fns
doT.exportCached = ->
str = ""
- for id, f of cache
- str += ",\"#{id}\": #{f.toString()}"
- "{#{str.substring(1)}}"
+ str += ",\"#{id}\": #{f.toString()}" for id, f of cache
+ "{#{str[1..]}}"
doT.addCached = (id, fn) ->
if 'object' == typeof id
for i, f of id
@@ -266,29 +263,27 @@ doT.addCached = (id, fn) ->
# doT.render() for transparent autoloding & caching
doT.render = (tmpl) ->
- ('object' != typeof tmpl) && (tmpl = { name: tmpl })
+ tmpl = name: tmpl unless 'object' == typeof tmpl
if !cache[tmpl.name]
src = doT.autoload tmpl.name
if false == src
throw new Error "Template not found: #{tmpl.name}"
doT.addCached tmpl.name, doT.compile src
- cache[tmpl.name].apply( this, tmpl.args || Array.prototype.slice.call( arguments, 1 ) )
+ cache[tmpl.name].apply this, tmpl.args || Array::slice.call arguments, 1
-doT.autoloadDOM = ( opts ) ->
- ( name ) ->
+doT.autoloadDOM = (opts) ->
+ (name) ->
src = document.getElementById name
- if !src || !src.type || 'text/x-dot-tmpl' != src.type
- false
- else
- src.innerHTML
+ return false unless src?.type is 'text/x-dot-tmpl'
+ src.innerHTML
-doT.autoloadFS = ( opts ) ->
- ( name ) ->
+doT.autoloadFS = (opts) ->
+ (name) ->
try
- opts.fs.readFileSync "#{opts.root}/#{name.replace( '.', '/' )}.tmpl"
+ opts.fs.readFileSync "#{opts.root}/#{name.replace('.', '/')}.tmpl"
catch e
false
-doT.autoloadFail = () -> false
+doT.autoloadFail = -> false
doT.autoload = doT.autoloadDOM();
View
13 src/express.coffee
@@ -1,13 +1,8 @@
-dot = require 'doT'
-
-module.exports = dot
-
(->
- @compile = dot.render
-
- @__express: (filename, options, cb) ->
+ @compile = @render
+ @__express = (filename, options, cb) =>
try
- cb null, dot.render
+ cb null, @render
catch err
cb err
-).call dot
+).call module.exports = require 'doT'
View
21 test/dot_spec.coffee
@@ -44,6 +44,27 @@ describe "doT", ->
assert.equal '<xml>data1</xml>', doT.render 'layout2', include1
assert.equal '<xml>data2 partial</xml>', doT.render 'layout2', include2
+ describe '#getCached', ->
+ it 'should return template function', ->
+ assert.equal 'data1', doT.getCached('body1') {}
+ it 'should return object with template functions', ->
+ assert.equal 'data1', doT.getCached().body1 {}
+
+ describe '#setCached', ->
+ it 'should set all cached functions', ->
+ cache = doT.getCached()
+ doT.setCached {}
+ assert.throws -> doT.render 'body1', {}
+ doT.setCached cache
+ assert.equal 'data1', doT.render 'body1', {}
+
+ describe '#exportCached', ->
+ it 'should export js object with template functions', ->
+ str = doT.exportCached()
+ obj = null
+ eval "obj = #{str}"
+ assert.equal 'data1', obj.body1 {}
+
describe 'content_for', ->
it 'returns map', ->
assert.deepEqual {_content: 'content end', title: 'title', footer: 'footer'},
Please sign in to comment.
Something went wrong with that request. Please try again.