From 83ee363cb4c19246ac977f55e46439dd8a1508ba Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Tue, 22 Jun 2021 21:52:00 +0100 Subject: [PATCH] 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? --- core/modules/filters/strings.js | 10 ++++++++++ editions/test/tiddlers/tests/test-filters.js | 6 ++++++ editions/tw5.com/tiddlers/filters/charcode.tid | 17 +++++++++++++++++ .../tiddlers/filters/examples/charcode.tid | 7 +++++++ 4 files changed, 40 insertions(+) create mode 100644 editions/tw5.com/tiddlers/filters/charcode.tid create mode 100644 editions/tw5.com/tiddlers/filters/examples/charcode.tid diff --git a/core/modules/filters/strings.js b/core/modules/filters/strings.js index 286907dca..6fd64df08 100644 --- a/core/modules/filters/strings.js +++ b/core/modules/filters/strings.js @@ -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("")]; +}; + })(); diff --git a/editions/test/tiddlers/tests/test-filters.js b/editions/test/tiddlers/tests/test-filters.js index ee428b1a0..9b8c2bd44 100644 --- a/editions/test/tiddlers/tests/test-filters.js +++ b/editions/test/tiddlers/tests/test-filters.js @@ -818,6 +818,12 @@ function runTests(wiki) { it("should handle the deserializers operator", function() { 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(""); + }); } diff --git a/editions/tw5.com/tiddlers/filters/charcode.tid b/editions/tw5.com/tiddlers/filters/charcode.tid new file mode 100644 index 000000000..85d70111e --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/charcode.tid @@ -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">> diff --git a/editions/tw5.com/tiddlers/filters/examples/charcode.tid b/editions/tw5.com/tiddlers/filters/examples/charcode.tid new file mode 100644 index 000000000..f68d23952 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/charcode.tid @@ -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]]">>