From 50118dbe138dee24d2037431d77db1a19280a3b5 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 19 Oct 2024 12:26:11 +0100 Subject: [PATCH] Initial commit This commit adds support for dynamic tokens within build commands. See the tiddlywiki.info file. Also adds an echo command to make debugging easier. I also intend to add a node type for prompting the user for a string. --- core/modules/commander.js | 145 +++++++++++++++++++++---------- core/modules/commands/echo.js | 32 +++++++ editions/tw5.com/tiddlywiki.info | 12 +++ 3 files changed, 142 insertions(+), 47 deletions(-) create mode 100644 core/modules/commands/echo.js diff --git a/core/modules/commander.js b/core/modules/commander.js index b55679a2e..4d43c8dc2 100644 --- a/core/modules/commander.js +++ b/core/modules/commander.js @@ -64,65 +64,116 @@ Commander.prototype.execute = function() { this.executeNextCommand(); }; +/* +Returns the next string token without consuming it, or null if there are none left +*/ +Commander.prototype.peekNextToken = function() { + if(this.nextToken >= this.commandTokens.length) { + return null; + } else { + return this.stringifyToken(this.nextToken); + } +}; + +/* +Returns and consumes the next string token, or null if there are none left +*/ +Commander.prototype.getNextToken = function() { + if(this.nextToken >= this.commandTokens.length) { + return null; + } else { + return this.stringifyToken(this.nextToken++); + } +}; + + +/* +Returns a specified stringified token, or null if the index does not exist +*/ +Commander.prototype.stringifyToken = function(index) { + if(index >= this.commandTokens.length) { + return null; + } else { + var token = this.commandTokens[index]; + if(typeof token === "string") { + return token; + } else if(typeof token === "object") { + switch(token.type) { + case "filter": + return this.wiki.filterTiddlers(token.text)[0] || ""; + break; + case "wikified": + return this.wiki.renderText("text/plain","text/vnd.tiddlywiki",token.text,{ + parseAsInline: false, + parentWidget: $tw.rootWidget + }); + break; + default: + throw "Unknown dynamic command token type: " + token.type; + break; + } + } + } +}; + /* Execute the next command in the sequence */ Commander.prototype.executeNextCommand = function() { var self = this; - // Invoke the callback if there are no more commands - if(this.nextToken >= this.commandTokens.length) { - this.callback(null); + // Get and check the command token + var commandName = this.getNextToken(); + if(!commandName) { + return this.callback(null); + } + if(commandName.substr(0,2) !== "--") { + this.callback("Missing command: " + commandName); } else { - // Get and check the command token - var commandName = this.commandTokens[this.nextToken++]; - if(commandName.substr(0,2) !== "--") { - this.callback("Missing command: " + commandName); + commandName = commandName.substr(2); // Trim off the -- + // Accumulate the parameters to the command + var params = [], + nextToken = this.peekNextToken(); + while(typeof nextToken === "string" && nextToken.substr(0,2) !== "--") { + params.push(this.getNextToken()); + nextToken = this.peekNextToken(); + } + // Get the command info + var command = $tw.commands[commandName], + c,err; + if(!command) { + this.callback("Unknown command: " + commandName); } else { - commandName = commandName.substr(2); // Trim off the -- - // Accumulate the parameters to the command - var params = []; - while(this.nextToken < this.commandTokens.length && - this.commandTokens[this.nextToken].substr(0,2) !== "--") { - params.push(this.commandTokens[this.nextToken++]); + if(this.verbose) { + this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); } - // Get the command info - var command = $tw.commands[commandName], - c,err; - if(!command) { - this.callback("Unknown command: " + commandName); - } else { - if(this.verbose) { - this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n"); + // Parse named parameters if required + if(command.info.namedParameterMode) { + params = this.extractNamedParameters(params,command.info.mandatoryParameters); + if(typeof params === "string") { + return this.callback(params); } - // Parse named parameters if required - if(command.info.namedParameterMode) { - params = this.extractNamedParameters(params,command.info.mandatoryParameters); - if(typeof params === "string") { - return this.callback(params); - } - } - if(command.info.synchronous) { - // Synchronous command - c = new command.Command(params,this); - err = c.execute(); - if(err) { - this.callback(err); - } else { - this.executeNextCommand(); - } + } + if(command.info.synchronous) { + // Synchronous command + c = new command.Command(params,this); + err = c.execute(); + if(err) { + this.callback(err); } else { - // Asynchronous command - c = new command.Command(params,this,function(err) { - if(err) { - self.callback(err); - } else { - self.executeNextCommand(); - } - }); - err = c.execute(); + this.executeNextCommand(); + } + } else { + // Asynchronous command + c = new command.Command(params,this,function(err) { if(err) { - this.callback(err); + self.callback(err); + } else { + self.executeNextCommand(); } + }); + err = c.execute(); + if(err) { + this.callback(err); } } } diff --git a/core/modules/commands/echo.js b/core/modules/commands/echo.js new file mode 100644 index 000000000..366cd8a53 --- /dev/null +++ b/core/modules/commands/echo.js @@ -0,0 +1,32 @@ +/*\ +title: $:/core/modules/commands/echo.js +type: application/javascript +module-type: command + +Command to echo input parameters + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.info = { + name: "echo", + synchronous: true +}; + +var Command = function(params,commander) { + this.params = params; + this.commander = commander; +}; + +Command.prototype.execute = function() { + this.commander.streams.output.write(JSON.stringify(this.params,null,4) + "\n"); + return null; +}; + +exports.Command = Command; + +})(); diff --git a/editions/tw5.com/tiddlywiki.info b/editions/tw5.com/tiddlywiki.info index 2f3ddade8..467efc836 100644 --- a/editions/tw5.com/tiddlywiki.info +++ b/editions/tw5.com/tiddlywiki.info @@ -23,6 +23,18 @@ "languages": [ ], "build": { + "dynamic": [ + "--echo","testing", + { + "type": "wikified", + "text": "<>-prod.html" + }, + "thingy", + { + "type": "filter", + "text": "[!match[5.3.6-prerelease]then[text/html]else[text/plain]]" + } + ], "index": [ "--savetiddlers","[tag[external-image]]","images", "--render","[tag[external-text]]","[encodeuricomponent[]addprefix[text/]addsuffix[.tid]]","text/plain","$:/core/templates/tid-tiddler",