[TW5] Need help with TW5-script

132 views
Skip to first unread message

Mark S.

unread,
Mar 25, 2016, 12:08:31 PM3/25/16
to TiddlyWiki
I wrote a javascript macro for converting html into TW5 markup. It works fine when I run it on a tiddler with a known name. But when I tried to launch it it from a form, it never gets the text -- even though the macro itself sitting on it's own line will run perfectly.

In the form, I collect the name of the tiddler containing the HTML to be converted. The name is stored in a system tiddler. The various macros are meant to fetch the contents of the named tiddler. The variable "text2convert" contains the original text. The ht2ml2tw expects to see variable "text2convert". 

I don't understand why adding one level of abstraction should break everything.

Thanks for any help!
Mark

This is the launcher code which doesn't work:

\define convertto() $(name_of_tiddler)$-converted
\define textin(tname) {{$tname$}}
\define textin2() <$macrocall $name="textin" tname="""$(name_of_tiddler)$"""/>

<$edit-text tiddler="$:/html2tw/title" rows="1" size="50" placeholder="Name of tiddler to convert"  tag="input"   ></$edit-text>

<$set name="name_of_tiddler"  value={{$:/html2tw/title}}>
<$set name="text2convert"  value=<<textin2>>  >
<$button>
<$action-setfield
    $tiddler
=<<convertto>>
    text
=<<html2tw>>   />
<$action-navigate $to=<<convertto>>/
>
Convert Tiddler Text
</$button>

<p/
>Using text from ''<<name_of_tiddler>>'' and sending to ''<<convertto>>''.

The following macro displays fine here:
<<html2tw>>

</$set>
</
$set>


This is the earlier code, with hard-wired tiddler names that does work. :

<$set name="text2convert"  value={{HTML2TW: Incoming HTML!!text}}>

<$button>
<$action-setfield
    $tiddler="HTML2TW: Converted Text"
    text=<
<html2tw>>   />
Convert Tiddler Text via action-setfield
</$button>
</$set>


Jed Carty

unread,
Mar 25, 2016, 3:12:38 PM3/25/16
to TiddlyWiki
It is probably the <$set name="text2convert"  value=<<textin2>>  > line that does it.

Why don't you just use one macro instead of creating a macro that just calls a second macro? Make textin:

\define textin() {{$(name_of_tiddler)$}}

and use that as the value for text2convert instead of textin2

Mark S.

unread,
Mar 25, 2016, 4:14:56 PM3/25/16
to TiddlyWiki
Hi Jed,

I've tried this in all sorts of iterations. I was hoping that wrapping a macro in a macro could finally force it to acknowledge the actual text in the dereferenced name tiddler.

Your variation put this into the "converted" tiddler:

{{TitleIUsedInTheForm}}

So the literal string with brackets is getting passed rather than the transcluded text. It seems like it ought to be easy.

Thanks for trying,
Mark

BJ

unread,
Mar 25, 2016, 4:55:17 PM3/25/16
to tiddl...@googlegroups.com
Hi mark,
this is a typical tw5 'gotcha'

<
$set name="text2convert"  value=<<textin2>>  >

the macro only does basic text substitution.

You need to use this pattern

<$set name="text2convert"  value={{$x$}}  >

and so must put your code inside a macro



\define amacro(x)
<$set name="name_of_tiddler"  value=$x$>
<$set name="text2convert"  value={{$x$}}  >

<$button>
<$action-setfield
    $tiddler
=<<convertto>>
    text
=<<html2tw>>   />
<$action-navigate $to=<<convertto>>/
>
Convert Tiddler Text
<
/$button>
\end


<$macrocall $name="amacro" tname=
{{$:/html2tw/title}}/>

all the best

BJ

Mark S.

unread,
Mar 25, 2016, 5:53:48 PM3/25/16
to TiddlyWiki
Thanks BJ. I had great hopes for this approach. But in this case, <<text2convert>> is empty and doesn't even show up when dropped in ... as shown below.

