2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2013-05-31 15:53:19 +00:00
|
|
|
title: $:/boot/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");
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:27:09 +00:00
|
|
|
$tw.utils = $tw.utils || {};
|
|
|
|
$tw.boot = $tw.boot || {};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2013-03-22 20:02:19 +00:00
|
|
|
/////////////////////////// Standard node.js libraries
|
|
|
|
|
|
|
|
var fs, path, vm;
|
|
|
|
if(!$tw.browser) {
|
|
|
|
fs = require("fs");
|
|
|
|
path = require("path");
|
|
|
|
vm = require("vm");
|
|
|
|
}
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
/////////////////////////// Utility functions
|
|
|
|
|
2012-11-19 16:55:10 +00:00
|
|
|
/*
|
|
|
|
Log a message
|
|
|
|
*/
|
|
|
|
$tw.utils.log = function(/* args */) {
|
|
|
|
if(console !== undefined && console.log !== undefined) {
|
2013-03-21 18:12:57 +00:00
|
|
|
return Function.apply.call(console.log, console, arguments);
|
2012-11-19 16:55:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-10 15:32:37 +00:00
|
|
|
/*
|
|
|
|
Check if an object has a property
|
|
|
|
*/
|
|
|
|
$tw.utils.hop = function(object,property) {
|
2013-05-15 16:32:17 +00:00
|
|
|
return object ? Object.prototype.hasOwnProperty.call(object,property) : false;
|
2012-10-10 15:32:37 +00:00
|
|
|
};
|
|
|
|
|
2012-11-16 17:44:47 +00:00
|
|
|
/*
|
2012-12-13 21:31:19 +00:00
|
|
|
Determine if a value is an array
|
|
|
|
*/
|
|
|
|
$tw.utils.isArray = function(value) {
|
|
|
|
return Object.prototype.toString.call(value) == "[object Array]";
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Iterate through all the own properties of an object or array. Callback is invoked with (element,title,object)
|
2012-11-16 17:44:47 +00:00
|
|
|
*/
|
|
|
|
$tw.utils.each = function(object,callback) {
|
2012-12-13 21:31:19 +00:00
|
|
|
var f;
|
2012-11-16 17:44:47 +00:00
|
|
|
if(object) {
|
2012-12-13 21:31:19 +00:00
|
|
|
if($tw.utils.isArray(object)) {
|
|
|
|
for(f=0; f<object.length; f++) {
|
2012-11-16 17:44:47 +00:00
|
|
|
callback(object[f],f,object);
|
|
|
|
}
|
2012-12-13 21:31:19 +00:00
|
|
|
} else {
|
|
|
|
for(f in object) {
|
|
|
|
if($tw.utils.hop(object,f)) {
|
|
|
|
callback(object[f],f,object);
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 17:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2013-06-26 22:52:21 +00:00
|
|
|
/*
|
|
|
|
Helper for making DOM elements
|
|
|
|
tag: tag name
|
|
|
|
options: see below
|
|
|
|
Options include:
|
|
|
|
attributes: hashmap of attribute values
|
|
|
|
text: text to add as a child node
|
|
|
|
children: array of further child nodes
|
|
|
|
innerHTML: optional HTML for element
|
|
|
|
class: class name(s)
|
|
|
|
document: defaults to current document
|
|
|
|
*/
|
|
|
|
$tw.utils.domMaker = function(tag,options) {
|
|
|
|
var doc = options.document || document;
|
|
|
|
var element = doc.createElement(tag);
|
|
|
|
if(options["class"]) {
|
|
|
|
element.className = options["class"];
|
|
|
|
}
|
|
|
|
if(options.text) {
|
|
|
|
element.appendChild(document.createTextNode(options.text));
|
|
|
|
}
|
|
|
|
$tw.utils.each(options.children,function(child) {
|
|
|
|
element.appendChild(child);
|
|
|
|
});
|
|
|
|
if(options.innerHTML) {
|
|
|
|
element.innerHTML = options.innerHTML;
|
|
|
|
}
|
|
|
|
$tw.utils.each(options.attributes,function(attribute,name) {
|
|
|
|
element.setAttribute(name,attribute);
|
|
|
|
});
|
|
|
|
return element;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Display an error and exit
|
|
|
|
*/
|
|
|
|
$tw.utils.error = function(err) {
|
|
|
|
// Prepare the error message
|
|
|
|
var errHeading = "Internal JavaScript Error",
|
|
|
|
promptMsg = "Well, this is embarrassing. It is recommended that you restart TiddlyWiki by refreshing your browser";
|
|
|
|
// Log the error to the console
|
|
|
|
console.error(err);
|
|
|
|
if($tw.browser) {
|
|
|
|
// Display an error message to the user
|
|
|
|
var dm = $tw.utils.domMaker,
|
|
|
|
heading = dm("h1",{text: errHeading}),
|
|
|
|
prompt = dm("div",{text: promptMsg, "class": "tw-error-prompt"}),
|
|
|
|
message = dm("div",{text: err}),
|
|
|
|
button = dm("button",{text: "close"}),
|
|
|
|
form = dm("form",{children: [heading,prompt,message,button], "class": "tw-error-form"});
|
|
|
|
document.body.insertBefore(form,document.body.firstChild);
|
|
|
|
form.addEventListener("submit",function(event) {
|
|
|
|
document.body.removeChild(form);
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
},true);
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// Exit if we're under node.js
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Use our custom error handler if we're in the browser
|
|
|
|
*/
|
|
|
|
if($tw.browser) {
|
|
|
|
window.onerror = function(errorMsg,url,lineNumber) {
|
|
|
|
$tw.utils.error(errorMsg);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:27:09 +00:00
|
|
|
/*
|
|
|
|
Extend an object with the properties from a list of source objects
|
|
|
|
*/
|
|
|
|
$tw.utils.extend = function(object /*, sourceObjectList */) {
|
|
|
|
$tw.utils.each(Array.prototype.slice.call(arguments,1),function(source) {
|
|
|
|
if(source) {
|
|
|
|
for (var p in source) {
|
|
|
|
object[p] = source[p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fill in any null or undefined properties of an object with the properties from a list of source objects. Each property that is an object is called recursively
|
|
|
|
*/
|
|
|
|
$tw.utils.deepDefaults = function(object /*, sourceObjectList */) {
|
|
|
|
$tw.utils.each(Array.prototype.slice.call(arguments,1),function(source) {
|
|
|
|
if(source) {
|
|
|
|
for (var p in source) {
|
|
|
|
if(object[p] == null) {
|
|
|
|
object[p] = source[p];
|
|
|
|
}
|
|
|
|
if(typeof object[p] === "object" && typeof source[p] === "object") {
|
|
|
|
$tw.utils.deepDefaults(object[p],source[p]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return object;
|
|
|
|
};
|
|
|
|
|
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) {
|
2013-07-04 15:55:47 +00:00
|
|
|
var item = match[1] || match[2];
|
|
|
|
if(results.indexOf(item) === -1) {
|
|
|
|
results.push(item);
|
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
};
|
|
|
|
|
2013-03-22 21:12:39 +00:00
|
|
|
/*
|
|
|
|
Register file type information
|
|
|
|
*/
|
|
|
|
$tw.utils.registerFileType = function(type,encoding,extension) {
|
|
|
|
$tw.config.fileExtensionInfo[extension] = {type: type};
|
|
|
|
$tw.config.contentTypeInfo[type] = {encoding: encoding, extension: extension};
|
|
|
|
}
|
|
|
|
|
2013-04-25 08:05:02 +00:00
|
|
|
/*
|
|
|
|
Run code globally with specified context variables in scope
|
|
|
|
*/
|
|
|
|
$tw.utils.evalGlobal = function(code,context,filename) {
|
|
|
|
var contextCopy = $tw.utils.extend({},context,{
|
|
|
|
exports: {}
|
|
|
|
});
|
|
|
|
// Get the context variables as a pair of arrays of names and values
|
|
|
|
var contextNames = [], contextValues = [];
|
|
|
|
$tw.utils.each(contextCopy,function(value,name) {
|
|
|
|
contextNames.push(name);
|
|
|
|
contextValues.push(value);
|
|
|
|
});
|
|
|
|
// Add the code prologue and epilogue
|
|
|
|
code = "(function(" + contextNames.join(",") + ") {(function(){\n" + code + ";})();\nreturn exports;\n})\n";
|
|
|
|
// Compile the code into a function
|
|
|
|
var fn;
|
|
|
|
if($tw.browser) {
|
|
|
|
fn = window["eval"](code);
|
|
|
|
} else {
|
|
|
|
fn = vm.runInThisContext(code,filename);
|
|
|
|
}
|
|
|
|
// Call the function and return the exports
|
|
|
|
return fn.apply(null,contextValues);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Run code in a sandbox with only the specified context variables in scope
|
|
|
|
*/
|
|
|
|
$tw.utils.evalSandboxed = $tw.browser ? $tw.utils.evalGlobal : function(code,context,filename) {
|
|
|
|
var sandbox = $tw.utils.extend({},context);
|
|
|
|
$tw.utils.extend(sandbox,{
|
|
|
|
exports: {}
|
|
|
|
});
|
|
|
|
vm.runInNewContext(code,sandbox,filename);
|
|
|
|
return sandbox.exports;
|
|
|
|
};
|
|
|
|
|
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
|
2013-06-26 22:52:21 +00:00
|
|
|
this.promptWrapper = $tw.utils.domMaker("div",{"class":"tw-password-wrapper"});
|
2012-11-16 16:59:47 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-03-08 17:50:28 +00:00
|
|
|
Adds a new password prompt. Options are:
|
|
|
|
submitText: text to use for submit button (defaults to "Login")
|
|
|
|
serviceName: text of the human readable service name
|
|
|
|
noUserName: set true to disable username prompt
|
|
|
|
callback: function to be called on submission with parameter of object {username:,password:}. Callback must return `true` to remove the password prompt
|
2012-11-16 16:59:47 +00:00
|
|
|
*/
|
|
|
|
$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",
|
2013-06-26 22:52:21 +00:00
|
|
|
dm = $tw.utils.domMaker,
|
|
|
|
children = [dm("h1",{text: options.serviceName})];
|
2012-11-16 16:59:47 +00:00
|
|
|
if(!options.noUserName) {
|
2013-06-26 22:52:21 +00:00
|
|
|
children.push(dm("input",{
|
|
|
|
attributes: {type: "text", name: "username", placeholder: "Username"},
|
|
|
|
"class": "input-small"
|
|
|
|
}));
|
2012-11-16 16:59:47 +00:00
|
|
|
}
|
2013-06-26 22:52:21 +00:00
|
|
|
children.push(dm("input",{
|
|
|
|
attributes: {type: "password", name: "password", placeholder: "Password"},
|
|
|
|
"class": "input-small"
|
|
|
|
}));
|
|
|
|
children.push(dm("button",{
|
|
|
|
attributes: {type: "submit"},
|
|
|
|
text: submitText,
|
|
|
|
"class": "btn"
|
|
|
|
}));
|
|
|
|
var form = dm("form",{
|
|
|
|
"class": "form-inline",
|
|
|
|
attributes: {autocomplete: "off"},
|
|
|
|
children: children
|
|
|
|
});
|
2012-11-16 16:59:47 +00:00
|
|
|
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;
|
2013-03-22 21:12:39 +00:00
|
|
|
$tw.utils.each(form.elements,function(element) {
|
|
|
|
if(element.name && element.value) {
|
|
|
|
data[element.name] = element.value;
|
2012-11-16 16:59:47 +00:00
|
|
|
}
|
2013-03-22 21:12:39 +00:00
|
|
|
});
|
2012-11-16 16:59:47 +00:00
|
|
|
// 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
|
2013-03-22 21:12:39 +00:00
|
|
|
$tw.utils.each(form.elements,function(element) {
|
|
|
|
if(element.name === "password") {
|
2013-05-07 14:52:21 +00:00
|
|
|
element.value = "";
|
2012-11-16 22:40:56 +00:00
|
|
|
}
|
2013-03-22 21:12:39 +00:00
|
|
|
});
|
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();
|
|
|
|
};
|
|
|
|
|
2013-01-31 10:20:13 +00:00
|
|
|
/*
|
|
|
|
Crypto helper object for encrypted content. It maintains the password text in a closure, and provides methods to change
|
|
|
|
the password, and to encrypt/decrypt a block of text
|
|
|
|
*/
|
2012-11-16 16:59:47 +00:00
|
|
|
$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;
|
2013-01-31 10:20:13 +00:00
|
|
|
this.updateCryptoStateTiddler();
|
|
|
|
};
|
|
|
|
this.updateCryptoStateTiddler = function() {
|
2013-03-22 19:44:12 +00:00
|
|
|
if($tw.wiki && $tw.wiki.addTiddler) {
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler({title: "$:/isEncrypted", text: password ? "yes" : "no"}));
|
|
|
|
}
|
2012-11-16 16:59:47 +00:00
|
|
|
};
|
2013-01-31 10:20:13 +00:00
|
|
|
this.hasPassword = function() {
|
|
|
|
return !!password;
|
|
|
|
}
|
2012-11-16 16:59:47 +00:00
|
|
|
this.encrypt = function(text) {
|
|
|
|
return callSjcl("encrypt",text);
|
|
|
|
};
|
|
|
|
this.decrypt = function(text) {
|
|
|
|
return callSjcl("decrypt",text);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-08-03 14:09:48 +00:00
|
|
|
/////////////////////////// Module mechanism
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2013-04-25 08:05:02 +00:00
|
|
|
/*
|
|
|
|
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
|
|
|
|
*/
|
|
|
|
$tw.modules.execute = function(moduleName,moduleRoot) {
|
|
|
|
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
|
|
|
|
moduleInfo = $tw.modules.titles[name],
|
|
|
|
tiddler = $tw.wiki.getTiddler(name),
|
|
|
|
sandbox = {
|
|
|
|
module: moduleInfo,
|
|
|
|
exports: {},
|
|
|
|
console: console,
|
|
|
|
setInterval: setInterval,
|
|
|
|
clearInterval: clearInterval,
|
|
|
|
setTimeout: setTimeout,
|
|
|
|
clearTimeout: clearTimeout,
|
|
|
|
$tw: $tw,
|
|
|
|
require: function(title) {
|
|
|
|
return $tw.modules.execute(title,name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if(!$tw.browser) {
|
|
|
|
$tw.utils.extend(sandbox,{
|
|
|
|
process: process
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(!moduleInfo) {
|
|
|
|
if($tw.browser) {
|
|
|
|
return $tw.utils.error("Cannot find module named '" + moduleName + "' required by module '" + moduleRoot + "', resolved to " + name);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If we don't have a module with that name, let node.js try to find it
|
|
|
|
return require(moduleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Execute the module if we haven't already done so
|
|
|
|
if(!moduleInfo.exports) {
|
|
|
|
try {
|
|
|
|
// Check the type of the definition
|
2013-07-03 17:36:51 +00:00
|
|
|
if(typeof moduleInfo.definition === "function") { // Function
|
|
|
|
moduleInfo.exports = {};
|
|
|
|
moduleInfo.definition(moduleInfo,moduleInfo.exports,sandbox.require);
|
|
|
|
} else if(typeof moduleInfo.definition === "string") { // String
|
2013-04-25 08:05:02 +00:00
|
|
|
moduleInfo.exports = $tw.utils.evalSandboxed(moduleInfo.definition,sandbox,tiddler.fields.title);
|
|
|
|
} else { // Object
|
|
|
|
moduleInfo.exports = moduleInfo.definition;
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
$tw.utils.error("Error executing boot module " + name + ":\n" + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return the exports of the module
|
|
|
|
return moduleInfo.exports;
|
|
|
|
};
|
|
|
|
|
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-12-13 21:31:19 +00:00
|
|
|
if(!targetObject) {
|
|
|
|
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-12-13 21:31:19 +00:00
|
|
|
return targetObject;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
2012-12-26 22:02:59 +00:00
|
|
|
/*
|
|
|
|
Return an array of classes created from the modules of a specified type. Each module should export the properties to be added to those of the optional base class
|
|
|
|
*/
|
|
|
|
$tw.modules.createClassesFromModules = function(moduleType,subType,baseClass) {
|
|
|
|
var classes = {};
|
|
|
|
$tw.modules.forEachModuleOfType(moduleType,function(title,moduleExports) {
|
|
|
|
if(!subType || moduleExports.types[subType]) {
|
|
|
|
var newClass = function() {};
|
|
|
|
if(baseClass) {
|
|
|
|
newClass.prototype = new baseClass();
|
|
|
|
newClass.prototype.constructor = baseClass;
|
|
|
|
}
|
|
|
|
$tw.utils.extend(newClass.prototype,moduleExports);
|
|
|
|
classes[moduleExports.name] = newClass;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return classes;
|
|
|
|
};
|
|
|
|
|
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];
|
2013-06-13 08:16:07 +00:00
|
|
|
if(fieldModule && fieldModule.parse) {
|
2012-08-03 14:09:48 +00:00
|
|
|
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
|
|
|
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
|
|
|
|
});
|
2013-06-13 08:16:07 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerfields/color","tiddlerfield",{
|
|
|
|
name: "color",
|
|
|
|
editType: "color"
|
|
|
|
});
|
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 = {};
|
2013-04-30 21:56:17 +00:00
|
|
|
this.plugins = []; // Array of registered plugins, ordered by priority
|
2013-04-03 12:23:26 +00:00
|
|
|
this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:}
|
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
|
|
|
/*
|
2013-04-30 21:56:17 +00:00
|
|
|
Register the plugin tiddlers of a particular type, optionally restricting registration to an array of tiddler titles. Return the array of titles affected
|
2012-08-06 21:34:16 +00:00
|
|
|
*/
|
2013-04-30 21:56:17 +00:00
|
|
|
$tw.Wiki.prototype.registerPluginTiddlers = function(pluginType,titles) {
|
2013-04-08 17:37:49 +00:00
|
|
|
var self = this,
|
2013-04-30 21:56:17 +00:00
|
|
|
registeredTitles = [];
|
|
|
|
// Go through the provided titles, or the entire tiddler list, looking for plugins of this type
|
|
|
|
var checkTiddler = function(tiddler) {
|
|
|
|
if(tiddler && tiddler.fields.type === "application/json" && tiddler.hasField("plugin") && tiddler.fields["plugin-type"] === pluginType) {
|
|
|
|
self.plugins.push(tiddler);
|
|
|
|
registeredTitles.push(tiddler.fields.title);
|
2012-08-06 21:34:16 +00:00
|
|
|
}
|
2013-04-30 21:56:17 +00:00
|
|
|
};
|
|
|
|
if(titles) {
|
|
|
|
$tw.utils.each(titles,function(title) {
|
|
|
|
checkTiddler(self.getTiddler(title));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$tw.utils.each(this.tiddlers,function(tiddler,title) {
|
|
|
|
checkTiddler(tiddler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return registeredTitles;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Unregister the plugin tiddlers of a particular type, returning an array of the titles affected
|
|
|
|
*/
|
|
|
|
$tw.Wiki.prototype.unregisterPluginTiddlers = function(pluginType) {
|
|
|
|
var self = this,
|
|
|
|
titles = [];
|
|
|
|
// Remove any previous registered plugins of this type
|
|
|
|
for(var t=this.plugins.length-1; t>=0; t--) {
|
|
|
|
var tiddler = this.plugins[t];
|
|
|
|
if(tiddler.fields["plugin-type"] === pluginType) {
|
|
|
|
titles.push(tiddler.fields.title);
|
|
|
|
this.plugins.splice(t,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return titles;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Unpack the currently registered plugins, creating shadow tiddlers for their constituent tiddlers
|
|
|
|
*/
|
|
|
|
$tw.Wiki.prototype.unpackPluginTiddlers = function() {
|
|
|
|
var self = this;
|
|
|
|
// Sort the plugin titles by the `plugin-priority` field
|
|
|
|
this.plugins.sort(function(a,b) {
|
2013-04-28 21:52:26 +00:00
|
|
|
if("plugin-priority" in a.fields && "plugin-priority" in b.fields) {
|
|
|
|
return a.fields["plugin-priority"] - b.fields["plugin-priority"];
|
|
|
|
} else if("plugin-priority" in a.fields) {
|
2013-04-08 17:37:49 +00:00
|
|
|
return -1;
|
2013-04-28 21:52:26 +00:00
|
|
|
} else if("plugin-priority" in b.fields) {
|
2013-04-08 17:37:49 +00:00
|
|
|
return +1;
|
|
|
|
} else if(a.fields.title < b.fields.title) {
|
|
|
|
return -1;
|
|
|
|
} else if(a.fields.title === b.fields.title) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return +1;
|
|
|
|
}
|
|
|
|
});
|
2013-04-30 21:56:17 +00:00
|
|
|
// Now go through the plugins in ascending order and assign the shadows
|
|
|
|
this.shadowTiddlers = {};
|
|
|
|
$tw.utils.each(this.plugins,function(tiddler) {
|
|
|
|
// Get the plugin information
|
|
|
|
var pluginInfo = JSON.parse(tiddler.fields.text);
|
2013-04-08 17:37:49 +00:00
|
|
|
// Extract the constituent tiddlers
|
|
|
|
$tw.utils.each(pluginInfo.tiddlers,function(constituentTiddler,constituentTitle) {
|
|
|
|
// Save the tiddler object
|
|
|
|
self.shadowTiddlers[constituentTitle] = {
|
|
|
|
source: tiddler.fields.title,
|
|
|
|
tiddler: new $tw.Tiddler(constituentTiddler,{title: constituentTitle})
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
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":
|
2013-04-10 15:56:17 +00:00
|
|
|
// We only define modules that haven't already been defined, because in the browser modules in system tiddlers are defined in inline script
|
|
|
|
if(!$tw.utils.hop($tw.modules.titles,tiddler.fields.title)) {
|
2012-11-17 13:08:25 +00:00
|
|
|
$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
|
|
|
|
*/
|
2013-04-30 21:56:17 +00:00
|
|
|
$tw.Wiki.prototype.defineShadowModules = function() {
|
2012-11-16 17:44:47 +00:00
|
|
|
var self = this;
|
2013-04-03 12:23:26 +00:00
|
|
|
$tw.utils.each(this.shadowTiddlers,function(element,title) {
|
2012-11-16 17:44:47 +00:00
|
|
|
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
|
2013-03-28 17:07:30 +00:00
|
|
|
if(tiddler.hasField("module-type")) {
|
2012-11-16 17:44:47 +00:00
|
|
|
// 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;
|
2013-03-23 21:41:28 +00:00
|
|
|
} else if($tw.utils.hop(this.shadowTiddlers,title)) {
|
2013-04-03 12:23:26 +00:00
|
|
|
return this.shadowTiddlers[title].tiddler;
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2013-04-01 17:55:49 +00:00
|
|
|
return deserializer.call(this,text,fields,type);
|
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);
|
2012-12-20 09:20:44 +00:00
|
|
|
if(split.length >= 1) {
|
2012-05-09 12:48:34 +00:00
|
|
|
fields = $tw.utils.parseFields(split[0],fields);
|
2012-12-20 09:20:44 +00:00
|
|
|
}
|
|
|
|
if(split.length >= 2) {
|
2012-05-09 12:48:34 +00:00
|
|
|
fields.text = split.slice(1).join("\n\n");
|
2012-04-30 11:23:03 +00:00
|
|
|
} else {
|
2012-12-20 09:20:44 +00:00
|
|
|
fields.text = "";
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
return [fields];
|
|
|
|
}
|
|
|
|
});
|
2012-11-15 10:40:03 +00:00
|
|
|
$tw.modules.define("$:/boot/tiddlerdeserializer/txt","tiddlerdeserializer",{
|
2013-04-01 17:55:49 +00:00
|
|
|
"text/plain": function(text,fields,type) {
|
2012-06-06 20:42:14 +00:00
|
|
|
fields.text = text;
|
2013-04-01 17:55:49 +00:00
|
|
|
fields.type = type || "text/plain";
|
2012-06-06 20:42:14 +00:00
|
|
|
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
|
|
|
/////////////////////////// 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
|
|
|
/*
|
2013-01-31 10:20:13 +00:00
|
|
|
Decrypt any tiddlers stored within the element with the ID "encryptedArea". The function is asynchronous to allow the user to be prompted for a password
|
|
|
|
callback: function to be called the decryption is complete
|
2012-11-16 16:59:47 +00:00
|
|
|
*/
|
2012-11-16 17:23:10 +00:00
|
|
|
$tw.boot.decryptEncryptedTiddlers = function(callback) {
|
2013-01-31 10:20:13 +00:00
|
|
|
var encryptedArea = document.getElementById("encryptedStoreArea");
|
2012-11-16 16:59:47 +00:00
|
|
|
if(encryptedArea) {
|
2013-01-31 10:20:13 +00:00
|
|
|
var encryptedText = encryptedArea.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);
|
2013-01-31 10:20:13 +00:00
|
|
|
var decryptedText = $tw.crypto.decrypt(encryptedText);
|
|
|
|
if(decryptedText) {
|
|
|
|
var json = JSON.parse(decryptedText);
|
|
|
|
for(var title in json) {
|
|
|
|
$tw.preloadTiddler(json[title]);
|
2012-11-16 17:23:10 +00:00
|
|
|
}
|
|
|
|
// 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 {
|
2013-01-31 10:20:13 +00:00
|
|
|
// Just invoke the callback straight away if there weren't any encrypted tiddlers
|
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
|
|
|
/*
|
|
|
|
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++) {
|
2013-06-27 08:50:46 +00:00
|
|
|
var childNode = node.childNodes[t],
|
|
|
|
tiddlers = extractTextTiddlers(childNode);
|
2012-10-10 15:32:37 +00:00
|
|
|
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",
|
2013-03-15 22:00:19 +00:00
|
|
|
"systemArea"
|
2012-11-15 10:40:03 +00:00
|
|
|
];
|
|
|
|
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
|
|
|
/*
|
2013-03-23 22:11:23 +00:00
|
|
|
Load the tiddlers contained in a particular file (and optionally extract fields from the accompanying .meta file) returned as {filepath:,type:,tiddlers:[],hasMetaFile:}
|
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],
|
2013-03-23 22:11:23 +00:00
|
|
|
type = extensionInfo ? extensionInfo.type : null,
|
|
|
|
typeInfo = type ? $tw.config.contentTypeInfo[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),
|
2013-03-23 22:11:23 +00:00
|
|
|
metafile = filepath + ".meta",
|
|
|
|
metadata;
|
2012-07-13 16:08:15 +00:00
|
|
|
if(ext !== ".json" && tiddlers.length === 1 && fs.existsSync(metafile)) {
|
2013-03-23 22:11:23 +00:00
|
|
|
metadata = fs.readFileSync(metafile,"utf8");
|
2012-04-30 11:23:03 +00:00
|
|
|
if(metadata) {
|
|
|
|
tiddlers = [$tw.utils.parseFields(metadata,tiddlers[0])];
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 22:11:23 +00:00
|
|
|
return {filepath: filepath, type: type, tiddlers: tiddlers, hasMetaFile: !!metadata};
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-03-23 22:43:05 +00:00
|
|
|
Load all the tiddlers recursively from a directory, including honouring `tiddlywiki.files` files for drawing in external files. Returns an array of {filepath:,type:,tiddlers: [{..fields...}],hasMetaFile:}. Note that no file information is returned for externally loaded tiddlers, just the `tiddlers` property.
|
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$/;
|
2013-03-23 21:37:48 +00:00
|
|
|
var tiddlers = [];
|
2012-07-13 16:08:15 +00:00
|
|
|
if(fs.existsSync(filepath)) {
|
2013-03-23 21:37:48 +00:00
|
|
|
var stat = fs.statSync(filepath);
|
2012-05-04 17:24:54 +00:00
|
|
|
if(stat.isDirectory()) {
|
2013-03-23 21:37:48 +00:00
|
|
|
var 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
|
2013-03-23 21:37:48 +00:00
|
|
|
var filesInfo = JSON.parse(fs.readFileSync(filepath + "/tiddlywiki.files","utf8"));
|
|
|
|
$tw.utils.each(filesInfo.tiddlers,function(tidInfo) {
|
|
|
|
var typeInfo = $tw.config.contentTypeInfo[tidInfo.fields.type || "text/plain"],
|
|
|
|
pathname = path.resolve(filepath,tidInfo.file),
|
|
|
|
text = fs.readFileSync(pathname,typeInfo ? typeInfo.encoding : "utf8");
|
2013-04-25 08:05:02 +00:00
|
|
|
if(tidInfo.prefix) {
|
|
|
|
text = tidInfo.prefix + text;
|
|
|
|
}
|
2013-07-14 22:06:37 +00:00
|
|
|
if(tidInfo.suffix) {
|
|
|
|
text = text + tidInfo.suffix;
|
|
|
|
}
|
2012-08-31 16:25:18 +00:00
|
|
|
tidInfo.fields.text = text;
|
2013-04-03 20:10:57 +00:00
|
|
|
tiddlers.push({tiddlers: [tidInfo.fields]});
|
2013-03-23 21:37:48 +00:00
|
|
|
});
|
2012-05-05 10:21:59 +00:00
|
|
|
} else {
|
|
|
|
// If not, read all the files in the directory
|
2013-03-23 21:37:48 +00:00
|
|
|
$tw.utils.each(files,function(file) {
|
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
|
|
|
}
|
2013-03-23 21:37:48 +00:00
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-05-04 17:24:54 +00:00
|
|
|
} else if(stat.isFile()) {
|
2013-03-23 22:11:23 +00:00
|
|
|
tiddlers.push($tw.loadTiddlersFromFile(filepath));
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 15:36:57 +00:00
|
|
|
return tiddlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2013-03-23 16:34:12 +00:00
|
|
|
Load the tiddlers from a plugin folder, and package them up into a proper JSON plugin tiddler
|
2012-08-31 15:36:57 +00:00
|
|
|
*/
|
2013-03-23 16:34:12 +00:00
|
|
|
$tw.loadPluginFolder = function(filepath,excludeRegExp) {
|
2012-11-13 08:46:44 +00:00
|
|
|
excludeRegExp = excludeRegExp || /^\.DS_Store$|.meta$/;
|
2013-03-23 16:34:12 +00:00
|
|
|
var stat, files, pluginInfo, pluginTiddlers = [], f, file, titlePrefix, t;
|
2012-11-13 08:46:44 +00:00
|
|
|
if(fs.existsSync(filepath)) {
|
|
|
|
stat = fs.statSync(filepath);
|
|
|
|
if(stat.isDirectory()) {
|
|
|
|
// Read the plugin information
|
2013-03-23 16:34:12 +00:00
|
|
|
pluginInfo = JSON.parse(fs.readFileSync(filepath + "/plugin.info","utf8"));
|
|
|
|
// Read the plugin files
|
2013-03-23 22:11:23 +00:00
|
|
|
files = fs.readdirSync(filepath);
|
2012-11-13 08:46:44 +00:00
|
|
|
for(f=0; f<files.length; f++) {
|
|
|
|
file = files[f];
|
2013-03-23 16:34:12 +00:00
|
|
|
if(!excludeRegExp.test(file) && file !== "plugin.info" && file !== "tiddlywiki.files") {
|
2013-03-23 21:37:48 +00:00
|
|
|
var tiddlerFiles = $tw.loadTiddlersFromPath(filepath + "/" + file,excludeRegExp);
|
|
|
|
$tw.utils.each(tiddlerFiles,function(tiddlerFile) {
|
|
|
|
pluginTiddlers.push.apply(pluginTiddlers,tiddlerFile.tiddlers);
|
|
|
|
});
|
2012-11-13 08:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 16:34:12 +00:00
|
|
|
// Save the plugin tiddlers into the plugin info
|
|
|
|
pluginInfo.tiddlers = pluginInfo.tiddlers || {};
|
|
|
|
for(t=0; t<pluginTiddlers.length; t++) {
|
2013-03-28 17:15:52 +00:00
|
|
|
pluginInfo.tiddlers[pluginTiddlers[t].title] = pluginTiddlers[t];
|
2012-11-13 08:46:44 +00:00
|
|
|
}
|
2012-11-12 22:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 16:34:12 +00:00
|
|
|
// Save the plugin tiddler
|
2013-04-08 17:37:49 +00:00
|
|
|
if(pluginInfo) {
|
|
|
|
var fields = {
|
|
|
|
title: pluginInfo.title,
|
|
|
|
type: "application/json",
|
|
|
|
plugin: "yes",
|
2013-04-28 17:16:22 +00:00
|
|
|
text: JSON.stringify(pluginInfo,null,4),
|
2013-04-28 21:52:26 +00:00
|
|
|
"plugin-priority": pluginInfo["plugin-priority"],
|
2013-05-01 11:01:43 +00:00
|
|
|
"name": pluginInfo["name"],
|
|
|
|
"thumbnail": pluginInfo["thumbnail"],
|
|
|
|
"description": pluginInfo["description"],
|
2013-04-28 21:52:26 +00:00
|
|
|
"plugin-type": pluginInfo["plugin-type"] || "plugin"
|
2013-04-08 17:37:49 +00:00
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2012-05-04 17:49:04 +00:00
|
|
|
};
|
2012-04-30 11:23:03 +00:00
|
|
|
|
2013-03-25 18:55:41 +00:00
|
|
|
/*
|
|
|
|
path: path of wiki directory
|
|
|
|
parentPaths: array of parent paths that we mustn't recurse into
|
|
|
|
*/
|
|
|
|
$tw.loadWikiTiddlers = function(wikiPath,parentPaths) {
|
|
|
|
parentPaths = parentPaths || [];
|
|
|
|
var wikiInfoPath = path.resolve(wikiPath,$tw.config.wikiInfo),
|
|
|
|
wikiInfo = {},
|
|
|
|
pluginFields;
|
2013-03-28 14:06:50 +00:00
|
|
|
// Bail if we don't have a wiki info file
|
|
|
|
if(!fs.existsSync(wikiInfoPath)) {
|
|
|
|
$tw.utils.error("Missing tiddlywiki.info file at " + wikiPath);
|
|
|
|
}
|
|
|
|
wikiInfo = JSON.parse(fs.readFileSync(wikiInfoPath,"utf8"));
|
|
|
|
// Load any parent wikis
|
|
|
|
if(wikiInfo.includeWikis) {
|
|
|
|
parentPaths = parentPaths.slice(0);
|
|
|
|
parentPaths.push(wikiPath);
|
|
|
|
$tw.utils.each(wikiInfo.includeWikis,function(includedWikiPath) {
|
|
|
|
var resolvedIncludedWikiPath = path.resolve(wikiPath,includedWikiPath);
|
|
|
|
if(parentPaths.indexOf(resolvedIncludedWikiPath) === -1) {
|
|
|
|
$tw.loadWikiTiddlers(resolvedIncludedWikiPath,parentPaths);
|
|
|
|
} else {
|
|
|
|
$tw.utils.error("Cannot recursively include wiki " + resolvedIncludedWikiPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Load any plugins listed in the wiki info file
|
|
|
|
if(wikiInfo.plugins) {
|
2013-03-28 17:07:30 +00:00
|
|
|
var pluginBasePath = path.resolve($tw.boot.corePath,$tw.config.pluginsPath);
|
2013-03-28 14:06:50 +00:00
|
|
|
for(var t=0; t<wikiInfo.plugins.length; t++) {
|
|
|
|
pluginFields = $tw.loadPluginFolder(path.resolve(pluginBasePath,"./" + wikiInfo.plugins[t]));
|
|
|
|
if(pluginFields) {
|
|
|
|
$tw.wiki.addTiddler(pluginFields);
|
2013-03-25 18:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-28 08:42:48 +00:00
|
|
|
// Load any themes listed in the wiki info file
|
|
|
|
if(wikiInfo.themes) {
|
|
|
|
var themesBasePath = path.resolve($tw.boot.corePath,$tw.config.themesPath);
|
|
|
|
for(var t=0; t<wikiInfo.themes.length; t++) {
|
|
|
|
pluginFields = $tw.loadPluginFolder(path.resolve(themesBasePath,"./" + wikiInfo.themes[t]));
|
|
|
|
if(pluginFields) {
|
|
|
|
$tw.wiki.addTiddler(pluginFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 22:11:23 +00:00
|
|
|
// Load the wiki files, registering them as writable
|
2013-03-25 18:55:41 +00:00
|
|
|
var resolvedWikiPath = path.resolve(wikiPath,$tw.config.wikiTiddlersSubDir);
|
|
|
|
$tw.utils.each($tw.loadTiddlersFromPath(resolvedWikiPath),function(tiddlerFile) {
|
2013-03-23 22:11:23 +00:00
|
|
|
$tw.wiki.addTiddlers(tiddlerFile.tiddlers);
|
2013-03-23 22:43:05 +00:00
|
|
|
if(tiddlerFile.filepath) {
|
|
|
|
$tw.utils.each(tiddlerFile.tiddlers,function(tiddler) {
|
|
|
|
$tw.boot.files[tiddler.title] = {
|
|
|
|
filepath: tiddlerFile.filepath,
|
|
|
|
type: tiddlerFile.type,
|
|
|
|
hasMetaFile: tiddlerFile.hasMetaFile
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2013-03-23 21:37:48 +00:00
|
|
|
});
|
2012-11-14 11:23:43 +00:00
|
|
|
// Load any plugins within the wiki folder
|
2013-03-25 18:55:41 +00:00
|
|
|
var wikiPluginsPath = path.resolve(wikiPath,$tw.config.wikiPluginsSubDir);
|
2012-11-14 11:23:43 +00:00
|
|
|
if(fs.existsSync(wikiPluginsPath)) {
|
|
|
|
var pluginFolders = fs.readdirSync(wikiPluginsPath);
|
|
|
|
for(t=0; t<pluginFolders.length; t++) {
|
2013-03-23 16:34:12 +00:00
|
|
|
pluginFields = $tw.loadPluginFolder(path.resolve(wikiPluginsPath,"./" + pluginFolders[t]));
|
|
|
|
if(pluginFields) {
|
|
|
|
$tw.wiki.addTiddler(pluginFields);
|
2012-11-14 11:23:43 +00:00
|
|
|
}
|
2012-11-13 08:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-14 22:08:04 +00:00
|
|
|
// Load any themes within the wiki folder
|
|
|
|
var wikiThemesPath = path.resolve(wikiPath,$tw.config.wikiThemesSubDir);
|
|
|
|
if(fs.existsSync(wikiThemesPath)) {
|
|
|
|
var themeFolders = fs.readdirSync(wikiThemesPath);
|
|
|
|
for(t=0; t<themeFolders.length; t++) {
|
|
|
|
pluginFields = $tw.loadPluginFolder(path.resolve(wikiThemesPath,"./" + themeFolders[t]));
|
|
|
|
if(pluginFields) {
|
|
|
|
$tw.wiki.addTiddler(pluginFields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 18:55:41 +00:00
|
|
|
return wikiInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
$tw.loadTiddlers = function() {
|
2013-03-28 17:07:30 +00:00
|
|
|
// Load the boot tiddlers
|
2013-03-25 18:55:41 +00:00
|
|
|
$tw.utils.each($tw.loadTiddlersFromPath($tw.boot.bootPath),function(tiddlerFile) {
|
|
|
|
$tw.wiki.addTiddlers(tiddlerFile.tiddlers);
|
|
|
|
});
|
2013-03-28 17:07:30 +00:00
|
|
|
// Load the core tiddlers
|
|
|
|
$tw.wiki.addTiddler($tw.loadPluginFolder($tw.boot.corePath));
|
2013-03-25 18:55:41 +00:00
|
|
|
// Load the tiddlers from the wiki directory
|
|
|
|
$tw.boot.wikiInfo = $tw.loadWikiTiddlers($tw.boot.wikiPath);
|
2012-11-15 10:40:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// End of if(!$tw.browser)
|
2012-11-12 22:16:49 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 20:02:19 +00:00
|
|
|
/////////////////////////// Main startup function called once tiddlers have been decrypted
|
2012-08-06 21:34:16 +00:00
|
|
|
|
2012-11-16 16:59:47 +00:00
|
|
|
$tw.boot.startup = function() {
|
2013-03-25 10:43:18 +00:00
|
|
|
// Initialise some more $tw properties
|
|
|
|
$tw.utils.deepDefaults($tw,{
|
|
|
|
modules: { // Information about each module
|
|
|
|
titles: {}, // hashmap by module title of {fn:, exports:, moduleType:}
|
|
|
|
types: {} // hashmap by module type of hashmap of exports
|
|
|
|
},
|
|
|
|
config: { // Configuration overridables
|
|
|
|
pluginsPath: "../plugins/",
|
2013-04-28 08:42:48 +00:00
|
|
|
themesPath: "../themes/",
|
2013-03-25 10:43:18 +00:00
|
|
|
wikiInfo: "./tiddlywiki.info",
|
|
|
|
wikiPluginsSubDir: "./plugins",
|
2013-07-14 22:08:04 +00:00
|
|
|
wikiThemesSubDir: "./themes",
|
2013-03-25 10:43:18 +00:00
|
|
|
wikiTiddlersSubDir: "./tiddlers",
|
2013-06-27 08:43:04 +00:00
|
|
|
jsModuleHeaderRegExpString: "^\\/\\*\\\\(?:\\r?\\n)((?:^[^\\r\\n]*(?:\\r?\\n))+?)(^\\\\\\*\\/$(?:\\r?\\n)?)",
|
2013-03-25 10:43:18 +00:00
|
|
|
fileExtensionInfo: {}, // Map file extension to {type:}
|
|
|
|
contentTypeInfo: {} // Map type to {encoding:,extension:}
|
|
|
|
}
|
|
|
|
});
|
2013-03-22 20:02:19 +00:00
|
|
|
if(!$tw.browser) {
|
2013-03-23 22:11:23 +00:00
|
|
|
// For writable tiddler files, a hashmap of title to {filepath:,type:,hasMetaFile:}
|
|
|
|
$tw.boot.files = {};
|
2013-03-22 20:02:19 +00:00
|
|
|
// System paths and filenames
|
|
|
|
$tw.boot.bootPath = path.dirname(module.filename);
|
2013-03-28 17:07:30 +00:00
|
|
|
$tw.boot.corePath = path.resolve($tw.boot.bootPath,"../core");
|
2013-03-22 20:02:19 +00:00
|
|
|
// If the first command line argument doesn't start with `--` then we
|
|
|
|
// interpret it as the path to the wiki folder, which will otherwise default
|
|
|
|
// to the current folder
|
|
|
|
$tw.boot.argv = Array.prototype.slice.call(process.argv,2);
|
|
|
|
if($tw.boot.argv[0] && $tw.boot.argv[0].indexOf("--") !== 0) {
|
|
|
|
$tw.boot.wikiPath = $tw.boot.argv[0];
|
|
|
|
$tw.boot.argv = $tw.boot.argv.slice(1);
|
|
|
|
} else {
|
|
|
|
$tw.boot.wikiPath = process.cwd();
|
|
|
|
}
|
2013-03-25 10:43:18 +00:00
|
|
|
$tw.boot.wikiTiddlersPath = path.resolve($tw.boot.wikiPath,$tw.config.wikiTiddlersSubDir);
|
2013-03-22 20:02:19 +00:00
|
|
|
// Read package info
|
2013-06-26 22:51:57 +00:00
|
|
|
$tw.packageInfo = require("../package");
|
2013-03-22 20:02:19 +00:00
|
|
|
// Check node version number
|
|
|
|
if($tw.utils.checkVersions($tw.packageInfo.engines.node.substr(2),process.version.substr(1))) {
|
2013-03-28 14:06:50 +00:00
|
|
|
$tw.utils.error("TiddlyWiki5 requires node.js version " + $tw.packageInfo.engine.node);
|
2013-03-22 20:02:19 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 21:12:39 +00:00
|
|
|
// Add file extension information
|
|
|
|
$tw.utils.registerFileType("text/vnd.tiddlywiki","utf8",".tid");
|
|
|
|
$tw.utils.registerFileType("application/x-tiddler","utf8",".tid");
|
|
|
|
$tw.utils.registerFileType("application/x-tiddler-html-div","utf8",".tiddler");
|
2013-05-13 16:42:07 +00:00
|
|
|
$tw.utils.registerFileType("text/vnd.tiddlywiki2-recipe","utf8",".recipe");
|
2013-03-22 21:12:39 +00:00
|
|
|
$tw.utils.registerFileType("text/plain","utf8",".txt");
|
|
|
|
$tw.utils.registerFileType("text/css","utf8",".css");
|
|
|
|
$tw.utils.registerFileType("text/html","utf8",".html");
|
|
|
|
$tw.utils.registerFileType("application/javascript","utf8",".js");
|
|
|
|
$tw.utils.registerFileType("application/json","utf8",".json");
|
|
|
|
$tw.utils.registerFileType("application/pdf","base64",".pdf");
|
|
|
|
$tw.utils.registerFileType("image/jpeg","base64",".jpg");
|
|
|
|
$tw.utils.registerFileType("image/png","base64",".png");
|
|
|
|
$tw.utils.registerFileType("image/gif","base64",".gif");
|
|
|
|
$tw.utils.registerFileType("image/svg+xml","utf8",".svg");
|
2013-05-01 12:04:27 +00:00
|
|
|
$tw.utils.registerFileType("application/font-woff","base64",".woff");
|
2013-03-22 20:02:19 +00:00
|
|
|
// Create the wiki store for the app
|
2013-03-22 19:44:12 +00:00
|
|
|
$tw.wiki = new $tw.Wiki();
|
|
|
|
// Install built in tiddler fields modules
|
|
|
|
$tw.Tiddler.fieldModules = $tw.modules.getModulesByTypeAsHashmap("tiddlerfield");
|
|
|
|
// Install the tiddler deserializer modules
|
|
|
|
$tw.Wiki.tiddlerDeserializerModules = {};
|
|
|
|
$tw.modules.applyMethods("tiddlerdeserializer",$tw.Wiki.tiddlerDeserializerModules);
|
|
|
|
// Load tiddlers
|
|
|
|
$tw.loadTiddlers();
|
2013-03-23 16:34:12 +00:00
|
|
|
// Unpack plugin tiddlers
|
2013-04-30 21:56:17 +00:00
|
|
|
$tw.wiki.registerPluginTiddlers("plugin");
|
|
|
|
$tw.wiki.unpackPluginTiddlers();
|
2013-03-22 19:44:12 +00:00
|
|
|
// Register typed modules from the tiddlers we've just loaded
|
|
|
|
$tw.wiki.defineTiddlerModules();
|
2013-03-23 16:34:12 +00:00
|
|
|
// And any modules within plugins
|
2013-04-30 21:56:17 +00:00
|
|
|
$tw.wiki.defineShadowModules();
|
2013-03-22 19:44:12 +00:00
|
|
|
// Make sure the crypto state tiddler is up to date
|
|
|
|
$tw.crypto.updateCryptoStateTiddler();
|
|
|
|
// Run any startup modules
|
|
|
|
$tw.modules.forEachModuleOfType("startup",function(title,module) {
|
|
|
|
if(module.startup) {
|
|
|
|
module.startup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-22 20:02:19 +00:00
|
|
|
/////////////////////////// Decrypt tiddlers and then startup
|
2013-03-22 19:27:09 +00:00
|
|
|
|
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();
|
|
|
|
}
|
2013-03-22 20:02:19 +00:00
|
|
|
// Preload any encrypted tiddlers
|
2012-11-16 17:23:10 +00:00
|
|
|
$tw.boot.decryptEncryptedTiddlers(function() {
|
2013-03-22 20:02:19 +00:00
|
|
|
// Startup
|
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
|
|
|
|
2013-06-12 11:40:48 +00:00
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
})();
|