1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-02 17:00:45 +00:00

WIP dumpvariables, search-variables seem to work now using the format-operator

This commit is contained in:
pmario 2024-02-21 17:39:00 +01:00
parent 0b69e88b29
commit 357e6f5ef3
13 changed files with 272 additions and 42 deletions

View File

@ -0,0 +1,33 @@
/*\
title: $:/core/modules/filters/format/variable.js
type: application/javascript
module-type: formatfilteroperator
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Export our filter function
*/
exports.variable = function(source,operand,options) {
var results = [],
widget = options.widget,
variable,
varInfo,
variableTemplate = (operand.length >=6 && operand) ? operand : "$type$ $name$($params$) $firstLine$";
source(function(tiddler,title) {
varInfo = widget.getVariableInfo(title, {});
varInfo.name = title;
if(title && title.length) {
variable = $tw.utils.formatVariableString(variableTemplate, varInfo);
results.push(variable);
}
});
return results;
};
})();

View File

@ -1,3 +1,5 @@
const { widget } = require("../widgets/widget");
/*\
title: $:/core/modules/filters/getvariable.js
type: application/javascript
@ -16,9 +18,13 @@ Filter operator for replacing input values by the value of the variable with the
Export our filter function
*/
exports.getvariable = function(source,operator,options) {
var results = [];
var results = [],
operand = operator.operand,
widget = options.widget;
source(function(tiddler,title) {
results.push(options.widget.getVariable(title) || "");
var variable = widget.getVariableInfo(title, {}),
text = (operand === "value") ? variable.srcVariable.value : variable.text;
results.push(text || "");
});
return results;
};

View File

@ -16,17 +16,56 @@ Filter operator for returning the names of the active variables
Export our filter function
*/
exports.variables = function(source,operator,options) {
var operands = [];
$tw.utils.each(operator.operands,function(operand,index){
operands.push({
name: (index + 1).toString(),
value: operand
});
});
var names = [],
widget = options.widget;
sort,
widget = options.widget,
included = (operands[0].value) ? operands[0].value : "var fn proc macro widget";
// variableTemplate = (operands.length > 1 && operands[1]) ? operands[1].value : "$type$ $name$($params$) $firstLine$";
switch(operator.suffix) {
case "raw":
sort = false;
break;
case "sort": // the fallthrough is intentional. "sort" is default
default:
sort = true;
break;
}
while(widget && !widget.hasOwnProperty("variables")) {
widget = widget.parentWidget;
}
if(widget && widget.variables) {
for(var variable in widget.variables) {
names.push(variable);
var varInfo = widget.getVariableInfo(variable, {});
// varInfo.name = variable;
// variable = $tw.utils.formatVariableString(variableTemplate, varInfo);
if ( ((included.indexOf("fn") !== -1) && varInfo.srcVariable.isFunctionDefinition ) ||
((included.indexOf("proc") !== -1) && varInfo.srcVariable.isProcedureDefinition ) ||
((included.indexOf("macro") !== -1) && varInfo.srcVariable.isMacroDefinition ) ||
((included.indexOf("widget") !== -1) && varInfo.srcVariable.isWidgetDefinition ) )
{
names.push(variable);
} else if ((included.indexOf("var") !== -1) && !varInfo.srcVariable.isFunctionDefinition && !varInfo.srcVariable.isProcedureDefinition && !varInfo.srcVariable.isMacroDefinition && !varInfo.srcVariable.isWidgetDefinition )
{
names.push(variable);
}
}
}
return names.sort();
if (sort) {
return names.sort();
} else {
return names;
}
};
})();

View File

