2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/boot.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
|
2012-11-14 11:23:43 +00:00
|
|
|
The main boot kernel for TiddlyWiki. This single file creates a barebones TW environment that is just sufficient to bootstrap the modules containing the main logic of the application.
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-11-14 11:23:43 +00:00
|
|
|
On the server this file is executed directly to boot TiddlyWiki. In the browser, this file is packed into a single HTML file along with other elements:
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
# bootprefix.js
|
|
|
|
# <module definitions>
|
|
|
|
# boot.js
|
|
|
|
|
|
|
|
The module definitions on the browser look like this:
|
|
|
|
|
|
|
|
$tw.defineModule("MyModule","moduletype",function(module,exports,require) {
|
|
|
|
// Module code inserted here
|
|
|
|
return exports;
|
|
|
|
});
|
|
|
|
|
|
|
|
In practice, each module is wrapped in a separate script block.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function() {
|
|
|
|
|
2012-05-04 17:49:04 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global modules: false, $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/////////////////////////// Setting up $tw
|
|
|
|
|
|
|
|
// Set up $tw global for the server
|
2012-11-15 10:40:03 +00:00
|
|
|
if(typeof(window) === "undefined") {
|
|
|
|
global.$tw = global.$tw || {}; // No `browser` member for the server
|
2012-08-30 13:33:07 +00:00
|
|
|
exports.$tw = $tw; // Export $tw for when boot.js is required directly in node.js
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 10:40:03 +00:00
|
|
|
// Include bootprefix if we're on the server
|
|
|
|
if(!$tw.browser) {
|
|
|
|
require("./bootprefix.js");
|
|
|
|
}
|
|
|
|
|
2012-05-02 10:02:47 +00:00
|
|
|
// Boot information
|
|
|
|
$tw.boot = {};
|
|
|
|
|
2012-11-13 18:42:50 +00:00
|
|
|
// Plugin state
|
|
|
|
$tw.plugins = {};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
// Modules store registers all the modules the system has seen
|
2012-04-30 18:14:39 +00:00
|
|
|
$tw.modules = $tw.modules || {};
|
2012-05-04 17:49:04 +00:00
|
|
|
$tw.modules.titles = $tw.modules.titles || {}; // hashmap by module title of {fn:, exports:, moduleType:}
|
2012-11-14 11:23:43 +00:00
|
|
|
$tw.modules.types = $tw.modules.types || {}; // hashmap by module type of hashmap of exports
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
// Config object
|
|
|
|
$tw.config = $tw.config || {};
|
|
|
|
|
|
|
|
// Constants
|
2012-11-13 08:46:44 +00:00
|
|
|
$tw.config.pluginsPath = "../plugins/";
|
|
|
|
$tw.config.wikiInfo = $tw.config.wikiInfo || "./tiddlywiki.info";
|
2012-05-04 17:24:54 +00:00
|
|
|
$tw.config.wikiPluginsSubDir = $tw.config.wikiPluginsSubDir || "./plugins";
|
2012-06-06 19:52:30 +00:00
|
|
|
$tw.config.wikiShadowsSubDir = $tw.config.wikiShadowsSubDir || "./wiki";
|
2012-05-20 14:19:38 +00:00
|
|
|
$tw.config.wikiTiddlersSubDir = $tw.config.wikiTiddlersSubDir || "./tiddlers";
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-08-25 13:16:11 +00:00
|
|
|
$tw.config.jsModuleHeaderRegExpString = "^\\/\\*\\\\\\n((?:^[^\\n]*\\n)+?)(^\\\\\\*\\/$\\n?)";
|
2012-08-22 11:33:21 +00:00
|
|
|
|
2012-06-08 10:47:05 +00:00
|
|
|
// File extension mappings
|
|
|
|
$tw.config.fileExtensionInfo = {
|
|
|
|
".tid": {type: "application/x-tiddler"},
|
|
|
|
".tiddler": {type: "application/x-tiddler-html-div"},
|
2012-11-18 15:25:40 +00:00
|
|
|
".recipe": {type: "application/vnd.tiddlywiki2-recipe"},
|
2012-06-08 10:47:05 +00:00
|
|
|
".txt": {type: "text/plain"},
|
|
|
|
".css": {type: "text/css"},
|
|
|
|
".html": {type: "text/html"},
|
|
|
|
".js": {type: "application/javascript"},
|
|
|
|
".json": {type: "application/json"},
|
2012-10-10 09:45:39 +00:00
|
|
|
".pdf": {type: "application/pdf"},
|
2012-06-08 10:47:05 +00:00
|
|
|
".jpg": {type: "image/jpeg"},
|
|
|
|
".jpeg": {type: "image/jpeg"},
|
|
|
|
".png": {type: "image/png"},
|
|
|
|
".gif": {type: "image/gif"},
|
|
|
|
".svg": {type: "image/svg+xml"}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Content type mappings
|
|
|
|
$tw.config.contentTypeInfo = {
|
2012-11-18 15:22:13 +00:00
|
|
|
"text/vnd.tiddlywiki": {encoding: "utf8", extension: ".tid"},
|
2012-11-11 14:13:08 +00:00
|
|
|
"application/x-tiddler": {encoding: "utf8", extension: ".tid"},
|
|
|
|
"application/x-tiddler-html-div": {encoding: "utf8", extension: ".tiddler"},
|
2012-11-18 15:25:40 +00:00
|
|
|
"application/vnd.tiddlywiki2-recipe": {encoding: "utf8", extension: ".recipe"},
|
2012-11-11 14:13:08 +00:00
|
|
|
"text/plain": {encoding: "utf8", extension: ".txt"},
|
|
|
|
"text/css": {encoding: "utf8", extension: ".css"},
|
|
|
|
"text/html": {encoding: "utf8", extension: ".html"},
|
|
|
|
"application/javascript": {encoding: "utf8", extension: ".js"},
|
|
|
|
"application/json": {encoding: "utf8", extension: ".json"},
|
|
|
|
"application/pdf": {encoding: "base64", extension: ".pdf"},
|
|
|
|
"image/jpeg": {encoding: "base64", extension: ".jpg"},
|
|
|
|
"image/png": {encoding: "base64", extension: ".png"},
|
|
|
|
"image/gif": {encoding: "base64", extension: ".gif"},
|
|
|
|
"image/svg+xml": {encoding: "utf8", extension: ".svg"}
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////// Utility functions
|
|
|
|
|
|
|
|
$tw.utils = $tw.utils || {};
|
|
|
|
|
2012-10-10 15:32:37 +00:00
|
|
|
/*
|
|
|
|
Check if an object has a property
|
|
|
|
*/
|
|
|
|
$tw.utils.hop = function(object,property) {
|
|
|
|
return Object.prototype.hasOwnProperty.call(object,property);
|
|
|
|
};
|
|
|
|
|
2012-11-16 17:44:47 +00:00
|
|
|
/*
|
|
|
|
Iterate through all the own properties of an object. Callback is invoked with (element,title,object)
|
|
|
|
*/
|
|
|
|
$tw.utils.each = function(object,callback) {
|
|
|
|
if(object) {
|
|
|
|
for(var f in object) {
|
|
|
|
if($tw.utils.hop(object,f)) {
|
|
|
|
callback(object[f],f,object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/*
|
|
|
|
Determine if a value is an array
|
|
|
|
*/
|
|
|
|
$tw.utils.isArray = function(value) {
|
|
|
|
return Object.prototype.toString.call(value) == "[object Array]";
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-10-10 15:32:37 +00:00
|
|
|
/*
|
|
|
|
Convert "&" to &, "<" to <, ">" to > and """ to "
|
|
|
|
*/
|
2012-04-30 11:23:03 +00:00
|
|
|
$tw.utils.htmlDecode = function(s) {
|
|
|
|
return s.toString().replace(/</mg,"<").replace(/>/mg,">").replace(/"/mg,"\"").replace(/&/mg,"&");
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pad a string to a given length with "0"s. Length defaults to 2
|
|
|
|
*/
|
|
|
|
$tw.utils.pad = function(value,length) {
|
|
|
|
length = length || 2;
|
|
|
|
var s = value.toString();
|
2012-05-04 17:49:04 +00:00
|
|
|
if(s.length < length) {
|
2012-04-30 11:23:03 +00:00
|
|
|
s = "000000000000000000000000000".substr(0,length - s.length) + s;
|
2012-05-04 17:49:04 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert a date into YYYYMMDDHHMM format
|
|
|
|
$tw.utils.stringifyDate = function(value) {
|
|
|
|
return value.getUTCFullYear() +
|
2012-05-05 13:17:51 +00:00
|
|
|
$tw.utils.pad(value.getUTCMonth() + 1) +
|
|
|
|
$tw.utils.pad(value.getUTCDate()) +
|
|
|
|
$tw.utils.pad(value.getUTCHours()) +
|
|
|
|
$tw.utils.pad(value.getUTCMinutes());
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Parse a date from a YYYYMMDDHHMMSSMMM format string
|
|
|
|
$tw.utils.parseDate = function(value) {
|
|
|
|
if(typeof value === "string") {
|
|
|
|
return new Date(Date.UTC(parseInt(value.substr(0,4),10),
|
|
|
|
parseInt(value.substr(4,2),10)-1,
|
|
|
|
parseInt(value.substr(6,2),10),
|
|
|
|
parseInt(value.substr(8,2)||"00",10),
|
|
|
|
parseInt(value.substr(10,2)||"00",10),
|
|
|
|
parseInt(value.substr(12,2)||"00",10),
|
|
|
|
parseInt(value.substr(14,3)||"000",10)));
|
|
|
|
} else if (value instanceof Date) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-30 13:43:13 +00:00
|
|
|
// Parse a string array from a bracketted list. For example "OneTiddler [[Another Tiddler]] LastOne"
|
2012-04-30 11:23:03 +00:00
|
|
|
$tw.utils.parseStringArray = function(value) {
|
|
|
|
if(typeof value === "string") {
|
2012-05-09 12:48:34 +00:00
|
|
|
var memberRegExp = /(?:\[\[([^\]]+)\]\])|([^\s]+)/mg,
|
2012-04-30 11:23:03 +00:00
|
|
|
results = [],
|
|
|
|
match;
|
|
|
|
do {
|
|
|
|
match = memberRegExp.exec(value);
|
|
|
|
if(match) {
|
|
|
|
results.push(match[1] || match[2]);
|
|
|
|
}
|
|
|
|
} while(match);
|
|
|
|
return results;
|
|
|
|
} else if ($tw.utils.isArray(value)) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse a block of name:value fields. The `fields` object is used as the basis for the return value
|
|
|
|
$tw.utils.parseFields = function(text,fields) {
|
2012-11-17 13:08:25 +00:00
|
|
|
fields = fields || {};
|
2012-05-09 12:48:34 +00:00
|
|
|
text.split(/\r?\n/mg).forEach(function(line) {
|
2012-11-17 20:18:03 +00:00
|
|
|
if(line.charAt(0) !== "#") {
|
|
|
|
var p = line.indexOf(":");
|
|
|
|
if(p !== -1) {
|
|
|
|
var field = line.substr(0, p).trim(),
|
|
|
|
value = line.substr(p+1).trim();
|
|
|
|
fields[field] = value;
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Resolves a source filepath delimited with `/` relative to a specified absolute root filepath.
|
|
|
|
In relative paths, the special folder name `..` refers to immediate parent directory, and the
|
|
|
|
name `.` refers to the current directory
|
|
|
|
*/
|
|
|
|
$tw.utils.resolvePath = function(sourcepath,rootpath) {
|
|
|
|
// If the source path starts with ./ or ../ then it is relative to the root
|
|
|
|
if(sourcepath.substr(0,2) === "./" || sourcepath.substr(0,3) === "../" ) {
|
|
|
|
var src = sourcepath.split("/"),
|
|
|
|
root = rootpath.split("/");
|
|
|
|
// Remove the filename part of the root
|
|
|
|
root.splice(root.length-1,1);
|
|
|
|
// Process the source path bit by bit onto the end of the root path
|
|
|
|
while(src.length > 0) {
|
|
|
|
var c = src.shift();
|
|
|
|
if(c === "..") { // Slice off the last root entry for a double dot
|
|
|
|
if(root.length > 0) {
|
|
|
|
root.splice(root.length-1,1);
|
|
|
|
}
|
|
|
|
} else if(c !== ".") { // Ignore dots
|
|
|
|
root.push(c); // Copy other elements across
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root.join("/");
|
|
|
|
} else {
|
|
|
|
// If it isn't relative, just return the path
|
|
|
|
return sourcepath;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-01 15:50:42 +00:00
|
|
|
/*
|
2012-11-12 22:16:49 +00:00
|
|
|
Returns true if the `actual` version is greater than or equal to the `required` version. Both are in `x.y.z` format.
|
2012-10-01 15:50:42 +00:00
|
|
|
*/
|
|
|
|
$tw.utils.checkVersions = function(required,actual) {
|
|
|
|
var targetVersion = required.split("."),
|
|
|
|
currVersion = actual.split("."),
|
|
|
|
diff = [parseInt(targetVersion[0],10) - parseInt(currVersion[0],10),
|
|
|
|
parseInt(targetVersion[1],10) - parseInt(currVersion[1],10),
|
|
|
|
parseInt(targetVersion[2],10) - parseInt(currVersion[2],10)];
|
|
|
|
return (diff[0] > 0) ||
|
|
|
|
(diff[0] === 0 && diff[1] > 0) ||
|
|
|
|
(diff[0] === 0 && diff[1] === 0 && diff[2] > 0);
|
|
|
|
};
|
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/*
|
|
|
|
Creates a PasswordPrompt object
|
|
|
|
*/
|
|
|
|
$tw.utils.PasswordPrompt = function() {
|
|
|
|
// Store of pending password prompts
|
|
|
|
this.passwordPrompts = [];
|
|
|
|
// Create the wrapper
|
|
|
|
this.promptWrapper = document.createElement("div");
|
|
|
|
this.promptWrapper.className = "tw-password-wrapper";
|
|
|
|
document.body.appendChild(this.promptWrapper);
|
|
|
|
// Hide the empty wrapper
|
|
|
|
this.setWrapperDisplay();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Hides or shows the wrapper depending on whether there are any outstanding prompts
|
|
|
|
*/
|
|
|
|
$tw.utils.PasswordPrompt.prototype.setWrapperDisplay = function() {
|
|
|
|
if(this.passwordPrompts.length) {
|
|
|
|
this.promptWrapper.style.display = "block";
|
|
|
|
} else {
|
|
|
|
this.promptWrapper.style.display = "none";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Adds a new password prompt
|
|
|
|
*/
|
|
|
|
$tw.utils.PasswordPrompt.prototype.createPrompt = function(options) {
|
|
|
|
// Create and add the prompt to the DOM
|
2012-11-16 17:23:10 +00:00
|
|
|
var submitText = options.submitText || "Login",
|
|
|
|
form = document.createElement("form"),
|
2012-11-16 16:59:47 +00:00
|
|
|
html = ["<h1>" + options.serviceName + "</h1>"];
|
|
|
|
if(!options.noUserName) {
|
|
|
|
html.push("<input type='text' name='username' class='input-small' placeholder='Username'>");
|
|
|
|
}
|
|
|
|
html.push("<input type='password' name='password' class='input-small' placeholder='Password'>",
|
2012-11-16 17:23:10 +00:00
|
|
|
"<button type='submit' class='btn'>" + submitText + "</button>");
|
2012-11-16 16:59:47 +00:00
|
|
|
form.className = "form-inline";
|
|
|
|
form.innerHTML = html.join("\n");
|
|
|
|
this.promptWrapper.appendChild(form);
|
2012-11-16 17:23:10 +00:00
|
|
|
window.setTimeout(function() {
|
|
|
|
form.elements[0].focus();
|
|
|
|
},10);
|
2012-11-16 16:59:47 +00:00
|
|
|
// Add a submit event handler
|
|
|
|
var self = this;
|
|
|
|
form.addEventListener("submit",function(event) {
|
|
|
|
// Collect the form data
|
2012-11-16 22:40:56 +00:00
|
|
|
var data = {},t;
|
|
|
|
for(t=0; t<form.elements.length; t++) {
|
2012-11-16 16:59:47 +00:00
|
|
|
var e = form.elements[t];
|
|
|
|
if(e.name && e.value) {
|
|
|
|
data[e.name] = e.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Call the callback
|
|
|
|
if(options.callback(data)) {
|
|
|
|
// Remove the prompt if the callback returned true
|
|
|
|
var i = self.passwordPrompts.indexOf(promptInfo);
|
|
|
|
if(i !== -1) {
|
|
|
|
self.passwordPrompts.splice(i,1);
|
|
|
|
promptInfo.form.parentNode.removeChild(promptInfo.form);
|
|
|
|
self.setWrapperDisplay();
|
|
|
|
}
|
2012-11-16 22:40:56 +00:00
|
|
|
} else {
|
|
|
|
// Clear the password if the callback returned false
|
|
|
|
for(t=0; t<form.elements.length; t++) {
|
|
|
|
var e = form.elements[t];
|
|
|
|
if(e.name === "password") {
|
|
|
|
form.elements[t].value = "";
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 16:59:47 +00:00
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
},true);
|
|
|
|
// Add the prompt to the list
|
|
|
|
var promptInfo = {
|
|
|
|
serviceName: options.serviceName,
|
|
|
|
callback: options.callback,
|
|
|
|
form: form
|
|
|
|
};
|
|
|
|
this.passwordPrompts.push(promptInfo);
|
|
|
|
// Make sure the wrapper is displayed
|
|
|
|
this.setWrapperDisplay();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Crypto helper object for encrypted content
|
|
|
|
$tw.utils.Crypto = function() {
|
|
|
|
var sjcl = $tw.browser ? window.sjcl : require("./sjcl.js"),
|
|
|
|
password = null,
|
|
|
|
callSjcl = function(method,inputText) {
|
|
|
|
var outputText;
|
|
|
|
try {
|
|
|
|
outputText = sjcl[method](password,inputText);
|
|
|
|
} catch(ex) {
|
|
|
|
console.log("Crypto error:" + ex);
|
|
|
|
outputText = null;
|
|
|
|
}
|
|
|
|
return outputText;
|
|
|
|
};
|
|
|
|
this.setPassword = function(newPassword) {
|
|
|
|
password = newPassword;
|
|
|
|
};
|
|
|
|
this.encrypt = function(text) {
|
|
|
|
return callSjcl("encrypt",text);
|
|
|
|
};
|
|
|
|
this.decrypt = function(text) {
|
|
|
|
return callSjcl("decrypt",text);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-10-01 15:50:42 +00:00
|
|
|
/////////////////////////// Server initialisation
|
|
|
|
|
|
|
|
var fs, path, vm;
|
|
|
|
if(!$tw.browser) {
|
|
|
|
// Standard node libraries
|
|
|
|
fs = require("fs");
|
|
|
|
path = require("path");
|
|
|
|
vm = require("vm");
|
|
|
|
// System paths and filenames
|
|
|
|
$tw.boot.bootFile = path.basename(module.filename);
|
|
|
|
$tw.boot.bootPath = path.dirname(module.filename);
|
|
|
|
$tw.boot.wikiPath = process.cwd();
|
|
|
|
// Read package info
|
|
|
|
$tw.packageInfo = JSON.parse(fs.readFileSync($tw.boot.bootPath + "/../package.json"));
|
|
|
|
// Check node version number
|
|
|
|
if($tw.utils.checkVersions($tw.packageInfo.engine.node.substr(2),process.version.substr(1))) {
|
|
|
|
throw "TiddlyWiki5 requires node.js version " + $tw.packageInfo.engine.node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
/////////////////////////// Module mechanism
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
/*
|
2012-11-14 11:23:43 +00:00
|
|
|
Apply a callback to each module of a particular type
|
|
|
|
moduleType: type of modules to enumerate
|
|
|
|
callback: function called as callback(title,moduleExports) for each module
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-14 11:23:43 +00:00
|
|
|
$tw.modules.forEachModuleOfType = function(moduleType,callback) {
|
|
|
|
var modules = $tw.modules.types[moduleType];
|
2012-11-16 17:44:47 +00:00
|
|
|
$tw.utils.each(modules,function(element,title,object) {
|
|
|
|
callback(title,$tw.modules.execute(title));
|
|
|
|
});
|
2012-11-14 11:23:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get all the modules of a particular type in a hashmap by their `name` field
|
|
|
|
*/
|
|
|
|
$tw.modules.getModulesByTypeAsHashmap = function(moduleType,nameField) {
|
|
|
|
nameField = nameField || "name";
|
|
|
|
var results = {};
|
|
|
|
$tw.modules.forEachModuleOfType(moduleType,function(title,module) {
|
|
|
|
results[module[nameField]] = module;
|
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-08-03 14:09:48 +00:00
|
|
|
Apply the exports of the modules of a particular type to a target object
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-16 17:44:47 +00:00
|
|
|
$tw.modules.applyMethods = function(moduleType,targetObject) {
|
2012-11-14 11:23:43 +00:00
|
|
|
$tw.modules.forEachModuleOfType(moduleType,function(title,module) {
|
2012-11-16 17:44:47 +00:00
|
|
|
$tw.utils.each(module,function(element,title,object) {
|
|
|
|
targetObject[title] = module[title];
|
|
|
|
});
|
2012-11-14 11:23:43 +00:00
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////// Barebones tiddler object
|
|
|
|
|
|
|
|
/*
|
|
|
|
Construct a tiddler object from a hashmap of tiddler fields. If multiple hasmaps are provided they are merged,
|
|
|
|
taking precedence to the right
|
|
|
|
*/
|
|
|
|
$tw.Tiddler = function(/* [fields,] fields */) {
|
|
|
|
this.fields = {};
|
|
|
|
for(var c=0; c<arguments.length; c++) {
|
|
|
|
var arg = arguments[c],
|
2012-06-06 11:20:48 +00:00
|
|
|
src = (arg instanceof $tw.Tiddler) ? arg.fields : arg;
|
2012-04-30 11:23:03 +00:00
|
|
|
for(var t in src) {
|
|
|
|
if(src[t] === undefined) {
|
|
|
|
if(t in this.fields) {
|
|
|
|
delete this.fields[t]; // If we get a field that's undefined, delete any previous field value
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-03 14:09:48 +00:00
|
|
|
// Parse the field with the associated field module (if any)
|
|
|
|
var fieldModule = $tw.Tiddler.fieldModules[t];
|
|
|
|
if(fieldModule) {
|
|
|
|
this.fields[t] = fieldModule.parse.call(this,src[t]);
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
this.fields[t] = src[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.Tiddler.prototype.hasField = function(field) {
|
|
|
|
return $tw.utils.hop(this.fields,field);
|
|
|
|
};
|
2012-11-15 10:40:03 +00:00
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/*
|
2012-08-03 14:09:48 +00:00
|
|
|
Hashmap of field modules by field name
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-08-03 14:09:48 +00:00
|
|
|
$tw.Tiddler.fieldModules = {};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
/*
|
2012-08-03 14:09:48 +00:00
|
|
|
Register and install the built in tiddler field modules
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerfields/modified","tiddlerfield",{
|
2012-04-30 11:23:03 +00:00
|
|
|
name: "modified",
|
|
|
|
parse: $tw.utils.parseDate,
|
|
|
|
stringify: $tw.utils.stringifyDate
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerfields/created","tiddlerfield",{
|
2012-04-30 11:23:03 +00:00
|
|
|
name: "created",
|
|
|
|
parse: $tw.utils.parseDate,
|
|
|
|
stringify: $tw.utils.stringifyDate
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerfields/tags","tiddlerfield",{
|
2012-04-30 11:23:03 +00:00
|
|
|
name: "tags",
|
|
|
|
parse: $tw.utils.parseStringArray,
|
|
|
|
stringify: function(value) {
|
|
|
|
var result = [];
|
|
|
|
for(var t=0; t<value.length; t++) {
|
|
|
|
if(value[t].indexOf(" ") !== -1) {
|
|
|
|
result.push("[[" + value[t] + "]]");
|
|
|
|
} else {
|
|
|
|
result.push(value[t]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.join(" ");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/////////////////////////// Barebones wiki store
|
|
|
|
|
|
|
|
/*
|
2012-06-06 11:07:33 +00:00
|
|
|
Construct a wiki store object
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-06-06 11:07:33 +00:00
|
|
|
$tw.Wiki = function() {
|
2012-04-30 11:23:03 +00:00
|
|
|
this.tiddlers = {};
|
2012-11-15 10:40:03 +00:00
|
|
|
this.bundles = {}; // Hashmap of plugin information by title
|
|
|
|
this.bundledTiddlers = {}; // Hashmap of constituent tiddlers from plugins by title
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
2012-11-11 14:31:45 +00:00
|
|
|
$tw.Wiki.prototype.addTiddler = function(tiddler) {
|
2012-04-30 11:23:03 +00:00
|
|
|
if(!(tiddler instanceof $tw.Tiddler)) {
|
|
|
|
tiddler = new $tw.Tiddler(tiddler);
|
|
|
|
}
|
2012-06-06 12:21:20 +00:00
|
|
|
this.tiddlers[tiddler.fields.title] = tiddler;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
2012-11-11 14:31:45 +00:00
|
|
|
$tw.Wiki.prototype.addTiddlers = function(tiddlers) {
|
2012-04-30 11:23:03 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
2012-11-11 14:31:45 +00:00
|
|
|
this.addTiddler(tiddlers[t]);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-06 21:34:16 +00:00
|
|
|
/*
|
2012-11-12 22:16:49 +00:00
|
|
|
Extract constituent tiddlers from bundle tiddlers so that we can easily access them in getTiddler()
|
2012-08-06 21:34:16 +00:00
|
|
|
*/
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.Wiki.prototype.unpackBundleTiddlers = function() {
|
2012-08-06 21:34:16 +00:00
|
|
|
// Collect up all the plugin tiddlers
|
2012-11-16 17:44:47 +00:00
|
|
|
var self = this;
|
|
|
|
$tw.utils.each(this.tiddlers,function(tiddler,title,object) {
|
2012-11-12 22:16:49 +00:00
|
|
|
if(tiddler.fields.type === "application/json" && tiddler.hasField("bundle")) {
|
|
|
|
// Save the bundle information
|
2012-11-16 17:44:47 +00:00
|
|
|
var bundleInfo = self.bundles[title] = JSON.parse(tiddler.fields.text);
|
2012-08-06 21:34:16 +00:00
|
|
|
// Extract the constituent tiddlers
|
2012-11-12 22:16:49 +00:00
|
|
|
for(var t in bundleInfo.tiddlers) {
|
|
|
|
var constituentTiddler = bundleInfo.tiddlers[t],
|
|
|
|
constituentTitle = bundleInfo.title + "/" + t;
|
2012-08-06 21:34:16 +00:00
|
|
|
// Don't overwrite tiddlers that already exist
|
2012-11-16 17:44:47 +00:00
|
|
|
if(!$tw.utils.hop(self.bundledTiddlers,constituentTitle)) {
|
2012-08-06 21:34:16 +00:00
|
|
|
// Save the tiddler object
|
2012-11-16 17:44:47 +00:00
|
|
|
self.bundledTiddlers[constituentTitle] = new $tw.Tiddler(constituentTiddler,{title: constituentTitle});
|
2012-08-06 21:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 17:44:47 +00:00
|
|
|
});
|
2012-08-06 21:34:16 +00:00
|
|
|
};
|
|
|
|
|
2012-08-30 14:00:08 +00:00
|
|
|
/*
|
2012-11-15 10:40:03 +00:00
|
|
|
Define all modules stored in ordinary tiddlers
|
2012-08-30 14:00:08 +00:00
|
|
|
*/
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.Wiki.prototype.defineTiddlerModules = function() {
|
2012-11-16 17:44:47 +00:00
|
|
|
$tw.utils.each(this.tiddlers,function(tiddler,title,object) {
|
2012-11-17 13:08:25 +00:00
|
|
|
if(tiddler.hasField("module-type")) {
|
|
|
|
switch (tiddler.fields.type) {
|
|
|
|
case "application/javascript":
|
|
|
|
// We don't need to register JavaScript tiddlers in the browser
|
|
|
|
if(!$tw.browser) {
|
|
|
|
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "application/json":
|
|
|
|
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],JSON.parse(tiddler.fields.text));
|
|
|
|
break;
|
|
|
|
case "application/x-tiddler-dictionary":
|
|
|
|
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],$tw.utils.parseFields(tiddler.fields.text));
|
|
|
|
break;
|
|
|
|
}
|
2012-08-31 11:34:39 +00:00
|
|
|
}
|
2012-11-16 17:44:47 +00:00
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Register all the module tiddlers that have a module type
|
|
|
|
*/
|
|
|
|
$tw.Wiki.prototype.defineBundledModules = function() {
|
2012-11-16 17:44:47 +00:00
|
|
|
var self = this;
|
|
|
|
$tw.utils.each(this.bundledTiddlers,function(element,title,object) {
|
|
|
|
var tiddler = self.getTiddler(title);
|
|
|
|
if(!$tw.utils.hop(self.tiddlers,title)) { // Don't define the module if it is overidden by an ordinary tiddler
|
|
|
|
if(tiddler.fields.type === "application/javascript" && tiddler.hasField("module-type")) {
|
|
|
|
// Define the module
|
|
|
|
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text);
|
2012-08-30 14:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-16 17:44:47 +00:00
|
|
|
});
|
2012-08-30 14:00:08 +00:00
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
$tw.Wiki.prototype.getTiddler = function(title) {
|
|
|
|
var t = this.tiddlers[title];
|
|
|
|
if(t instanceof $tw.Tiddler) {
|
|
|
|
return t;
|
2012-11-12 22:16:49 +00:00
|
|
|
} else if($tw.utils.hop(this.bundledTiddlers,title)) {
|
|
|
|
return this.bundledTiddlers[title];
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-11-18 13:14:28 +00:00
|
|
|
return undefined;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-08-30 14:03:01 +00:00
|
|
|
Hashmap of serializer modules by serializer name
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-08-03 14:09:48 +00:00
|
|
|
$tw.Wiki.tiddlerDeserializerModules = {};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Extracts tiddlers from a typed block of text, specifying default field values
|
|
|
|
*/
|
|
|
|
$tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) {
|
|
|
|
srcFields = srcFields || {};
|
2012-08-03 14:09:48 +00:00
|
|
|
var deserializer = $tw.Wiki.tiddlerDeserializerModules[type],
|
2012-04-30 11:23:03 +00:00
|
|
|
fields = {};
|
2012-06-08 10:47:05 +00:00
|
|
|
if(!deserializer && $tw.config.fileExtensionInfo[type]) {
|
2012-04-30 11:23:03 +00:00
|
|
|
// If we didn't find the serializer, try converting it from an extension to a content type
|
2012-06-08 10:47:05 +00:00
|
|
|
type = $tw.config.fileExtensionInfo[type].type;
|
2012-08-03 14:09:48 +00:00
|
|
|
deserializer = $tw.Wiki.tiddlerDeserializerModules[type];
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-05-05 13:17:51 +00:00
|
|
|
if(!deserializer) {
|
|
|
|
// If we still don't have a deserializer, treat it as plain text
|
2012-08-03 14:09:48 +00:00
|
|
|
deserializer = $tw.Wiki.tiddlerDeserializerModules["text/plain"];
|
2012-05-05 13:17:51 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
for(var f in srcFields) {
|
2012-05-04 17:49:04 +00:00
|
|
|
fields[f] = srcFields[f];
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
if(deserializer) {
|
2012-05-03 20:47:16 +00:00
|
|
|
return deserializer.call(this,text,fields);
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
// Return a raw tiddler for unknown types
|
|
|
|
fields.text = text;
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-08-03 14:09:48 +00:00
|
|
|
Register the built in tiddler deserializer modules
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/js","tiddlerdeserializer",{
|
2012-05-03 20:47:16 +00:00
|
|
|
"application/javascript": function(text,fields) {
|
2012-08-22 11:33:21 +00:00
|
|
|
var headerCommentRegExp = new RegExp($tw.config.jsModuleHeaderRegExpString,"mg"),
|
2012-04-30 11:23:03 +00:00
|
|
|
match = headerCommentRegExp.exec(text);
|
|
|
|
fields.text = text;
|
|
|
|
if(match) {
|
2012-05-09 12:48:34 +00:00
|
|
|
fields = $tw.utils.parseFields(match[1].split(/\r?\n\r?\n/mg)[0],fields);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/tid","tiddlerdeserializer",{
|
2012-05-03 20:47:16 +00:00
|
|
|
"application/x-tiddler": function(text,fields) {
|
2012-05-09 12:48:34 +00:00
|
|
|
var split = text.split(/\r?\n\r?\n/mg);
|
|
|
|
if(split.length > 1) {
|
|
|
|
fields = $tw.utils.parseFields(split[0],fields);
|
|
|
|
fields.text = split.slice(1).join("\n\n");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
fields.text = text;
|
|
|
|
}
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/txt","tiddlerdeserializer",{
|
2012-06-06 20:42:14 +00:00
|
|
|
"text/plain": function(text,fields) {
|
|
|
|
fields.text = text;
|
|
|
|
fields.type = "text/plain";
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/html","tiddlerdeserializer",{
|
2012-06-06 20:42:14 +00:00
|
|
|
"text/html": function(text,fields) {
|
|
|
|
fields.text = text;
|
|
|
|
fields.type = "text/html";
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/json","tiddlerdeserializer",{
|
2012-10-27 13:51:43 +00:00
|
|
|
"application/json": function(text,fields) {
|
|
|
|
var tiddlers = JSON.parse(text);
|
|
|
|
return tiddlers;
|
|
|
|
}
|
|
|
|
});
|
2012-06-06 20:42:14 +00:00
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/////////////////////////// Intermediate initialisation
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create the wiki store for the app
|
|
|
|
*/
|
|
|
|
$tw.wiki = new $tw.Wiki();
|
|
|
|
|
|
|
|
/////////////////////////// Browser definitions
|
|
|
|
|
2012-05-19 10:29:51 +00:00
|
|
|
if($tw.browser) {
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/*
|
|
|
|
Get any encrypted tiddlers
|
|
|
|
*/
|
2012-11-16 17:23:10 +00:00
|
|
|
$tw.boot.decryptEncryptedTiddlers = function(callback) {
|
2012-11-16 16:59:47 +00:00
|
|
|
var encryptedArea = document.getElementById("encryptedArea"),
|
|
|
|
encryptedTiddlers = [];
|
|
|
|
if(encryptedArea) {
|
|
|
|
for(var t = 0; t <encryptedArea.childNodes.length; t++) {
|
|
|
|
var childNode = encryptedArea.childNodes[t];
|
|
|
|
if(childNode.hasAttribute && childNode.hasAttribute("data-tw-encrypted-tiddlers")) {
|
|
|
|
var e = childNode.firstChild;
|
|
|
|
while(e && e.nodeName.toLowerCase() !== "pre") {
|
|
|
|
e = e.nextSibling;
|
|
|
|
}
|
|
|
|
encryptedTiddlers.push($tw.utils.htmlDecode(e.innerHTML));
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 17:23:10 +00:00
|
|
|
// Prompt for the password
|
|
|
|
$tw.passwordPrompt.createPrompt({
|
|
|
|
serviceName: "Enter a password to decrypt this TiddlyWiki",
|
|
|
|
noUserName: true,
|
|
|
|
submitText: "Decrypt",
|
|
|
|
callback: function(data) {
|
|
|
|
// Attempt to decrypt the tiddlers
|
|
|
|
$tw.crypto.setPassword(data.password);
|
|
|
|
for(var t=encryptedTiddlers.length-1; t>=0; t--) {
|
|
|
|
var decrypted = $tw.crypto.decrypt(encryptedTiddlers[t]);
|
|
|
|
if(decrypted) {
|
|
|
|
var json = JSON.parse(decrypted);
|
|
|
|
for(var title in json) {
|
2012-11-16 17:44:47 +00:00
|
|
|
$tw.preloadTiddler(json[title]);
|
2012-11-16 17:23:10 +00:00
|
|
|
}
|
|
|
|
encryptedTiddlers.splice(t,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if we're all done
|
|
|
|
if(encryptedTiddlers.length === 0) {
|
|
|
|
// Call the callback
|
|
|
|
callback();
|
|
|
|
// Exit and remove the password prompt
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// We didn't decrypt everything, so continue to prompt for password
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-11-16 16:59:47 +00:00
|
|
|
} else {
|
2012-11-16 17:23:10 +00:00
|
|
|
// Just invoke the callback straight away if there wasn't any encrypted tiddlers
|
|
|
|
callback();
|
2012-11-16 16:59:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/*
|
|
|
|
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
|
|
|
|
*/
|
2012-04-30 18:14:39 +00:00
|
|
|
$tw.modules.execute = function(moduleName,moduleRoot) {
|
2012-11-15 12:38:25 +00:00
|
|
|
/*jslint evil: true */
|
2012-04-30 11:23:03 +00:00
|
|
|
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
|
|
|
|
require = function(modRequire) {
|
2012-04-30 18:14:39 +00:00
|
|
|
return $tw.modules.execute(modRequire,name);
|
2012-04-30 11:23:03 +00:00
|
|
|
},
|
|
|
|
exports = {},
|
2012-11-15 10:40:03 +00:00
|
|
|
moduleInfo = $tw.modules.titles[name];
|
|
|
|
if(!moduleInfo) {
|
2012-04-30 11:23:03 +00:00
|
|
|
throw new Error("Cannot find module named '" + moduleName + "' required by module '" + moduleRoot + "', resolved to " + name);
|
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
if(!moduleInfo.exports) {
|
|
|
|
if(typeof moduleInfo.definition === "string") { // String
|
|
|
|
moduleInfo.definition = window["eval"]("(function(module,exports,require) {" + moduleInfo.definition + "})");
|
|
|
|
moduleInfo.exports = {};
|
|
|
|
moduleInfo.definition(moduleInfo,moduleInfo.exports,require);
|
|
|
|
} else if(typeof moduleInfo.definition === "function") { // Function
|
|
|
|
moduleInfo.exports = {};
|
|
|
|
moduleInfo.definition(moduleInfo,moduleInfo.exports,require);
|
|
|
|
} else { // Object
|
|
|
|
moduleInfo.exports = moduleInfo.definition;
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
return moduleInfo.exports;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Register a deserializer that can extract tiddlers from the DOM
|
|
|
|
*/
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/dom","tiddlerdeserializer",{
|
2012-05-03 20:47:16 +00:00
|
|
|
"(DOM)": function(node) {
|
2012-09-02 19:29:47 +00:00
|
|
|
var extractTextTiddlers = function(node) {
|
2012-04-30 11:23:03 +00:00
|
|
|
var e = node.firstChild;
|
|
|
|
while(e && e.nodeName.toLowerCase() !== "pre") {
|
|
|
|
e = e.nextSibling;
|
|
|
|
}
|
|
|
|
var title = node.getAttribute ? node.getAttribute("title") : null;
|
|
|
|
if(e && title) {
|
|
|
|
var attrs = node.attributes,
|
|
|
|
tiddler = {
|
|
|
|
text: $tw.utils.htmlDecode(e.innerHTML)
|
|
|
|
};
|
|
|
|
for(var i=attrs.length-1; i >= 0; i--) {
|
2012-08-31 10:38:30 +00:00
|
|
|
tiddler[attrs[i].name] = attrs[i].value;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-09-02 19:29:47 +00:00
|
|
|
return [tiddler];
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2012-09-02 19:29:47 +00:00
|
|
|
extractModuleTiddlers = function(node) {
|
2012-04-30 11:23:03 +00:00
|
|
|
if(node.hasAttribute && node.hasAttribute("data-tiddler-title")) {
|
|
|
|
var text = node.innerHTML,
|
|
|
|
s = text.indexOf("{"),
|
|
|
|
e = text.lastIndexOf("}");
|
2012-05-05 16:42:42 +00:00
|
|
|
if(node.hasAttribute("data-module") && s !== -1 && e !== -1) {
|
2012-07-10 22:18:07 +00:00
|
|
|
text = text.substring(s+1,e);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-05-05 16:42:42 +00:00
|
|
|
var fields = {text: text},
|
|
|
|
attributes = node.attributes;
|
|
|
|
for(var a=0; a<attributes.length; a++) {
|
|
|
|
if(attributes[a].nodeName.substr(0,13) === "data-tiddler-") {
|
2012-08-31 10:38:30 +00:00
|
|
|
fields[attributes[a].nodeName.substr(13)] = attributes[a].value;
|
2012-05-05 16:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-02 19:29:47 +00:00
|
|
|
return [fields];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
t,result = [];
|
2012-10-10 15:32:37 +00:00
|
|
|
if(node) {
|
|
|
|
for(t = 0; t < node.childNodes.length; t++) {
|
|
|
|
var tiddlers = extractTextTiddlers(node.childNodes[t]),
|
|
|
|
childNode = node.childNodes[t];
|
|
|
|
tiddlers = tiddlers || extractModuleTiddlers(childNode);
|
|
|
|
if(tiddlers) {
|
|
|
|
result.push.apply(result,tiddlers);
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-09-02 19:29:47 +00:00
|
|
|
return result;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
|
|
|
|
$tw.loadTiddlers = function() {
|
|
|
|
// In the browser, we load tiddlers from certain elements
|
|
|
|
var containerIds = [
|
|
|
|
"libraryModules",
|
|
|
|
"modules",
|
|
|
|
"bootKernelPrefix",
|
|
|
|
"bootKernel",
|
|
|
|
"styleArea",
|
|
|
|
"storeArea",
|
|
|
|
"shadowArea"
|
|
|
|
];
|
|
|
|
for(var t=0; t<containerIds.length; t++) {
|
|
|
|
$tw.wiki.addTiddlers($tw.wiki.deserializeTiddlers("(DOM)",document.getElementById(containerIds[t])));
|
|
|
|
}
|
|
|
|
// Load any preloaded tiddlers
|
|
|
|
if($tw.preloadTiddlers) {
|
|
|
|
$tw.wiki.addTiddlers($tw.preloadTiddlers);
|
|
|
|
}
|
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-05-19 10:29:51 +00:00
|
|
|
// End of if($tw.browser)
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////// Server definitions
|
|
|
|
|
2012-05-19 10:29:51 +00:00
|
|
|
if(!$tw.browser) {
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/*
|
|
|
|
Get any encrypted tiddlers
|
|
|
|
*/
|
2012-11-16 17:23:10 +00:00
|
|
|
$tw.boot.decryptEncryptedTiddlers = function(callback) {
|
2012-11-16 16:59:47 +00:00
|
|
|
// Storing encrypted tiddlers on the server isn't supported yet
|
2012-11-16 17:23:10 +00:00
|
|
|
callback();
|
2012-11-16 16:59:47 +00:00
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/*
|
2012-08-30 14:20:57 +00:00
|
|
|
Load the tiddlers contained in a particular file (and optionally extract fields from the accompanying .meta file)
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.loadTiddlersFromFile = function(filepath,fields) {
|
2012-08-31 15:36:57 +00:00
|
|
|
var ext = path.extname(filepath),
|
2012-06-08 10:47:05 +00:00
|
|
|
extensionInfo = $tw.config.fileExtensionInfo[ext],
|
|
|
|
typeInfo = extensionInfo ? $tw.config.contentTypeInfo[extensionInfo.type] : null,
|
2012-11-13 08:46:44 +00:00
|
|
|
data = fs.readFileSync(filepath,typeInfo ? typeInfo.encoding : "utf8"),
|
2012-04-30 11:23:03 +00:00
|
|
|
tiddlers = $tw.wiki.deserializeTiddlers(ext,data,fields),
|
2012-08-31 15:36:57 +00:00
|
|
|
metafile = filepath + ".meta";
|
2012-07-13 16:08:15 +00:00
|
|
|
if(ext !== ".json" && tiddlers.length === 1 && fs.existsSync(metafile)) {
|
2012-11-13 08:46:44 +00:00
|
|
|
var metadata = fs.readFileSync(metafile,"utf8");
|
2012-04-30 11:23:03 +00:00
|
|
|
if(metadata) {
|
|
|
|
tiddlers = [$tw.utils.parseFields(metadata,tiddlers[0])];
|
|
|
|
}
|
|
|
|
}
|
2012-08-31 15:36:57 +00:00
|
|
|
return tiddlers;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-08-03 14:09:48 +00:00
|
|
|
Load all the tiddlers from a directory
|
2012-04-30 11:23:03 +00:00
|
|
|
*/
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.loadTiddlersFromPath = function(filepath,excludeRegExp) {
|
2012-05-03 20:47:16 +00:00
|
|
|
excludeRegExp = excludeRegExp || /^\.DS_Store$|.meta$/;
|
2012-11-06 17:21:56 +00:00
|
|
|
var tiddlers = [],
|
|
|
|
stat, files, pluginInfo, pluginTiddlers, f, file, titlePrefix, t, filesInfo, p, tidInfo, typeInfo, text;
|
2012-07-13 16:08:15 +00:00
|
|
|
if(fs.existsSync(filepath)) {
|
2012-11-06 17:21:56 +00:00
|
|
|
stat = fs.statSync(filepath);
|
2012-05-04 17:24:54 +00:00
|
|
|
if(stat.isDirectory()) {
|
2012-11-06 17:21:56 +00:00
|
|
|
files = fs.readdirSync(filepath);
|
2012-08-31 14:04:08 +00:00
|
|
|
// Look for a tiddlywiki.files file
|
2012-11-12 22:16:49 +00:00
|
|
|
if(files.indexOf("tiddlywiki.files") !== -1) {
|
2012-05-05 10:21:59 +00:00
|
|
|
// If so, process the files it describes
|
2012-11-13 08:46:44 +00:00
|
|
|
filesInfo = JSON.parse(fs.readFileSync(filepath + "/tiddlywiki.files","utf8"));
|
2012-11-06 17:21:56 +00:00
|
|
|
for(p=0; p<filesInfo.tiddlers.length; p++) {
|
|
|
|
tidInfo = filesInfo.tiddlers[p];
|
|
|
|
typeInfo = $tw.config.contentTypeInfo[tidInfo.fields.type || "text/plain"];
|
2012-11-13 08:46:44 +00:00
|
|
|
text = fs.readFileSync(path.resolve(filepath,tidInfo.file),typeInfo ? typeInfo.encoding : "utf8");
|
2012-08-31 16:25:18 +00:00
|
|
|
tidInfo.fields.text = text;
|
|
|
|
tiddlers.push(tidInfo.fields);
|
2012-05-05 10:21:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If not, read all the files in the directory
|
2012-11-06 17:21:56 +00:00
|
|
|
for(f=0; f<files.length; f++) {
|
|
|
|
file = files[f];
|
2012-05-05 10:21:59 +00:00
|
|
|
if(!excludeRegExp.test(file)) {
|
2012-11-12 22:16:49 +00:00
|
|
|
tiddlers.push.apply(tiddlers,$tw.loadTiddlersFromPath(filepath + "/" + file,excludeRegExp));
|
2012-05-05 10:21:59 +00:00
|
|
|
}
|
2012-05-04 17:24:54 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-05-04 17:24:54 +00:00
|
|
|
} else if(stat.isFile()) {
|
2012-11-12 22:16:49 +00:00
|
|
|
tiddlers.push.apply(tiddlers,$tw.loadTiddlersFromFile(filepath));
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 15:36:57 +00:00
|
|
|
return tiddlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2012-11-12 22:16:49 +00:00
|
|
|
Load the tiddlers from a bundle folder, and package them up into a proper JSON bundle tiddler
|
2012-08-31 15:36:57 +00:00
|
|
|
*/
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.loadBundleFolder = function(filepath,excludeRegExp) {
|
2012-11-13 08:46:44 +00:00
|
|
|
excludeRegExp = excludeRegExp || /^\.DS_Store$|.meta$/;
|
2012-11-14 11:23:43 +00:00
|
|
|
var stat, files, bundleInfo, bundleTiddlers = [], f, file, titlePrefix, t;
|
2012-11-13 08:46:44 +00:00
|
|
|
if(fs.existsSync(filepath)) {
|
|
|
|
stat = fs.statSync(filepath);
|
|
|
|
if(stat.isDirectory()) {
|
|
|
|
files = fs.readdirSync(filepath);
|
|
|
|
// Read the plugin information
|
|
|
|
bundleInfo = JSON.parse(fs.readFileSync(filepath + "/plugin.bundle","utf8"));
|
|
|
|
// Read the bundle files
|
|
|
|
for(f=0; f<files.length; f++) {
|
|
|
|
file = files[f];
|
|
|
|
if(!excludeRegExp.test(file) && file !== "plugin.bundle" && file !== "tiddlywiki.files") {
|
|
|
|
bundleTiddlers.push.apply(bundleTiddlers,$tw.loadTiddlersFromPath(filepath + "/" + file,excludeRegExp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save the bundle tiddlers into the bundle
|
|
|
|
bundleInfo.tiddlers = bundleInfo.tiddlers || {};
|
|
|
|
titlePrefix = bundleInfo.title + "/";
|
|
|
|
for(t=0; t<bundleTiddlers.length; t++) {
|
|
|
|
// 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) {
|
|
|
|
bundleInfo.tiddlers[bundleTiddlers[t].title.substr(titlePrefix.length)] = bundleTiddlers[t];
|
|
|
|
} else {
|
|
|
|
console.log("Error extracting plugin bundle: The bundle '" + bundleInfo.title + "' cannot contain a tiddler titled '" + bundleTiddlers[t].title + "'");
|
|
|
|
}
|
|
|
|
}
|
2012-11-12 22:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Save the bundle tiddler
|
2012-11-14 11:23:43 +00:00
|
|
|
return bundleInfo ? {
|
2012-11-12 22:16:49 +00:00
|
|
|
title: bundleInfo.title,
|
|
|
|
type: "application/json",
|
|
|
|
bundle: "yes",
|
|
|
|
text: JSON.stringify(bundleInfo)
|
2012-11-14 11:23:43 +00:00
|
|
|
} : null;
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
|
|
|
|
*/
|
2012-04-30 18:14:39 +00:00
|
|
|
$tw.modules.execute = function(moduleName,moduleRoot) {
|
2012-04-30 11:23:03 +00:00
|
|
|
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
|
2012-11-15 10:40:03 +00:00
|
|
|
moduleInfo = $tw.modules.titles[name],
|
2012-04-30 11:23:03 +00:00
|
|
|
tiddler = $tw.wiki.getTiddler(name),
|
|
|
|
sandbox = {
|
2012-11-15 10:40:03 +00:00
|
|
|
module: moduleInfo,
|
2012-04-30 11:23:03 +00:00
|
|
|
exports: {},
|
|
|
|
console: console,
|
2012-05-02 10:02:47 +00:00
|
|
|
process: process,
|
2012-04-30 11:23:03 +00:00
|
|
|
$tw: $tw,
|
|
|
|
require: function(title) {
|
2012-04-30 18:14:39 +00:00
|
|
|
return $tw.modules.execute(title,name);
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-15 10:40:03 +00:00
|
|
|
if(!moduleInfo) {
|
|
|
|
// If we don't have a module with that name, let node.js try to find it
|
2012-04-30 11:23:03 +00:00
|
|
|
return require(moduleName);
|
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
// Execute the module if we haven't already done so
|
|
|
|
if(!moduleInfo.exports) {
|
2012-05-26 22:37:03 +00:00
|
|
|
try {
|
2012-11-15 10:40:03 +00:00
|
|
|
// Check the type of the definition
|
|
|
|
if(typeof moduleInfo.definition === "string") { // String
|
|
|
|
vm.runInNewContext(moduleInfo.definition,sandbox,tiddler.fields.title);
|
|
|
|
moduleInfo.exports = sandbox.exports;
|
|
|
|
} else if(typeof moduleInfo.definition === "function") { // Function
|
|
|
|
moduleInfo.exports = moduleInfo.definition(moduleInfo,sandbox.require,moduleInfo.exports);
|
|
|
|
} else { // Object
|
|
|
|
moduleInfo.exports = moduleInfo.definition;
|
|
|
|
}
|
2012-05-26 22:37:03 +00:00
|
|
|
} catch(e) {
|
2012-11-15 10:40:03 +00:00
|
|
|
throw "Error executing boot module " + name + ":\n" + e;
|
2012-05-26 22:37:03 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
// Return the exports of the module
|
2012-11-15 10:40:03 +00:00
|
|
|
return moduleInfo.exports;
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.loadTiddlers = function() {
|
2012-11-12 22:16:49 +00:00
|
|
|
// On the server, we load tiddlers from specified folders
|
|
|
|
var folders = [
|
|
|
|
$tw.boot.bootPath,
|
|
|
|
path.resolve($tw.boot.wikiPath,$tw.config.wikiShadowsSubDir),
|
|
|
|
path.resolve($tw.boot.wikiPath,$tw.config.wikiTiddlersSubDir)
|
|
|
|
];
|
2012-11-15 10:40:03 +00:00
|
|
|
for(var t=0; t<folders.length; t++) {
|
2012-11-12 22:16:49 +00:00
|
|
|
$tw.wiki.addTiddlers($tw.loadTiddlersFromPath(folders[t]));
|
|
|
|
}
|
2012-11-14 11:23:43 +00:00
|
|
|
// Load any plugins listed in the wiki info file
|
2012-11-15 12:46:42 +00:00
|
|
|
var wikiInfoPath = path.resolve($tw.boot.wikiPath,$tw.config.wikiInfo),
|
|
|
|
bundle;
|
2012-11-13 08:46:44 +00:00
|
|
|
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++) {
|
2012-11-15 12:46:42 +00:00
|
|
|
bundle = $tw.loadBundleFolder(path.resolve(pluginBasePath,"./" + wikiInfo.plugins[t]));
|
2012-11-14 11:23:43 +00:00
|
|
|
if(bundle) {
|
|
|
|
$tw.wiki.addTiddler(bundle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load any plugins within the wiki folder
|
|
|
|
var wikiPluginsPath = path.resolve($tw.boot.wikiPath,$tw.config.wikiPluginsSubDir);
|
|
|
|
if(fs.existsSync(wikiPluginsPath)) {
|
|
|
|
var pluginFolders = fs.readdirSync(wikiPluginsPath);
|
|
|
|
for(t=0; t<pluginFolders.length; t++) {
|
2012-11-15 12:46:42 +00:00
|
|
|
bundle = $tw.loadBundleFolder(path.resolve(wikiPluginsPath,"./" + pluginFolders[t]));
|
2012-11-14 11:23:43 +00:00
|
|
|
if(bundle) {
|
|
|
|
$tw.wiki.addTiddler(bundle);
|
|
|
|
}
|
2012-11-13 08:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-15 10:40:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// End of if(!$tw.browser)
|
2012-11-12 22:16:49 +00:00
|
|
|
}
|
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/////////////////////////// Starting up
|
2012-08-06 21:34:16 +00:00
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/*
|
|
|
|
Main startup function
|
|
|
|
*/
|
|
|
|
$tw.boot.startup = function() {
|
|
|
|
// Install built in tiddler fields modules
|
|
|
|
$tw.Tiddler.fieldModules = $tw.modules.getModulesByTypeAsHashmap("tiddlerfield");
|
|
|
|
// Install the tiddler deserializer modules
|
|
|
|
$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules);
|
|
|
|
// Load tiddlers
|
|
|
|
$tw.loadTiddlers();
|
|
|
|
// Unpack bundle tiddlers
|
|
|
|
$tw.wiki.unpackBundleTiddlers();
|
|
|
|
// Register typed modules from the tiddlers we've just loaded
|
2012-11-17 13:08:25 +00:00
|
|
|
$tw.wiki.defineTiddlerModules();
|
|
|
|
// And any modules within bundles
|
2012-11-16 16:59:47 +00:00
|
|
|
$tw.wiki.defineBundledModules();
|
|
|
|
// Run any startup modules
|
|
|
|
$tw.modules.forEachModuleOfType("startup",function(title,module) {
|
|
|
|
if(module.startup) {
|
|
|
|
module.startup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
/*
|
2012-11-16 17:23:10 +00:00
|
|
|
Initialise crypto and then startup
|
2012-11-16 16:59:47 +00:00
|
|
|
*/
|
|
|
|
// Initialise crypto object
|
|
|
|
$tw.crypto = new $tw.utils.Crypto();
|
|
|
|
// Initialise password prompter
|
|
|
|
if($tw.browser) {
|
|
|
|
$tw.passwordPrompt = new $tw.utils.PasswordPrompt();
|
|
|
|
}
|
|
|
|
// Get any encrypted tiddlers
|
2012-11-16 17:23:10 +00:00
|
|
|
$tw.boot.decryptEncryptedTiddlers(function() {
|
2012-11-16 16:59:47 +00:00
|
|
|
$tw.boot.startup();
|
2012-11-16 17:23:10 +00:00
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
|
|
|
|
})();
|