mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-31 07:32:59 +00:00 
			
		
		
		
	Preparing to add support for JSON files loaded from TiddlySpace/TiddlyWeb
This commit is contained in:
		
							
								
								
									
										4
									
								
								cook.js
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								cook.js
									
									
									
									
									
								
							| @@ -3,7 +3,8 @@ | ||||
| // Usage: node cook.js <recipefile> | ||||
|  | ||||
| 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]; | ||||
|  | ||||
| @@ -12,4 +13,3 @@ var store = new TiddlyWiki(); | ||||
| var theRecipe = new Recipe(store,filename,function() { | ||||
| 	process.stdout.write(theRecipe.cook()); | ||||
| }); | ||||
|  | ||||
|   | ||||
| @@ -44,8 +44,9 @@ var ArgParser = function(argString,options) { | ||||
| 	var skipSpace = "(?:\\s*)"; | ||||
| 	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 match; | ||||
| 	do { | ||||
| 		var match = re.exec(argString); | ||||
| 		match = re.exec(argString); | ||||
| 		if(match) { | ||||
| 			var n = parseToken(match,1); | ||||
| 			if(options.noNames) { | ||||
|   | ||||
| @@ -16,7 +16,6 @@ var FileRetriever = exports; | ||||
| var fileRequestQueue = utils.queue(function(task,callback) { | ||||
| 	fs.readFile(task.filepath,"utf8", function(err,data) { | ||||
| 		callback(err,data); | ||||
| console.error("Retrieved " + task.filepath); | ||||
| 	}); | ||||
| },10); | ||||
|  | ||||
| @@ -31,11 +30,10 @@ var httpRequestQueue = utils.queue(function(task,callback) { | ||||
| 		} else { | ||||
| 			var data = []; | ||||
| 			res.on("data", function(chunk) { | ||||
| 				data.push(chunk) | ||||
| 				data.push(chunk); | ||||
| 			}); | ||||
| 			res.on("end", function() { | ||||
| 				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); | ||||
| 	} | ||||
| 	return result; | ||||
| } | ||||
| }; | ||||
|   | ||||
							
								
								
									
										59
									
								
								js/Recipe.js
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								js/Recipe.js
									
									
									
									
									
								
							| @@ -80,17 +80,28 @@ Recipe.prototype.processRecipe = function (data,contextPath) { | ||||
| 			if(marker === "recipe") { | ||||
| 				me.readRecipe(value,contextPath); | ||||
| 			} 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)) { | ||||
| 					me.ingredients[marker] = []; | ||||
| 				} | ||||
| 				var ingredientLocation = me.ingredients[marker].push(null) - 1; | ||||
| 				me.readIngredient(value,contextPath,function(fields) { | ||||
| 					var postProcess = me.readIngredientPostProcess[marker]; | ||||
| 					if(postProcess) | ||||
| 						fields = postProcess(fields); | ||||
| 					var ingredientTiddler = new Tiddler(fields); | ||||
| 					me.store.addTiddler(ingredientTiddler); | ||||
| 					me.ingredients[marker][ingredientLocation] = ingredientTiddler; | ||||
| 				me.readIngredient(value,contextPath,function(tiddlers) { | ||||
| 					for(var t=0; t<tiddlers.length; t++) { | ||||
| 						var fields = tiddlers[t]; | ||||
| 						var postProcess = me.readIngredientPostProcess[marker]; | ||||
| 						if(postProcess) { | ||||
| 							fields = postProcess(fields); | ||||
| 						} | ||||
| 						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) { | ||||
| 	var me = this; | ||||
| 	me.incFetchCount(); | ||||
| @@ -116,20 +128,25 @@ Recipe.prototype.readIngredient = function(filepath,contextPath,callback) { | ||||
| 		var fields = { | ||||
| 			title: rf.basename | ||||
| 		}; | ||||
| 		fields = tiddlerInput.parseTiddler(data,rf.extname,fields); | ||||
| 		var tiddlers = tiddlerInput.parseTiddlerFile(data,rf.extname,fields); | ||||
| 		// Check for the .meta file | ||||
| 		var metafile = filepath + ".meta"; | ||||
| 		me.incFetchCount(); | ||||
| 		retrieveFile(metafile,contextPath,function(err,data) { | ||||
| 			if(err && err.code !== "ENOENT" && err.code !== "404") { | ||||
| 				throw err; | ||||
| 			} | ||||
| 			if(!err) { | ||||
| 				fields = tiddlerInput.parseMetaDataBlock(data,fields); | ||||
| 			} | ||||
| 			callback(fields); | ||||
| 			me.decFetchCount(); | ||||
| 		}); | ||||
| 		if(rf.extname !== ".json" && tiddlers.length === 1) { | ||||
| 			var metafile = filepath + ".meta"; | ||||
| 			me.incFetchCount(); | ||||
| 			retrieveFile(metafile,contextPath,function(err,data) { | ||||
| 				if(err && err.code !== "ENOENT" && err.code !== "404") { | ||||
| 					throw err; | ||||
| 				} | ||||
| 				var fields = tiddlers[0]; | ||||
| 				if(!err) { | ||||
| 					fields = tiddlerInput.parseMetaDataBlock(data,fields); | ||||
| 				} | ||||
| 				callback([fields]); | ||||
| 				me.decFetchCount(); | ||||
| 			}); | ||||
| 		} else { | ||||
| 			callback(tiddlers); | ||||
| 		} | ||||
| 		me.decFetchCount(); | ||||
| 	}); | ||||
| } | ||||
|   | ||||
| @@ -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. | ||||
| */ | ||||
|  | ||||
| tiddlerInput.parseTiddler = function(text,type,fields) { | ||||
| tiddlerInput.parseTiddlerFile = function(text,type,fields) { | ||||
| 	if(fields === undefined) { | ||||
| 		var fields = {}; | ||||
| 	} | ||||
| @@ -25,13 +25,12 @@ tiddlerInput.parseTiddler = function(text,type,fields) { | ||||
| 	if(fileExtensionMapping) | ||||
| 		type = fileExtensionMapping; | ||||
| 	// Invoke the parser for the specified mimetype | ||||
| 	var parser = tiddlerInput.parseTiddlerByMimeType[type]; | ||||
| 	var parser = tiddlerInput.parseTiddlerFileByMimeType[type]; | ||||
| 	if(parser) { | ||||
| 		return parser(text,fields); | ||||
| 	} 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 = { | ||||
| @@ -39,21 +38,21 @@ tiddlerInput.fileExtensionMappings = { | ||||
| 	".html": "text/html", | ||||
| 	".tiddler": "application/x-tiddler-html-div", | ||||
| 	".tid": "application/x-tiddler", | ||||
| 	".js": "application/javascript" | ||||
| 	".js": "application/javascript", | ||||
| 	".json": "application/json" | ||||
| } | ||||
|  | ||||
| tiddlerInput.parseTiddlerByMimeType = { | ||||
| tiddlerInput.parseTiddlerFileByMimeType = { | ||||
| 	"text/plain": function(text,fields) { | ||||
| 		fields.text = text; | ||||
| 		return fields; | ||||
| 		return [fields]; | ||||
| 	}, | ||||
| 	"text/html": function(text,fields) { | ||||
| 		fields.text = text; | ||||
| 		return fields; | ||||
| 		return [fields]; | ||||
| 	}, | ||||
| 	"application/x-tiddler-html-div": function(text,fields) { | ||||
| 		fields = tiddlerInput.parseTiddlerDiv(text,fields); | ||||
| 		return fields; | ||||
| 		return [tiddlerInput.parseTiddlerDiv(text,fields)]; | ||||
| 	}, | ||||
| 	"application/x-tiddler": function(text,fields) { | ||||
| 		var split = text.indexOf("\n\n"); | ||||
| @@ -62,11 +61,14 @@ tiddlerInput.parseTiddlerByMimeType = { | ||||
| 		} | ||||
| 		fields = tiddlerInput.parseMetaDataBlock(text.substr(0,split),fields); | ||||
| 		fields.text = text.substr(split + 2); | ||||
| 		return fields; | ||||
| 		return [fields]; | ||||
| 	}, | ||||
| 	"application/javascript": function(text,fields) { | ||||
| 		fields.text = text; | ||||
| 		return fields; | ||||
| 		return [fields]; | ||||
| 	}, | ||||
| 	"application/json": function(text,fields) { | ||||
| 		 | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -122,8 +124,9 @@ tiddlerInput.parseTiddlerDiv = function(text,fields) { | ||||
| 		} else { | ||||
| 			fields.text = match[2];  | ||||
| 		} | ||||
| 		var attrMatch; | ||||
| 		do { | ||||
| 			var attrMatch = attrRegExp.exec(match[1]); | ||||
| 			attrMatch = attrRegExp.exec(match[1]); | ||||
| 			if(attrMatch) { | ||||
| 				var name = attrMatch[1]; | ||||
| 				var value = attrMatch[2]; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jeremy Ruston
					Jeremy Ruston