1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Minor refactorings

This commit is contained in:
Jeremy Ruston 2012-05-05 11:21:59 +01:00
parent 575f49fd45
commit 5bad23e675
2 changed files with 56 additions and 51 deletions

View File

@ -198,7 +198,7 @@ $tw.plugins.registerPlugin = function(name,moduleType,moduleExports) {
/*
Register all plugin module tiddlers
*/
$tw.plugins.registerPlugins = function() {
$tw.plugins.registerPluginModules = function() {
for(var title in $tw.wiki.shadows.tiddlers) {
var tiddler = $tw.wiki.shadows.getTiddler(title);
if(tiddler.fields.type === "application/javascript" && tiddler.fields["module-type"] !== undefined) {
@ -276,13 +276,6 @@ Hashmap of field plugins by plugin name
*/
$tw.Tiddler.fieldPlugins = {};
/*
Install any tiddler field plugin modules
*/
$tw.Tiddler.installPlugins = function() {
$tw.Tiddler.fieldPlugins = $tw.plugins.getPluginsByTypeAsHashmap("tiddlerfield");
};
/*
Register and install the built in tiddler field plugins
*/
@ -312,7 +305,7 @@ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerfields/tags","tiddl
}
});
// Install built in tiddler fields plugins so that they are available immediately
$tw.Tiddler.installPlugins();
$tw.Tiddler.fieldPlugins = $tw.plugins.getPluginsByTypeAsHashmap("tiddlerfield");
/////////////////////////// Barebones wiki store
@ -370,6 +363,9 @@ $tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) {
for(var f in srcFields) {
fields[f] = srcFields[f];
}
if(!fields.type) {
fields.type = type;
}
if(deserializer) {
return deserializer.call(this,text,fields);
} else {
@ -527,11 +523,8 @@ $tw.boot.wikiPath = process.cwd();
/*
Load the tiddlers contained in a particular file (and optionally the accompanying .meta file)
*/
$tw.plugins.loadTiddlersFromFile = function(file,basetitle) {
$tw.plugins.loadTiddlersFromFile = function(file,fields) {
var ext = path.extname(file),
fields = {
title: basetitle
},
extensionInfo = $tw.config.fileExtensions[ext],
data = fs.readFileSync(file).toString(extensionInfo ? extensionInfo.encoding : "utf8"),
tiddlers = $tw.wiki.deserializeTiddlers(ext,data,fields),
@ -548,21 +541,31 @@ $tw.plugins.loadTiddlersFromFile = function(file,basetitle) {
/*
Load all the plugins from the plugins directory
*/
$tw.plugins.loadPlugins = function(filepath,basetitle,excludeRegExp) {
$tw.plugins.loadPluginsFromFolder = function(filepath,basetitle,excludeRegExp) {
basetitle = basetitle || "$:/plugins";
excludeRegExp = excludeRegExp || /^\.DS_Store$|.meta$/;
if(path.existsSync(filepath)) {
var stat = fs.statSync(filepath);
if(stat.isDirectory()) {
var files = fs.readdirSync(filepath);
// Look for a tiddlywiki.plugin file
if(files.indexOf("tiddlywiki.plugin") !== -1) {
// If so, process the files it describes
var pluginInfo = JSON.parse(fs.readFileSync(filepath + "/tiddlywiki.plugin").toString("utf8"));
for(var p=0; p<pluginInfo.tiddlers.length; p++) {
$tw.plugins.loadTiddlersFromFile(path.resolve(filepath,pluginInfo.tiddlers[p].file),pluginInfo.tiddlers[p].fields);
}
} else {
// If not, read all the files in the directory
for(var f=0; f<files.length; f++) {
var file = files[f];
if(!excludeRegExp.test(file)) {
$tw.plugins.loadPlugins(filepath + "/" + file,basetitle + "/" + file,excludeRegExp);
$tw.plugins.loadPluginsFromFolder(filepath + "/" + file,basetitle + "/" + file,excludeRegExp);
}
}
}
} else if(stat.isFile()) {
$tw.plugins.loadTiddlersFromFile(filepath,basetitle);
$tw.plugins.loadTiddlersFromFile(filepath,{title: basetitle});
}
}
};
@ -603,10 +606,10 @@ $tw.modules.execute = function(moduleName,moduleRoot) {
};
// Load plugins from the plugins directory
$tw.plugins.loadPlugins(path.resolve($tw.boot.bootPath,$tw.config.bootModuleSubDir));
$tw.plugins.loadPluginsFromFolder(path.resolve($tw.boot.bootPath,$tw.config.bootModuleSubDir));
// Load any plugins in the wiki plugins directory
$tw.plugins.loadPlugins(require("path").resolve($tw.boot.wikiPath,$tw.config.wikiPluginsSubDir));
$tw.plugins.loadPluginsFromFolder(path.resolve($tw.boot.wikiPath,$tw.config.wikiPluginsSubDir));
// End of if(!$tw.isBrowser)
}
@ -614,11 +617,7 @@ $tw.plugins.loadPlugins(require("path").resolve($tw.boot.wikiPath,$tw.config.wik
/////////////////////////// Final initialisation
// Register plugins from the tiddlers we've just loaded
$tw.plugins.registerPlugins();
// Now we can properly install all of our extension plugins
$tw.Tiddler.installPlugins();
$tw.plugins.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerPlugins);
$tw.plugins.registerPluginModules();
// Run any startup plugin modules
var mainModules = $tw.plugins.moduleTypes["startup"];

View File

@ -13,10 +13,13 @@ This is the main application logic for both the client and server
"use strict";
exports.startup = function() {
var modules,n,m,f;
var modules,n,m,f,commander;
// Set up additional global objects
$tw.plugins.applyMethods("global",$tw);
// Wire up plugin modules
// Reinstall the plugin categories that were installed during the kernel boot process
$tw.Tiddler.fieldPlugins = $tw.plugins.getPluginsByTypeAsHashmap("tiddlerfield");
$tw.plugins.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerPlugins);
// Wire up other plugin modules
$tw.plugins.applyMethods("config",$tw.config);
$tw.plugins.applyMethods("utils",$tw.utils);
$tw.version = $tw.utils.extractVersionInfo();
@ -24,9 +27,9 @@ exports.startup = function() {
$tw.plugins.applyMethods("wikimethod",$tw.Wiki.prototype);
$tw.plugins.applyMethods("treeutils",$tw.Tree);
$tw.plugins.applyMethods("treenode",$tw.Tree);
// Load up the tiddlers in the root of the core directory
// Load up the tiddlers in the root of the core directory (we couldn't do before because we didn't have the serializers installed)
if(!$tw.isBrowser) {
$tw.plugins.loadPlugins($tw.boot.bootPath,"$:/core",/^\.DS_Store$|.meta$|^modules$/);
$tw.plugins.loadPluginsFromFolder($tw.boot.bootPath,"$:/core",/^\.DS_Store$|.meta$|^modules$/);
}
// Set up the wiki store
$tw.wiki.initMacros();
@ -34,8 +37,9 @@ exports.startup = function() {
$tw.wiki.initParsers();
// Set up the command plugins
$tw.Commander.initCommands();
if($tw.isBrowser) {
// Host-specific startup
if($tw.isBrowser) {
// Display the PageTemplate
var renderer = $tw.wiki.parseTiddler("PageTemplate");
renderer.execute([],"PageTemplate");
renderer.renderInDom(document.body);
@ -43,8 +47,9 @@ if($tw.isBrowser) {
renderer.refreshInDom(changes);
});
console.log("$tw",$tw);
} else {
var commander = new $tw.Commander(
} else {
// Start a commander with the command line arguments
commander = new $tw.Commander(
Array.prototype.slice.call(process.argv,2),
function(err) {
if(err) {
@ -55,7 +60,8 @@ if($tw.isBrowser) {
{output: process.stdout, error: process.stderr}
);
commander.execute();
}
}
};