Permalink
Browse files

code formatting

  • Loading branch information...
1 parent 95035e4 commit 3db3bddb7c6004cd4019d04a7cc687e10133bb5a @olado committed Apr 28, 2012
Showing with 93 additions and 86 deletions.
  1. +43 −40 benchmarks/templating/doT.js
  2. +43 −40 doT.js
  3. +7 −6 doT.min.js
@@ -7,35 +7,52 @@
(function() {
"use strict";
- var doT = { version : '0.2.0' };
+ var doT = {
+ version: '0.2.0',
+ templateSettings: {
+ evaluate: /\{\{([\s\S]+?)\}\}/g,
+ interpolate: /\{\{=([\s\S]+?)\}\}/g,
+ encode: /\{\{!([\s\S]+?)\}\}/g,
+ use: /\{\{#([\s\S]+?)\}\}/g,
+ define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
+ conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
+ iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
+ varname: 'it',
+ strip: true,
+ append: true,
+ selfcontained: false
+ },
+ template: undefined, //fn, compile template
+ compile: undefined //fn, for express
+ };
- var global = (function () { return this || (0 || eval)('this'); }());
+ var global = (function(){ return this || (0||eval)('this'); }());
if (typeof module !== 'undefined' && module.exports) {
module.exports = doT;
} else if (typeof define === 'function' && define.amd) {
- define(function() { return doT; });
+ define(function(){return doT;});
} else {
global.doT = doT;
}
- doT.templateSettings = {
- evaluate: /\{\{([\s\S]+?)\}\}/g,
- interpolate: /\{\{=([\s\S]+?)\}\}/g,
- encode: /\{\{!([\s\S]+?)\}\}/g,
- use: /\{\{#([\s\S]+?)\}\}/g,
- define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
- conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
- iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
- varname: 'it',
- strip : true,
- append: true,
- selfcontained: false
- };
+ function encodeHTMLSource() {
+ var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
+ matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
+ return function(code) {
+ return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : code;
+ };
+ }
+ global.encodeHTML = encodeHTMLSource();
+
+ var startend = {
+ append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
+ split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
+ }, skip = /$^/;
function resolveDefs(c, block, def) {
return ((typeof block === 'string') ? block : block.toString())
- .replace(c.define, function (m, code, assign, value) {
+ .replace(c.define || skip, function(m, code, assign, value) {
if (code.indexOf('def.') === 0) {
code = code.substring(4);
}
@@ -48,30 +65,16 @@
}
return '';
})
- .replace(c.use, function(m, code) {
+ .replace(c.use || skip, function(m, code) {
var v = eval(code);
return v ? resolveDefs(c, v, def) : v;
});
}
function unescape(code) {
- return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
+ return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}
- function encodeHTMLSource() {
- var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
- matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
- return function(code) {
- return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
- };
- }
- global.encodeHTML = encodeHTMLSource();
-
- var startend = { // optimal choice depends on platform/size of templates
- append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
- split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
- };
-
doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode, sid=0, indv;
@@ -85,25 +88,25 @@
str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
- .replace(c.interpolate, function(m, code) {
+ .replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
})
- .replace(c.encode, function(m, code) {
+ .replace(c.encode || skip, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
- .replace(c.conditional, function(m, elsecase, code) {
+ .replace(c.conditional || skip, function(m, elsecase, code) {
return elsecase ?
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
- .replace(c.iterate, function(m, iterate, vname, iname) {
+ .replace(c.iterate || skip, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
- sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
+ sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
- .replace(c.evaluate, function(m, code) {
+ .replace(c.evaluate || skip, function(m, code) {
return "';" + unescape(code) + "out+='";
})
+ "';return out;")
@@ -114,7 +117,7 @@
.split("var out='';out+=").join('var out=');
if (needhtmlencode && c.selfcontained) {
- str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
+ str = "var encodeHTML=(" + encodeHTMLSource.toString() + "());" + str;
}
try {
return new Function(c.varname, str);
View
83 doT.js
@@ -7,35 +7,52 @@
(function() {
"use strict";
- var doT = { version : '0.2.0' };
+ var doT = {
+ version: '0.2.0',
+ templateSettings: {
+ evaluate: /\{\{([\s\S]+?)\}\}/g,
+ interpolate: /\{\{=([\s\S]+?)\}\}/g,
+ encode: /\{\{!([\s\S]+?)\}\}/g,
+ use: /\{\{#([\s\S]+?)\}\}/g,
+ define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
+ conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
+ iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
+ varname: 'it',
+ strip: true,
+ append: true,
+ selfcontained: false
+ },
+ template: undefined, //fn, compile template
+ compile: undefined //fn, for express
+ };
- var global = (function () { return this || (0 || eval)('this'); }());
+ var global = (function(){ return this || (0||eval)('this'); }());
if (typeof module !== 'undefined' && module.exports) {
module.exports = doT;
} else if (typeof define === 'function' && define.amd) {
- define(function() { return doT; });
+ define(function(){return doT;});
} else {
global.doT = doT;
}
- doT.templateSettings = {
- evaluate: /\{\{([\s\S]+?)\}\}/g,
- interpolate: /\{\{=([\s\S]+?)\}\}/g,
- encode: /\{\{!([\s\S]+?)\}\}/g,
- use: /\{\{#([\s\S]+?)\}\}/g,
- define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
- conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
- iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
- varname: 'it',
- strip : true,
- append: true,
- selfcontained: false
- };
+ function encodeHTMLSource() {
+ var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
+ matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
+ return function(code) {
+ return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : code;
+ };
+ }
+ global.encodeHTML = encodeHTMLSource();
+
+ var startend = {
+ append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
+ split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
+ }, skip = /$^/;
function resolveDefs(c, block, def) {
return ((typeof block === 'string') ? block : block.toString())
- .replace(c.define, function (m, code, assign, value) {
+ .replace(c.define || skip, function(m, code, assign, value) {
if (code.indexOf('def.') === 0) {
code = code.substring(4);
}
@@ -48,30 +65,16 @@
}
return '';
})
- .replace(c.use, function(m, code) {
+ .replace(c.use || skip, function(m, code) {
var v = eval(code);
return v ? resolveDefs(c, v, def) : v;
});
}
function unescape(code) {
- return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
+ return code.replace(/\\'/g, "'").replace(/\\\\/g,"\\").replace(/[\r\t\n]/g, ' ');
}
- function encodeHTMLSource() {
- var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
- matchHTML = /&(?!\\w+;)|<|>|\"|'|\//g;
- return function(code) {
- return code ? code.toString().replace(matchHTML, function(m) { return encodeHTMLRules[m] || m; }) : code;
- };
- }
- global.encodeHTML = encodeHTMLSource();
-
- var startend = { // optimal choice depends on platform/size of templates
- append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
- split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML("}
- };
-
doT.template = function(tmpl, c, def) {
c = c || doT.templateSettings;
var cse = c.append ? startend.append : startend.split, str, needhtmlencode, sid=0, indv;
@@ -85,25 +88,25 @@
str = ("var out='" + ((c.strip) ? str.replace(/\s*<!\[CDATA\[\s*|\s*\]\]>\s*|[\r\n\t]|(\/\*[\s\S]*?\*\/)/g, ''): str)
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
- .replace(c.interpolate, function(m, code) {
+ .replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
})
- .replace(c.encode, function(m, code) {
+ .replace(c.encode || skip, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
- .replace(c.conditional, function(m, elsecase, code) {
+ .replace(c.conditional || skip, function(m, elsecase, code) {
return elsecase ?
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
- .replace(c.iterate, function(m, iterate, vname, iname) {
+ .replace(c.iterate || skip, function(m, iterate, vname, iname) {
if (!iterate) return "'} };out+='";
- sid+=1;indv = iname || "i"+sid;iterate=unescape(iterate);
+ sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
})
- .replace(c.evaluate, function(m, code) {
+ .replace(c.evaluate || skip, function(m, code) {
return "';" + unescape(code) + "out+='";
})
+ "';return out;")
@@ -114,7 +117,7 @@
.split("var out='';out+=").join('var out=');
if (needhtmlencode && c.selfcontained) {
- str = "var encodeHTML=("+ encodeHTMLSource.toString()+"());"+str;
+ str = "var encodeHTML=(" + encodeHTMLSource.toString() + "());" + str;
}
try {
return new Function(c.varname, str);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 3db3bdd

Please sign in to comment.