diff --git a/core/modules/filters/format/json.js b/core/modules/filters/format/json.js new file mode 100644 index 000000000..2130a76ed --- /dev/null +++ b/core/modules/filters/format/json.js @@ -0,0 +1,35 @@ +/*\ +title: $:/core/modules/filters/format/json.js +type: application/javascript +module-type: formatfilteroperator +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.json = function(source,operand,options) { + var results = [], + spaces = null; + if(operand) { + spaces = /^\d+$/.test(operand) ? parseInt(operand,10) : operand; + } + source(function(tiddler,title) { + var data = $tw.utils.parseJSONSafe(title); + try { + data = JSON.parse(title); + } catch(e) { + data = undefined; + } + if(data !== undefined) { + results.push(JSON.stringify(data,null,spaces)); + } + }); + return results; +}; + +})(); \ No newline at end of file diff --git a/core/modules/filters/json-ops.js b/core/modules/filters/json-ops.js new file mode 100644 index 000000000..d5e8c33af --- /dev/null +++ b/core/modules/filters/json-ops.js @@ -0,0 +1,153 @@ +/*\ +title: $:/core/modules/filters/json-ops.js +type: application/javascript +module-type: filteroperator + +Filter operators for JSON operations + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports["jsonget"] = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + var data = $tw.utils.parseJSONSafe(title,title); + if(data) { + var item = getDataItemValueAsString(data,operator.operands); + if(item !== undefined) { + results.push(item); + } + } + }); + return results; +}; + +exports["jsonindexes"] = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + var data = $tw.utils.parseJSONSafe(title,title); + if(data) { + var item = getDataItemKeysAsStrings(data,operator.operands); + if(item !== undefined) { + results.push.apply(results,item); + } + } + }); + return results; +}; + +exports["jsontype"] = function(source,operator,options) { + var results = []; + source(function(tiddler,title) { + var data = $tw.utils.parseJSONSafe(title,title); + if(data) { + var item = getDataItemType(data,operator.operands); + if(item !== undefined) { + results.push(item); + } + } + }); + return results; +}; + +/* +Given a JSON data structure and an array of index strings, return an array of the string representation of the values at the end of the index chain, or "undefined" if any of the index strings are invalid +*/ +function getDataItemValueAsString(data,indexes) { + // Get the item + var item = getDataItem(data,indexes); + // Return the item as a string + return convertDataItemValueToString(item); +} + +/* +Given a JSON data structure and an array of index strings, return an array of the string representation of the keys of the item at the end of the index chain, or "undefined" if any of the index strings are invalid +*/ +function getDataItemKeysAsStrings(data,indexes) { + // Get the item + var item = getDataItem(data,indexes); + // Return the item keys as a string + return convertDataItemKeysToStrings(item); +} + +/* +Return an array of the string representation of the values of a data item, or "undefined" if the item is undefined +*/ +function convertDataItemValueToString(item) { + // Return the item as a string + if(item === undefined) { + return item; + } + if(typeof item === "object") { + return JSON.stringify(item); + } + return item.toString(); +} + +/* +Return an array of the string representation of the keys of a data item, or "undefined" if the item is undefined +*/ +function convertDataItemKeysToStrings(item) { + // Return the item as a string + if(item === undefined) { + return item; + } else if(typeof item === "object") { + if(item === null) { + return []; + } + var results = []; + if($tw.utils.isArray(item)) { + for(var i=0; i> -All tiddler titles tagged with <> formatted as a title list : +All tiddler titles tagged with <> formatted as a title list: <<.operator-example 5 """[tag[TableOfContents]format:titlelist[]]""">> +A JSON string formatted as JSON – note how the JSON string is normalised to remove the duplicated properties: +<<.operator-example 6 """[[{"one":"first","one":"another","two":"second"}]format:json[]]""">> + <<.tip "To create a string to save a [[title list|Title List]] into a list field, use `format:titlelist[]` with the [[join operator|join Operator]]">> <<.operator-example 6 """[tag[TableOfContents]format:titlelist[]join[ ]]""">> For example, to save titles tagged `TableOfContents` to the titles field of the tiddler [[format titlelist test]]: diff --git a/editions/tw5.com/tiddlers/filters/format.tid b/editions/tw5.com/tiddlers/filters/format.tid index 9cc846139..e222e9d50 100644 --- a/editions/tw5.com/tiddlers/filters/format.tid +++ b/editions/tw5.com/tiddlers/filters/format.tid @@ -1,6 +1,6 @@ caption: format created: 20201020100834443 -modified: 20220523075550449 +modified: 20220611104737314 op-input: a [[selection of titles|Title Selection]] op-output: input strings formatted according to the specified suffix <<.place B>> op-parameter: optional format string for the formats @@ -17,9 +17,10 @@ type: text/vnd.tiddlywiki The suffix <<.place B>> is one of the following supported string formats: |!Format |!Description | -|^`date` |The input string is interpreted as a UTC date and displayed according to the DateFormat specified in the optional operator parameter. (Defaults to "YYYY MM DD 0hh:0mm") | -|^`relativedate` |The input string is interpreted as a UTC date and displayed as the interval from the present instant. Any operator parameters are ignored. | -|^`titlelist` |<<.from-version "5.2.0">>The input string wrapped in double square brackets if it contains a space. Appropriate for use in a [[title list|Title List]]. | +|^`date` |The input string is interpreted as a UTC date and displayed according to the DateFormat specified in the optional operator operand. (Defaults to "YYYY MM DD 0hh:0mm") | +|^`json` |<<.from-version "5.3.0">> The input string is interpreted as JSON and displayed with standard formatting. The optional operator operand specifies the number of spaces to use for indenting, or a string to use for indenting. Nothing is returned if the input string is not valid JSON | +|^`relativedate` |The input string is interpreted as a UTC date and displayed as the interval from the present instant. Any operator parameters are ignored | +|^`titlelist` |<<.from-version "5.2.0">> The input string wrapped in double square brackets if it contains a space. Appropriate for use in a [[title list|Title List]]. | <<.warning """The [[Title List]] format cannot reliably represent items that contain certain specific character sequences such as `]] `. Thus it should not be used where there is a possibility of such sequences occurring.""">> diff --git a/editions/tw5.com/tiddlers/filters/jsonget.tid b/editions/tw5.com/tiddlers/filters/jsonget.tid new file mode 100644 index 000000000..a21c0d4fd --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/jsonget.tid @@ -0,0 +1,86 @@ +created: 20220611104737314 +modified: 20220611104737314 +tags: [[Filter Operators]] [[JSON Operators]] +title: jsonget Operator +caption: jsonget +op-purpose: retrieve the value of a property from JSON strings +op-input: a selection of JSON strings +op-parameter: one or more indexes of the property to retrieve +op-output: the values of each of the retrieved properties + +<<.from-version "5.3.0">> See [[JSON in TiddlyWiki]] for background. + +The <<.op jsonget>> operator is used to retrieve values from JSON data. See also the following related operators: + +* <<.olink jsontype>> to retrieve the type of a JSON value +* <<.olink jsonindexes>> to retrieve the names of the fields of a JSON object, or the indexes of a JSON array + +Properties within a JSON object are identified by a sequence of indexes. In the following example, the value at `[a]` is `one`, and the value at `[d][f][0]` is `five`. + +``` +{ + "a": "one", + "b": "", + "c": "three", + "d": { + "e": "four", + "f": [ + "five", + "six", + true, + false, + null + ], + "g": { + "x": "max", + "y": "may", + "z": "maize" + } + } +} +``` + +The following examples assume that this JSON data is contained in a variable called `jsondata`. + +The <<.op jsonget>> operator uses multiple operands to specify the indexes of the property to retrieve: + +``` +[jsonget[a]] --> "one" +[jsonget[d],[e]] --> "four" +[jsonget[d],[f],[0]] --> "five" +``` + +Indexes can be dynamically composed from variables and transclusions: + +``` +[jsonget,{!!field},[0]] +``` + +Boolean values and null are returned as normal strings. The <<.olink jsontype>> operator can be used to retrieve a string identifying the original type. Thus: + +``` +[jsontype[a]] --> "string" +[jsontype[d]] --> "object" +[jsontype[d],[f]] --> "array" +[jsontype[d],[f],[2]] --> "boolean" +``` + +Using the <<.op jsonget>> operator to retrieve an object or an array returns a JSON string of the values. For example: + +``` +[jsonget[d],[f]] --> `["five","six",true,false,null]` +[jsonget[d],[g]] --> `{"x": "max","y": "may","z": "maize"}` +``` + +The <<.olink jsonindexes>> operator retrieves the corresponding indexes: + +``` +[jsonindexes[d],[f]] --> "0", "1", "2", "3", "4" +[jsonindexes[d],[g]] --> "x", "y", "z" +``` + +A subtlety is that the special case of a single blank operand is used to identify the root object. Thus: + +``` +[jsonindexes[]] --> "a", "b", "c", "d" +``` diff --git a/editions/tw5.com/tiddlers/filters/jsonindexes.tid b/editions/tw5.com/tiddlers/filters/jsonindexes.tid new file mode 100644 index 000000000..132343832 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/jsonindexes.tid @@ -0,0 +1,64 @@ +created: 20220611104737314 +modified: 20220611104737314 +tags: [[Filter Operators]] [[JSON Operators]] +title: jsonindexes Operator +caption: jsonindexes +op-purpose: retrieve the value of a property from JSON strings +op-input: a selection of JSON strings +op-parameter: one or more indexes of the property to retrieve +op-output: the values of each of the retrieved properties + +<<.from-version "5.3.0">> See [[JSON in TiddlyWiki]] for background. + +The <<.op jsonindexes>> operator is used to retrieve the property names of JSON objects or the index names of JSON arrays. See also the following related operators: + +* <<.olink jsonget>> to retrieve the values of a property in JSON data +* <<.olink jsontype>> to retrieve the type of a JSON value + +Properties within a JSON object are identified by a sequence of indexes. In the following example, the value at `[a]` is `one`, and the value at `[d][f][0]` is `five`. + +``` +{ + "a": "one", + "b": "", + "c": "three", + "d": { + "e": "four", + "f": [ + "five", + "six", + true, + false, + null + ], + "g": { + "x": "max", + "y": "may", + "z": "maize" + } + } +} +``` + +The following examples assume that this JSON data is contained in a variable called `jsondata`. + +The <<.op jsonindexes>> operator uses multiple operands to specify the indexes of the property to retrieve: + +``` +[jsonindexes[d],[f]] --> "0", "1", "2", "3", "4" +[jsonindexes[d],[g]] --> "x", "y", "z" +``` + +Indexes can be dynamically composed from variables and transclusions: + +``` +[jsonindexes,{!!field}] +``` + +Retrieving the indexes of JSON properties that are not objects or arrays will return nothing. + +A subtlety is that the special case of a single blank operand is used to identify the root object. Thus: + +``` +[jsonindexes[]] --> "a", "b", "c", "d" +``` diff --git a/editions/tw5.com/tiddlers/filters/jsontype.tid b/editions/tw5.com/tiddlers/filters/jsontype.tid new file mode 100644 index 000000000..90907cec6 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/jsontype.tid @@ -0,0 +1,73 @@ +created: 20220611104737314 +modified: 20220611104737314 +tags: [[Filter Operators]] [[JSON Operators]] +title: jsontype Operator +caption: jsontype +op-purpose: retrieve the type of a property from JSON strings +op-input: a selection of JSON strings +op-parameter: one or more indexes of the property whose type is to be retrieved +op-output: the types of each of the retrieved properties + +<<.from-version "5.3.0">> See [[JSON in TiddlyWiki]] for background. + +The <<.op jsontype>> operator is used to retrieve the type of a property in JSON data. See also the following related operators: + +* <<.olink jsonget>> to retrieve the values of a property in JSON data +* <<.olink jsonindexes>> to retrieve the names of the fields of a JSON object, or the indexes of a JSON array + +JSON supports the following data types: + +* ''string'' - a Unicode string +* ''number'' - a floating point number +* ''boolean'' - Boolean value (true or false) +* ''array'' - an array of values +* ''object'' - an object of name/value pairs +* ''null'' - a special type representing a missing value + +Properties within a JSON object are identified by a sequence of indexes. In the following example, the value at `[a]` is `one`, and the value at `[d][f][0]` is `five`. + +``` +{ + "a": "one", + "b": "", + "c": "three", + "d": { + "e": "four", + "f": [ + "five", + "six", + true, + false, + null + ], + "g": { + "x": "max", + "y": "may", + "z": "maize" + } + } +} +``` + +The following examples assume that this JSON data is contained in a variable called `jsondata`. + +The <<.op jsontype>> operator uses multiple operands to specify the indexes of the property whose type is to be retrieved: + +``` +[jsontype[a]] --> "string" +[jsontype[d]] --> "object" +[jsontype[d],[f]] --> "array" +[jsontype[d],[f],[2]] --> "boolean" +``` + +Indexes can be dynamically composed from variables and transclusions: + +``` +[jsontype,{!!field},[0]] +``` + +A subtlety is that the special case of a single blank operand is used to identify the root object. Thus: + +``` +[jsontype[]] --> "object" +``` diff --git a/plugins/tiddlywiki/internals/editpreviews/parse-tree.tid b/plugins/tiddlywiki/internals/editpreviews/parse-tree.tid index aafa30ecf..205ef7cae 100644 --- a/plugins/tiddlywiki/internals/editpreviews/parse-tree.tid +++ b/plugins/tiddlywiki/internals/editpreviews/parse-tree.tid @@ -3,13 +3,121 @@ tags: $:/tags/EditPreview list-after: $:/core/ui/EditTemplate/body/preview/output caption: parse tree -\define preview(mode) -<$wikify name="preview-text" text={{!!text}} type={{!!type}} mode="$mode$" output="parsetree"> -
-
-<$text text=<>/>
-
-
+\whitespace trim + +\procedure preview-node-properties(node) +<$let excludeProperties="text type tag children attributes orderedAttributes"> +<$list filter="[jsonindexes[]] -[subfilter] +[limit[1]]" variable="ignore"> + + +<$list filter="[jsonindexes[]] -[subfilter] +[sort[]]" variable="index"> + + + + + + +
+<$text text=<>/> + +<$text text={{{ [jsonget] }}}/> +
+ + +\end + +\procedure preview-node-attribute-string(attribute) +<$text text={{{ [jsonget[value]] }}}/> +\end + +\procedure preview-node-attribute-indirect(attribute) +{{<$text text={{{ [jsonget[textReference]] }}}/>}} +\end + +\procedure preview-node-attribute-macro(attribute) +<< +<$text text={{{ [jsonget[value],[name]] }}}/> +<$list filter="[jsonindexes[value],[params]]" variable="index"> +  +<$list filter="[jsonget[value],[params],,[name]]" variable="ignore"> +<$text text={{{ [jsonget[value],[params],,[name]] }}}/> +: + +<$text text={{{ [jsonget[value],[params],,[value]] }}}/> + +>> +\end + +\procedure preview-node-attributes(node) +<$list filter="[jsonindexes[attributes]limit[1]]" variable="ignore"> + + +<$list filter="[jsonindexes[attributes]sort[]]" variable="index"> + + + + + + +
+<$text text=<>/> + +<$let type={{{ [jsonget[attributes],,[type]] }}}> +<$transclude $variable={{{ [match[string]then[preview-node-attribute-string]] :else[match[indirect]then[preview-node-attribute-indirect]] :else[match[macro]then[preview-node-attribute-macro]] }}} attribute={{{ [jsonget[attributes],] }}}/> + +
+ +\end + +\procedure preview-node-children(node) +
+<$transclude $variable="preview-node-properties" node=<>/> +<$transclude $variable="preview-node-attributes" node=<>/> +<$transclude $variable="preview-node-list" nodeList={{{ [jsonget[children]] }}}/> +
+\end + +\procedure preview-node-title-widget(node) +
+
+<$<$text text={{{ [jsonget[type]] }}}/>> +
+<$transclude $variable="preview-node-children" node=<>/> +
+\end + +\procedure preview-node-title-element(node) +
+
+<<$text text={{{ [jsonget[tag]] }}}/>> +
+<$transclude $variable="preview-node-children" node=<>/> +
+\end + +\procedure preview-node-title-text(node) +
+
+"<$text text={{{ [jsonget[text]] }}}/>" +
+
+\end + +\procedure preview-node(node) +<$let type={{{ [jsonget[type]] }}}> +<$transclude $variable={{{ [match[element]then[preview-node-title-element]] :else[match[text]then[preview-node-title-text]] :else[[preview-node-title-widget]] }}} node=<>/> + +\end + +\procedure preview-node-list(nodeList) +<$list filter="[jsonindexes[]]" variable="index"> +<$transclude $variable="preview-node" node={{{ [jsonget] }}}/> + +\end + +\procedure preview(mode) +<$wikify name="preview-json" text={{!!text}} type={{!!type}} mode=<> output="parsetree"> +<$transclude $variable="preview-node-list" nodeList=<>/> \end