1234567891011121314151617181920212223242526272829 |
- /* doT + auto-compilation of doT templates
- *
- * 2012, Laura Doktorova, https://github.com/olado/doT
- * Licensed under the MIT license
- *
- * Compiles .def, .dot, .jst files found under the specified path.
- * It ignores sub-directories.
- * Template files can have multiple extensions at the same time.
- * Files with .def extension can be included in other files via {{#def.name}}
- * Files with .dot extension are compiled into functions with the same name and
- * can be accessed as renderer.filename
- * Files with .jst extension are compiled into .js files. Produced .js file can be
- * loaded as a commonJS, AMD module, or just installed into a global variable
- * (default is set to window.render).
- * All inline defines defined in the .jst file are
- * compiled into separate functions and are available via _render.filename.definename
- *
- * Basic usage:
- * var dots = require("dot").process({path: "./views"});
- * dots.mytemplate({foo:"hello world"});
- *
- * The above snippet will:
- * 1. Compile all templates in views folder (.dot, .def, .jst)
- * 2. Place .js files compiled from .jst templates into the same folder.
- * These files can be used with require, i.e. require("./views/mytemplate").
- * 3. Return an object with functions compiled from .dot templates as its properties.
- * 4. Render mytemplate template.
- */
- function InstallDots(t){this.__path=t.path||"./","/"!==this.__path[this.__path.length-1]&&(this.__path+="/"),this.__destination=t.destination||this.__path,"/"!==this.__destination[this.__destination.length-1]&&(this.__destination+="/"),this.__global=t.global||"window.render",this.__rendermodule=t.rendermodule||{},this.__settings=Object.prototype.hasOwnProperty.call(t,"templateSettings")?copy(t.templateSettings,copy(doT.templateSettings)):void 0,this.__includes={}}function addexports(t){for(var e="",o=0;o<t.length;o++)e+="itself."+t[o]+"="+t[o]+";";return e}function copy(t,e){e=e||{};for(var o in t)e[o]=t[o];return e}function readdata(t){var e=fs.readFileSync(t);return e?e.toString():void console.log("problems with "+t)}var fs=require("fs"),doT=module.exports=require("./doT");doT.process=function(t){return new InstallDots(t).compileAll()},InstallDots.prototype.compileToFile=function(t,e,o){o=o||{};var i,n=t.substring(t.lastIndexOf("/")+1,t.lastIndexOf(".")),s=copy(this.__includes,copy(o)),l=this.__settings||doT.templateSettings,d=copy(l),r=doT.template(e,l,s),a=[],p="";for(var _ in s)s[_]!==o[_]&&s[_]!==this.__includes[_]&&(i=void 0,"string"==typeof s[_]?i=doT.template(s[_],l,s):"function"==typeof s[_]?i=s[_]:s[_].arg&&(d.varname=s[_].arg,i=doT.template(s[_].text,d,s)),i&&(p+=i.toString().replace("anonymous",_),a.push(_)));p+=r.toString().replace("anonymous",n),fs.writeFileSync(t,"(function(){"+p+"var itself="+n+", _encodeHTML=("+doT.encodeHTMLSource.toString()+"("+(l.doNotSkipEncoded||"")+"));"+addexports(a)+"if(typeof module!=='undefined' && module.exports) module.exports=itself;else if(typeof define==='function')define(function(){return itself;});else {"+this.__global+"="+this.__global+"||{};"+this.__global+"['"+n+"']=itself;}}());")},InstallDots.prototype.compilePath=function(t){var e=readdata(t);return e?doT.template(e,this.__settings||doT.templateSettings,copy(this.__includes)):void 0},InstallDots.prototype.compileAll=function(){doT.log&&console.log("Compiling all doT templates...");var t,e,o,i=this.__path,n=fs.readdirSync(i);for(t=0,e=n.length;e>t;t++)o=n[t],/\.def(\.dot|\.jst)?$/.test(o)&&(doT.log&&console.log("Loaded def "+o),this.__includes[o.substring(0,o.indexOf("."))]=readdata(i+o));for(t=0,e=n.length;e>t;t++)o=n[t],/\.dot(\.def|\.jst)?$/.test(o)&&(doT.log&&console.log("Compiling "+o+" to function"),this.__rendermodule[o.substring(0,o.indexOf("."))]=this.compilePath(i+o)),/\.jst(\.dot|\.def)?$/.test(o)&&(doT.log&&console.log("Compiling "+o+" to file"),this.compileToFile(this.__destination+o.substring(0,o.indexOf("."))+".js",readdata(i+o)));return this.__rendermodule};
|