(function(){ /* TiddlyWiki command line interface */ /*jslint node: true */ "use strict"; var WikiStore = require("./js/WikiStore.js").WikiStore, Tiddler = require("./js/Tiddler.js").Tiddler, Recipe = require("./js/Recipe.js").Recipe, tiddlerInput = require("./js/TiddlerInput.js"), tiddlerOutput = require("./js/TiddlerOutput.js"), TextProcessors = require("./js/TextProcessors.js").TextProcessors, WikiTextProcessor = require("./js/WikiTextProcessor.js").WikiTextProcessor, TiddlerConverters = require("./js/TiddlerConverters.js").TiddlerConverters, util = require("util"), fs = require("fs"), url = require("url"), path = require("path"), aync = require("async"), http = require("http"); 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; } result.push({switchName: m[1], args: switchArgs}); } else { result.push({switchName: defaultSwitch, args: [args[a++]]}); } } return result; }; var textProcessors = new TextProcessors(), tiddlerConverters = new TiddlerConverters(), switches = parseOptions(Array.prototype.slice.call(process.argv,2),"dummy"), store = new WikiStore({ textProcessors: textProcessors }), recipe = null, lastRecipeFilepath = null, currSwitch = 0; textProcessors.registerTextProcessor("text/x-tiddlywiki",new WikiTextProcessor({ textProcessors: textProcessors })); // Register the standard tiddler serializers and deserializers tiddlerInput.register(tiddlerConverters); tiddlerOutput.register(tiddlerConverters); // Add the shadow tiddlers that are built into TiddlyWiki var shadowShadowStore = new WikiStore({ textProcessors: textProcessors, shadowStore: null }), shadowShadows = [ {title: "StyleSheet", text: ""}, {title: "MarkupPreHead", text: ""}, {title: "MarkupPostHead", text: ""}, {title: "MarkupPreBody", text: ""}, {title: "MarkupPostBody", text: ""}, {title: "TabTimeline", text: "<>"}, {title: "TabAll", text: "<>"}, {title: "TabTags", text: "<>"}, {title: "TabMoreMissing", text: "<>"}, {title: "TabMoreOrphans", text: "<>"}, {title: "TabMoreShadowed", text: "<>"}, {title: "AdvancedOptions", text: "<>"}, {title: "PluginManager", text: "<>"}, {title: "SystemSettings", text: ""}, {title: "ToolbarCommands", text: "|~ViewToolbar|closeTiddler closeOthers +editTiddler > fields syncing permalink references jump|\n|~EditToolbar|+saveTiddler -cancelTiddler deleteTiddler|"}, {title: "WindowTitle", text: "<> - <>"}, {title: "DefaultTiddlers", text: "[[GettingStarted]]"}, {title: "MainMenu", text: "[[GettingStarted]]"}, {title: "SiteTitle", text: "My TiddlyWiki"}, {title: "SiteSubtitle", text: "a reusable non-linear personal web notebook"}, {title: "SiteUrl", text: ""}, {title: "SideBarOptions", text: '<><><><><><><>'}, {title: "SideBarTabs", text: '<>'}, {title: "TabMore", text: '<>'} ]; store.shadows.shadows = shadowShadowStore; for(var t=0; t 0 ? args[0] : 8000; // Dumbly, this implementation wastes the recipe processing that happened on the --recipe switch http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/html"}); store = new WikiStore({ textProcessors: textProcessors }); recipe = new Recipe(store,lastRecipeFilepath,function() { response.end(recipe.cook(), "utf8"); }); }).listen(port); } }, servetiddlers: { args: {min: 0, max: 1}, handler: function(args,callback) { var port = args.length > 0 ? args[0] : 8000; http.createServer(function (request, response) { var title = decodeURIComponent(url.parse(request.url).pathname.substr(1)), tiddler = store.getTiddler(title); if(tiddler) { response.writeHead(200, {"Content-Type": "text/html"}); response.end(store.renderTiddler("text/html",title),"utf8"); } else { response.writeHead(404); response.end(); } }).listen(port); } }, verbose: { args: {min: 0, max: 0}, handler: function(args,callback) { process.nextTick(function() {callback(null);}); } } }; var processNextSwitch = function() { if(currSwitch < switches.length) { var s = switches[currSwitch++], csw = commandLineSwitches[s.switchName]; 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"; } csw.handler(s.args,function (err) { if(err) { throw err; } process.nextTick(processNextSwitch); }); } }; process.nextTick(processNextSwitch); })();