From d23fbe5ef14a724247f7e56d5d635c4aba603266 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 29 May 2012 22:01:52 +0100 Subject: [PATCH] Added support for TiddlyWiki old school .recipe files --- core/modules/recipe.js | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 core/modules/recipe.js diff --git a/core/modules/recipe.js b/core/modules/recipe.js new file mode 100644 index 000000000..724f8bbdf --- /dev/null +++ b/core/modules/recipe.js @@ -0,0 +1,69 @@ +/*\ +title: $:/core/modules/recipe.js +type: application/javascript +module-type: tiddlerdeserializer + +Plugin to deserialize tiddlers from an old school TiddlyWiki recipe file. + +The idea is to process the recipe file recursively, loading tiddlers into the main store using synchronous file operations. The tiddlers have their titles prefixed with the associated marker in curly brackets ("{shadow}", "{tiddler}" etc). + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports["application/x-tiddlywiki-recipe"] = function(text,fields) { + var path = require("path"), + fs = require("fs"), + tiddlers = [], + parseRecipe = function(text) { + var recipe = []; + text.toString().split(/\r?\n/mg).forEach(function(line) { + var p = line.indexOf(":"); + if(p !== -1) { + recipe.push({ + name: line.substr(0,p).trim(), + value: line.substr(p+1).trim() + }); + } + }); + return recipe; + }, + loadTiddlersFromFile = function(sourcePath,prefix) { + var ext = path.extname(sourcePath), + extensionInfo = $tw.config.fileExtensions[ext], + data = fs.readFileSync(sourcePath).toString(extensionInfo ? extensionInfo.encoding : "utf8"), + fields = {title: sourcePath}, + tids = $tw.wiki.deserializeTiddlers(ext,data,fields), + metafile = sourcePath + ".meta"; + if(ext !== ".json" && tids.length === 1 && path.existsSync(metafile)) { + var metadata = fs.readFileSync(metafile).toString("utf8"); + if(metadata) { + tids = [$tw.utils.parseFields(metadata,tids[0])]; + } + } + tids.forEach(function(tid) { + tid.title = prefix + tid.title; + }); + tiddlers.push.apply(tiddlers,tids); + }, + processRecipe = function(sourcePath,text) { + var recipe = parseRecipe(text); + for(var t=0; t