1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-10 20:56:39 +00:00

Add jsonstringify operator

This commit is contained in:
Jermolene 2017-10-29 15:53:53 +00:00
parent e646d207c3
commit d7a6816307
5 changed files with 49 additions and 3 deletions

View File

@ -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) {

View File

@ -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
};
/*

View File

@ -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[]]""">>

View File

@ -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[]]""">>

View File

@ -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">>