I had to tweak your suggested code to match up parameter and argument names. I put the label THIS IS EMPTY to show where there's no contents at all being retrieved. Here's what it looks like:

\define convertto() $(name_of_tiddler)$-converted
\define amacro(x)
<$set name="name_of_tiddler"  value=$x$>
<$set name="text2convert"  value={{$x$}}  >

<$button>
<$action-setfield
    $tiddler
=<<convertto>>
    text
=<<html2tw>>   />
<$action-navigate $to=<<convertto>>/
>
Convert Tiddler Text
<
/$button>
<p/
>Using text from ''<<name_of_tiddler>>'' and sending to ''<<convertto>>''.

THIS IS EMPTY
: <<text2convert>>
</$set>
</
$set>

\end


<$edit-text tiddler="$:/html2tw/title" rows="1" size="50" placeholder="Name of tiddler to convert"  tag="input"   ></$edit-text>

<$macrocall $name="amacro" x="""{{$:/html2tw/title}}"""/>


Maybe some other tweak?

Thanks,
Mark

Mat

unread,
Mar 25, 2016, 6:19:51 PM3/25/16
to tiddl...@googlegroups.com
Could be more problems but one is it should probably read:

<$set name="name_of_tiddler" value="$x$">

i.e with quotes around $x$. Maybe even """$x$""".


Also, just to make sure it is intentional: You first call call amacro with the content inside $:/html2tw/title and then transclude the "content of that content".



<:-)

Eric Shulman

unread,
Mar 25, 2016, 6:25:12 PM3/25/16
to TiddlyWiki
On Friday, March 25, 2016 at 2:53:48 PM UTC-7, Mark S. wrote:
<$macrocall $name="amacro" x="""{{$:/html2tw/title}}"""/>

Maybe some other tweak?

should be:

<$macrocall $name="amacro" x={{$:/html2tw/title}}/>

e.g., remove the tripled-quotes so that the parameter retrieves the value that was entered in the input field.

enjoy,
-e



Mark S.

unread,
Mar 25, 2016, 7:10:03 PM3/25/16
to TiddlyWiki
Hi Eric,

That seems to be the final tweak. I put the triple quotes in (not BJ) before I realized that the name/argument were mismatched. I fixed the mismatch, but should have removed the quotes.

I'm still very confused about all this.  If all that a macro does is substitution, how does it help to put everything inside of the macro? For instance, the line:

<$set name="text2convert"  value={{$x$}}  >



is inside of the macro now. If there is no interpolation/transclusion inside of the macro, why does value now get properly assigned when it wasn't being properly assigned when interpreted outside the macro? It feels like it should be the other way around.

Thanks Eric, Jed, and BJ!
Mark

Eric Shulman

unread,
Mar 25, 2016, 8:45:18 PM3/25/16
to TiddlyWiki
On Friday, March 25, 2016 at 4:10:03 PM UTC-7, Mark S. wrote:
Hi Eric,

That seems to be the final tweak. I put the triple quotes in (not BJ) before I realized that the name/argument were mismatched. I fixed the mismatch, but should have removed the quotes.

I'm still very confused about all this.  If all that a macro does is substitution, how does it help to put everything inside of the macro? For instance, the line:

<$set name="text2convert"  value={{$x$}}  >

The macro just assembles the code, but the parsing of the parameter value is done afterwards, when the fully expanded macro is *rendered* in the calling context.  Putting everything inside a macro definition allows you to use the $x$ and $(x)$ syntax for substitutions of params and variables.

-e

Knut Franke

unread,
Mar 29, 2016, 5:19:22 PM3/29/16
to TiddlyWiki
On Saturday, March 26, 2016 at 12:10:03 AM UTC+1, Mark S. wrote:
I'm still very confused about all this.  If all that a macro does is substitution, how does it help to put everything inside of the macro?

You're not alone. As Eric pointed out, you basically use macros to trigger some substitutions that you can't construct otherwise, which is somewhat counter-intuitive. There's a related thread over at TiddlyWikiDev.


Cheers,
Knut
Reply all
Reply to author
Forward
0 new messages