mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-08-07 14:23:53 +00:00
Refactorings to keep JSHint happy
This commit is contained in:
parent
d43efc5e18
commit
3f5912949f
@ -46,15 +46,15 @@ var ArgParser = function(argString,options) {
|
|||||||
skipSpace = "(?:\\s*)",
|
skipSpace = "(?:\\s*)",
|
||||||
token = "(?:" + dblQuote + "|" + sngQuote + "|" + dblSquare + "|" + dblBrace + "|" + unQuoted + "|" + emptyQuote + ")",
|
token = "(?:" + dblQuote + "|" + sngQuote + "|" + dblSquare + "|" + dblBrace + "|" + unQuoted + "|" + emptyQuote + ")",
|
||||||
re = options.noNames ? new RegExp(token,"mg") : new RegExp(skipSpace + token + skipSpace + "(?:(\\:)" + skipSpace + token + ")?","mg"),
|
re = options.noNames ? new RegExp(token,"mg") : new RegExp(skipSpace + token + skipSpace + "(?:(\\:)" + skipSpace + token + ")?","mg"),
|
||||||
match;
|
match,n,v;
|
||||||
do {
|
do {
|
||||||
match = re.exec(argString);
|
match = re.exec(argString);
|
||||||
if(match) {
|
if(match) {
|
||||||
var n = parseToken(match,1);
|
n = parseToken(match,1);
|
||||||
if(options.noNames) {
|
if(options.noNames) {
|
||||||
this.byPos.push({n:"", v:n});
|
this.byPos.push({n:"", v:n});
|
||||||
} else {
|
} else {
|
||||||
var v = parseToken(match,8);
|
v = parseToken(match,8);
|
||||||
if(v === null && options.defaultName) {
|
if(v === null && options.defaultName) {
|
||||||
v = n;
|
v = n;
|
||||||
n = options.defaultName;
|
n = options.defaultName;
|
||||||
@ -71,25 +71,25 @@ var ArgParser = function(argString,options) {
|
|||||||
} while(match);
|
} while(match);
|
||||||
this.byName = {};
|
this.byName = {};
|
||||||
for(var t=0; t<this.byPos.length; t++) {
|
for(var t=0; t<this.byPos.length; t++) {
|
||||||
var n = this.byPos[t].n,
|
n = this.byPos[t].n;
|
||||||
v = this.byPos[t].v;
|
v = this.byPos[t].v;
|
||||||
if(n in this.byName)
|
if(n in this.byName)
|
||||||
this.byName[n].push(v);
|
this.byName[n].push(v);
|
||||||
else
|
else
|
||||||
this.byName[n] = [v];
|
this.byName[n] = [v];
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Retrieve the first occurance of a named parameter, or the default if missing
|
// Retrieve the first occurance of a named parameter, or the default if missing
|
||||||
ArgParser.prototype.getValueByName = function(n,defaultValue) {
|
ArgParser.prototype.getValueByName = function(n,defaultValue) {
|
||||||
var v = this.byName[n];
|
var v = this.byName[n];
|
||||||
return v && v.length > 0 ? v[0] : defaultValue;
|
return v && v.length > 0 ? v[0] : defaultValue;
|
||||||
}
|
};
|
||||||
|
|
||||||
// Retrieve all the values of a named parameter as an array
|
// Retrieve all the values of a named parameter as an array
|
||||||
ArgParser.prototype.getValuesByName = function(n,defaultValue) {
|
ArgParser.prototype.getValuesByName = function(n,defaultValue) {
|
||||||
var v = this.byName[n];
|
var v = this.byName[n];
|
||||||
return v && v.length > 0 ? v : defaultValue;
|
return v && v.length > 0 ? v : defaultValue;
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.ArgParser = ArgParser
|
exports.ArgParser = ArgParser;
|
||||||
|
@ -40,7 +40,7 @@ var httpRequestQueue = utils.queue(function(task,callback) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
request.addListener("error", function(err) {
|
request.addListener("error", function(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
request.end();
|
request.end();
|
||||||
},4);
|
},4);
|
||||||
|
16
js/Recipe.js
16
js/Recipe.js
@ -47,19 +47,19 @@ var Recipe = function(store,filepath,callback) {
|
|||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.fetchCount = 0;
|
this.fetchCount = 0;
|
||||||
this.readRecipe(filepath,process.cwd()); // Read the recipe file
|
this.readRecipe(filepath,process.cwd()); // Read the recipe file
|
||||||
}
|
};
|
||||||
|
|
||||||
// The fetch counter is used to keep track of the number of asynchronous requests outstanding
|
// The fetch counter is used to keep track of the number of asynchronous requests outstanding
|
||||||
Recipe.prototype.incFetchCount = function() {
|
Recipe.prototype.incFetchCount = function() {
|
||||||
this.fetchCount++;
|
this.fetchCount++;
|
||||||
}
|
};
|
||||||
|
|
||||||
// When the fetch counter reaches zero, all the results are in, so invoke the recipe callback
|
// When the fetch counter reaches zero, all the results are in, so invoke the recipe callback
|
||||||
Recipe.prototype.decFetchCount = function() {
|
Recipe.prototype.decFetchCount = function() {
|
||||||
if(--this.fetchCount === 0) {
|
if(--this.fetchCount === 0) {
|
||||||
this.callback();
|
this.callback();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Process the contents of a recipe file
|
// Process the contents of a recipe file
|
||||||
Recipe.prototype.readRecipe = function(filepath,contextPath) {
|
Recipe.prototype.readRecipe = function(filepath,contextPath) {
|
||||||
@ -70,7 +70,7 @@ Recipe.prototype.readRecipe = function(filepath,contextPath) {
|
|||||||
me.processRecipe(data,rf.path);
|
me.processRecipe(data,rf.path);
|
||||||
me.decFetchCount();
|
me.decFetchCount();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
Recipe.prototype.processRecipe = function (data,contextPath) {
|
Recipe.prototype.processRecipe = function (data,contextPath) {
|
||||||
var me = this;
|
var me = this;
|
||||||
@ -108,7 +108,7 @@ Recipe.prototype.processRecipe = function (data,contextPath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// Special post-processing required for certain ingredient types
|
// Special post-processing required for certain ingredient types
|
||||||
Recipe.prototype.readIngredientPostProcess = {
|
Recipe.prototype.readIngredientPostProcess = {
|
||||||
@ -151,7 +151,7 @@ Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
|
|||||||
}
|
}
|
||||||
me.decFetchCount();
|
me.decFetchCount();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// Return a string of the cooked recipe
|
// Return a string of the cooked recipe
|
||||||
Recipe.prototype.cook = function() {
|
Recipe.prototype.cook = function() {
|
||||||
@ -169,7 +169,7 @@ Recipe.prototype.cook = function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
return out.join("\n");
|
return out.join("\n");
|
||||||
}
|
};
|
||||||
|
|
||||||
// Output all the tiddlers in the recipe with a particular marker
|
// Output all the tiddlers in the recipe with a particular marker
|
||||||
Recipe.prototype.outputIngredient = function(out,marker) {
|
Recipe.prototype.outputIngredient = function(out,marker) {
|
||||||
@ -179,7 +179,7 @@ Recipe.prototype.outputIngredient = function(out,marker) {
|
|||||||
if(outputter && ingredient) {
|
if(outputter && ingredient) {
|
||||||
outputter(out,ingredient);
|
outputter(out,ingredient);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// Allows for specialised processing for certain markers
|
// Allows for specialised processing for certain markers
|
||||||
Recipe.ingredientOutputMapper = {
|
Recipe.ingredientOutputMapper = {
|
||||||
|
@ -30,7 +30,7 @@ tiddlerInput.parseTiddlerFile = function(text,type,fields) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error("Unknown tiddler type in tiddlerInput.parseTiddlerFile: " + type);
|
throw new Error("Unknown tiddler type in tiddlerInput.parseTiddlerFile: " + type);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
tiddlerInput.fileExtensionMappings = {
|
tiddlerInput.fileExtensionMappings = {
|
||||||
".txt": "text/plain",
|
".txt": "text/plain",
|
||||||
@ -39,7 +39,7 @@ tiddlerInput.fileExtensionMappings = {
|
|||||||
".tid": "application/x-tiddler",
|
".tid": "application/x-tiddler",
|
||||||
".js": "application/javascript",
|
".js": "application/javascript",
|
||||||
".json": "application/json"
|
".json": "application/json"
|
||||||
}
|
};
|
||||||
|
|
||||||
tiddlerInput.parseTiddlerFileByMimeType = {
|
tiddlerInput.parseTiddlerFileByMimeType = {
|
||||||
"text/plain": function(text,fields) {
|
"text/plain": function(text,fields) {
|
||||||
@ -81,7 +81,7 @@ tiddlerInput.parseTiddlerFileByMimeType = {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Parse a block of metadata and merge the results into a hashmap of tiddler fields.
|
Parse a block of metadata and merge the results into a hashmap of tiddler fields.
|
||||||
@ -96,19 +96,22 @@ tags: browsers issues
|
|||||||
creator: psd
|
creator: psd
|
||||||
*/
|
*/
|
||||||
tiddlerInput.parseMetaDataBlock = function(metaData,fields) {
|
tiddlerInput.parseMetaDataBlock = function(metaData,fields) {
|
||||||
if(fields === undefined) {
|
var result = {};
|
||||||
var fields = {};
|
if(fields) {
|
||||||
|
for(var t in fields) {
|
||||||
|
result[t] = fields[t];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
metaData.split("\n").forEach(function(line) {
|
metaData.split("\n").forEach(function(line) {
|
||||||
var p = line.indexOf(":");
|
var p = line.indexOf(":");
|
||||||
if(p !== -1) {
|
if(p !== -1) {
|
||||||
var field = line.substr(0, p).trim();
|
var field = line.substr(0, p).trim();
|
||||||
var value = line.substr(p+1).trim();
|
var value = line.substr(p+1).trim();
|
||||||
fields[field] = value;
|
result[field] = value;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return fields;
|
return result;
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Parse an old-style tiddler DIV. It looks like this:
|
Parse an old-style tiddler DIV. It looks like this:
|
||||||
@ -149,4 +152,4 @@ tiddlerInput.parseTiddlerDiv = function(text,fields) {
|
|||||||
} while(attrMatch);
|
} while(attrMatch);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
@ -39,7 +39,7 @@ tiddlerOutput.outputTiddler = function(tid) {
|
|||||||
result.push("\n");
|
result.push("\n");
|
||||||
result.push(tid.fields.text);
|
result.push(tid.fields.text);
|
||||||
return result.join("");
|
return result.join("");
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Output a tiddler as an HTML <DIV>
|
Output a tiddler as an HTML <DIV>
|
||||||
@ -78,7 +78,7 @@ tiddlerOutput.outputTiddlerDiv = function(tid) {
|
|||||||
result.push(utils.htmlEncode(tid.fields.text));
|
result.push(utils.htmlEncode(tid.fields.text));
|
||||||
result.push("</pre>\n</div>");
|
result.push("</pre>\n</div>");
|
||||||
return result.join("");
|
return result.join("");
|
||||||
}
|
};
|
||||||
|
|
||||||
tiddlerOutput.stringifyTags = function(tags) {
|
tiddlerOutput.stringifyTags = function(tags) {
|
||||||
var results = [];
|
var results = [];
|
||||||
@ -90,6 +90,6 @@ tiddlerOutput.stringifyTags = function(tags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results.join(" ");
|
return results.join(" ");
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,20 +8,20 @@ var TiddlyWiki = function() {
|
|||||||
|
|
||||||
TiddlyWiki.prototype.clear = function() {
|
TiddlyWiki.prototype.clear = function() {
|
||||||
this.tiddlers = {};
|
this.tiddlers = {};
|
||||||
}
|
};
|
||||||
|
|
||||||
TiddlyWiki.prototype.fetchTiddler = function(title) {
|
TiddlyWiki.prototype.fetchTiddler = function(title) {
|
||||||
var t = this.tiddlers[title];
|
var t = this.tiddlers[title];
|
||||||
return t instanceof Tiddler ? t : null;
|
return t instanceof Tiddler ? t : null;
|
||||||
}
|
};
|
||||||
|
|
||||||
TiddlyWiki.prototype.deleteTiddler = function(title) {
|
TiddlyWiki.prototype.deleteTiddler = function(title) {
|
||||||
delete this.tiddlers[title];
|
delete this.tiddlers[title];
|
||||||
}
|
};
|
||||||
|
|
||||||
TiddlyWiki.prototype.addTiddler = function(tiddler) {
|
TiddlyWiki.prototype.addTiddler = function(tiddler) {
|
||||||
this.tiddlers[tiddler.title] = tiddler;
|
this.tiddlers[tiddler.title] = tiddler;
|
||||||
}
|
};
|
||||||
|
|
||||||
TiddlyWiki.prototype.forEachTiddler = function(callback) {
|
TiddlyWiki.prototype.forEachTiddler = function(callback) {
|
||||||
var t;
|
var t;
|
||||||
@ -30,6 +30,6 @@ TiddlyWiki.prototype.forEachTiddler = function(callback) {
|
|||||||
if(tiddler instanceof Tiddler)
|
if(tiddler instanceof Tiddler)
|
||||||
callback.call(this,t,tiddler);
|
callback.call(this,t,tiddler);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.TiddlyWiki = TiddlyWiki
|
exports.TiddlyWiki = TiddlyWiki;
|
||||||
|
@ -9,6 +9,25 @@ var tiddlerInput = require("./TiddlerInput.js"),
|
|||||||
|
|
||||||
var tiddlyWikiInput = exports;
|
var tiddlyWikiInput = exports;
|
||||||
|
|
||||||
|
function locateStoreArea(tiddlywikidoc)
|
||||||
|
{
|
||||||
|
var startSaveArea = '<div id="' + 'storeArea">',
|
||||||
|
startSaveAreaRegExp = /<div id=["']?storeArea['"]?>/gi,
|
||||||
|
endSaveArea = '</d' + 'iv>',
|
||||||
|
endSaveAreaCaps = '</D' + 'IV>',
|
||||||
|
posOpeningDiv = tiddlywikidoc.search(startSaveAreaRegExp),
|
||||||
|
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-STOREAREA--"+">");
|
||||||
|
if(limitClosingDiv == -1) {
|
||||||
|
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-BODY-START--"+">");
|
||||||
|
}
|
||||||
|
var start = limitClosingDiv == -1 ? tiddlywikidoc.length : limitClosingDiv,
|
||||||
|
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveArea,start);
|
||||||
|
if(posClosingDiv == -1) {
|
||||||
|
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveAreaCaps,start);
|
||||||
|
}
|
||||||
|
return (posOpeningDiv != -1 && posClosingDiv != -1) ? [posOpeningDiv + startSaveArea.length,posClosingDiv] : null;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Parses the text of a TiddlyWiki HTML file, and returns the tiddlers as an array of hashmaps of raw fields.
|
Parses the text of a TiddlyWiki HTML file, and returns the tiddlers as an array of hashmaps of raw fields.
|
||||||
|
|
||||||
@ -31,23 +50,4 @@ tiddlyWikiInput.parseTiddlyWiki = function(tiddlywikidoc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
};
|
||||||
|
|
||||||
function locateStoreArea(tiddlywikidoc)
|
|
||||||
{
|
|
||||||
var startSaveArea = '<div id="' + 'storeArea">',
|
|
||||||
startSaveAreaRegExp = /<div id=["']?storeArea['"]?>/gi,
|
|
||||||
endSaveArea = '</d' + 'iv>',
|
|
||||||
endSaveAreaCaps = '</D' + 'IV>',
|
|
||||||
posOpeningDiv = tiddlywikidoc.search(startSaveAreaRegExp),
|
|
||||||
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-STOREAREA--"+">");
|
|
||||||
if(limitClosingDiv == -1) {
|
|
||||||
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-BODY-START--"+">");
|
|
||||||
}
|
|
||||||
var start = limitClosingDiv == -1 ? tiddlywikidoc.length : limitClosingDiv,
|
|
||||||
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveArea,start);
|
|
||||||
if(posClosingDiv == -1) {
|
|
||||||
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveAreaCaps,start);
|
|
||||||
}
|
|
||||||
return (posOpeningDiv != -1 && posClosingDiv != -1) ? [posOpeningDiv + startSaveArea.length,posClosingDiv] : null;
|
|
||||||
}
|
|
||||||
|
58
js/Utils.js
58
js/Utils.js
@ -74,33 +74,33 @@ utils.htmlDecode = function(s)
|
|||||||
// });
|
// });
|
||||||
// q.push(taskData,callback) is used to queue a new task
|
// q.push(taskData,callback) is used to queue a new task
|
||||||
utils.queue = function(worker, concurrency) {
|
utils.queue = function(worker, concurrency) {
|
||||||
var workers = 0;
|
var workers = 0;
|
||||||
var q = {
|
var q = {
|
||||||
tasks: [],
|
tasks: [],
|
||||||
concurrency: concurrency,
|
concurrency: concurrency,
|
||||||
push: function (data, callback) {
|
push: function (data, callback) {
|
||||||
q.tasks.push({data: data, callback: callback});
|
q.tasks.push({data: data, callback: callback});
|
||||||
process.nextTick(q.process);
|
process.nextTick(q.process);
|
||||||
},
|
},
|
||||||
process: function () {
|
process: function () {
|
||||||
if (workers < q.concurrency && q.tasks.length) {
|
if (workers < q.concurrency && q.tasks.length) {
|
||||||
var task = q.tasks.shift();
|
var task = q.tasks.shift();
|
||||||
workers += 1;
|
workers += 1;
|
||||||
worker(task.data, function () {
|
worker(task.data, function () {
|
||||||
workers -= 1;
|
workers -= 1;
|
||||||
if (task.callback) {
|
if (task.callback) {
|
||||||
task.callback.apply(task, arguments);
|
task.callback.apply(task, arguments);
|
||||||
}
|
}
|
||||||
q.process();
|
q.process();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
length: function () {
|
length: function () {
|
||||||
return q.tasks.length;
|
return q.tasks.length;
|
||||||
},
|
},
|
||||||
running: function () {
|
running: function () {
|
||||||
return workers;
|
return workers;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return q;
|
return q;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user