Refactored return value of retrieveFile()

This commit is contained in:
Jeremy Ruston 2011-11-30 11:41:26 +00:00
parent 0ae98bcd88
commit 82f4f34059
2 changed files with 25 additions and 20 deletions

View File

@ -47,21 +47,29 @@ console.error("Retrieved " + task.url);
// Retrieve a file given a filepath specifier and a context path. 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
// a filepath or a URL. It returns the final path used to reach the file. On completion, the callback
// function is called as callback(err,data)
// a filepath or a URL. On completion, the callback function is called as callback(err,data). It
// returns an object:
// path: full path used to reach the file
// basename: the basename of the file (used as the default tiddler title)
// extname: the extension of the file
FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
var httpRegExp = /^(https?:\/\/)/gi,
newpath,
result = {},
filepathIsHttp = httpRegExp.test(filepath),
contextPathIsHttp = httpRegExp.test(contextPath);
if(contextPathIsHttp || filepathIsHttp) {
// If we've got a full HTTP URI then we're good to go
newpath = url.resolve(contextPath,filepath);
httpRequestQueue.push({url: newpath},callback);
result.path = url.resolve(contextPath,filepath);
var parsedPath = url.parse(result.path);
result.extname = path.extname(parsedPath.pathname);
result.basename = path.basename(parsedPath.extname);
httpRequestQueue.push({url: result.path},callback);
} else {
// It's a file requested in a file context
newpath = path.resolve(path.dirname(contextPath),filepath);
fileRequestQueue.push({filepath: newpath},callback);
result.path = path.resolve(path.dirname(contextPath),filepath);
result.extname = path.extname(result.path);
result.basename = path.basename(result.path,result.extname);
fileRequestQueue.push({filepath: result.path},callback);
}
return newpath;
return result;
}

View File

@ -63,9 +63,9 @@ Recipe.prototype.decFetchCount = function() {
Recipe.prototype.readRecipe = function(filepath,contextPath) {
var me = this;
this.incFetchCount();
var actualPath = retrieveFile(filepath, contextPath, function(err, data) {
var rf = retrieveFile(filepath, contextPath, function(err, data) {
if (err) throw err;
me.processRecipe(data,actualPath);
me.processRecipe(data,rf.path);
me.decFetchCount();
});
}
@ -83,8 +83,7 @@ Recipe.prototype.processRecipe = function (data,contextPath) {
if(!(marker in me.ingredients)) {
me.ingredients[marker] = [];
}
var ingredientLocation = me.ingredients[marker].length;
me.ingredients[marker][ingredientLocation] = null;
var ingredientLocation = me.ingredients[marker].push(null) - 1;
me.readIngredient(value,contextPath,function(fields) {
var postProcess = me.readIngredientPostProcess[marker];
if(postProcess)
@ -109,17 +108,15 @@ Recipe.prototype.readIngredientPostProcess = {
// Read an ingredient file and return it as a hashmap of tiddler fields. Also read the .meta file, if present
Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
var me = this,
extname = path.extname(filepath),
basename = path.basename(filepath,extname),
fields = {
title: basename
};
var me = this;
me.incFetchCount();
// Read the tiddler file
retrieveFile(filepath,contextPath,function(err,data) {
var rf = retrieveFile(filepath,contextPath,function(err,data) {
if (err) throw err;
fields = tiddlerInput.parseTiddler(data,extname,fields);
var fields = {
title: rf.basename
};
fields = tiddlerInput.parseTiddler(data,rf.extname,fields);
// Check for the .meta file
var metafile = filepath + ".meta";
me.incFetchCount();