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