mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
Added ginsu.js for splitting TiddlyWiki files into separate tiddlers
This commit is contained in:
parent
21e06b9833
commit
b45c3249df
30
ginsu.js
Normal file
30
ginsu.js
Normal file
@ -0,0 +1,30 @@
|
||||
// Break apart a TiddlyWiki HTML file into separate .tid files
|
||||
//
|
||||
// Usage: node ginsu.js <tiddlywikifile> <outputdir>
|
||||
//
|
||||
// The .html extension is optional
|
||||
//
|
||||
// Ginsu creates the specified places the .tid files in the specified directory (which must already exist)
|
||||
var sys = require("sys"),
|
||||
fs = require("fs"),
|
||||
path = require("path"),
|
||||
tiddler = require("./js/Tiddler.js"),
|
||||
tiddlyWikiInput = require("./js/TiddlyWikiInput.js"),
|
||||
tiddlerOutput = require("./js/TiddlerOutput.js");
|
||||
|
||||
var tiddlywikifilename = process.argv[2];
|
||||
var outputdir = process.argv[3];
|
||||
|
||||
var tiddlywikidoc = fs.readFileSync(tiddlywikifilename,"utf8");
|
||||
var tiddlers = tiddlyWikiInput.parseTiddlyWiki(tiddlywikidoc);
|
||||
|
||||
var recipe = [];
|
||||
for(var t=0; t<tiddlers.length; t++) {
|
||||
var tid = new tiddler.Tiddler(tiddlers[t]);
|
||||
var filename = tid.fields.title + ".tid";
|
||||
fs.writeFileSync(path.join(outputdir,filename),tiddlerOutput.outputTiddler(tid));
|
||||
recipe.push("tiddler: " + filename + "\n");
|
||||
}
|
||||
|
||||
fs.writeFileSync(path.join(outputdir,"split.recipe"),recipe.join());
|
||||
|
@ -7,6 +7,39 @@ var argParser = require("./ArgParser.js"),
|
||||
|
||||
var tiddlerOutput = exports;
|
||||
|
||||
/*
|
||||
Output a tiddler as a .tid file
|
||||
*/
|
||||
tiddlerOutput.outputTiddler = function(tid) {
|
||||
var result = [];
|
||||
var outputAttribute = function(name,value) {
|
||||
result.push(name + ": " + value + "\n");
|
||||
};
|
||||
for(var t in tid.fields) {
|
||||
switch(t) {
|
||||
case "text":
|
||||
// Ignore the text field
|
||||
break;
|
||||
case "tags":
|
||||
// Output tags as a list
|
||||
outputAttribute(t,tiddlerOutput.stringifyTags(tid.fields.tags));
|
||||
break;
|
||||
case "modified":
|
||||
case "created":
|
||||
// Output dates in YYYYMMDDHHMM
|
||||
outputAttribute(t,utils.convertToYYYYMMDDHHMM(tid.fields[t]));
|
||||
break;
|
||||
default:
|
||||
// Output other attributes raw
|
||||
outputAttribute(t,tid.fields[t]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
result.push("\n");
|
||||
result.push(tid.fields.text);
|
||||
return result.join("");
|
||||
}
|
||||
|
||||
/*
|
||||
Output a tiddler as an HTML <DIV>
|
||||
out - array to push the output strings
|
||||
|
50
js/TiddlyWikiInput.js
Executable file
50
js/TiddlyWikiInput.js
Executable file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Functions concerned with parsing TiddlyWiki files
|
||||
*/
|
||||
|
||||
var tiddlerInput = require("./TiddlerInput.js"),
|
||||
utils = require("./Utils.js");
|
||||
|
||||
var tiddlyWikiInput = exports;
|
||||
|
||||
/*
|
||||
Parses the text of a TiddlyWiki HTML file, and returns the tiddlers as an array of hashmaps of raw fields.
|
||||
|
||||
*/
|
||||
tiddlyWikiInput.parseTiddlyWiki = function(tiddlywikidoc) {
|
||||
var results = [];
|
||||
var storeAreaPos = locateStoreArea(tiddlywikidoc);
|
||||
if(storeAreaPos) {
|
||||
var endOfDivRegExp = /(<\/div>\s*)/gi;
|
||||
var startPos = storeAreaPos[0];
|
||||
endOfDivRegExp.lastIndex = startPos;
|
||||
var match = endOfDivRegExp.exec(tiddlywikidoc);
|
||||
while(match && startPos < storeAreaPos[1]) {
|
||||
var endPos = endOfDivRegExp.lastIndex;
|
||||
var fields = tiddlerInput.parseTiddlerDiv(tiddlywikidoc.substring(startPos,endPos));
|
||||
results.push(fields);
|
||||
startPos = endPos;
|
||||
match = endOfDivRegExp.exec(tiddlywikidoc);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function locateStoreArea(tiddlywikidoc)
|
||||
{
|
||||
var startSaveArea = '<div id="' + 'storeArea">';
|
||||
var startSaveAreaRegExp = /<div id=["']?storeArea['"]?>/gi;
|
||||
var endSaveArea = '</d' + 'iv>';
|
||||
var endSaveAreaCaps = '</D' + 'IV>';
|
||||
var posOpeningDiv = tiddlywikidoc.search(startSaveAreaRegExp);
|
||||
var limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-STOREAREA--"+">");
|
||||
if(limitClosingDiv == -1) {
|
||||
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-BODY-START--"+">");
|
||||
}
|
||||
var start = limitClosingDiv == -1 ? tiddlywikidoc.length : limitClosingDiv;
|
||||
var posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveArea,start);
|
||||
if(posClosingDiv == -1) {
|
||||
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveAreaCaps,start);
|
||||
}
|
||||
return (posOpeningDiv != -1 && posClosingDiv != -1) ? [posOpeningDiv + startSaveArea.length,posClosingDiv] : null;
|
||||
}
|
7
test.sh
7
test.sh
@ -10,4 +10,9 @@ cook $PWD/test/data/tiddlywiki.com/index.html.recipe -d $PWD -o tmp/oldcooked.ht
|
||||
node cook.js $PWD/test/data/tiddlywiki.com/index.html.recipe > tmp/newcooked.html || exit 1
|
||||
|
||||
# compare the two
|
||||
opendiff tmp/oldcooked.html tmp/newcooked.html
|
||||
# opendiff tmp/oldcooked.html tmp/newcooked.html
|
||||
|
||||
# split the newly cooked tiddlywiki into tiddlers
|
||||
mkdir -p tmp/tiddlers
|
||||
rm -f tmp/tiddlers/*
|
||||
node ginsu.js tmp/newcooked.html tmp/tiddlers
|
||||
|
Loading…
Reference in New Issue
Block a user