1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Preparing to add support for JSON files loaded from TiddlySpace/TiddlyWeb

This commit is contained in:
Jeremy Ruston 2011-11-30 16:06:34 +00:00
parent 769b28b548
commit 80f4250a62
5 changed files with 60 additions and 41 deletions

View File

@ -3,7 +3,8 @@
// Usage: node cook.js <recipefile> // Usage: node cook.js <recipefile>
var TiddlyWiki = require("./js/TiddlyWiki.js").TiddlyWiki, var TiddlyWiki = require("./js/TiddlyWiki.js").TiddlyWiki,
Recipe = require("./js/Recipe.js").Recipe; Recipe = require("./js/Recipe.js").Recipe,
util = require("util");
var filename = process.argv[2]; var filename = process.argv[2];
@ -12,4 +13,3 @@ var store = new TiddlyWiki();
var theRecipe = new Recipe(store,filename,function() { var theRecipe = new Recipe(store,filename,function() {
process.stdout.write(theRecipe.cook()); process.stdout.write(theRecipe.cook());
}); });

View File

@ -44,8 +44,9 @@ var ArgParser = function(argString,options) {
var skipSpace = "(?:\\s*)"; var skipSpace = "(?:\\s*)";
var token = "(?:" + dblQuote + "|" + sngQuote + "|" + dblSquare + "|" + dblBrace + "|" + unQuoted + "|" + emptyQuote + ")"; var token = "(?:" + dblQuote + "|" + sngQuote + "|" + dblSquare + "|" + dblBrace + "|" + unQuoted + "|" + emptyQuote + ")";
var re = options.noNames ? new RegExp(token,"mg") : new RegExp(skipSpace + token + skipSpace + "(?:(\\:)" + skipSpace + token + ")?","mg"); var re = options.noNames ? new RegExp(token,"mg") : new RegExp(skipSpace + token + skipSpace + "(?:(\\:)" + skipSpace + token + ")?","mg");
var match;
do { do {
var match = re.exec(argString); match = re.exec(argString);
if(match) { if(match) {
var n = parseToken(match,1); var n = parseToken(match,1);
if(options.noNames) { if(options.noNames) {

View File

@ -16,7 +16,6 @@ var FileRetriever = exports;
var fileRequestQueue = utils.queue(function(task,callback) { var fileRequestQueue = utils.queue(function(task,callback) {
fs.readFile(task.filepath,"utf8", function(err,data) { fs.readFile(task.filepath,"utf8", function(err,data) {
callback(err,data); callback(err,data);
console.error("Retrieved " + task.filepath);
}); });
},10); },10);
@ -31,11 +30,10 @@ var httpRequestQueue = utils.queue(function(task,callback) {
} else { } else {
var data = []; var data = [];
res.on("data", function(chunk) { res.on("data", function(chunk) {
data.push(chunk) data.push(chunk);
}); });
res.on("end", function() { res.on("end", function() {
callback(null,data.join("")); callback(null,data.join(""));
console.error("Retrieved " + task.url);
}); });
} }
}); });
@ -72,4 +70,4 @@ FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
fileRequestQueue.push({filepath: result.path},callback); fileRequestQueue.push({filepath: result.path},callback);
} }
return result; return result;
} };

View File

@ -80,17 +80,28 @@ Recipe.prototype.processRecipe = function (data,contextPath) {
if(marker === "recipe") { if(marker === "recipe") {
me.readRecipe(value,contextPath); me.readRecipe(value,contextPath);
} else { } else {
// Reserve a place in the ingredients array for this ingredient, just to keep tiddler ordering
// compatible with cook.rb
if(!(marker in me.ingredients)) { if(!(marker in me.ingredients)) {
me.ingredients[marker] = []; me.ingredients[marker] = [];
} }
var ingredientLocation = me.ingredients[marker].push(null) - 1; var ingredientLocation = me.ingredients[marker].push(null) - 1;
me.readIngredient(value,contextPath,function(fields) { me.readIngredient(value,contextPath,function(tiddlers) {
var postProcess = me.readIngredientPostProcess[marker]; for(var t=0; t<tiddlers.length; t++) {
if(postProcess) var fields = tiddlers[t];
fields = postProcess(fields); var postProcess = me.readIngredientPostProcess[marker];
var ingredientTiddler = new Tiddler(fields); if(postProcess) {
me.store.addTiddler(ingredientTiddler); fields = postProcess(fields);
me.ingredients[marker][ingredientLocation] = ingredientTiddler; }
var ingredientTiddler = new Tiddler(fields);
me.store.addTiddler(ingredientTiddler);
if(ingredientLocation !== -1) {
me.ingredients[marker][ingredientLocation] = ingredientTiddler;
ingredientLocation = -1;
} else {
me.ingredients[marker].push(ingredientTiddler);
}
}
}); });
} }
} }
@ -106,7 +117,8 @@ 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 callback with an array of hashmaps of tiddler fields. For single
// tiddler files it also looks for an accompanying .meta file
Recipe.prototype.readIngredient = function(filepath,contextPath,callback) { Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
var me = this; var me = this;
me.incFetchCount(); me.incFetchCount();
@ -116,20 +128,25 @@ Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
var fields = { var fields = {
title: rf.basename title: rf.basename
}; };
fields = tiddlerInput.parseTiddler(data,rf.extname,fields); var tiddlers = tiddlerInput.parseTiddlerFile(data,rf.extname,fields);
// Check for the .meta file // Check for the .meta file
var metafile = filepath + ".meta"; if(rf.extname !== ".json" && tiddlers.length === 1) {
me.incFetchCount(); var metafile = filepath + ".meta";
retrieveFile(metafile,contextPath,function(err,data) { me.incFetchCount();
if(err && err.code !== "ENOENT" && err.code !== "404") { retrieveFile(metafile,contextPath,function(err,data) {
throw err; if(err && err.code !== "ENOENT" && err.code !== "404") {
} throw err;
if(!err) { }
fields = tiddlerInput.parseMetaDataBlock(data,fields); var fields = tiddlers[0];
} if(!err) {
callback(fields); fields = tiddlerInput.parseMetaDataBlock(data,fields);
me.decFetchCount(); }
}); callback([fields]);
me.decFetchCount();
});
} else {
callback(tiddlers);
}
me.decFetchCount(); me.decFetchCount();
}); });
} }

