[TW5] pass text field to macro

869 views
Skip to first unread message

Andrew

unread,
Mar 9, 2016, 6:00:52 PM3/9/16
to TiddlyWiki
Hello everyone,
    Using the same button format as Jeremy's new editor toolbar in the 5.1.12-prerelease, I'm trying to make a reduce button plugin that I can add to the toolbar when it comes out.
Here is what I have so far but since I really am not a programmer, all I get is error messages so it is obvious I don't know what I am doing. Any suggestions? Here is what I have so far:
In the button:
<$set name="mytext" value={{!!text}}>
<$button class="tc-btn-invisible" tooltip={{$:/language/Buttons/Reduce/Hint}} aria-label={{$:/language/Buttons/Reduce/Caption}}>
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="replace-all"
	text=<<reduce "$(mytext)$">>
/>
{{$:/plugin/ajh/reduce/image}}
</$button>
</$set>
 In the Macro:
/*\
$:/plugin/ajh/reduce/macro.js
type: application/javascript
module-type: macro

<<reduce text>>

Examples:
<<reduce>>
<<reduce "one two three">>
<$macrocall $name="reduce" text={{!!text}}/>

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
This is a macro to compress text into a blob for use with making external files seem searchable
*/

exports.name = "reduce";

exports.params = [
	{ name: "text" }
];

/*
Run the macro
*/
exports.run = function(text) {
	if(!text)
		text = "mytext";
    var zwrds = text.toLowerCase().replace(	/\W/g," ");
    var ztrc = zwrds.split(" ").sort().reverse();
    var zntrc = " ";
    var add = "";
    for (var i = 0; i < ztrc.length; i++) {
        if (zntrc.indexOf(ztrc[i]) < 1) {
            add = ztrc[i].toString();
            zntrc += add;
        }
    }
    var format= zntrc.slice(1);
	return reduce; 
};

})();

Mark S.

unread,
Mar 9, 2016, 7:37:17 PM3/9/16
to TiddlyWiki
Being a programmer won't help you much with macro invocation, which can leave just about anyone beating their head against a wall.

I'm pretty sure that this bit won't work:

text=<<reduce "$(mytext)$">>

At least it's never worked for me to invoke a macro with a parameter inside a widget. If there's magic to make this happen, I haven't found it.

But since you're rolling your own JS, you have another option. You can retrieve the variable inside your JS macro with something like this:

 if(!text)
	text = this.getVariable("mytext") ;

Then your invocation can just be like this:

... text=<<reduce>> ...

I don't know if your JS macro works since I didn't run it, but if you start getting those red alert boxes then that's a clue there might be something wrong.

HTH
Mark

Mat

unread,
Mar 10, 2016, 6:11:11 AM3/10/16
to TiddlyWiki
I'm pretty sure that this bit won't work:

text=<<reduce "$(mytext)$">>


I just recently did a lot of experimenting and (1) assuming that the above in deed is the problem, and (2) that I don't misunderstand the issue then FWIW here's a setup that I do get to work. The idea is to externalize the invocation with a $set and then, inside the widget, call the "name" of the set variable instead. Please tell me if I'm missing the point/issue when suggesting this.

Example:

title:Foo
test
: (blank)

\define foo() This is $(arg)$

<$set name="arg" value="myarg">
<$set name="tmp" value=<<foo>>>
<$button>
   
<$action-setfield
              $tiddler
="Foo"
              test
=<<tmp>>
   
/>
Click
</
$button>
</$set>
</
$set>

test
= {{!!test}}


<:-)

andrew harrison

unread,
Mar 11, 2016, 1:57:33 AM3/11/16
to tiddl...@googlegroups.com
Regrettably I still can't seem to accomplish what I want:
I got the following to pass the current tiddler title to the macro:
<$vars arg=<<currentTiddler>> tmp=<<reduce title:$(arg)$>>>
<$button class="tc-btn-invisible">
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="replace-all"
	text=<<tmp>>
/>
{{$:/plugin/ajh/reduce/image}}
</$button>
</$vars>
but I think what I want is to either pass the current text field to the macro or somehow get the macro to pull the text field information before manipulating and returning the reduced text. This seems impossible. Maybe I'm looking at this all wrong. I wish there was a <<currentText>> or something. All I want to do is the following:
    var text = $tw.wiki.getTiddlerText(title);
    var zwrds = text.toLowerCase().replace(	/\W/g," ");
    var ztrc = zwrds.split(" ").sort().reverse();
    var zntrc = " ";
    var add = "";
    for (var i = 0; i < ztrc.length; i++) {
        if (zntrc.indexOf(ztrc[i]) < 1) {
            add = ztrc[i].toString();
            zntrc += add;
        }
    }
    var t= zntrc.slice(1);
    return t;

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/f1743db2-8d22-494a-950c-e91bf062fd56%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mark S.

