diff --git a/core/modules/filters/getvariablejson.js b/core/modules/filters/getvariablejson.js deleted file mode 100644 index 32b665461..000000000 --- a/core/modules/filters/getvariablejson.js +++ /dev/null @@ -1,31 +0,0 @@ -/*\ -title: $:/core/modules/filters/getvariablejson.js -type: application/javascript -module-type: filteroperator - -Filter operator for replacing input values by the value of the variable with the same name, or blank if the variable is missing - -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -/* -Export our filter function -*/ -exports.getvariablejson = function(source,operator,options) { - var results = [], - space = operator.operands[1] || null, - replacerList = (operator.operands[2]) ? operator.operands[2].split(" ") : null, - widget = options.widget; - source(function(tiddler,title) { - var variable = widget.getVariableInfo(title, {}), - text = JSON.stringify(variable,replacerList,space); - results.push(text || ""); - }); - return results; -}; - -})(); diff --git a/core/modules/filters/jsonvariable.js b/core/modules/filters/jsonvariable.js new file mode 100644 index 000000000..cb5e1a575 --- /dev/null +++ b/core/modules/filters/jsonvariable.js @@ -0,0 +1,33 @@ +/*\ +title: $:/core/modules/filters/jsonvariable.js +type: application/javascript +module-type: filteroperator + +Filter operator to get widget variable info and +Display as JSON with basic formatting + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Export our filter function +*/ +exports.jsonvariable = function(source,operator,options) { + var results = [], + widget = options.widget; + // "replacer" must be defined, otherwise JSON.stringify will throw a circular reference error RSOD + // "replacer" does not contain: isCacheable + var replacer= "params name value default resultList srcVariable text isFunctionDefinition isProcedureDefinition isWidgetDefinition isMacroDefinition".split(" "); + source(function(tiddler,title) { + var variable = widget.getVariableInfo(title, {}), + text = JSON.stringify(variable,replacer); + results.push(text || ""); + }); + return results; +}; + +})(); diff --git a/editions/tw5.com/tiddlers/jsonvariable Operator (Examples).tid b/editions/tw5.com/tiddlers/jsonvariable Operator (Examples).tid new file mode 100644 index 000000000..37d060eca --- /dev/null +++ b/editions/tw5.com/tiddlers/jsonvariable Operator (Examples).tid @@ -0,0 +1,42 @@ +created: 20240506132850677 +modified: 20240506150652759 +tags: [[Operator Examples]] [[jsonvariable Operator]] +title: jsonvariable Operator (Examples) +type: text/vnd.tiddlywiki + +The following example shows the "internal" structure of the `fn.test` function defined in <<.olink jsonvariable>> operator + +<<.example 1 """\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] + +
<$text text={{{[[fn.test]jsonvariable[]]}}}/>
+""">> + +The following example shows the same structure ''pretty printed'' using the <<.olink format>> filter operator + +<<.example 2 """\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] + +\function tab() [charcode[9]] + +
<$text text={{{[[fn.test]jsonvariable[]format:json]}}}/>
+""">> + +The following example extracts the ''function definition'' using the <<.olink jsonextract>> filter operator + +<<.example 3 """\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] + +
<$text text={{{[[fn.test]jsonvariable[]jsonextract[srcVariable],[value]]}}}/>
+""">> + +The following example extracts the `srcVariables.params` without pretty printing using <<.olink jsonextract>> + +<<.example 4 """\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] + +\function tab() [charcode[9]] + +
<$text text={{{[[fn.test]jsonvariable[]jsonextract[srcVariable],[params]format:json]}}}/>
""">> + + diff --git a/editions/tw5.com/tiddlers/jsonvariable Operator.tid b/editions/tw5.com/tiddlers/jsonvariable Operator.tid new file mode 100644 index 000000000..17b1b058f --- /dev/null +++ b/editions/tw5.com/tiddlers/jsonvariable Operator.tid @@ -0,0 +1,45 @@ +caption: jsonvariable +created: 20240506131935424 +modified: 20240506145944709 +op-input: a selection variable names +op-output: the JSON string values of each of the retrieved properties +op-parameter: one or more indexes of the property to retrieve +op-parameter-name: R +op-purpose: retrieve the JSON string from a ~TiddlyWiki variable +tags: [[Filter Operators]] [[JSON Operators]] +title: jsonvariable Operator +type: text/vnd.tiddlywiki + +\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] +\function tab() [charcode[9]] + +<<.from-version "5.3.4">> + +The <<.op jsonvariable>> operator is used to retrieve variables as JSON substrings. See also the following related operators: + +Properties within a JSON object are identified by a sequence of indexes. In the following example, we use a procedure: `testProc` and a function: `fn.test` to show the possibilities of <<.op jsonvariable>>. The `tab`-function is needed for the <<.olink format>> ooperator, which is used to "pretty-print" the output. + +``` +\procedure testProc() [[aa aa]] bb +\function fn.test(splitChar:" ") [enlistformat:titlelist[]] + +\function tab() [charcode[9]] + +
<$text text={{{[[fn.test]jsonvariable[]format:json]}}}/>
+``` + +Which results to: + +
<$text text={{{[[fn.test]jsonvariable[]format:json]}}}/>
+ +Also see: + +* <<.olink jsonextract>> to retrieve a JSON value as a string of JSON +* <<.olink jsonget>> to retrieve the values of a property in JSON data +* <<.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 + +Every TW variable has this internal structure, which we can use to extract the info we need. + +!! <<.operator-examples "jsonvariable">> \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/test-getvariable.tid b/editions/tw5.com/tiddlers/test-getvariable.tid deleted file mode 100644 index f1cf427b9..000000000 --- a/editions/tw5.com/tiddlers/test-getvariable.tid +++ /dev/null @@ -1,14 +0,0 @@ -created: 20240505165315301 -modified: 20240505170435200 -tags: -title: test-getvariable -type: text/vnd.tiddlywiki - -\procedure proc() some text - -\function fn() [] - -\function xx() [[proc]] [[fn]] :map[getvariable[value]] +[join[ ]] - -<> - diff --git a/editions/tw5.com/tiddlers/test-getvariablejson.tid b/editions/tw5.com/tiddlers/test-getvariablejson.tid deleted file mode 100644 index 8a02a714c..000000000 --- a/editions/tw5.com/tiddlers/test-getvariablejson.tid +++ /dev/null @@ -1,29 +0,0 @@ -created: 20240506064659892 -modified: 20240506101142810 -tags: -title: test-getvariablejson -type: text/vnd.tiddlywiki - -\define test-macro(a:10) test macro - -\procedure test-proc(a:10) test proc - -\function my.test(x:3, abc:def) [split[ ]] - -\function tab() [charcode[9]] - - -\define elements() srcVariable params name default -\define openBracket() [ -\define closeBracket() ] - - -<<.example 1 """\procedure test-proc(a:10) test proc -\function my.test(x:3, abc:def) [split[ ]] -\function tab() [charcode[9]] - -
<$text text={{{[[my.test]getvariablejson[],]}}}/>
-""">> - -<<.example 2 """
<$text text={{{[[my.test]getvariablejson[],] +[jsonextract[srcVariable],[params]] +[format:json]  }}}/>
""">> - diff --git a/editions/tw5.com/tiddlers/test-getvariablejson-conat.tid b/editions/tw5.com/tiddlers/test-jsonvariable-conat.tid similarity index 54% rename from editions/tw5.com/tiddlers/test-getvariablejson-conat.tid rename to editions/tw5.com/tiddlers/test-jsonvariable-conat.tid index 303bed912..274da207f 100644 --- a/editions/tw5.com/tiddlers/test-getvariablejson-conat.tid +++ b/editions/tw5.com/tiddlers/test-jsonvariable-conat.tid @@ -1,35 +1,35 @@ created: 20240506101435113 -modified: 20240506105325655 +modified: 20240506140610448 tags: -title: test-getvariablejson-conat +title: test-jsonvariable-conat type: text/vnd.tiddlywiki ''Nice to have'' <<.example 1 """\function tab() [charcode[9]] -
<$text text={{{[[.concat]getvariablejson[]]}}}/>
+
<$text text={{{[[.concat]jsonvariable[]]}}}/>
""">> ''Human readable version'' <<.example 2 """\function tab() [charcode[9]] -
<$text text={{{[[.concat]getvariablejson[]format:json]}}}/>
+
<$text text={{{[[.concat]jsonvariable[]format:json]}}}/>
""">> ''Function definition'' <<.example 3 """\function tab() [charcode[9]] -
<$text text={{{[[.concat]getvariablejson[]jsonextract[srcVariable],[value]format:json]}}}/>
+
<$text text={{{[[.concat]jsonvariable[]jsonextract[srcVariable],[value]format:json]}}}/>
""">> ''Get parameters'' <<.example 4 """\function tab() [charcode[9]] -
<$text text={{{[[.concat]getvariablejson[]jsonextract[srcVariable],[params]format:json]}}}/>
+
<$text text={{{[[.concat]jsonvariable[]jsonextract[srcVariable],[params]format:json]}}}/>
""">> ''Expected''