mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-09-06 01:58:54 +00:00
Add new tagger demo
This commit is contained in:
+80
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/*
|
||||
A socket server that listens on a host/port for connections and suggests tags for the incoming text
|
||||
|
||||
badtagger.js <port> <host>
|
||||
|
||||
This utility is provided as an example of using an external task that doesn't have any prior knowledge of
|
||||
TiddlyWiki. Like many Unix utilities, it just reads and writes to a socket.
|
||||
|
||||
*/
|
||||
|
||||
var net = require("net"),
|
||||
port = parseInt(process.argv[2] || "",10) || 8081, // Port
|
||||
host = process.argv[3] || "127.0.0.1"; // Host
|
||||
|
||||
var server = net.createServer();
|
||||
|
||||
server.listen(port,host);
|
||||
|
||||
server.on("connection", function(sock) {
|
||||
console.log("CONNECTED: " + sock.remoteAddress +":"+ sock.remotePort);
|
||||
// Trap errors
|
||||
sock.on("error",function(e) {
|
||||
console.log("ERROR: " + e);
|
||||
});
|
||||
// Read data until the end
|
||||
var accumulator = Buffer.alloc(0);
|
||||
sock.on("data",function(data) {
|
||||
console.log("DATA " + sock.remoteAddress + ": " + data.length);
|
||||
accumulator = Buffer.concat([accumulator,Buffer.from(data)]);
|
||||
while(accumulator.length > 4) {
|
||||
var length = accumulator.readInt32BE(0);
|
||||
if(accumulator.length >= (length + 4)) {
|
||||
if(length < 1) {
|
||||
throw "ERROR: Incoming message length field is less than 1";
|
||||
}
|
||||
var type = accumulator.readUInt8(4),
|
||||
dataLength = length - 1,
|
||||
data = accumulator.toString("latin1",5,dataLength + 5);
|
||||
accumulator = accumulator.slice(length + 4);
|
||||
// Recase it
|
||||
console.log("MESSAGE",length,type);
|
||||
var suggestedTags = Buffer.from(suggestTags(data),"latin1");
|
||||
// Send it back
|
||||
var lengthBytes = Buffer.alloc(4);
|
||||
lengthBytes.writeUInt32BE(suggestedTags.length + 1,0)
|
||||
console.log("RESPONSE",1,suggestedTags.length)
|
||||
sock.write(lengthBytes);
|
||||
var typeByte = Buffer.alloc(1);
|
||||
typeByte.writeUInt8(1,0);
|
||||
sock.write(typeByte);
|
||||
sock.write(suggestedTags);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
sock.on("end",function() {
|
||||
console.log("END")
|
||||
sock.end();
|
||||
});
|
||||
sock.on("close", function(data) {
|
||||
console.log("CLOSED: " + sock.remoteAddress +" "+ sock.remotePort);
|
||||
});
|
||||
});
|
||||
|
||||
function suggestTags(str) {
|
||||
var tags = [];
|
||||
if(/e/mi.test(str)) {
|
||||
tags.push("elephant");
|
||||
}
|
||||
if(/s/mi.test(str)) {
|
||||
tags.push("snake");
|
||||
}
|
||||
if(/c/mi.test(str)) {
|
||||
tags.push("cow");
|
||||
}
|
||||
return tags.join("\n");
|
||||
}
|
||||
@@ -32,8 +32,8 @@ server.on("connection", function(sock) {
|
||||
while(accumulator.length > 4) {
|
||||
var length = accumulator.readInt32BE(0);
|
||||
if(accumulator.length >= (length + 4)) {
|
||||
if(length < 2) {
|
||||
throw "ERROR: Incoming message length field is less than 2";
|
||||
if(length < 1) {
|
||||
throw "ERROR: Incoming message length field is less than 1";
|
||||
}
|
||||
var type = accumulator.readUInt8(4),
|
||||
dataLength = length - 1,
|
||||
|
||||
@@ -70,7 +70,7 @@ function computeStats(tiddlers) {
|
||||
// Output
|
||||
return [
|
||||
{
|
||||
title: "HelloThere",
|
||||
title: "PipeOutput",
|
||||
text: numTiddlers + " tiddlers in sample.\n" + wordCount + " words in sample.\n" + sortedWords.filter(function(word) {
|
||||
return word.length > 1 && wordFrequency[word] > 1;
|
||||
}).map(function(word) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
title: $:/plugins/tiddlywiki/externaltasksdemo/EditorToolbar/SuggestTags
|
||||
tags: $:/tags/EditorToolbar
|
||||
icon: $:/core/images/tag-button
|
||||
caption: Suggest Tags
|
||||
description: Invoke external programme to suggest tags for this tiddler
|
||||
condition: [<targetTiddler>!has[type]] [<targetTiddler>type[text/vnd.tiddlywiki]]
|
||||
button-classes: tc-text-editor-toolbar-item-start-group
|
||||
shortcuts: ((suggest-tags))
|
||||
dropdown: $:/plugins/tiddlywiki/externaltasksdemo/EditorToolbar/SuggestTagsDropdown
|
||||
|
||||
<$set name="inputFilter" filter="[<targetTiddler>]">
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="badtagger" 3=<<inputFilter>> 4="PipeOutput" statusTitle="JobStatus"/>
|
||||
</$set>
|
||||
Suggest Tags - <$text text=<<targetTiddler>>/>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
title: $:/plugins/tiddlywiki/externaltasksdemo/EditorToolbar/SuggestTagsDropdown
|
||||
|
||||
<$button message="tm-server-refresh">Refresh</$button>
|
||||
|
||||
Status: {{JobStatus}} {{JobStatus!!message}}
|
||||
|
||||
Results: {{PipeOutput}}
|
||||
@@ -11,7 +11,7 @@ Status: {{JobStatus}} {{JobStatus!!message}}
|
||||
This demo requires the example task `reverser.js 8081` to be running in a separate command window. You can edit the [[Sample Text]].
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="reverser" 3="[[Sample Text]]" statusTitle="JobStatus"/>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="reverser" 3="[[Sample Text]]" 4="PipeOutput" statusTitle="JobStatus"/>
|
||||
Reverser
|
||||
</$button>
|
||||
|
||||
@@ -20,21 +20,30 @@ Reverser
|
||||
This demo requires the example task `./recaser.js 8081` or `./recase_erl.sh` to be running in a separate command window. You can edit the [[Sample Text]].
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="recaser" 3="[[Sample Text]]" statusTitle="JobStatus"/>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="recaser" 3="[[Sample Text]]" 4="PipeOutput" statusTitle="JobStatus"/>
|
||||
Recaser
|
||||
</$button>
|
||||
|
||||
!! Tagger
|
||||
|
||||
This demo requires the example task `./badtagger.js 8081` to be running in a separate command window. You can edit the [[Sample Text]].
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="badtagger" 3="[[Sample Text]]" 4="PipeOutput" statusTitle="JobStatus"/>
|
||||
Tagger
|
||||
</$button>
|
||||
|
||||
!! Mimic
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="mimic" 3="[[Alice in Wonderland]]" 4="5" 5="5000" statusTitle="JobStatus"/>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="mimic" 3="[[Alice in Wonderland]]" 4="PipeOutput" 5="5" 6="5000" statusTitle="JobStatus"/>
|
||||
Mimic
|
||||
</$button>
|
||||
|
||||
!! Stats
|
||||
|
||||
<$button>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="stats" 3="[[Alice in Wonderland]]" statusTitle="JobStatus"/>
|
||||
<$action-sendmessage $message="tm-execute-job" 1="--pipe" 2="stats" 3="[[Alice in Wonderland]]" 4="PipeOutput" statusTitle="JobStatus"/>
|
||||
Stats
|
||||
</$button>
|
||||
|
||||
|
||||
@@ -94,6 +94,26 @@
|
||||
"MY_VARIABLE": "value"
|
||||
},
|
||||
"timeout": 100
|
||||
},
|
||||
"badtagger": {
|
||||
"type": "socket-erlang",
|
||||
"host": "127.0.0.1",
|
||||
"port": 8081,
|
||||
"encoding": "latin1",
|
||||
"input": {
|
||||
"format": "rendered-text"
|
||||
},
|
||||
"output": {
|
||||
"format": "text",
|
||||
"tiddler": {
|
||||
"title": "PipeOutput",
|
||||
"type": "text/plain"
|
||||
}
|
||||
},
|
||||
"environment": {
|
||||
"MY_VARIABLE": "value"
|
||||
},
|
||||
"timeout": 100
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user