unread,
Mar 11, 2016, 10:24:11 AM3/11/16
to TiddlyWiki
Did you try my approach? I see you're still trying to pass a macro with parameters inside a widget.  I've never gotten that to work.

What is the code trying to do? I think it's making a unique list of words as an index, but I'm not sure. Do you know for sure that that part of the code works?

Good luck,

Mark

Mark S.

unread,
Mar 11, 2016, 6:41:33 PM3/11/16
to TiddlyWiki
Ok, this seems to work. In my example, the contents of a given tiddler (MyReductionText) are replaced with their reduced subset when the button is pushed.

 First the regular macro stuff:

\define tmp2()
<<reduce """$(mytext)$""">>
\end


<$set name="mytext" value={{MyReductionText!!text}}>
<<tmp2>>
<$button>
<$action-setfield
    $tiddler
="MyReductionText"
    text
=<<tmp2>>   />
<$action-navigate $to="MyReductionText"/
>
Modify and Go to Reduction Text
</$button>
</
$set>

And then the javascript macro. Remember that the macro has to have content-type set to applicaton/javascript with an additional field "module-type" with value "macro".

/*\
title: $:/Macro/reduce.js
type: application/javascript
module-type: macro
This is a macro finds and returns all unique words in a given string of text

\*/

(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";


exports
.name = "reduce";

exports
.params = [    { name: "text" } ];

/*
Run the macro
*/


exports
.run = function(text) {

   
if(!text)        text = "mytext";



   
var araWords = text.toLowerCase().replace(    /\W/g," ").split(" ") ;
   
var mapWords = {} ;    
   
var araReduced = [];
   
    araWords
.forEach(function (x) {
       
if (!mapWords[x]) {
          araReduced
.push(x);
          mapWords
[x] = true;
       
}
   
});


/*
 
*/


 
return araReduced.sort().reverse().join(" ") ;

};

})();



andrew harrison

unread,
Mar 12, 2016, 11:29:14 PM3/12/16
to tiddl...@googlegroups.com
Hi Mark,
    Thanks for trying. When I put your suggested in what I get out is a seperate tiddler as follows:



--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.

andrew harrison

unread,
Mar 12, 2016, 11:44:24 PM3/12/16
to tiddl...@googlegroups.com
How come the following doesn't work even though I'm successfully passing the tiddler title to the macro? What am I missing?
/*\
title: $:/plugin/ajh/reduce/module.js
type: application/javascript
module-type: macro

Setup the root widget handler that can be used to compress text into a blob for use with making external files seem searchable.

\*/
(function () {

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.name = "reduce";

exports.params = [
	{name: "title"}
];

/*
Run the macro
*/
exports.run = function(title) {
    var text = $tw.wiki.getTiddlerText(title);
	if(!text) {
		return title;
	}
    var zwrds = text.toLowerCase().replace(	/\W/g," ");
    var ztrc = zwrds.split(" ").sort().reverse();
    var zntrc = " ";
    var add = "";
    for (var i = 0; i < ztrc.length; i++) {
        if (zntrc.indexOf(ztrc[i]) < 1) {
            add = ztrc[i].toString();
            zntrc += add;
        }
    }
    var t= zntrc.slice(1);
    return t;
};

})();

andrew harrison

unread,
Mar 13, 2016, 12:04:20 AM3/13/16
to tiddl...@googlegroups.com
Well, just to let everyone know, I finally succeeded with the following macro. Please be kind because I'm not a programmer by trade. Turns out I don't have to pass the title or the text to the macro. Now the next question, is does anyone know if there is a better way of doing this? I mean I have this feeling that I reinvented the wheel and I don't even know if it will still work after 5.1.12 is released.
title: $:/plugin/ajh/reduce/macro.js
type: application/javascript
module-type: macro

Reduce text into a blob removing duplicate words and spaces.

\*/
(function () {

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.name = "reduce";

exports.params = [];

/*
Run the macro
*/
exports.run = function() {
    this.editTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
    var text = $tw.wiki.getTiddlerText(this.editTitle);
    var zwrds = text.toLowerCase().replace(	/\W/g," ");
    var ztrc = zwrds.split(" ").sort().reverse();
    var zntrc = " ";
    var add = "";
    for (var i = 0; i < ztrc.length; i++) {
        if (zntrc.indexOf(ztrc[i]) < 1) {
            add = ztrc[i].toString();
            zntrc += add;
        }
    }
    var t= zntrc.slice(1);
    return t;
};

})();
Reply all
Reply to author
Forward
0 new messages