mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Refactored return value of retrieveFile()
This commit is contained in:
parent
0ae98bcd88
commit
82f4f34059
@ -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
|
// 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
|
// 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
|
// a filepath or a URL. On completion, the callback function is called as callback(err,data). It
|
||||||
// function is called as callback(err,data)
|
// 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) {
|
FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
|
||||||
var httpRegExp = /^(https?:\/\/)/gi,
|
var httpRegExp = /^(https?:\/\/)/gi,
|
||||||
newpath,
|
result = {},
|
||||||
filepathIsHttp = httpRegExp.test(filepath),
|
filepathIsHttp = httpRegExp.test(filepath),
|
||||||
contextPathIsHttp = httpRegExp.test(contextPath);
|
contextPathIsHttp = httpRegExp.test(contextPath);
|
||||||
if(contextPathIsHttp || filepathIsHttp) {
|
if(contextPathIsHttp || 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
|
||||||
newpath = url.resolve(contextPath,filepath);
|
result.path = url.resolve(contextPath,filepath);
|
||||||
httpRequestQueue.push({url: newpath},callback);
|
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 {
|
} else {
|
||||||
// It's a file requested in a file context
|
// It's a file requested in a file context
|
||||||
newpath = path.resolve(path.dirname(contextPath),filepath);
|
result.path = path.resolve(path.dirname(contextPath),filepath);
|
||||||
fileRequestQueue.push({filepath: newpath},callback);
|
result.extname = path.extname(result.path);
|
||||||
|
result.basename = path.basename(result.path,result.extname);
|
||||||
|
fileRequestQueue.push({filepath: result.path},callback);
|
||||||
}
|
}
|
||||||
return newpath;
|
return result;
|
||||||
}
|
}
|
||||||
|
21
js/Recipe.js
21
js/Recipe.js
@ -63,9 +63,9 @@ Recipe.prototype.decFetchCount = function() {
|
|||||||
Recipe.prototype.readRecipe = function(filepath,contextPath) {
|
Recipe.prototype.readRecipe = function(filepath,contextPath) {
|
||||||
var me = this;
|
var me = this;
|
||||||
this.incFetchCount();
|
this.incFetchCount();
|
||||||
var actualPath = retrieveFile(filepath, contextPath, function(err, data) {
|
var rf = retrieveFile(filepath, contextPath, function(err, data) {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
me.processRecipe(data,actualPath);
|
me.processRecipe(data,rf.path);
|
||||||
me.decFetchCount();
|
me.decFetchCount();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -83,8 +83,7 @@ Recipe.prototype.processRecipe = function (data,contextPath) {
|
|||||||
if(!(marker in me.ingredients)) {
|
if(!(marker in me.ingredients)) {
|
||||||
me.ingredients[marker] = [];
|
me.ingredients[marker] = [];
|
||||||
}
|
}
|
||||||
var ingredientLocation = me.ingredients[marker].length;
|
var ingredientLocation = me.ingredients[marker].push(null) - 1;
|
||||||
me.ingredients[marker][ingredientLocation] = null;
|
|
||||||
me.readIngredient(value,contextPath,function(fields) {
|
me.readIngredient(value,contextPath,function(fields) {
|
||||||
var postProcess = me.readIngredientPostProcess[marker];
|
var postProcess = me.readIngredientPostProcess[marker];
|
||||||
if(postProcess)
|
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
|
// 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) {
|
Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
|
||||||
var me = this,
|
var me = this;
|
||||||
extname = path.extname(filepath),
|
|
||||||
basename = path.basename(filepath,extname),
|
|
||||||
fields = {
|
|
||||||
title: basename
|
|
||||||
};
|
|
||||||
me.incFetchCount();
|
me.incFetchCount();
|
||||||
// Read the tiddler file
|
// Read the tiddler file
|
||||||
retrieveFile(filepath,contextPath,function(err,data) {
|
var rf = retrieveFile(filepath,contextPath,function(err,data) {
|
||||||
if (err) throw err;
|
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
|
// Check for the .meta file
|
||||||
var metafile = filepath + ".meta";
|
var metafile = filepath + ".meta";
|
||||||
me.incFetchCount();
|
me.incFetchCount();
|
||||||
|
Loading…
Reference in New Issue
Block a user