diff --git a/ginsu.js b/ginsu.js new file mode 100644 index 000000000..3e638662b --- /dev/null +++ b/ginsu.js @@ -0,0 +1,30 @@ +// Break apart a TiddlyWiki HTML file into separate .tid files +// +// Usage: node ginsu.js +// +// 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 out - array to push the output strings diff --git a/js/TiddlyWikiInput.js b/js/TiddlyWikiInput.js new file mode 100755 index 000000000..1fbb60eed --- /dev/null +++ b/js/TiddlyWikiInput.js @@ -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 = '
'; + var startSaveAreaRegExp = /
/gi; + var endSaveArea = ''; + var endSaveAreaCaps = ''; + 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; +} diff --git a/test.sh b/test.sh index 35d15ec26..38b4d36e3 100755 --- a/test.sh +++ b/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