Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilate sub-directory support #155

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doT.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"use strict";

var doT = {
version: "1.0.3",
version: "1.1.4",
templateSettings: {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
Expand Down Expand Up @@ -91,7 +91,7 @@
var cse = c.append ? startend.append : startend.split, needhtmlencode, sid = 0, indv,
str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;

str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ")
str = (c.varname + "=" + c.varname + "||{};" + "var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ")
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""): str)
.replace(/'|\\/g, "\\$&")
.replace(c.interpolate || skip, function(m, code) {
Expand Down
198 changes: 127 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/

var fs = require("fs"),
path = require("path"),
doT = module.exports = require("./doT");

doT.process = function(options) {
Expand All @@ -36,55 +37,119 @@ doT.process = function(options) {
};

function InstallDots(o) {
this.__path = o.path || "./";
if (this.__path[this.__path.length-1] !== '/') this.__path += '/';
this.__destination = o.destination || this.__path;
if (this.__destination[this.__destination.length-1] !== '/') this.__destination += '/';
this.__global = o.global || "window.render";
this.__rendermodule = o.rendermodule || {};
this.__settings = o.templateSettings ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
this.__includes = {};
this.__global = o.global || "window.render";
this.__rendermodule = o.rendermodule || {};
this.__settings = o.templateSettings ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
this.__includes = {};
this.__subDirectories = o.subDirectories || false;
this.__path = o.path ? [o.path] : ["./"];
if (this.__path[0][this.__path[0].length - 1] !== '/') {
this.__path[0] += '/';
}
this.__destination = o.destination || this.__path[0];
if (this.__destination[this.__destination.length - 1] !== '/') {
this.__destination += '/';
}
if (this.__subDirectories) {
this.__path = getDirs(this.__path[0]);
}
}

InstallDots.prototype.compileToFile = function(path, template, def) {
def = def || {};
var modulename = path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."))
, defs = copy(this.__includes, copy(def))
, settings = this.__settings || doT.templateSettings
, compileoptions = copy(settings)
, defaultcompiled = doT.template(template, settings, defs)
, exports = []
, compiled = ""
, fn;

for (var property in defs) {
if (defs[property] !== def[property] && defs[property] !== this.__includes[property]) {
fn = undefined;
if (typeof defs[property] === 'string') {
fn = doT.template(defs[property], settings, defs);
} else if (typeof defs[property] === 'function') {
fn = defs[property];
} else if (defs[property].arg) {
compileoptions.varname = defs[property].arg;
fn = doT.template(defs[property].text, compileoptions, defs);
var modulename = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")),
defs = copy(this.__includes, copy(def)),
settings = this.__settings || doT.templateSettings,
compileoptions = copy(settings),
defaultcompiled = doT.template(template, settings, defs),
exports = [],
compiled = "",
fn;

// Prevents seperate function generation for each define
// for (var property in defs) {
// if (defs[property] !== def[property] && defs[property] !== this.__includes[property]) {
// fn = undefined;
// if (typeof defs[property] === 'string') {
// fn = doT.template(defs[property], settings, defs);
// } else if (typeof defs[property] === 'function') {
// fn = defs[property];
// } else if (defs[property].arg) {
// compileoptions.varname = defs[property].arg;
// fn = doT.template(defs[property].text, compileoptions, defs);
// }
// if (fn) {
// compiled += fn.toString().replace('anonymous', property);
// exports.push(property);
// }
// }
// }
compiled += defaultcompiled.toString().replace('anonymous', modulename);
fs.writeFileSync(path, "(function(){" + compiled + "var itself=" + modulename + ", _encodeHTML=(" + doT.encodeHTMLSource.toString() + "(" + (settings.doNotSkipEncoded || '') + "));" + addexports(exports) + "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 + "['" + modulename + "']=itself;}}());");
};

InstallDots.prototype.compilePath = function(path) {
var data = readdata(path);
if (data) {
return doT.template(data,
this.__settings || doT.templateSettings,
copy(this.__includes));
}
};

// Has to be done in two loops, so .def files from subdirectories
// get discovered, before compiling the .dot and .jst files
InstallDots.prototype.compileAll = function() {
console.log("Compiling all doT templates...");

var k, l, name,
dirIndex, defFolder, sources,
totalTemplates = 0;

for (dirIndex = 0; dirIndex < this.__path.length; dirIndex++) {
defFolder = this.__path[dirIndex];
sources = fs.readdirSync(defFolder);

for (k = 0, l = sources.length; k < l; k++) {
name = sources[k];

if (/\.def(\.dot|\.jst)?$/.test(name)) {
console.log("Loaded def " + name);
this.__includes[name.substring(0, name.indexOf('.'))] = readdata(defFolder + name);
totalTemplates++;
}
}
}

for (dirIndex = 0; dirIndex < this.__path.length; dirIndex++) {
defFolder = this.__path[dirIndex];
sources = fs.readdirSync(defFolder);

for (k = 0, l = sources.length; k < l; k++) {
name = sources[k];

if (/\.dot(\.def|\.jst)?$/.test(name)) {
console.log("Compiling " + name + " to function");
this.__rendermodule[name.substring(0, name.indexOf('.'))] = this.compilePath(defFolder + name);
totalTemplates++;
}
if (fn) {
compiled += fn.toString().replace('anonymous', property);
exports.push(property);

if (/\.jst(\.dot|\.def)?$/.test(name)) {
console.log("Compiling " + name + " to file");
this.compileToFile(this.__destination + name.substring(0, name.indexOf('.')) + '.js',
readdata(defFolder + name));
totalTemplates++;
}
}
}
compiled += defaultcompiled.toString().replace('anonymous', modulename);
fs.writeFileSync(path, "(function(){" + compiled
+ "var itself=" + modulename + ", _encodeHTML=(" + doT.encodeHTMLSource.toString() + "(" + (settings.doNotSkipEncoded || '') + "));"
+ addexports(exports)
+ "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 + "['" + modulename + "']=itself;}}());");

console.log("Finished compiling %d templates", totalTemplates);
return this.__rendermodule;
};

function addexports(exports) {
for (var ret ='', i=0; i< exports.length; i++) {
ret += "itself." + exports[i]+ "=" + exports[i]+";";
for (var ret = '', i = 0; i < exports.length; i++) {
ret += "itself." + exports[i] + "=" + exports[i] + ";";
}
return ret;
}
Expand All @@ -103,41 +168,32 @@ function readdata(path) {
console.log("problems with " + path);
}

InstallDots.prototype.compilePath = function(path) {
var data = readdata(path);
if (data) {
return doT.template(data,
this.__settings || doT.templateSettings,
copy(this.__includes));
}
};
function getDirs(dir) {
var dirs = walk(dir);

InstallDots.prototype.compileAll = function() {
console.log("Compiling all doT templates...");
return addEndSeparator(dirs);
}

var defFolder = this.__path,
sources = fs.readdirSync(defFolder),
k, l, name;
function walk(dir) {
var results = [path.resolve(dir)],
list = fs.readdirSync(dir);

for( k = 0, l = sources.length; k < l; k++) {
name = sources[k];
if (/\.def(\.dot|\.jst)?$/.test(name)) {
console.log("Loaded def " + name);
this.__includes[name.substring(0, name.indexOf('.'))] = readdata(defFolder + name);
}
}
list.forEach(function(file) {
file = path.resolve(dir, file);
var stat = fs.statSync(file);

for( k = 0, l = sources.length; k < l; k++) {
name = sources[k];
if (/\.dot(\.def|\.jst)?$/.test(name)) {
console.log("Compiling " + name + " to function");
this.__rendermodule[name.substring(0, name.indexOf('.'))] = this.compilePath(defFolder + name);
if (stat && stat.isDirectory()) {
results = results.concat(walk(file));
}
if (/\.jst(\.dot|\.def)?$/.test(name)) {
console.log("Compiling " + name + " to file");
this.compileToFile(this.__destination + name.substring(0, name.indexOf('.')) + '.js',
readdata(defFolder + name));
}
}
return this.__rendermodule;
};
});

return results;
}

function addEndSeparator(dirs) {
dirs.forEach(function(element, index, array) {
array[index] = element + path.sep;
});

return dirs;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"simple",
"templating"
],
"version": "1.0.3",
"version": "1.1.4",
"main": "index",
"bin": {
"dottojs": "./bin/dot-packer"
Expand Down