@ -309,6 +309,66 @@ exports.slowInSlowOut = function(t) {
return (1 - ((Math.cos(t * Math.PI) + 1) / 2));
};
exports.formatVariableString = function(template,options) {
var name = options.name || "",
params = options.params || "",
firstLine = "",
os = options.srcVariable,
type = (os.isFunctionDefinition) ? "\\\\function" : (os.isMacroDefinition) ? "\\\\define" :
(os.isProcedureDefinition) ? "\\\\procedure" : (os.isWidgetDefinition) ? "\\\\widget" : "",
varType = (os.isFunctionDefinition) ? "fn" : (os.isMacroDefinition) ? "macro" :
(os.isProcedureDefinition) ? "proc" : (os.isWidgetDefinition) ? "widget" : "var";
var result = "",
t = (!os.isFunctionDefinition && !os.isMacroDefinition && !os.isProcedureDefinition && !os.isWidgetDefinition) ? "$name$" : template,
matches = [
[/^\$type\$/i, function() {
return (type) ? type : "";
}],
[/^\$name\$/i, function() {
return name;
}],
[/^\$params\$/i, function() {
var elements = [],
paramString = "";
if (params && params[0] && params[0].name) {
$tw.utils.each(params, function(p) {
elements.push(p.name + ((p.default) ? ':"' + p.default + '"' : ""));
});
paramString = elements.join(", ");
}
// return (type) ? "(" + paramString + ")" : "";
return (type) ? paramString : "";
}],
[/^\$firstLine\$/i, function() {
var lines = os.value.split("\n"),
suffix = (lines.length > 1) ? "..." : "";
return (os.isFunctionDefinition) ? lines[0].replace("\\", "\\\\") + suffix: "";
}],
[/^\$varType\$/i, function() {
return varType;
}]
];
while(t.length){
var matchString = "";
$tw.utils.each(matches, function(m) {
var match = m[0].exec(t);
if(match) {
matchString = m[1].call(null,match);
t = t.substr(match[0].length);
return false;
}
});
if(matchString) {
result += matchString;
} else {
result += t.charAt(0);
t = t.substr(1);
}
}
result = result.replace(/\\(.)/g,"$1");
return result.trim();
};
exports.formatTitleString = function(template,options) {
var base = options.base || "",
separator = options.separator || "",

View File

@ -166,6 +166,8 @@ Widget.prototype.getVariableInfo = function(name,options) {
});
resultList = this.wiki.filterTiddlers(value,this.makeFakeWidgetWithVariables(variables),options.source);
value = resultList[0] || "";
} else {
params = variable.params;
}
return {
text: value,

View File

@ -1,22 +1,31 @@
title: $:/core/macros/dumpvariables
tags: $:/tags/Macro
tags: $:/tags/Global
\whitespace trim
\procedure _initRawFilter() [variables:raw<type>]
\procedure _initFilter() [variables<type>]
\function _f.filterString() [<sort>match[raw]then<_initRawFilter>else<_initFilter>]
\function _f.varFormat() [<format>!is[blank]then<format>else[$type$ $name$($params$)]]
\function _f.formattedVar() [<varname>format:variable<_f.varFormat>]
<!-- =================== -->
\procedure dumpvariables(subfilter:"[variables[]]")
\procedure dumpvariables(type sort:"sort" subfilter:"[[]]" format)
<ul>
<$list filter="[variables[]] :and[subfilter<subfilter>]" variable="varname">
<li><<transclusion>></li>
<$list filter="[subfilter<_f.filterString>] +[filter<subfilter>]" variable="varname">
<li>
<strong><code><$text text=<<varname>>/></code></strong>:<br/>
<code><$text text=<<_f.formattedVar>>/></code><br/>
<% if [<_f.formattedVar>prefix[\function]] %>
<$codeblock code={{{ [<varname>getvariable[value]] }}}/>
<% endif %>
<$codeblock code={{{ [<varname>getvariable[]] }}}/>
</li>
</$list>
</ul>
\end
<!-- =================== -->
\procedure search-variables(subfilter:"[variables[]]")
\procedure search() $:/temp/search/dump
\procedure sub-search() $:/temp/search/sub-dump
@ -28,7 +37,7 @@ tags: $:/tags/Macro
\procedure toggleInfoState()
<$action-setfield $tiddler=<<getToggleInfoState>> text={{{ [<getToggleInfoState>get[text]] +[toggle[yes],[no]] }}}/>
<$action-setfield $tiddler=<<getSubSearch>> text={{{ [<getSubSearchText>!is[blank]then<getSubSearchText>] :else[<varname>] }}} />
<$action-setfield $tiddler=<<getSubSearch>> text={{{ [<getSubSearchText>!is[blank]then<getSubSearchText>] :else[<varname>format:variable[$type$ $name$$params$]] }}} />
\end toggleInfoState
\procedure clearStatesButton()
@ -48,7 +57,7 @@ tags: $:/tags/Macro
\procedure expandAllStatesButton()
<$button class="tc-btn-invisible tc-tiny-gap-left">
<$action-setfield $tiddler=<<state>> text={{{ [<getToggleInfoState>get[text]] +[toggle[yes],[no]] }}}/>
<$list filter="[variables[]] +[search::some<getSearchText>] :filter[subfilter<subfilter>]" variable="varname">
<$list filter="[subfilter<_f.filterString>] +[search::some<getSearchText>] +[filter<subfilter>]" variable="varname">
<$action-setfield $tiddler=<<getVarState>> text="yes"/>
</$list>
{{$:/core/images/unfold-all-button}}
@ -58,11 +67,13 @@ tags: $:/tags/Macro
\procedure info()
<div class="multi-columns">
<$list filter="[all[tiddlers+shadows]] :filter[search:text,tags:words<getSubSearchText>]
-[[$:/config/OriginalTiddlerPaths]]
-[[$:/HistoryList]]
-[[$:/StoryList]]
-[[$:/core]]
:filter[!type[application/javascript]]
:filter[!prefix[$:/temp]]
:filter[!prefix[$:/state]]
-[[$:/config/OriginalTiddlerPaths]]
:filter[!type[application/javascript]]
-[[$:/core]]
:filter[!plugin-type[plugin]]"
>
<$link class="tc-small-gap-left"/><br>
@ -78,7 +89,11 @@ tags: $:/tags/Macro
\function getVarState() [<state>addsuffix<varname>] [<qualify>] +[join[]]
\function getToggleInfoState() [<state>addsuffix<varname>]
<$text text="Search Variables - "/><%if [<thisTiddler>!match[$:/AdvancedSearch]]%><code class="tc-small-gap-right"><<subfilter>></code><%endif%>
<!-- =================== -->
\procedure search-variables(type sort:"sort" subfilter:"[[]]" format)
\function _f.varFormat() [<format>!is[blank]then<format>else[$type$ $name$($params$) $firstLine$]]
<$text text="Search Variables - "/><%if [<thisTiddler>!match[$:/AdvancedSearch]]%><code class="tc-small-gap-right">sort: <<sort>>, type:<<type>></code><%endif%>
<$edit-text tiddler=<<search>> tag=input/> <<clearSearchButton>>
<% if [<state>get[text]match[yes]] %>
<<clearStatesButton>>
@ -86,8 +101,8 @@ tags: $:/tags/Macro
<<expandAllStatesButton>>
<% endif %>
<!-- <$list filter="[variables[]] +[search::some<getSearchText>] :filter[subfilter<subfilter>]" variable="varname"> -->
<$list filter="[variables[]] +[search::some<getSearchText>] :and[subfilter<subfilter>]" variable="varname">
<!-- <$list filter="[subfilter<subfilter>] +[search::some<getSearchText>]" variable="varname"> -->
<$list filter="[subfilter<_f.filterString>] +[search::some<getSearchText>] +[filter<subfilter>]" variable="varname">
<div class="tc-var-item">
<$button actions=<<toggleState>> class="tc-small-gap-left tc-btn-invisible">
<% if [<getVarState>get[text]match[yes]] %>
@ -95,8 +110,8 @@ tags: $:/tags/Macro
<% else %>
{{$:/core/images/right-arrow}}
<% endif %>
<strong class="tc-tiny-gap-right"><code><$text text=<<varname>>/></code></strong>
<$text text={{{ "#" [<varname>getvariable[]length[]] +[join[ ]] }}}/>
<code class="tc-tiny-gap-right"><$text text=<<_f.formattedVar>>/></code>
<!-- <$text text={{{ "#" [<varname>getvariable[]length[]] +[join[ ]] }}}/> -->
</$button>
<% if [<getVarState>get[text]match[yes]] %>
@ -114,6 +129,9 @@ tags: $:/tags/Macro
</div>
<<info>>
<% endif %>
<% if [<varname>prefix[\function]] %>
<$codeblock code={{{ [<varname>getvariable[value]] }}}/>
<% endif %>
<$codeblock code={{{ [<varname>getvariable[]] }}}/>
<% endif %>
</div>

View File

@ -0,0 +1,20 @@
created: 20240219185725834
modified: 20240221012350449
tags: Features
title: VariableFormat
type: text/vnd.tiddlywiki
<<.from-version "5.3.4">>
Finding "self explaining" variable names for functions, procedures, widgets and macros is difficult. So if variables are listed with <<.mlink dumpvariables>>, <<.mlink search-variables>> or from the "Variables" tab in the $:/AdvancedSearch, it's not obvious ''how'' they work and how they are defined.
The <<.olink variables>> operator returns all "visible" variables from the current context, depending on where the filter run is executed. The <<.olink getvariable>> allows us to return the "text" or the "value" of variables. But this information is not enough to get a good understanding of existing variables.
So using <<.olink getvariables>> operator in combination with the <<.olink format>> operator we are able to improve information in variable listings.
|Token |Substituted Value |h
|`$type$` |variabe type eg: `\function`, `\procedure` and so on |
|`$name$` |returns the name of the variable |
|`$params$` |returns a list of parameters, which are defined with functions, procedures ... |
|`$firstLine$` |returns the first line of the variable content-type as used by <<.olink getvariables>> |
|`$varType$` |returns `var`, `fn`, ` proc`, ` macro` or ` widget` which can be used with the <<.olink variables>> operator |

View File

@ -1,7 +1,13 @@
created: 20190330100101453
modified: 20190330100101453
modified: 20240219184535837
tags: [[variables Operator]] [[Operator Examples]]
title: variables Operator (Examples)
type: text/vnd.tiddlywiki
<<.operator-example 1 "[variables[]prefix[colour]]" "returns the names of any variables whose names start with ''colour''">>
<<.operator-example 2 "[variables[fn]]" "returns the names of any variables which are defined as `\function`s">>
<<.operator-example 3 "[variables[proc]]" "returns the names of any variables which are defined as `\procedure`s">>
<<.tip """Also see the filter operator <<.olink format>> to find out about variable "title" formatting""">>

View File

@ -1,6 +1,6 @@
caption: format
created: 20201020100834443
modified: 20220611104737314
modified: 20240219201201466
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
@ -22,6 +22,7 @@ The suffix <<.place B>> is one of the following supported string formats:
|^`relativedate` |The input string is interpreted as a UTC date and displayed as the interval from the present instant. Any operator parameters are ignored |
|^`timestamp` |<<.from-version "5.3.0">> The input string is interpreted as number of milliseconds since the [[ECMAScript epoch|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps]], 1 January 1970, and displayed according to the DateFormat specified in the optional operator operand. (Defaults to "[UTC]YYYY0MM0DD0hh0mm0ss0XXX") |
|^`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]]. |
|^`variable` |<<.from-version "5.3.4">> The input string is interpreted as a variable name, which can be formatted using the VariableFormat. Defaults to `$type$ $name$($params$) $firstLine$`|
Invalid input strings are dropped by the <<.op format>> operator.

View File

@ -1,12 +1,13 @@
caption: getvariable
created: 20190330100101453
modified: 20190330100101453
modified: 20240219183835439
op-input: a selection of variable names
op-output: the values of each of the variables named in the input titles (or blank if the variable is not defined)
op-parameter: <<.from-version "5.3.4">> defaults to: "text". optional: "value". Content type <<.param T>> of the varible content that should be returned
op-purpose: select all values of variables named in the input titles
tags: [[Filter Operators]] [[Special Operators]]
title: getvariable Operator
caption: getvariable
op-purpose: select all values of variables named in the input titles
op-input: a selection of variable names
op-parameter: ignored
op-output: the values of each of the variables named in the input titles (or blank if the variable is not defined)
type: text/vnd.tiddlywiki
<<.from-version "5.1.20">> The usual way to retrieve a variable value within a filter is with the angle brackets notation. For example, `[<currentTiddler>]` will retrieve the value of the variable called `currentTiddler`.
@ -14,4 +15,13 @@ The `getvariable` filter operator provides an alternative way to retrieve a vari
The advantage of `getvariable` is that it makes it possible to work with variables whose name is computed, and not known in advance. For example, `[<myvariable>getvariable[]]` gets the value of the variable whose name is given in the variable `myvariable`.
''Content Type:'' <<.from-version "5.3.4">>
The Prameter <<.param T>> by default is "text". Variables, that are defined as eg: `\function`s have two useful content types:
* ''text'' ... contains the wikified text of the filter expression
* ''value'' ... contains the definition string
For any other variable type "text" and "value" are the same thing.
<<.operator-examples "getvariable">>

View File

@ -1,16 +1,37 @@
caption: variables
created: 20190330100101453
modified: 20190330100101453
modified: 20240219185349859
op-input: ignored
op-output: the names of all the actively defined variables
op-parameter: <<.from-version "5.3.4">> whitespace separated list of variable-types
op-parameter-name: T
op-purpose: select the names of all the actively defined variables
op-suffix: <<.from-version "5.3.4">> deafults to: "sort", optional: "raw", see more details below
op-suffix-name: S
tags: [[Filter Operators]] [[Special Operators]] [[Selection Constructors]]
title: variables Operator
type: text/vnd.tiddlywiki
caption: variables
op-purpose: select the names of all the actively defined variables
op-input: ignored
op-parameter: none
op-output: the names of all the actively defined variables
<<.from-version "5.1.20">> The primary purpose of the `variables` operator is to implement the [[dumpvariables Macro]]:
<<.from-version "5.1.20">> The primary purpose of the `variables` operator is to implement the <<.mlink dumpvariables>> or the <<.mlink serach-variables>> macros.
`[variables:<sort>[<parameters>]]`
''Possible Suffixes: <<.place S>>:'' <<.from-version "5.3.4">>
* ''sort'' (default): By default the returned list is alphabetically sorted
* ''raw'': The variable list will be returned as created. The variables "near" the filter run will show up first. So if variables are defined in the tiddler, where the <<.mlink dumpvariables>> or <<.mlink search-variables>> macros are executed, they will be listed first.
''Parameters <<.place T>>:'' <<.from-version "5.3.4">>
The parameters <<.place T>> allow a "whitespace separated" list of variable codes that should be listed
* Default is all: `fn, var, proc, macro, widget`
* ''fn'' ... will only show variables defined as functions eg: `\function test-01()`
* ''var'' ... will only show standard variables defined with eg: `<$let test="test">...</$let>`
* ''proc'' ... will only show variables defined as procedures eg: `\procedure test-02()`
* ''macro'' ... will only show variables defined as macros eg: `\define test-macro()`
* ''widgets'' ... will only show variables defined as widgets eg: `\widget my-widget()`
<$codeblock code={{$:/core/macros/dumpvariables}}/>
<<.operator-examples "variables">>

View File

@ -1,6 +1,6 @@
caption: dumpvariables
created: 20140908104107181
modified: 20240216150310916
modified: 20240221005326328
tags: Macros [[Core Macros]]
title: dumpvariables Macro
type: text/vnd.tiddlywiki
@ -13,7 +13,16 @@ Placeholders are replaced with values in the normal way, but using the default v
!! Parameters
; type
: If type is empty all variable types are shown: `fn, var, proc, macro, widget`. Using the type parameter will reduce the list.
; raw
: Is used to allow the <<.olink variables>> to return a raw list. By default the list is alphabetically sorted.
; subfilter
: This parameter limits the number of variables that are listed
: This parameter limits the number of variables that are listed eg: `subfilter:"[search::some[sort .attr]]" `
; format
: Format the output string using the VariableFormat. Defaults to `$type$ $name$($params$) $firstLine$`.
!!! <<.macro-examples "dumpvariables">>

View File

@ -1,20 +1,25 @@
created: 20150221151358000
modified: 20240216150546249
modified: 20240220015920165
tags: [[dumpvariables Macro]] [[Macro Examples]]
title: dumpvariables Macro (Examples)
type: text/vnd.tiddlywiki
<$macrocall $name=".example" n="1"
eg="""<$let EXAMPLE="123" >
<<dumpvariables subfilter:"EXAMPLE dumpvariables">>
<<dumpvariables subfilter:"[search::some[EXAMPLE dumpvariables]]">>
</$let>"""/>
<$macrocall $name=".example" n="2"
eg="""<<dumpvariables subfilter:"[prefix[.a]]">>"""/>
The following list will be very long. It shows all variables known within this tiddler. Using the macro in a different context. Eg: the sidebar, variables will have different values.
Listing only specific varibles can be achieved using the <<.olink search>> operator as a subfilter
<$macrocall $name=".example" n="3"
eg="""<<dumpvariables sort:"raw" subfilter:"[search::some[sort .attr]]">>"""/>
The following list will be very long. It shows all variables known within this tiddler. Using the macro in a different context. Eg: the sidebar, variables will have different values.
<$macrocall $name=".example" n="4"
eg="""<<dumpvariables>>"""/>