View File

@ -16,7 +16,7 @@ For example ".txt" file extension is mapped to the "text/plain" mimetype.
Special processing to extract embedded metadata is applied to some mimetypes. Special processing to extract embedded metadata is applied to some mimetypes.
*/ */
tiddlerInput.parseTiddler = function(text,type,fields) { tiddlerInput.parseTiddlerFile = function(text,type,fields) {
if(fields === undefined) { if(fields === undefined) {
var fields = {}; var fields = {};
} }
@ -25,13 +25,12 @@ tiddlerInput.parseTiddler = function(text,type,fields) {
if(fileExtensionMapping) if(fileExtensionMapping)
type = fileExtensionMapping; type = fileExtensionMapping;
// Invoke the parser for the specified mimetype // Invoke the parser for the specified mimetype
var parser = tiddlerInput.parseTiddlerByMimeType[type]; var parser = tiddlerInput.parseTiddlerFileByMimeType[type];
if(parser) { if(parser) {
return parser(text,fields); return parser(text,fields);
} else { } else {
throw new Error("Unknown tiddler type in tiddlerInput.parseTiddler: " + type); throw new Error("Unknown tiddler type in tiddlerInput.parseTiddlerFile: " + type);
} }
return fields;
} }
tiddlerInput.fileExtensionMappings = { tiddlerInput.fileExtensionMappings = {
@ -39,21 +38,21 @@ tiddlerInput.fileExtensionMappings = {
".html": "text/html", ".html": "text/html",
".tiddler": "application/x-tiddler-html-div", ".tiddler": "application/x-tiddler-html-div",
".tid": "application/x-tiddler", ".tid": "application/x-tiddler",
".js": "application/javascript" ".js": "application/javascript",
".json": "application/json"
} }
tiddlerInput.parseTiddlerByMimeType = { tiddlerInput.parseTiddlerFileByMimeType = {
"text/plain": function(text,fields) { "text/plain": function(text,fields) {
fields.text = text; fields.text = text;
return fields; return [fields];
}, },
"text/html": function(text,fields) { "text/html": function(text,fields) {
fields.text = text; fields.text = text;
return fields; return [fields];
}, },
"application/x-tiddler-html-div": function(text,fields) { "application/x-tiddler-html-div": function(text,fields) {
fields = tiddlerInput.parseTiddlerDiv(text,fields); return [tiddlerInput.parseTiddlerDiv(text,fields)];
return fields;
}, },
"application/x-tiddler": function(text,fields) { "application/x-tiddler": function(text,fields) {
var split = text.indexOf("\n\n"); var split = text.indexOf("\n\n");
@ -62,11 +61,14 @@ tiddlerInput.parseTiddlerByMimeType = {
} }
fields = tiddlerInput.parseMetaDataBlock(text.substr(0,split),fields); fields = tiddlerInput.parseMetaDataBlock(text.substr(0,split),fields);
fields.text = text.substr(split + 2); fields.text = text.substr(split + 2);
return fields; return [fields];
}, },
"application/javascript": function(text,fields) { "application/javascript": function(text,fields) {
fields.text = text; fields.text = text;
return fields; return [fields];
},
"application/json": function(text,fields) {
} }
} }
@ -122,8 +124,9 @@ tiddlerInput.parseTiddlerDiv = function(text,fields) {
} else { } else {
fields.text = match[2]; fields.text = match[2];
} }
var attrMatch;
do { do {
var attrMatch = attrRegExp.exec(match[1]); attrMatch = attrRegExp.exec(match[1]);
if(attrMatch) { if(attrMatch) {
var name = attrMatch[1]; var name = attrMatch[1];
var value = attrMatch[2]; var value = attrMatch[2];