mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 18:17:20 +00:00
Refactored TiddlyWikiInput to be a type of tiddlerFileInput
This commit is contained in:
parent
dc8871f9a2
commit
eb9e8891cb
4
ginsu.js
4
ginsu.js
@ -11,14 +11,14 @@
|
|||||||
var fs = require("fs"),
|
var fs = require("fs"),
|
||||||
path = require("path"),
|
path = require("path"),
|
||||||
Tiddler = require("./js/Tiddler.js").Tiddler,
|
Tiddler = require("./js/Tiddler.js").Tiddler,
|
||||||
tiddlyWikiInput = require("./js/TiddlyWikiInput.js"),
|
tiddlerInput = require("./js/TiddlerInput.js"),
|
||||||
tiddlerOutput = require("./js/TiddlerOutput.js");
|
tiddlerOutput = require("./js/TiddlerOutput.js");
|
||||||
|
|
||||||
var tiddlywikifilename = process.argv[2];
|
var tiddlywikifilename = process.argv[2];
|
||||||
var outputdir = process.argv[3];
|
var outputdir = process.argv[3];
|
||||||
|
|
||||||
var tiddlywikidoc = fs.readFileSync(tiddlywikifilename,"utf8");
|
var tiddlywikidoc = fs.readFileSync(tiddlywikifilename,"utf8");
|
||||||
var tiddlers = tiddlyWikiInput.parseTiddlyWiki(tiddlywikidoc);
|
var tiddlers = tiddlerInput.parseTiddlerFile(tiddlywikidoc,"application/x-tiddlywiki");
|
||||||
|
|
||||||
var recipe = [];
|
var recipe = [];
|
||||||
for(var t=0; t<tiddlers.length; t++) {
|
for(var t=0; t<tiddlers.length; t++) {
|
||||||
|
@ -37,7 +37,8 @@ tiddlerInput.fileExtensionMappings = {
|
|||||||
".tiddler": "application/x-tiddler-html-div",
|
".tiddler": "application/x-tiddler-html-div",
|
||||||
".tid": "application/x-tiddler",
|
".tid": "application/x-tiddler",
|
||||||
".js": "application/javascript",
|
".js": "application/javascript",
|
||||||
".json": "application/json"
|
".json": "application/json",
|
||||||
|
".tiddlywiki": "application/x-tiddlywiki"
|
||||||
};
|
};
|
||||||
|
|
||||||
tiddlerInput.parseTiddlerFileByMimeType = {
|
tiddlerInput.parseTiddlerFileByMimeType = {
|
||||||
@ -79,9 +80,46 @@ tiddlerInput.parseTiddlerFileByMimeType = {
|
|||||||
result.push(getKnownFields(tiddlers[t]));
|
result.push(getKnownFields(tiddlers[t]));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
},
|
||||||
|
"application/x-tiddlywiki": function(text,fields) {
|
||||||
|
var results = [],
|
||||||
|
storeAreaPos = tiddlerInput.locateStoreArea(text);
|
||||||
|
if(storeAreaPos) {
|
||||||
|
var endOfDivRegExp = /(<\/div>\s*)/gi,
|
||||||
|
startPos = storeAreaPos[0];
|
||||||
|
endOfDivRegExp.lastIndex = startPos;
|
||||||
|
var match = endOfDivRegExp.exec(text);
|
||||||
|
while(match && startPos < storeAreaPos[1]) {
|
||||||
|
var endPos = endOfDivRegExp.lastIndex,
|
||||||
|
fields = tiddlerInput.parseTiddlerDiv(text.substring(startPos,endPos));
|
||||||
|
fields.text = utils.htmlDecode(fields.text);
|
||||||
|
results.push(fields);
|
||||||
|
startPos = endPos;
|
||||||
|
match = endOfDivRegExp.exec(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tiddlerInput.locateStoreArea = function(tiddlywikidoc) {
|
||||||
|
var startSaveArea = '<div id="' + 'storeArea">',
|
||||||
|
startSaveAreaRegExp = /<div id=["']?storeArea['"]?>/gi,
|
||||||
|
endSaveArea = '</d' + 'iv>',
|
||||||
|
endSaveAreaCaps = '</D' + 'IV>',
|
||||||
|
posOpeningDiv = tiddlywikidoc.search(startSaveAreaRegExp),
|
||||||
|
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-STOREAREA--"+">");
|
||||||
|
if(limitClosingDiv == -1) {
|
||||||
|
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-BODY-START--"+">");
|
||||||
|
}
|
||||||
|
var start = limitClosingDiv == -1 ? tiddlywikidoc.length : limitClosingDiv,
|
||||||
|
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveArea,start);
|
||||||
|
if(posClosingDiv == -1) {
|
||||||
|
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveAreaCaps,start);
|
||||||
|
}
|
||||||
|
return (posOpeningDiv != -1 && posClosingDiv != -1) ? [posOpeningDiv + startSaveArea.length,posClosingDiv] : null;
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Parse a block of metadata and merge the results into a hashmap of tiddler fields.
|
Parse a block of metadata and merge the results into a hashmap of tiddler fields.
|
||||||
|
|
||||||
|
@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
Functions concerned with parsing TiddlyWiki files
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*global require: false, exports: false */
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
var tiddlerInput = require("./TiddlerInput.js"),
|
|
||||||
utils = require("./Utils.js");
|
|
||||||
|
|
||||||
var tiddlyWikiInput = exports;
|
|
||||||
|
|
||||||
function locateStoreArea(tiddlywikidoc)
|
|
||||||
{
|
|
||||||
var startSaveArea = '<div id="' + 'storeArea">',
|
|
||||||
startSaveAreaRegExp = /<div id=["']?storeArea['"]?>/gi,
|
|
||||||
endSaveArea = '</d' + 'iv>',
|
|
||||||
endSaveAreaCaps = '</D' + 'IV>',
|
|
||||||
posOpeningDiv = tiddlywikidoc.search(startSaveAreaRegExp),
|
|
||||||
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-STOREAREA--"+">");
|
|
||||||
if(limitClosingDiv == -1) {
|
|
||||||
limitClosingDiv = tiddlywikidoc.indexOf("<"+"!--POST-BODY-START--"+">");
|
|
||||||
}
|
|
||||||
var start = limitClosingDiv == -1 ? tiddlywikidoc.length : limitClosingDiv,
|
|
||||||
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveArea,start);
|
|
||||||
if(posClosingDiv == -1) {
|
|
||||||
posClosingDiv = tiddlywikidoc.lastIndexOf(endSaveAreaCaps,start);
|
|
||||||
}
|
|
||||||
return (posOpeningDiv != -1 && posClosingDiv != -1) ? [posOpeningDiv + startSaveArea.length,posClosingDiv] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
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 = [],
|
|
||||||
storeAreaPos = locateStoreArea(tiddlywikidoc);
|
|
||||||
if(storeAreaPos) {
|
|
||||||
var endOfDivRegExp = /(<\/div>\s*)/gi,
|
|
||||||
startPos = storeAreaPos[0];
|
|
||||||
endOfDivRegExp.lastIndex = startPos;
|
|
||||||
var match = endOfDivRegExp.exec(tiddlywikidoc);
|
|
||||||
while(match && startPos < storeAreaPos[1]) {
|
|
||||||
var endPos = endOfDivRegExp.lastIndex,
|
|
||||||
fields = tiddlerInput.parseTiddlerDiv(tiddlywikidoc.substring(startPos,endPos));
|
|
||||||
fields.text = utils.htmlDecode(fields.text);
|
|
||||||
results.push(fields);
|
|
||||||
startPos = endPos;
|
|
||||||
match = endOfDivRegExp.exec(tiddlywikidoc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
};
|
|
@ -9,7 +9,6 @@ var WikiStore = require("./js/WikiStore.js").WikiStore,
|
|||||||
Tiddler = require("./js/Tiddler.js").Tiddler,
|
Tiddler = require("./js/Tiddler.js").Tiddler,
|
||||||
Recipe = require("./js/Recipe.js").Recipe,
|
Recipe = require("./js/Recipe.js").Recipe,
|
||||||
Tiddler = require("./js/Tiddler.js").Tiddler,
|
Tiddler = require("./js/Tiddler.js").Tiddler,
|
||||||
tiddlyWikiInput = require("./js/TiddlyWikiInput.js"),
|
|
||||||
tiddlerOutput = require("./js/TiddlerOutput.js"),
|
tiddlerOutput = require("./js/TiddlerOutput.js"),
|
||||||
tiddlerInput = require("./js/TiddlerInput.js"),
|
tiddlerInput = require("./js/TiddlerInput.js"),
|
||||||
util = require("util"),
|
util = require("util"),
|
||||||
@ -101,8 +100,10 @@ var commandLineSwitches = {
|
|||||||
if(err) {
|
if(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
} else {
|
} else {
|
||||||
var fields = {title: args[0]};
|
var fields = {title: args[0]},
|
||||||
var tiddlers = tiddlerInput.parseTiddlerFile(data,path.extname(args[0]),fields);
|
extname = path.extname(args[0]),
|
||||||
|
type = extname === ".html" ? "application/x-tiddlywiki" : extname;
|
||||||
|
var tiddlers = tiddlerInput.parseTiddlerFile(data,type,fields);
|
||||||
for(var t=0; t<tiddlers.length; t++) {
|
for(var t=0; t<tiddlers.length; t++) {
|
||||||
store.addTiddler(new Tiddler(tiddlers[t]));
|
store.addTiddler(new Tiddler(tiddlers[t]));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user