From d7a6816307f4fd47df29cb8e0343ea39c079a2b0 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sun, 29 Oct 2017 15:53:53 +0000 Subject: [PATCH] Add jsonstringify operator --- core/modules/filters/encodings.js | 8 ++++++++ core/modules/utils/utils.js | 19 ++++++++++++++++++- .../stringify_Operator_(Examples).tid | 4 ++-- .../jsonstringify Operator (Examples).tid | 7 +++++++ .../tiddlers/jsonstringify Operator.tid | 14 ++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 editions/tw5.com/tiddlers/jsonstringify Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/jsonstringify Operator.tid diff --git a/core/modules/filters/encodings.js b/core/modules/filters/encodings.js index 24c44aaa3..b55911cdd 100644 --- a/core/modules/filters/encodings.js +++ b/core/modules/filters/encodings.js @@ -72,6 +72,14 @@ exports.stringify = function(source,operator,options) { return results; }; +exports.jsonstringify = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + results.push($tw.utils.jsonStringify(title)); + }); + return results; +}; + exports.escaperegexp = function(source,operator,options) { var results = []; source(function(tiddler,title) { diff --git a/core/modules/utils/utils.js b/core/modules/utils/utils.js index c80b6543d..a52f0cbf3 100644 --- a/core/modules/utils/utils.js +++ b/core/modules/utils/utils.js @@ -543,7 +543,24 @@ exports.stringify = function(s) { .replace(/'/g, "\\'") // single quote character .replace(/\r/g, '\\r') // carriage return .replace(/\n/g, '\\n') // line feed - .replace(/[\x80-\uFFFF]/g, exports.escape); // non-ASCII characters + .replace(/[\x00-\x1f\x80-\uFFFF]/g, exports.escape); // non-ASCII characters +}; + +// Turns a string into a legal JSON string +// Derived from peg.js, thanks to David Majda +exports.jsonStringify = function(s) { + // See http://www.json.org/ + return (s || "") + .replace(/\\/g, '\\\\') // backslash + .replace(/"/g, '\\"') // double quote character + .replace(/\r/g, '\\r') // carriage return + .replace(/\n/g, '\\n') // line feed + .replace(/\x08/g, '\\b') // backspace + .replace(/\x0c/g, '\\f') // formfeed + .replace(/\t/g, '\\t') // tab + .replace(/[\x00-\x1f\x80-\uFFFF]/g,function(s) { + return '\\u' + $tw.utils.pad(s.charCodeAt(0).toString(16).toUpperCase(),4); + }); // non-ASCII characters }; /* diff --git a/editions/tw5.com/tiddlers/filters/examples/stringify_Operator_(Examples).tid b/editions/tw5.com/tiddlers/filters/examples/stringify_Operator_(Examples).tid index 8e33bc6be..1378287f5 100644 --- a/editions/tw5.com/tiddlers/filters/examples/stringify_Operator_(Examples).tid +++ b/editions/tw5.com/tiddlers/filters/examples/stringify_Operator_(Examples).tid @@ -1,7 +1,7 @@ created: 20161017154944352 -modified: 20161017155350400 +modified: 20171029155233487 tags: [[Operator Examples]] [[stringify Operator]] title: stringify Operator (Examples) type: text/vnd.tiddlywiki -<<.operator-example 1 """[[Title with "double quotes" and \backslash]] +[stringify[]]""">> +<<.operator-example 1 """[[Title with "double quotes" and single ' and \backslash]] +[stringify[]]""">> diff --git a/editions/tw5.com/tiddlers/jsonstringify Operator (Examples).tid b/editions/tw5.com/tiddlers/jsonstringify Operator (Examples).tid new file mode 100644 index 000000000..20f7bff75 --- /dev/null +++ b/editions/tw5.com/tiddlers/jsonstringify Operator (Examples).tid @@ -0,0 +1,7 @@ +created: 20171029155046637 +modified: 20171029155227382 +tags: [[Operator Examples]] [[stringify Operator]] +title: jsonstringify Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 """[[Title with "double quotes" and single ' and \backslash]] +[jsonstringify[]]""">> diff --git a/editions/tw5.com/tiddlers/jsonstringify Operator.tid b/editions/tw5.com/tiddlers/jsonstringify Operator.tid new file mode 100644 index 000000000..14ca58c3f --- /dev/null +++ b/editions/tw5.com/tiddlers/jsonstringify Operator.tid @@ -0,0 +1,14 @@ +caption: jsonstringify +created: 20171029155051467 +from-version: 5.1.14 +modified: 20171029155143797 +op-input: a [[selection of titles|Title Selection]] +op-output: the input with JSON string encodings applied +op-parameter: +op-parameter-name: +op-purpose: apply JSON string encoding to a string +tags: [[Filter Operators]] [[String Operators]] +title: jsonstringify Operator +type: text/vnd.tiddlywiki + +<<.operator-examples "jsonstringify">>