Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Evaluating export code inside ^action()^ (Read 816 times)
Scott Heftler
Full Member
*
Offline



Posts: 33

Evaluating export code inside ^action()^
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?
Back to top
 
« Last Edit: Apr 18th, 2016, 1:44am by Scott Heftler »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Evaluating export code inside ^action()^
Reply #1 - 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)?  Smiley
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Scott Heftler
Full Member
*
Offline



Posts: 33

Re: Evaluating export code inside ^action()^
Reply #2 - 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?
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Evaluating export code inside ^action()^
Reply #3 - 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.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print