1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-05-10 11:24:08 +00:00

More refactoring around the plugin implementation

Now wikis can specify which plugins should be loaded
This commit is contained in:
Jeremy Ruston 2012-11-13 08:46:44 +00:00
parent 3b11713e88
commit 11b2af34d2
2 changed files with 46 additions and 24 deletions

View File

@ -86,7 +86,8 @@ $tw.modules.types = $tw.modules.types || {}; // hashmap by module type of array
$tw.config = $tw.config || {}; $tw.config = $tw.config || {};
// Constants // Constants
$tw.config.bootModuleSubDir = $tw.config.bootModuleSubDir || "./modules"; $tw.config.pluginsPath = "../plugins/";
$tw.config.wikiInfo = $tw.config.wikiInfo || "./tiddlywiki.info";
$tw.config.wikiPluginsSubDir = $tw.config.wikiPluginsSubDir || "./plugins"; $tw.config.wikiPluginsSubDir = $tw.config.wikiPluginsSubDir || "./plugins";
$tw.config.wikiShadowsSubDir = $tw.config.wikiShadowsSubDir || "./wiki"; $tw.config.wikiShadowsSubDir = $tw.config.wikiShadowsSubDir || "./wiki";
$tw.config.wikiTiddlersSubDir = $tw.config.wikiTiddlersSubDir || "./tiddlers"; $tw.config.wikiTiddlersSubDir = $tw.config.wikiTiddlersSubDir || "./tiddlers";
@ -724,11 +725,11 @@ $tw.loadTiddlersFromFile = function(filepath,fields) {
var ext = path.extname(filepath), var ext = path.extname(filepath),
extensionInfo = $tw.config.fileExtensionInfo[ext], extensionInfo = $tw.config.fileExtensionInfo[ext],
typeInfo = extensionInfo ? $tw.config.contentTypeInfo[extensionInfo.type] : null, typeInfo = extensionInfo ? $tw.config.contentTypeInfo[extensionInfo.type] : null,
data = fs.readFileSync(filepath).toString(typeInfo ? typeInfo.encoding : "utf8"), data = fs.readFileSync(filepath,typeInfo ? typeInfo.encoding : "utf8"),
tiddlers = $tw.wiki.deserializeTiddlers(ext,data,fields), tiddlers = $tw.wiki.deserializeTiddlers(ext,data,fields),
metafile = filepath + ".meta"; metafile = filepath + ".meta";
if(ext !== ".json" && tiddlers.length === 1 && fs.existsSync(metafile)) { if(ext !== ".json" && tiddlers.length === 1 && fs.existsSync(metafile)) {
var metadata = fs.readFileSync(metafile).toString("utf8"); var metadata = fs.readFileSync(metafile,"utf8");
if(metadata) { if(metadata) {
tiddlers = [$tw.utils.parseFields(metadata,tiddlers[0])]; tiddlers = [$tw.utils.parseFields(metadata,tiddlers[0])];
} }
@ -750,11 +751,11 @@ $tw.loadTiddlersFromPath = function(filepath,excludeRegExp) {
// Look for a tiddlywiki.files file // Look for a tiddlywiki.files file
if(files.indexOf("tiddlywiki.files") !== -1) { if(files.indexOf("tiddlywiki.files") !== -1) {
// If so, process the files it describes // If so, process the files it describes
filesInfo = JSON.parse(fs.readFileSync(filepath + "/tiddlywiki.files").toString("utf8")); filesInfo = JSON.parse(fs.readFileSync(filepath + "/tiddlywiki.files","utf8"));
for(p=0; p<filesInfo.tiddlers.length; p++) { for(p=0; p<filesInfo.tiddlers.length; p++) {
tidInfo = filesInfo.tiddlers[p]; tidInfo = filesInfo.tiddlers[p];
typeInfo = $tw.config.contentTypeInfo[tidInfo.fields.type || "text/plain"]; typeInfo = $tw.config.contentTypeInfo[tidInfo.fields.type || "text/plain"];
text = fs.readFileSync(path.resolve(filepath,tidInfo.file)).toString(typeInfo ? typeInfo.encoding : "utf8"); text = fs.readFileSync(path.resolve(filepath,tidInfo.file),typeInfo ? typeInfo.encoding : "utf8");
tidInfo.fields.text = text; tidInfo.fields.text = text;
tiddlers.push(tidInfo.fields); tiddlers.push(tidInfo.fields);
} }
@ -778,10 +779,15 @@ $tw.loadTiddlersFromPath = function(filepath,excludeRegExp) {
Load the tiddlers from a bundle folder, and package them up into a proper JSON bundle tiddler Load the tiddlers from a bundle folder, and package them up into a proper JSON bundle tiddler
*/ */
$tw.loadBundleFolder = function(filepath,excludeRegExp) { $tw.loadBundleFolder = function(filepath,excludeRegExp) {
excludeRegExp = excludeRegExp || /^\.DS_Store$|.meta$/;
var stat, files, bundleInfo = {tiddlers: []}, bundleTiddlers = [], f, file, titlePrefix, t;
if(fs.existsSync(filepath)) {
stat = fs.statSync(filepath);
if(stat.isDirectory()) {
files = fs.readdirSync(filepath);
// Read the plugin information // Read the plugin information
var bundleInfo = JSON.parse(fs.readFileSync(filepath).toString("utf8")); bundleInfo = JSON.parse(fs.readFileSync(filepath + "/plugin.bundle","utf8"));
// Read the bundle files // Read the bundle files
var bundleTiddlers = [];
for(f=0; f<files.length; f++) { for(f=0; f<files.length; f++) {
file = files[f]; file = files[f];
if(!excludeRegExp.test(file) && file !== "plugin.bundle" && file !== "tiddlywiki.files") { if(!excludeRegExp.test(file) && file !== "plugin.bundle" && file !== "tiddlywiki.files") {
@ -790,13 +796,15 @@ $tw.loadBundleFolder = function(filepath,excludeRegExp) {
} }
// Save the bundle tiddlers into the bundle // Save the bundle tiddlers into the bundle
bundleInfo.tiddlers = bundleInfo.tiddlers || {}; bundleInfo.tiddlers = bundleInfo.tiddlers || {};
var titlePrefix = bundleInfo.title + "/"; titlePrefix = bundleInfo.title + "/";
for(t=0; t<bundleTiddlers.length; t++) { for(t=0; t<bundleTiddlers.length; t++) {
// Check that the constituent tiddler has the bundle title as a prefix // Check that the constituent tiddler has the bundle title as a prefix
if(bundleTiddlers[t].title.indexOf(titlePrefix) === 0 && bundleTiddlers[t].title.length > titlePrefix.length) { if(bundleTiddlers[t].title.indexOf(titlePrefix) === 0 && bundleTiddlers[t].title.length > titlePrefix.length) {
bundleInfo.tiddlers[bundleTiddlers[t].title.substr(titlePrefix.length)] = bundleTiddlers[t]; bundleInfo.tiddlers[bundleTiddlers[t].title.substr(titlePrefix.length)] = bundleTiddlers[t];
} else { } else {
throw "The bundle '" + bundleInfo.title + "' cannot contain a tiddler titled '" + bundleTiddlers[t].title + "'"; console.log("Error extracting plugin bundle: The bundle '" + bundleInfo.title + "' cannot contain a tiddler titled '" + bundleTiddlers[t].title + "'");
}
}
} }
} }
// Save the bundle tiddler // Save the bundle tiddler
@ -883,6 +891,15 @@ if($tw.browser) {
for(t=0; t<folders.length; t++) { for(t=0; t<folders.length; t++) {
$tw.wiki.addTiddlers($tw.loadTiddlersFromPath(folders[t])); $tw.wiki.addTiddlers($tw.loadTiddlersFromPath(folders[t]));
} }
// Load any plugins specified within the wiki folder
var wikiInfoPath = path.resolve($tw.boot.wikiPath,$tw.config.wikiInfo);
if(fs.existsSync(wikiInfoPath)) {
var wikiInfo = JSON.parse(fs.readFileSync(wikiInfoPath,"utf8")),
pluginBasePath = path.resolve($tw.boot.bootPath,$tw.config.pluginsPath);
for(t=0; t<wikiInfo.plugins.length; t++) {
$tw.wiki.addTiddler($tw.loadBundleFolder(path.resolve(pluginBasePath,"./" + wikiInfo.plugins[t])));
}
}
} }
// Install plugins // Install plugins

5
tw5.com/tiddlywiki.info Normal file
View File

@ -0,0 +1,5 @@
{
"plugins": [
"tiddlywiki/fullscreen"
]
}