Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Questions and Answers >> Evaluating export code inside ^action()^
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1460933859

Message started by Scott Heftler on Apr 17th, 2016, 6:57pm

Title: Evaluating export code inside ^action()^
Post by Scott Heftler on Apr 17th, 2016, 6:57pm

Hello. I need to construct a string in a loop inside macro. I am using ^action()^ to do this.  The problem is, I need to embed ^root^ (evaluated) into the string.  When the string is constructed, I print it to the HTML file using ^value($MyString)^.  As you can guess, the ^root^ is exported literally as "^root^".

For example:

MediaThumbs =
^action($tmpStr1="$2".replace("(<p>|</p>)", "").replace("\`\r","");$tmpLst1=$tmpStr1.split("\r");$tmpStr3="";$tmpLst1.each(X){$tmpLst2= X.split("\t+");$tmpStr1=$tmpLst2.at(0);$tmpStr2=$tmpLst2.at(1);$tmpStr3=$tmpStr3 + '<div class=\"$1\"><div class=\"thumbnail\"><a href=\"' + "^root^img/" + $tmpStr1 + '\"><img src=\"' + "^root^img/" + $tmpStr1 + '\" alt=\"' + $tmpStr2 + '\" title=\"' + $tmpStr2 + '\"></a></div><div class=\"caption\"><p>' + $tmpStr2 + '</p></div></div>'})^
<div class="row gallery">
^value($tmpStr3)^
</div>


(My workaround has been to replace the "^root^img/" bits with "../../../img" for now since I can't get ^root^ to evaluate.)

When you call it like this, say:

^do(MediaThumbs, "col-xs-6 col-md-3", "
sf_Craft.png     The Craft segment
sf_Health.png    The Health segment
sf_Magic.png     The Magic segment
sf_Safety.png    The Safety segment")^


You get this:

<div class="row gallery">
     <div class="col-xs-6 col-md-3">
     <div class="thumbnail"><a href="^root^img/sf_Craft.png"><img src="^root^img/sf_Craft.png" alt="The Craft segment" title="The Craft segment"></a></div>
     <div class="caption"><p>The Craft segment</p></div>
     </div>
     <div class="col-xs-6 col-md-3">
     <div class="thumbnail"><a href="^root^img/sf_Health.png"><img src="^root^img/sf_Health.png" alt="The Health segment" title="The Health segment"></a></div>
     <div class="caption"><p>The Health segment</p></div>
     </div>
     <div class="col-xs-6 col-md-3">
     <div class="thumbnail"><a href="^root^img/sf_Magic.png"><img src="^root^img/sf_Magic.png" alt="The Magic segment" title="The Magic segment"></a></div>
     <div class="caption"><p>The Magic segment</p></div>
     </div>
     <div class="col-xs-6 col-md-3">
     <div class="thumbnail"><a href="^root^img/sf_Safety.png"><img src="^root^img/sf_Safety.png" alt="The Safety segment" title="The Safety segment"></a></div>
     <div class="caption"><p>The Safety segment</p></div
     </div>
</div>


Because it's a loop inside of ^action()^, I can't very well divide it into literal and evaluated parts and print it as

^value($FirstPart)^^root^^value($SecondPart)^

or I would have.  

Put simply, I need to know how to do something like this:

^action($tmpStr1=eval(^root^))^

Any ideas?

Title: Re: Evaluating export code inside ^action()^
Post by Mark Anderson on Apr 18th, 2016, 4:17am

Have you tried moving ^root^ out of a literal string, e.g.

…<img src=\"' + ^root^ + "img/" + $tmpStr1…

Note how the export code is moved out of a quoted literal string. In principle using export code in action code is deprecated - but supported so existing files don't break. Here might be a good exception to the rule!

Note: there a lot of scaffolding to make a test file for this question and I'm too busy right now to test the suggestions given here.

Other possible routes…

Store the value of ^root^ in an action code variable and use the latter in the loop. I'm not sure if you can do this - but give it a try.

Use a macro . These can be invoked via action  or export code. Your HTML, per thumbnail, is mostly boilerplate with just two variables which can be passed as the macro's inputs.  Incidentally, unless for personal use, it is considered kind (in in some contexts legally required) to include an  image 'alt' tag. The latter can re-use the items caption value.

If still stuck, is there any chance you could post a link to a small doc with the scaffolding in place to assist testing (and ensure a consistent point of reference)?  :)

Title: Re: Evaluating export code inside ^action()^
Post by Scott Heftler on Jun 4th, 2016, 2:20pm

Hello.  I fixed the previous problem with the most ridiculous workaround ever.  I used the $Path of the current note to finagle the proper number of parent directories and then injected this into the export (the workaround is in the first line):

^action($tmpStr4=$Path(current).replace("/[^/]+", "../").replace("\`\.\./", "");
$tmpStr1="$2".replace("(<p>|</p>)", "").replace("\`\r","");
$tmpLst1=$tmpStr1.split("\n");
$tmpNum1=count($tmpLst1);
$tmpStr3="";
$tmpLst1.each(X){$tmpLst2= X.split("\t+");
$tmpStr1=$tmpLst2.at(0);
$tmpStr2=$tmpLst2.at(1);
$tmpStr3=$tmpStr3 + '<div class=\"$1\"><div class=\"thumbnail\"><a href=\"' + $tmpStr4 + 'img/' + $tmpStr1 + '\"><img src=\"' + $tmpStr4 + 'img/' + $tmpStr1 + '\" alt=\"' + $tmpStr2 + '\" title=\"' + $tmpStr2 + '\"></a></div><div class=\"caption\"><p>' + $tmpStr2 + '</p></div></div>'})^
<div class="row gallery">
^value($tmpStr3)^
</div>


Now I have a problem that CANNOT be finagled.  I need to get the relative URL of a note minus the filename.

For example,  if the relative URL is ../a/b/c.html, I just want ../a/b/.  The problem is getting the relative URL into a variable.  I can print the URL to export using ^url(NoteName)^, but I cannot get this string into a variable for manipulation.  This does not work:

^action($tmpStr1=^url(NoteName)^)^

Is there another way?  What's the general procedure for manipulating strings that are only accessible through action code?

Title: Re: Evaluating export code inside ^action()^
Post by Mark Anderson on Jun 4th, 2016, 3:43pm

Not tested, but do() is effectively an action wrapper for ^do()^ and as such allows export code [sic] inside the macro. ISTM that ^action(do(macro))^ might work.

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.