1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

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.
This commit is contained in:
Jeremy Ruston 2024-10-19 12:26:11 +01:00
parent e259f414d9
commit 50118dbe13
3 changed files with 142 additions and 47 deletions

View File

@ -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);
}
}
}

View File

@ -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;
})();

View File

@ -23,6 +23,18 @@
"languages": [
],
"build": {
"dynamic": [
"--echo","testing",
{
"type": "wikified",
"text": "<<version>>-prod.html"
},
"thingy",
{
"type": "filter",
"text": "[<version>!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",