1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Removed TextProcessors() and TiddlerConverters() classes

This commit is contained in:
Jeremy Ruston 2011-12-28 17:16:56 +00:00
parent 92a1ddf2e1
commit e02a484591
12 changed files with 103 additions and 178 deletions

View File

@ -13,31 +13,24 @@ var WikiStore = require("./WikiStore.js").WikiStore,
Tiddler = require("./Tiddler.js").Tiddler,
tiddlerInput = require("./TiddlerInput.js"),
tiddlerOutput = require("./TiddlerOutput.js"),
TextProcessors = require("./TextProcessors.js").TextProcessors,
WikiTextProcessor = require("./WikiTextProcessor.js").WikiTextProcessor,
TiddlerConverters = require("./TiddlerConverters.js").TiddlerConverters,
Navigators = require("./Navigators.js").Navigators,
StoryNavigator = require("./StoryNavigator.js").StoryNavigator;
var textProcessors = new TextProcessors(),
tiddlerConverters = new TiddlerConverters(),
store = new WikiStore({
textProcessors: textProcessors
}),
var store = new WikiStore(),
t;
// Register the wikitext processor
textProcessors.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
textProcessors: textProcessors
store.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
store: store
}));
// Register the standard tiddler serializers and deserializers
tiddlerInput.register(tiddlerConverters);
tiddlerOutput.register(tiddlerConverters);
tiddlerInput.register(store);
tiddlerOutput.register(store);
// Add the shadow tiddlers that are built into TiddlyWiki
var shadowShadowStore = new WikiStore({
textProcessors: textProcessors,
shadowStore: null
}),
shadowShadows = [
@ -46,25 +39,12 @@ var shadowShadowStore = new WikiStore({
{title: "MarkupPostHead", text: ""},
{title: "MarkupPreBody", text: ""},
{title: "MarkupPostBody", text: ""},
{title: "TabTimeline", text: "<<timeline>>"},
{title: "TabAll", text: "<<list all>>"},
{title: "TabTags", text: "<<allTags excludeLists>>"},
{title: "TabMoreMissing", text: "<<list missing>>"},
{title: "TabMoreOrphans", text: "<<list orphans>>"},
{title: "TabMoreShadowed", text: "<<list shadowed>>"},
{title: "AdvancedOptions", text: "<<options>>"},
{title: "PluginManager", text: "<<plugins>>"},
{title: "SystemSettings", text: ""},
{title: "ToolbarCommands", text: "|~ViewToolbar|closeTiddler closeOthers +editTiddler > fields syncing permalink references jump|\n|~EditToolbar|+saveTiddler -cancelTiddler deleteTiddler|"},
{title: "WindowTitle", text: "<<tiddler SiteTitle>> - <<tiddler SiteSubtitle>>"},
{title: "DefaultTiddlers", text: "[[GettingStarted]]"},
{title: "MainMenu", text: "[[GettingStarted]]"},
{title: "SiteTitle", text: "My TiddlyWiki"},
{title: "SiteSubtitle", text: "a reusable non-linear personal web notebook"},
{title: "SiteUrl", text: ""},
{title: "SideBarOptions", text: '<<search>><<closeAll>><<permaview>><<newTiddler>><<newJournal "DD MMM YYYY" "journal">><<saveChanges>><<slider chkSliderOptionsPanel OptionsPanel "options \u00bb" "Change TiddlyWiki advanced options">>'},
{title: "SideBarTabs", text: '<<tabs txtMainTab "Timeline" "Timeline" TabTimeline "All" "All tiddlers" TabAll "Tags" "All tags" TabTags "More" "More lists" TabMore>>'},
{title: "TabMore", text: '<<tabs txtMoreTab "Missing" "Missing tiddlers" TabMoreMissing "Orphans" "Orphaned tiddlers" TabMoreOrphans "Shadowed" "Shadowed tiddlers" TabMoreShadowed>>'}
{title: "SiteUrl", text: ""}
];
store.shadows.shadows = shadowShadowStore;
for(t=0; t<shadowShadows.length; t++) {
@ -73,7 +53,7 @@ for(t=0; t<shadowShadows.length; t++) {
// Load the tiddlers built into the TiddlyWiki document
var storeArea = document.getElementById("storeArea"),
tiddlers = tiddlerConverters.deserialize("(DOM)",storeArea);
tiddlers = store.deserializeTiddlers("(DOM)",storeArea);
for(t=0; t<tiddlers.length; t++) {
store.addTiddler(new Tiddler(tiddlers[t]));
}

View File

@ -51,8 +51,6 @@ var Recipe = function(options,callback) {
var me = this;
this.filepath = options.filepath;
this.store = options.store;
this.tiddlerConverters = options.tiddlerConverters;
this.textProcessors = options.textProcessors;
this.callback = callback;
this.recipe = [];
this.markers = {};
@ -174,7 +172,7 @@ Recipe.prototype.processRecipeFile = function(recipe,text,contextPath) {
}
var fields = {};
if(fieldLines.length > 0) {
fields = this.tiddlerConverters.deserialize("application/x-tiddler",fieldLines.join("\n"),{})[0];
fields = this.store.deserializeTiddlers("application/x-tiddler",fieldLines.join("\n"),{})[0];
}
recipe.push({marker: match.marker, filepath: match.value, contextPath: contextPath, fields: fields});
}
@ -192,7 +190,7 @@ Recipe.prototype.readTiddlerFile = function(filepath,contextPath,callback) {
var fields = {
title: data.path
};
var tiddlers = me.tiddlerConverters.deserialize(data.extname,data.text,fields);
var tiddlers = me.store.deserializeTiddlers(data.extname,data.text,fields);
// Check for the .meta file
if(data.extname !== ".json" && tiddlers.length === 1) {
var metafile = filepath + ".meta";
@ -204,7 +202,7 @@ Recipe.prototype.readTiddlerFile = function(filepath,contextPath,callback) {
if(!err) {
var text = data.text.split("\n\n")[0];
if(text) {
fields = me.tiddlerConverters.deserialize("application/x-tiddler",text,fields)[0];
fields = me.store.deserializeTiddlers("application/x-tiddler",text,fields)[0];
}
}
callback(null,[fields]);
@ -272,7 +270,7 @@ Recipe.tiddlerOutputter = {
// Ordinary tiddlers are output as a <DIV>
for(var t=0; t<tiddlers.length; t++) {
var tid = this.store.getTiddler(tiddlers[t]);
out.push(this.tiddlerConverters.serialize("application/x-tiddler-html-div",tid));
out.push(this.store.serializeTiddler("application/x-tiddler-html-div",tid));
}
},
javascript: function(out,tiddlers) {
@ -295,7 +293,7 @@ Recipe.tiddlerOutputter = {
for(var t=0; t<tiddlers.length; t++) {
var title = tiddlers[t],
tid = this.store.shadows.getTiddler(title);
out.push(this.tiddlerConverters.serialize("application/x-tiddler-html-div",tid));
out.push(this.store.serializeTiddler("application/x-tiddler-html-div",tid));
}
},
title: function(out,tiddlers) {

View File

@ -1,34 +0,0 @@
/*\
title: js/TextProcessors.js
\*/
(function(){
/*jslint node: true */
"use strict";
var util = require("util");
var TextProcessors = function() {
this.processors = {};
};
TextProcessors.prototype.registerTextProcessor = function(type,processor) {
this.processors[type] = processor;
};
TextProcessors.prototype.parse = function(type,text) {
var processor = this.processors[type];
if(!processor) {
processor = this.processors["text/x-tiddlywiki"];
}
if(processor) {
return processor.parse(text);
} else {
return null;
}
};
exports.TextProcessors = TextProcessors;
})();

View File

@ -1,54 +0,0 @@
/*\
title: js/TiddlerConverters.js
\*/
(function(){
/*jslint node: true */
"use strict";
var TiddlerConverters = function() {
this.serializers = {};
this.deserializers = {};
};
TiddlerConverters.prototype.registerSerializer = function(extension,mimeType,serializer) {
this.serializers[extension] = serializer;
this.serializers[mimeType] = serializer;
};
TiddlerConverters.prototype.registerDeserializer = function(extension,mimeType,deserializer) {
this.deserializers[extension] = deserializer;
this.deserializers[mimeType] = deserializer;
};
TiddlerConverters.prototype.serialize = function(type,tiddler) {
var serializer = this.serializers[type];
if(serializer) {
return serializer(tiddler);
} else {
return null;
}
};
TiddlerConverters.prototype.deserialize = function(type,text,srcFields) {
var fields = {},
deserializer = this.deserializers[type],
t;
if(srcFields) {
for(t in srcFields) {
fields[t] = srcFields[t];
}
}
if(deserializer) {
return deserializer(text,fields);
} else {
// Return a raw tiddler for unknown types
fields.text = text;
return [fields];
}
};
exports.TiddlerConverters = TiddlerConverters;
})();

View File

@ -208,14 +208,14 @@ var inputTiddlerDOM = function(node) {
return tiddlers;
};
tiddlerInput.register = function(tiddlerConverters) {
tiddlerConverters.registerDeserializer(".txt","text/plain",inputTiddlerPlain);
tiddlerConverters.registerDeserializer(".js","application/javascript",inputTiddlerJavaScript);
tiddlerConverters.registerDeserializer(".tiddler","application/x-tiddler-html-div",inputTiddlerDiv);
tiddlerConverters.registerDeserializer(".tid","application/x-tiddler",inputTiddler);
tiddlerConverters.registerDeserializer(".json","application/json",inputTiddlerJSON);
tiddlerConverters.registerDeserializer(".tiddlywiki","application/x-tiddlywiki",inputTiddlyWiki);
tiddlerConverters.registerDeserializer("(DOM)","(DOM)",inputTiddlerDOM);
tiddlerInput.register = function(store) {
store.registerTiddlerDeserializer(".txt","text/plain",inputTiddlerPlain);
store.registerTiddlerDeserializer(".js","application/javascript",inputTiddlerJavaScript);
store.registerTiddlerDeserializer(".tiddler","application/x-tiddler-html-div",inputTiddlerDiv);
store.registerTiddlerDeserializer(".tid","application/x-tiddler",inputTiddler);
store.registerTiddlerDeserializer(".json","application/json",inputTiddlerJSON);
store.registerTiddlerDeserializer(".tiddlywiki","application/x-tiddlywiki",inputTiddlyWiki);
store.registerTiddlerDeserializer("(DOM)","(DOM)",inputTiddlerDOM);
};
})();

View File

@ -104,9 +104,9 @@ var outputTiddlerDiv = function(tid) {
return result.join("");
};
tiddlerOutput.register = function(tiddlerConverters) {
tiddlerConverters.registerSerializer(".tid","application/x-tiddler",outputTiddler);
tiddlerConverters.registerSerializer(".tiddler","application/x-tiddler-html-div",outputTiddlerDiv);
tiddlerOutput.register = function(store) {
store.registerTiddlerSerializer(".tid","application/x-tiddler",outputTiddler);
store.registerTiddlerSerializer(".tiddler","application/x-tiddler-html-div",outputTiddlerDiv);
};
})();

View File

@ -10,17 +10,34 @@ title: js/WikiStore.js
var Tiddler = require("./Tiddler.js").Tiddler,
util = require("util");
/* Creates a new WikiStore object
Available options are:
shadowStore: An existing WikiStore to use for shadow tiddler storage. Pass null to prevent a default shadow store from being created
*/
var WikiStore = function WikiStore(options) {
options = options || {};
this.tiddlers = {};
this.textProcessors = {};
this.tiddlerSerializers = {};
this.tiddlerDeserializers = {};
this.shadows = options.shadowStore !== undefined ? options.shadowStore : new WikiStore({
shadowStore: null,
textProcessors: options.textProcessors
shadowStore: null
});
this.textProcessors = options.textProcessors;
};
WikiStore.prototype.clear = function() {
this.tiddlers = {};
WikiStore.prototype.registerTextProcessor = function(type,processor) {
this.textProcessors[type] = processor;
};
WikiStore.prototype.registerTiddlerSerializer = function(extension,mimeType,serializer) {
this.tiddlerSerializers[extension] = serializer;
this.tiddlerSerializers[mimeType] = serializer;
};
WikiStore.prototype.registerTiddlerDeserializer = function(extension,mimeType,deserializer) {
this.tiddlerDeserializers[extension] = deserializer;
this.tiddlerDeserializers[mimeType] = deserializer;
};
WikiStore.prototype.getTiddler = function(title) {
@ -115,10 +132,49 @@ WikiStore.prototype.getShadowTitles = function() {
return this.shadows ? this.shadows.getTitles() : [];
};
WikiStore.prototype.serializeTiddler = function(type,tiddler) {
var serializer = this.tiddlerSerializers[type];
if(serializer) {
return serializer(tiddler);
} else {
return null;
}
};
WikiStore.prototype.deserializeTiddlers = function(type,text,srcFields) {
var fields = {},
deserializer = this.tiddlerDeserializers[type],
t;
if(srcFields) {
for(t in srcFields) {
fields[t] = srcFields[t];
}
}
if(deserializer) {
return deserializer(text,fields);
} else {
// Return a raw tiddler for unknown types
fields.text = text;
return [fields];
}
};
WikiStore.prototype.parseText = function(type,text) {
var processor = this.textProcessors[type];
if(!processor) {
processor = this.textProcessors["text/x-tiddlywiki"];
}
if(processor) {
return processor.parse(text);
} else {
return null;
}
};
WikiStore.prototype.parseTiddler = function(title) {
var tiddler = this.getTiddler(title);
if(tiddler) {
return this.textProcessors.parse(tiddler.fields.type,tiddler.fields.text);
return this.parseText(tiddler.fields.type,tiddler.fields.text);
} else {
return null;
}

View File

@ -24,7 +24,6 @@ Planned:
extraMacros: An array of additional macro handlers to add
*/
var WikiTextProcessor = function(options) {
this.textProcessors = options.textProcessors;
this.rules = WikiTextRules.rules;
var pattern = [];
for(var n=0; n<this.rules.length; n++) {

View File

@ -207,7 +207,7 @@ WikiTextRenderer.macros = {
}
for(t=tiddlers.length-1; t>=last; t--) {
var tiddler = tiddlers[t],
theGroupParseTree = this.parser.processor.textProcessors.parse(groupTemplateType,groupTemplateText),
theGroupParseTree = this.store.parseText(groupTemplateType,groupTemplateText),
theGroup = theGroupParseTree.render("text/plain",theGroupParseTree.children,this.store,tiddler);
if(theGroup !== "") {
if(ul === undefined || theGroup !== lastGroup) {
@ -226,7 +226,7 @@ WikiTextRenderer.macros = {
children: []
}]};
ul.children.push(item);
item.children[0].children = this.parser.processor.textProcessors.parse(templateType,templateText).children;
item.children[0].children = this.store.parseText(templateType,templateText).children;
}
}
this.executeMacros(macroNode.output,title);
@ -260,7 +260,7 @@ WikiTextRenderer.macros = {
children: []
} ]
};
li.children[0].children = this.parser.processor.textProcessors.parse(templateType,templateText).children;
li.children[0].children = this.store.parseText(templateType,templateText).children;
ul.children.push(li);
}
if(ul.children.length > 0) {
@ -325,7 +325,7 @@ WikiTextRenderer.macros = {
var placeholderRegExp = new RegExp("\\$"+(t+1),"mg");
text = text.replace(placeholderRegExp,withTokens[t]);
}
macroNode.output = this.parser.processor.textProcessors.parse(tiddler.fields.type,text).children;
macroNode.output = this.store.parseText(tiddler.fields.type,text).children;
// Execute any macros in the copy
this.executeMacros(macroNode.output,title);
}
@ -367,7 +367,7 @@ WikiTextRenderer.macros = {
});
break;
case "wikified":
macroNode.output = this.parser.processor.textProcessors.parse("text/x-tiddlywiki",value).children;
macroNode.output = this.store.parseText("text/x-tiddlywiki",value).children;
// Execute any macros in the copy
this.executeMacros(macroNode.output,title);
break;

View File

@ -12,9 +12,7 @@ var WikiStore = require("./js/WikiStore.js").WikiStore,
Recipe = require("./js/Recipe.js").Recipe,
tiddlerInput = require("./js/TiddlerInput.js"),
tiddlerOutput = require("./js/TiddlerOutput.js"),
TextProcessors = require("./js/TextProcessors.js").TextProcessors,
WikiTextProcessor = require("./js/WikiTextProcessor.js").WikiTextProcessor,
TiddlerConverters = require("./js/TiddlerConverters.js").TiddlerConverters,
util = require("util"),
fs = require("fs"),
url = require("url"),
@ -45,27 +43,21 @@ var parseOptions = function(args,defaultSwitch) {
return result;
};
var textProcessors = new TextProcessors(),
tiddlerConverters = new TiddlerConverters(),
switches = parseOptions(Array.prototype.slice.call(process.argv,2),"dummy"),
store = new WikiStore({
textProcessors: textProcessors
}),
var switches = parseOptions(Array.prototype.slice.call(process.argv,2),"dummy"),
store = new WikiStore(),
recipe = null,
lastRecipeFilepath = null,
currSwitch = 0;
textProcessors.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
textProcessors: textProcessors
store.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
store: store
}));
// Register the standard tiddler serializers and deserializers
tiddlerInput.register(tiddlerConverters);
tiddlerOutput.register(tiddlerConverters);
tiddlerInput.register(store);
tiddlerOutput.register(store);
// Add the shadow tiddlers that are built into TiddlyWiki
var shadowShadowStore = new WikiStore({
textProcessors: textProcessors,
shadowStore: null
}),
shadowShadows = [
@ -114,9 +106,7 @@ var commandLineSwitches = {
lastRecipeFilepath = args[0];
recipe = new Recipe({
filepath: args[0],
store: store,
tiddlerConverters: tiddlerConverters,
textProcessors: textProcessors
store: store
},function() {
callback(null);
});
@ -196,9 +186,7 @@ var commandLineSwitches = {
// Dumbly, this implementation wastes the recipe processing that happened on the --recipe switch
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
store = new WikiStore({
textProcessors: textProcessors
});
store = new WikiStore();
recipe = new Recipe(store,lastRecipeFilepath,function() {
response.end(recipe.cook(), "utf8");
});

View File

@ -11,11 +11,9 @@ jsmodule: ../js/ArgParser.js
jsmodule: ../js/FileRetriever.js
jsmodule: ../js/Utils.js
jsmodule: ../js/Tiddler.js
jsmodule: ../js/TiddlerConverters.js
jsmodule: ../js/TiddlerInput.js
jsmodule: ../js/TiddlerOutput.js
jsmodule: ../js/WikiStore.js
jsmodule: ../js/TextProcessors.js
jsmodule: ../js/WikiTextProcessor.js
jsmodule: ../js/WikiTextParser.js
jsmodule: ../js/WikiTextRules.js

View File

@ -16,9 +16,7 @@ verifying that the output matches `<tiddlername>.html` and `<tiddlername>.txt`.
var Tiddler = require("./js/Tiddler.js").Tiddler,
WikiStore = require("./js/WikiStore.js").WikiStore,
TextProcessors = require("./js/TextProcessors.js").TextProcessors,
WikiTextProcessor = require("./js/WikiTextProcessor.js").WikiTextProcessor,
TiddlerConverters = require("./js/TiddlerConverters.js").TiddlerConverters,
tiddlerInput = require("./js/TiddlerInput.js"),
utils = require("./js/Utils.js"),
util = require("util"),
@ -26,24 +24,20 @@ var Tiddler = require("./js/Tiddler.js").Tiddler,
path = require("path");
var testdirectory = process.argv[2],
textProcessors = new TextProcessors(),
tiddlerConverters = new TiddlerConverters(),
store = new WikiStore({
textProcessors: textProcessors
}),
store = new WikiStore(),
files = fs.readdirSync(testdirectory),
titles = [],
f,t,extname,basename;
textProcessors.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
textProcessors: textProcessors
store.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({
store: store
}));
tiddlerInput.register(tiddlerConverters);
tiddlerInput.register(store);
for(f=0; f<files.length; f++) {
extname = path.extname(files[f]);
if(extname === ".tid") {
var tiddlers = tiddlerConverters.deserialize(extname,fs.readFileSync(path.resolve(testdirectory,files[f]),"utf8"));
var tiddlers = store.deserializeTiddlers(extname,fs.readFileSync(path.resolve(testdirectory,files[f]),"utf8"));
if(tiddlers.length > 1) {
throw "Cannot use .JSON files";
}