Skip to content

Commit

Permalink
Prototype style
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Jan 19, 2010
1 parent eece5e8 commit b8d6aee
Showing 1 changed file with 81 additions and 74 deletions.
155 changes: 81 additions & 74 deletions lib/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,133 +156,141 @@ Templates.prototype = {
var FileHandler = function(
docStack, sourceTree, buildDoc, indexBuilder, copy, options) {

var pm = new plugins.PluginManager(sourceTree);
this.docStack = docStack;
this.sourceTree = sourceTree;
this.buildDoc = buildDoc;
this.indexBuilder = indexBuilder;
this.copy = copy;
this.options = options;

var that = {
numGenerated: 0,
numCopied: 0,
dirStack: [] // to know the current directory
};
this.pm = new plugins.PluginManager(sourceTree);

this.numGenerated = 0;
this.numCopied = 0;
this.dirStack = [];

var sourceExtRegex = new RegExp(options.get('source-extensions').join('|')),
sourceFilter = options.get('source-filter');
this.sourceExtRegex = new RegExp(options.get('source-extensions').join('|'));

var sourceFilter = options.get('source-filter');
if (sourceFilter) {
var r = new RegExp(sourceFilter);
sourceFilter = function(path) { return !!path.match(r); };
this.sourceFilter = function(path) { return !!path.match(r); };
} else {
sourceFilter = function(path) { return true; };
this.sourceFilter = function(path) { return true; };
}
}

FileHandler.prototype = {

/* PRIVATE */

// Recompute members derived from 'dirStack'
var recompute = function() {
that.currentDir = that.dirStack.join('/');
that.destDir = toDestPath(that.currentDir) + '/'; // need trailing slash
};
_recompute: function() {
this.currentDir = this.dirStack.join('/');
this.destDir = toDestPath(this.currentDir) + '/'; // need trailing slash
},

// Check the name of the *current* file to see if it's a source file
var checkIsSource = function(ext) {
_checkIsSource: function(ext) {
var isSource;
if (ext.match(sourceExtRegex)) {
if (ext.match(this.sourceExtRegex)) {
isSource = true;
} else {
// If the filename or any directory starts with _, consider it source
for (var i = 0; i < that.dirStack.length; i++) {
if (that.dirStack[i].charAt(0) == '_') {
for (var i = 0; i < this.dirStack.length; i++) {
if (this.dirStack[i].charAt(0) == '_') {
isSource = true;
break;
}
}
}
return isSource;
};
},

/* PUBLIC */

// Called before every doc
that.enterTree = function(dirName) {
enterTree: function(dirName) {
log.info('Entering source dir');
log.push(); // indentation

pm.enterDir();
docStack.push({dest: {dir: ''}});
indexBuilder.enterDir('');
this.pm.enterDir();
this.docStack.push({dest: {dir: ''}});
this.indexBuilder.enterDir('');
// We already read the configuration, so don't read it again
};
},

// Called after every doc
that.exitTree = function(dirName) {
indexBuilder.exitDir();
docStack.pop(); // for per-dir doc defaults
pm.exitDir();
exitTree: function(dirName) {
this.indexBuilder.exitDir();
this.docStack.pop(); // for per-dir doc defaults
this.pm.exitDir();

log.pop(); // indentation
log.info('Leaving source dir');

// Check invariants
assert.eq(0, that.dirStack.length);
assert.isTrue(pm.empty());
assert.isTrue(indexBuilder.empty());
};
assert.eq(0, this.dirStack.length);
assert.isTrue(this.pm.empty());
assert.isTrue(this.indexBuilder.empty());
},

// Called before entering each directory
// When we enter a directory, we look for:
// __config.json Contains variables and other config data
// __plugins.js Contains code
// _index.jsont Template to be expanded with child data
that.enterDir = function(dirName) {
enterDir: function(dirName) {
log.info('Entering dir: %s', dirName);
log.push(); // indentation

that.dirStack.push(dirName);
recompute(); // after modifying dirStack
this.dirStack.push(dirName);
this._recompute(); // after modifying dirStack

pm.enterDir(that.currentDir);
this.pm.enterDir(this.currentDir);

// The default destination dir is a "cleaned" source dir. Can be overidden
// in __config.json
docStack.push({dest: {dir: that.destDir}});
this.docStack.push({dest: {dir: this.destDir}});

var dirConfig = readConfig(sourceTree, that.currentDir),
var dirConfig = readConfig(this.sourceTree, this.currentDir),
vars = dirConfig.vars || {};
docStack.push(vars);
this.docStack.push(vars);

indexBuilder.enterDir(that.destDir);
};
this.indexBuilder.enterDir(this.destDir);
},

// Called after exiting each directory
that.exitDir = function() {
indexBuilder.exitDir();
docStack.pop(); // for per-dir doc defaults
docStack.pop(); // for per-dir __config
pm.exitDir();
exitDir: function() {
this.indexBuilder.exitDir();
this.docStack.pop(); // for per-dir doc defaults
this.docStack.pop(); // for per-dir __config
this.pm.exitDir();

that.dirStack.pop();
recompute(); // after modifying dirStack
this.dirStack.pop();
this._recompute(); // after modifying dirStack

log.pop(); // indentation
log.info('Leaving dir');
};
},

// Called for every file
that.onFile = function(filename) {
onFile: function(filename) {
// Directories starting with __ were already ignored by the walker
if (filename.slice(0, 2) == '__') {
log.info('Ignoring: %s', filename)
return; // EARLY RETURN
}

that.dirStack.push(filename);
this.dirStack.push(filename);

var ext = file.extension(filename),
relativePath = that.dirStack.join('/'),
isSource = checkIsSource(ext);
relativePath = this.dirStack.join('/'),
isSource = this._checkIsSource(ext);

that.dirStack.pop();
this.dirStack.pop();

if (!sourceFilter(relativePath)) {
if (!this.sourceFilter(relativePath)) {
return; // EARLY RETURN
}

Expand All @@ -291,66 +299,65 @@ var FileHandler = function(
log.info("Source file: %s", filename);
log.push();

var items = doc.readSource(sourceTree, relativePath);
var items = doc.readSource(this.sourceTree, relativePath);

// Default extension is HTML. Defaults can be overidden per doc.
docStack.push(
this.docStack.push(
{dest: {basename: filename.slice(0, -ext.length), ext: '.html'}}
);

// Push each in the array of items on the stack
for (var i = 0; i < items.length; i++) {
docStack.push(items[i]);
this.docStack.push(items[i]);
}

var numPushed = pm.onDoc(docStack);
var numPushed = this.pm.onDoc(this.docStack);

// TODO: --unhide flag
var skip = false;
if (docStack.get('.hidden')) {
if (this.docStack.get('.hidden')) {
log.info('Skipping hidden file %s', filename);
skip = true;
}

if (docStack.get('.index')) {
indexBuilder.onIndexDoc(relativePath);
if (this.docStack.get('.index')) {
this.indexBuilder.onIndexDoc(relativePath);
skip = true;
}

if (!skip) {
var error = buildDoc(docStack);
var error = this.buildDoc(this.docStack);
if (error) {
log.error('Error converting %s: %s', filename, error);
if (!options.get('keep-going')) {
if (!this.options.get('keep-going')) {
throw error;
}
}
that.numGenerated += 1;
indexBuilder.onDoc(items);
this.numGenerated += 1;
this.indexBuilder.onDoc(items);
}

// Now pop everything we pushed when evaluating plugins
for (var i = 0; i < numPushed; i++) {
docStack.pop();
this.docStack.pop();
}

// Pop the actual doc
for (var i = 0; i < items.length; i++) {
docStack.pop();
this.docStack.pop();
}

docStack.pop(); // for defaults
this.docStack.pop(); // for defaults

log.pop();

} else {
log.info("Copying: %s", relativePath);
copy(relativePath);
that.numCopied += 1;
this.copy(relativePath);
this.numCopied += 1;
}
};
}

return that;
};

// Copy a file from one tree to another.
Expand Down Expand Up @@ -501,7 +508,7 @@ exports.recipeApp = recipeApp = function(options) {

var indexBuilder = new doc.IndexBuilder(sourceTree, docStack, buildDoc);

var handler = FileHandler(
var handler = new FileHandler(
docStack, sourceTree, buildDoc, indexBuilder, copy, options);
oil.walk(sourceTree, handler, {ignoreDirs: /^__/});

Expand Down

0 comments on commit b8d6aee

Please sign in to comment.