1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add charcode operator to make it easier to generate strings containing control characters

Avoids some confusing hacks. @saqimtiaz I'm guessing you might have already done something like this?
This commit is contained in:
jeremy@jermolene.com 2021-06-22 21:52:00 +01:00
parent 63fa0c4fa4
commit 83ee363cb4
4 changed files with 40 additions and 0 deletions

View File

@ -172,4 +172,14 @@ exports.pad = function(source,operator,options) {
return results;
}
exports.charcode = function(source,operator,options) {
var chars = [];
$tw.utils.each(operator.operands,function(operand) {
if(operand !== "") {
chars.push(String.fromCharCode($tw.utils.parseInt(operand)));
}
});
return [chars.join("")];
};
})();

View File

@ -819,6 +819,12 @@ function runTests(wiki) {
expect(wiki.filterTiddlers("[deserializers[]]").join(",")).toBe("application/javascript,application/json,application/x-tiddler,application/x-tiddler-html-div,application/x-tiddlers,text/css,text/html,text/plain");
});
it("should handle the charcode operator", function() {
expect(wiki.filterTiddlers("[charcode[9]]").join(" ")).toBe(String.fromCharCode(9));
expect(wiki.filterTiddlers("[charcode[9],[10]]").join(" ")).toBe(String.fromCharCode(9) + String.fromCharCode(10));
expect(wiki.filterTiddlers("[charcode[]]").join(" ")).toBe("");
});
}
});

View File

@ -0,0 +1,17 @@
caption: charcode
created: 20210622214425635
modified: 20210622214425635
op-input: ignored
op-output: a string formed from concatenating the characters specified by the numeric codes given in the operand(s)
op-parameter: numeric character code
op-parameter-name: C
op-purpose: generates string characters from their numeric character codes
tags: [[Filter Operators]]
title: charcode Operator
type: text/vnd.tiddlywiki
<<.from-version "5.1.24">>
This operator returns a string formed from concatenating the characters specified by the numeric codes given in one or more operands. It is useful for generating special characters such as tab (`charcode[9]`) or new line (`charcode[13],[10]`).
<<.operator-examples "charcode">>

View File

@ -0,0 +1,7 @@
created: 20210622214849214
modified: 20210622214849214
tags: [[charcode Operator]] [[Operator Examples]]
title: charcode Operator (Examples)
type: text/vnd.tiddlywiki
<<.operator-example 1 "[charcode[65]match[A]]">>