2011-12-12 10:52:04 +00:00
|
|
|
(function(){
|
|
|
|
|
2011-12-07 16:44:23 +00:00
|
|
|
/*
|
|
|
|
TiddlyWiki command line interface
|
|
|
|
*/
|
|
|
|
|
2011-12-09 16:34:02 +00:00
|
|
|
/*jslint node: true */
|
2011-12-07 16:44:23 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-01-03 12:23:02 +00:00
|
|
|
var App = require("./js/App.js").App,
|
|
|
|
WikiStore = require("./js/WikiStore.js").WikiStore,
|
2012-04-05 11:21:49 +00:00
|
|
|
LocalFileSync = require("./js/LocalFileSync.js").LocalFileSync,
|
2011-12-07 16:44:23 +00:00
|
|
|
Tiddler = require("./js/Tiddler.js").Tiddler,
|
|
|
|
Recipe = require("./js/Recipe.js").Recipe,
|
2011-12-07 17:26:50 +00:00
|
|
|
tiddlerInput = require("./js/TiddlerInput.js"),
|
2011-12-11 14:51:48 +00:00
|
|
|
tiddlerOutput = require("./js/TiddlerOutput.js"),
|
2011-12-07 16:44:23 +00:00
|
|
|
util = require("util"),
|
|
|
|
fs = require("fs"),
|
2011-12-07 18:02:09 +00:00
|
|
|
url = require("url"),
|
2011-12-07 16:44:23 +00:00
|
|
|
path = require("path"),
|
|
|
|
aync = require("async"),
|
|
|
|
http = require("http");
|
|
|
|
|
2012-01-03 12:23:02 +00:00
|
|
|
var app = new App();
|
|
|
|
|
2011-12-07 16:44:23 +00:00
|
|
|
var parseOptions = function(args,defaultSwitch) {
|
|
|
|
var result = [],
|
|
|
|
a = 0,
|
|
|
|
switchRegExp = /^--([\S]+)$/gi;
|
|
|
|
while(a < args.length) {
|
|
|
|
switchRegExp.lastIndex = 0;
|
|
|
|
var m = switchRegExp.exec(args[a]);
|
|
|
|
if(m) {
|
|
|
|
a++;
|
|
|
|
var switchArgs = [];
|
|
|
|
switchRegExp.lastIndex = 0;
|
|
|
|
while(a < args.length && !switchRegExp.test(args[a])) {
|
|
|
|
switchArgs.push(args[a++]);
|
|
|
|
switchRegExp.lastIndex = 0;
|
|
|
|
}
|
2011-12-09 16:34:02 +00:00
|
|
|
result.push({switchName: m[1], args: switchArgs});
|
2011-12-07 16:44:23 +00:00
|
|
|
} else {
|
2011-12-09 16:34:02 +00:00
|
|
|
result.push({switchName: defaultSwitch, args: [args[a++]]});
|
2011-12-07 16:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2011-12-28 17:16:56 +00:00
|
|
|
var switches = parseOptions(Array.prototype.slice.call(process.argv,2),"dummy"),
|
2012-04-05 11:21:49 +00:00
|
|
|
verbose = false,
|
2011-12-07 16:44:23 +00:00
|
|
|
recipe = null,
|
2012-04-05 11:21:49 +00:00
|
|
|
localFileSync = null,
|
2011-12-07 16:44:23 +00:00
|
|
|
lastRecipeFilepath = null,
|
|
|
|
currSwitch = 0;
|
|
|
|
|
2011-12-08 16:20:11 +00:00
|
|
|
|
2011-12-07 16:44:23 +00:00
|
|
|
/*
|
|
|
|
Each command line switch is represented by a function that takes a string array of arguments and a callback to
|
|
|
|
be invoked when the switch processing has completed. The only argument to the callback is an error code, or null
|
|
|
|
for success.
|
|
|
|
*/
|
|
|
|
var commandLineSwitches = {
|
|
|
|
recipe: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
if(recipe) {
|
|
|
|
callback("--recipe: Cannot process more than one recipe file");
|
|
|
|
} else {
|
|
|
|
lastRecipeFilepath = args[0];
|
2011-12-11 14:51:48 +00:00
|
|
|
recipe = new Recipe({
|
|
|
|
filepath: args[0],
|
2012-01-03 12:23:02 +00:00
|
|
|
store: app.store
|
2011-12-28 22:07:17 +00:00
|
|
|
},function(err) {
|
|
|
|
callback(err);
|
2011-12-07 16:44:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2011-12-07 17:26:50 +00:00
|
|
|
dumpstore: {
|
|
|
|
args: {min: 0, max: 0},
|
|
|
|
handler: function(args,callback) {
|
2012-01-03 12:23:02 +00:00
|
|
|
console.log("Store is:\n%s",util.inspect(app.store,false,10));
|
2011-12-07 17:26:50 +00:00
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
|
|
|
},
|
2011-12-10 11:46:13 +00:00
|
|
|
dumprecipe: {
|
|
|
|
args: {min: 0, max: 0},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
console.log("Recipe is:\n%s",util.inspect(recipe,false,10));
|
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
|
|
|
},
|
2011-12-07 16:44:23 +00:00
|
|
|
load: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
2011-12-07 17:26:50 +00:00
|
|
|
fs.readFile(args[0],"utf8",function(err,data) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2011-12-07 17:39:55 +00:00
|
|
|
var fields = {title: args[0]},
|
|
|
|
extname = path.extname(args[0]),
|
|
|
|
type = extname === ".html" ? "application/x-tiddlywiki" : extname;
|
2012-01-23 19:04:29 +00:00
|
|
|
var tiddlers = app.store.deserializeTiddlers(type,data,fields);
|
2011-12-07 17:26:50 +00:00
|
|
|
for(var t=0; t<tiddlers.length; t++) {
|
2012-01-03 12:23:02 +00:00
|
|
|
app.store.addTiddler(new Tiddler(tiddlers[t]));
|
2011-12-07 17:26:50 +00:00
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
2011-12-07 16:44:23 +00:00
|
|
|
}
|
|
|
|
},
|
2012-04-03 13:43:13 +00:00
|
|
|
store: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
2012-04-05 11:21:49 +00:00
|
|
|
localFileSync = new LocalFileSync(args[0],app.store,function() {
|
2012-04-03 13:43:13 +00:00
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2012-04-07 11:10:46 +00:00
|
|
|
links: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
var type = args[0];
|
|
|
|
app.store.linkMassager = function(linkInfo) {
|
|
|
|
switch(type) {
|
|
|
|
case "none":
|
|
|
|
if(!linkInfo.isExternal) {
|
|
|
|
linkInfo.suppressLink = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
|
|
|
},
|
2011-12-07 16:44:23 +00:00
|
|
|
savewiki: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
if(!recipe) {
|
|
|
|
callback("--savewiki requires a recipe to be loaded first");
|
|
|
|
}
|
2011-12-08 16:20:11 +00:00
|
|
|
fs.writeFile(path.resolve(args[0],"index.html"),recipe.cook(),"utf8",function(err) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
fs.writeFile(path.resolve(args[0],"index.xml"),recipe.cookRss(),"utf8",function(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
2011-12-07 16:44:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2012-01-15 11:44:50 +00:00
|
|
|
savetiddler: {
|
|
|
|
args: {min: 2, max: 3},
|
|
|
|
handler: function(args,callback) {
|
2012-03-03 16:50:11 +00:00
|
|
|
var title = args[0],
|
|
|
|
filename = args[1],
|
|
|
|
type = args[2] || "text/html";
|
|
|
|
fs.writeFileSync(filename,app.store.renderTiddler(type,title),"utf8");
|
2012-01-15 11:44:50 +00:00
|
|
|
}
|
|
|
|
},
|
2011-12-07 16:44:23 +00:00
|
|
|
savetiddlers: {
|
2012-03-03 16:50:11 +00:00
|
|
|
args: {min: 1, max: 2},
|
2011-12-07 16:44:23 +00:00
|
|
|
handler: function(args,callback) {
|
2012-03-03 16:50:11 +00:00
|
|
|
var outdir = args[0],
|
|
|
|
recipe = [];
|
2012-01-03 12:23:02 +00:00
|
|
|
app.store.forEachTiddler(function(title,tiddler) {
|
2012-01-17 13:01:55 +00:00
|
|
|
var filename = encodeURIComponent(tiddler.title.replace(/ /g,"_")) + ".tid";
|
2012-04-05 11:21:49 +00:00
|
|
|
fs.writeFileSync(path.resolve(outdir,filename),app.store.serializeTiddlers([tiddler],"application/x-tiddler")[0].data,"utf8");
|
2011-12-07 16:44:23 +00:00
|
|
|
recipe.push("tiddler: " + filename + "\n");
|
|
|
|
});
|
|
|
|
fs.writeFileSync(path.join(args[0],"split.recipe"),recipe.join(""));
|
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
|
|
|
},
|
2012-03-03 16:50:11 +00:00
|
|
|
savehtml: {
|
|
|
|
args: {min: 1, max: 1},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
var outdir = args[0];
|
|
|
|
app.store.forEachTiddler(function(title,tiddler) {
|
|
|
|
var filename = encodeURIComponent(title.replace(/ /g,"_")) + ".html";
|
|
|
|
fs.writeFileSync(path.resolve(outdir,filename),app.store.renderTiddler("text/html",title),"utf8");
|
|
|
|
});
|
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
|
|
|
},
|
2011-12-07 16:44:23 +00:00
|
|
|
servewiki: {
|
|
|
|
args: {min: 0, max: 1},
|
|
|
|
handler: function(args,callback) {
|
|
|
|
if(!lastRecipeFilepath) {
|
|
|
|
callback("--servewiki must be preceded by a --recipe");
|
|
|
|
}
|
|
|
|
var port = args.length > 0 ? args[0] : 8000;
|
|
|
|
http.createServer(function(request, response) {
|
2012-04-05 11:21:49 +00:00
|
|
|
var path = url.parse(request.url).pathname;
|
|
|
|
switch(request.method) {
|
|
|
|
case "PUT":
|
|
|
|
var data = "";
|
|
|
|
request.on("data",function(chunk) {
|
|
|
|
data += chunk.toString();
|
|
|
|
});
|
|
|
|
request.on("end",function() {
|
|
|
|
var title = decodeURIComponent(path.substr(1));
|
|
|
|
app.store.addTiddler(new Tiddler(JSON.parse(data),{title: title}));
|
|
|
|
response.writeHead(204, "OK");
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
break;
|
2012-04-05 17:25:39 +00:00
|
|
|
case "DELETE":
|
|
|
|
app.store.deleteTiddler(decodeURIComponent(path.substr(1)));
|
|
|
|
response.writeHead(204, "OK");
|
|
|
|
response.end();
|
|
|
|
break;
|
2012-04-05 11:21:49 +00:00
|
|
|
case "GET":
|
|
|
|
if(path === "/") {
|
|
|
|
response.writeHead(200, {"Content-Type": "text/html"});
|
|
|
|
response.end(recipe.cook(), "utf8");
|
|
|
|
} else {
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-07 16:44:23 +00:00
|
|
|
}).listen(port);
|
2012-04-05 11:21:49 +00:00
|
|
|
process.nextTick(function() {callback(null);});
|
2011-12-07 16:44:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
servetiddlers: {
|
2011-12-07 18:02:09 +00:00
|
|
|
args: {min: 0, max: 1},
|
2011-12-07 16:44:23 +00:00
|
|
|
handler: function(args,callback) {
|
2011-12-07 18:02:09 +00:00
|
|
|
var port = args.length > 0 ? args[0] : 8000;
|
|
|
|
http.createServer(function (request, response) {
|
2011-12-07 18:06:25 +00:00
|
|
|
var title = decodeURIComponent(url.parse(request.url).pathname.substr(1)),
|
2012-01-03 12:23:02 +00:00
|
|
|
tiddler = app.store.getTiddler(title);
|
2011-12-07 18:02:09 +00:00
|
|
|
if(tiddler) {
|
|
|
|
response.writeHead(200, {"Content-Type": "text/html"});
|
2012-01-03 12:23:02 +00:00
|
|
|
response.end(app.store.renderTiddler("text/html",title),"utf8");
|
2011-12-07 18:02:09 +00:00
|
|
|
} else {
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
}).listen(port);
|
2012-04-05 11:21:49 +00:00
|
|
|
process.nextTick(function() {callback(null);});
|
2011-12-07 16:44:23 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
verbose: {
|
|
|
|
args: {min: 0, max: 0},
|
|
|
|
handler: function(args,callback) {
|
2012-04-05 11:21:49 +00:00
|
|
|
verbose = true;
|
2011-12-07 16:44:23 +00:00
|
|
|
process.nextTick(function() {callback(null);});
|
|
|
|
}
|
2012-01-03 17:08:32 +00:00
|
|
|
},
|
|
|
|
wikitest: {
|
2012-03-29 13:57:36 +00:00
|
|
|
args: {min: 1, max: 2},
|
2012-01-03 17:08:32 +00:00
|
|
|
handler: function(args,callback) {
|
|
|
|
var testdirectory = args[0],
|
2012-03-29 13:57:36 +00:00
|
|
|
saveResults = args[1] === "save",
|
2012-01-03 17:08:32 +00:00
|
|
|
files = fs.readdirSync(testdirectory),
|
|
|
|
titles = [],
|
|
|
|
f,t,extname,basename;
|
|
|
|
for(f=0; f<files.length; f++) {
|
|
|
|
extname = path.extname(files[f]);
|
|
|
|
if(extname === ".tid") {
|
|
|
|
var tiddlers = app.store.deserializeTiddlers(extname,fs.readFileSync(path.resolve(testdirectory,files[f]),"utf8"));
|
|
|
|
if(tiddlers.length > 1) {
|
|
|
|
throw "Cannot use .JSON files";
|
|
|
|
}
|
|
|
|
app.store.addTiddler(new Tiddler(tiddlers[0]));
|
|
|
|
titles.push(tiddlers[0].title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(t=0; t<titles.length; t++) {
|
2012-03-29 13:57:36 +00:00
|
|
|
var htmlFilename = path.resolve(testdirectory,titles[t] + ".html"),
|
|
|
|
plainFilename = path.resolve(testdirectory,titles[t] + ".txt"),
|
|
|
|
htmlTarget = fs.readFileSync(htmlFilename,"utf8"),
|
|
|
|
plainTarget = fs.readFileSync(plainFilename,"utf8"),
|
2012-01-03 17:08:32 +00:00
|
|
|
tiddler = app.store.getTiddler(titles[t]),
|
|
|
|
htmlRender = app.store.renderTiddler("text/html",titles[t]),
|
|
|
|
plainRender = app.store.renderTiddler("text/plain",titles[t]);
|
2012-03-29 13:57:36 +00:00
|
|
|
if(saveResults) {
|
|
|
|
// Save results
|
|
|
|
fs.writeFileSync(htmlFilename,htmlRender,"utf8");
|
|
|
|
fs.writeFileSync(plainFilename,plainRender,"utf8");
|
|
|
|
} else {
|
|
|
|
// Report results
|
|
|
|
if(htmlTarget !== htmlRender) {
|
|
|
|
console.error("Tiddler %s html error\nTarget: %s\nFound: %s\n",titles[t],htmlTarget,htmlRender);
|
|
|
|
}
|
|
|
|
if(plainTarget !== plainRender) {
|
|
|
|
console.error("Tiddler %s plain text error\nTarget: %s\nFound: %s\n",titles[t],plainTarget,plainRender);
|
|
|
|
}
|
2012-01-03 17:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
process.nextTick(function() {callback(null);});
|
2012-01-03 17:08:32 +00:00
|
|
|
}
|
2011-12-07 16:44:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-12 10:52:04 +00:00
|
|
|
var processNextSwitch = function() {
|
|
|
|
if(currSwitch < switches.length) {
|
|
|
|
var s = switches[currSwitch++],
|
|
|
|
csw = commandLineSwitches[s.switchName];
|
2012-04-07 11:10:46 +00:00
|
|
|
if(!csw) {
|
|
|
|
throw "Unknown command line switch --" + s.switchName;
|
|
|
|
}
|
2011-12-12 10:52:04 +00:00
|
|
|
if(s.args.length < csw.args.min) {
|
|
|
|
throw "Command line switch --" + s.switchName + " should have a minimum of " + csw.args.min + " arguments";
|
|
|
|
}
|
|
|
|
if(s.args.length > csw.args.max) {
|
|
|
|
throw "Command line switch --" + s.switchName + " should have a maximum of " + csw.args.max + " arguments";
|
|
|
|
}
|
2012-04-05 11:21:49 +00:00
|
|
|
if(verbose) {
|
|
|
|
console.log("Processing --" + s.switchName + " " + s.args.join(" "));
|
|
|
|
}
|
2011-12-12 10:52:04 +00:00
|
|
|
csw.handler(s.args,function (err) {
|
|
|
|
if(err) {
|
2012-01-22 17:37:21 +00:00
|
|
|
throw "Error while executing option '--" + s.switchName + "' was:\n" + err;
|
2011-12-12 10:52:04 +00:00
|
|
|
}
|
|
|
|
process.nextTick(processNextSwitch);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
process.nextTick(processNextSwitch);
|
|
|
|
|
|
|
|
})();
|