1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 01:20:30 +00:00

More fixes for recipe directory handling

Done some variable name refactoring to make things clearer
This commit is contained in:
Jeremy Ruston 2012-01-21 17:08:07 +00:00
parent 33283f71e1
commit 17826a72fb
2 changed files with 25 additions and 25 deletions

View File

@ -68,30 +68,30 @@ var httpRequest = function(fileurl,callback) {
request.end(); request.end();
}; };
// Retrieve a file given a filepath specifier and a context path. If the filepath isn't an absolute // Retrieve a file given a filepath specifier and a base directory. If the filepath isn't an absolute
// filepath or an absolute URL, then it is interpreted relative to the context path, which can also be // filepath or an absolute URL, then it is interpreted relative to the base directory, which can also be
// a filepath or a URL. On completion, the callback function is called as callback(err,data). The // a local directory or a URL. On completion, the callback function is called as callback(err,data). The
// data hashmap is as follows: // data hashmap is as follows:
// text: full text of file // text: full text of file
// path: full path used to reach the file // path: full path used to reach the file
// basename: the basename of the file // basename: the basename of the file
// extname: the extension of the file // extname: the extension of the file
FileRetriever.retrieveFile = function(filepath,contextPath,callback) { FileRetriever.retrieveFile = function(filepath,baseDir,callback) {
var httpRegExp = /^(https?:\/\/)/gi, var httpRegExp = /^(https?:\/\/)/gi,
result = {}, result = {},
filepathIsHttp = httpRegExp.test(filepath), filepathIsHttp = httpRegExp.test(filepath),
contextPathIsHttp = httpRegExp.test(contextPath), baseDirIsHttp = httpRegExp.test(baseDir),
requester; requester;
if(contextPathIsHttp || filepathIsHttp) { if(baseDirIsHttp || filepathIsHttp) {
// If we've got a full HTTP URI then we're good to go // If we've got a full HTTP URI then we're good to go
result.path = url.resolve(contextPath,filepath); result.path = url.resolve(baseDir,filepath);
var parsedPath = url.parse(result.path); var parsedPath = url.parse(result.path);
result.extname = path.extname(parsedPath.pathname); result.extname = path.extname(parsedPath.pathname);
result.basename = path.basename(parsedPath.extname); result.basename = path.basename(parsedPath.extname);
requester = httpRequest; requester = httpRequest;
} else { } else {
// It's a file requested in a file context // It's a file requested in a file context
result.path = path.resolve(contextPath,filepath); result.path = path.resolve(baseDir,filepath);
result.extname = path.extname(result.path); result.extname = path.extname(result.path);
result.basename = path.basename(result.path,result.extname); result.basename = path.basename(result.path,result.extname);
requester = fileRequest; requester = fileRequest;

View File

@ -6,13 +6,13 @@ Recipe processing is in four parts:
1) The recipe file is parsed and any subrecipe files loaded recursively into this structure: 1) The recipe file is parsed and any subrecipe files loaded recursively into this structure:
this.recipe = [ this.recipe = [
{marker: <marker>, filepath: <filepath>, contextPath: <contextPath>}, {marker: <marker>, filepath: <filepath>, baseDir: <baseDir>},
... ...
{marker: <marker>, filepath: <filepath>, contextPath: <contextPath>}, {marker: <marker>, filepath: <filepath>, baseDir: <baseDir>},
[ [
{marker: <marker>, filepath: <filepath>, contextPath: <contextPath>}, {marker: <marker>, filepath: <filepath>, baseDir: <baseDir>},
... ...
{marker: <marker>, filepath: <filepath>, contextPath: <contextPath>}, {marker: <marker>, filepath: <filepath>, baseDir: <baseDir>},
] ]
]; ];
@ -66,18 +66,18 @@ var Recipe = function(options,callback) {
this.markers = {}; this.markers = {};
// A task queue for loading recipe files // A task queue for loading recipe files
this.recipeQueue = async.queue(function(task,callback) { this.recipeQueue = async.queue(function(task,callback) {
retrieveFile(task.filepath,task.contextPath,function(err,data) { retrieveFile(task.filepath,task.baseDir,function(err,data) {
if(err) { if(err) {
me.callback(err); me.callback(err);
} else { } else {
me.processRecipeFile(task.recipe,data.text,path.dirname(data.path)); me.processRecipeFile(task.recipe,data.text,data.path);
callback(null); callback(null);
} }
}); });
},1); },1);
// A task queue for loading tiddler files // A task queue for loading tiddler files
this.tiddlerQueue = async.queue(function(task,callback) { this.tiddlerQueue = async.queue(function(task,callback) {
me.readTiddlerFile(task.filepath,task.contextPath,function(err,data) { me.readTiddlerFile(task.filepath,task.baseDir,function(err,data) {
if(err) { if(err) {
callback(err); callback(err);
} else { } else {
@ -113,7 +113,7 @@ var Recipe = function(options,callback) {
}; };
// Start the process off by queueing up the loading of the initial recipe // Start the process off by queueing up the loading of the initial recipe
this.recipeQueue.push({filepath: this.filepath, this.recipeQueue.push({filepath: this.filepath,
contextPath: process.cwd(), baseDir: process.cwd(),
recipe: this.recipe}); recipe: this.recipe});
}; };
@ -133,18 +133,18 @@ Recipe.prototype.loadTiddlerFiles = function(recipeLine) {
posStar = filename.indexOf("*"); posStar = filename.indexOf("*");
if(posStar !== -1) { if(posStar !== -1) {
var fileRegExp = new RegExp("^" + filename.replace(/[\-\[\]{}()+?.,\\\^$|#\s]/g, "\\$&").replace("*",".*") + "$"); var fileRegExp = new RegExp("^" + filename.replace(/[\-\[\]{}()+?.,\\\^$|#\s]/g, "\\$&").replace("*",".*") + "$");
var files = fs.readdirSync(path.resolve(path.dirname(recipeLine.contextPath),filedir)); var files = fs.readdirSync(path.resolve(recipeLine.baseDir,filedir));
for(var f=0; f<files.length; f++) { for(var f=0; f<files.length; f++) {
if(fileRegExp.test(files[f])) { if(fileRegExp.test(files[f])) {
me.tiddlerQueue.push({ me.tiddlerQueue.push({
filepath: filedir + "/" + files[f], filepath: filedir + "/" + files[f],
contextPath: recipeLine.contextPath, baseDir: recipeLine.baseDir,
recipeLine: recipeLine recipeLine: recipeLine
}); });
} }
} }
} else { } else {
me.tiddlerQueue.push({filepath: filepath, contextPath: recipeLine.contextPath, recipeLine: recipeLine}); me.tiddlerQueue.push({filepath: filepath, baseDir: recipeLine.baseDir, recipeLine: recipeLine});
} }
} }
}; };
@ -199,7 +199,7 @@ Recipe.prototype.sortTiddlersForMarker = function(marker) {
/* /*
Process the contents of a recipe file Process the contents of a recipe file
*/ */
Recipe.prototype.processRecipeFile = function(recipe,text,contextPath) { Recipe.prototype.processRecipeFile = function(recipe,text,recipePath) {
var matchLine = function(linetext) { var matchLine = function(linetext) {
var lineRegExp = /^(#?)(\s*)(#?)([^\s\:]+)\s*:\s*(.+)*\s*$/, var lineRegExp = /^(#?)(\s*)(#?)([^\s\:]+)\s*:\s*(.+)*\s*$/,
match = lineRegExp.exec(linetext); match = lineRegExp.exec(linetext);
@ -223,7 +223,7 @@ Recipe.prototype.processRecipeFile = function(recipe,text,contextPath) {
var insertionPoint = recipe.push([]) - 1; var insertionPoint = recipe.push([]) - 1;
this.recipeQueue.push({ this.recipeQueue.push({
filepath: match.value, filepath: match.value,
contextPath: contextPath, baseDir: path.dirname(recipePath),
recipe: recipe[insertionPoint] recipe: recipe[insertionPoint]
}); });
} else { } else {
@ -237,7 +237,7 @@ Recipe.prototype.processRecipeFile = function(recipe,text,contextPath) {
if(fieldLines.length > 0) { if(fieldLines.length > 0) {
fields = this.store.deserializeTiddlers("application/x-tiddler",fieldLines.join("\n"),{})[0]; fields = this.store.deserializeTiddlers("application/x-tiddler",fieldLines.join("\n"),{})[0];
} }
recipe.push({marker: match.marker, filepath: match.value, contextPath: contextPath, fields: fields}); recipe.push({marker: match.marker, filepath: match.value, baseDir: path.dirname(recipePath), fields: fields});
} }
} }
} }
@ -245,10 +245,10 @@ Recipe.prototype.processRecipeFile = function(recipe,text,contextPath) {
// Read a tiddler file and callback with an array of hashmaps of tiddler fields. For single // Read a tiddler file and callback with an array of hashmaps of tiddler fields. For single
// tiddler files it also looks for an accompanying .meta file // tiddler files it also looks for an accompanying .meta file
Recipe.prototype.readTiddlerFile = function(filepath,contextPath,callback) { Recipe.prototype.readTiddlerFile = function(filepath,baseDir,callback) {
var me = this; var me = this;
// Read the tiddler file // Read the tiddler file
retrieveFile(filepath,contextPath,function(err,data) { retrieveFile(filepath,baseDir,function(err,data) {
if (err) throw err; if (err) throw err;
// Use the filepath as the default title for the tiddler // Use the filepath as the default title for the tiddler
var fields = { var fields = {
@ -258,7 +258,7 @@ Recipe.prototype.readTiddlerFile = function(filepath,contextPath,callback) {
// Check for the .meta file // Check for the .meta file
if(data.extname !== ".json" && tiddlers.length === 1) { if(data.extname !== ".json" && tiddlers.length === 1) {
var metafile = filepath + ".meta"; var metafile = filepath + ".meta";
retrieveFile(metafile,contextPath,function(err,data) { retrieveFile(metafile,baseDir,function(err,data) {
if(err && err.code !== "ENOENT" && err.code !== "404") { if(err && err.code !== "ENOENT" && err.code !== "404") {
callback(err); callback(err);
} else { } else {