2012-05-29 21:01:52 +00:00
|
|
|
/*\
|
2013-08-04 21:06:36 +00:00
|
|
|
title: $:/plugins/tiddlywiki/classictools/recipe.js
|
2012-05-29 21:01:52 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: tiddlerdeserializer
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
Module to deserialize tiddlers from an old school TiddlyWiki recipe file.
|
2012-05-29 21:01:52 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2013-05-13 16:42:07 +00:00
|
|
|
exports["text/vnd.tiddlywiki2-recipe"] = function(text,fields) {
|
2012-06-14 10:36:10 +00:00
|
|
|
var self = this,
|
|
|
|
path = require("path"),
|
2012-05-29 21:01:52 +00:00
|
|
|
fs = require("fs"),
|
|
|
|
tiddlers = [],
|
|
|
|
parseRecipe = function(text) {
|
|
|
|
var recipe = [];
|
|
|
|
text.toString().split(/\r?\n/mg).forEach(function(line) {
|
2013-03-08 16:10:20 +00:00
|
|
|
// Check if the line is a comment
|
|
|
|
if(line.charAt(0) !== "#") {
|
|
|
|
// Find the colon splitting the name from the value
|
|
|
|
var p = line.indexOf(":");
|
|
|
|
if(p !== -1) {
|
|
|
|
recipe.push({
|
|
|
|
name: line.substr(0,p).trim(),
|
|
|
|
value: line.substr(p+1).trim()
|
|
|
|
});
|
|
|
|
}
|
2012-05-29 21:01:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return recipe;
|
|
|
|
},
|
|
|
|
loadTiddlersFromFile = function(sourcePath,prefix) {
|
|
|
|
var ext = path.extname(sourcePath),
|
2012-06-10 12:40:35 +00:00
|
|
|
extensionInfo = $tw.config.fileExtensionInfo[ext],
|
2012-06-08 10:47:05 +00:00
|
|
|
typeInfo = extensionInfo ? $tw.config.contentTypeInfo[extensionInfo.type] : null,
|
2013-03-01 09:13:44 +00:00
|
|
|
data = fs.readFileSync(sourcePath,typeInfo ? typeInfo.encoding : "utf8"),
|
2012-05-29 21:01:52 +00:00
|
|
|
fields = {title: sourcePath},
|
2012-06-14 10:36:10 +00:00
|
|
|
tids = self.deserializeTiddlers(ext,data,fields),
|
2012-05-29 21:01:52 +00:00
|
|
|
metafile = sourcePath + ".meta";
|
2012-07-13 16:08:15 +00:00
|
|
|
if(ext !== ".json" && tids.length === 1 && fs.existsSync(metafile)) {
|
2013-03-01 09:13:44 +00:00
|
|
|
var metadata = fs.readFileSync(metafile,"utf8");
|
2012-05-29 21:01:52 +00:00
|
|
|
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<recipe.length; t++) {
|
|
|
|
if(recipe[t].name === "recipe") {
|
|
|
|
var recipeFile = path.resolve(path.dirname(sourcePath),recipe[t].value);
|
2013-03-01 09:13:44 +00:00
|
|
|
processRecipe(recipeFile,fs.readFileSync(recipeFile,"utf8"));
|
2012-05-29 21:01:52 +00:00
|
|
|
} else {
|
|
|
|
var tiddlerFile = path.resolve(path.dirname(sourcePath),recipe[t].value);
|
|
|
|
loadTiddlersFromFile(tiddlerFile,"{" + recipe[t].name + "}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sourcePath = fields.title; // Bit of a hack to take advantage of the default title being the path to the tiddler file
|
|
|
|
processRecipe(sourcePath,text);
|
|
|
|
return tiddlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|