Amazing wizardry with
format(), a
do() and
.replace()!
I've attached a
demo tbx showing a much less elegant and more verbose (but, for me, easier to replicate and in some ways possibly more flexible, e.g. can get a line break where needed) "loop" method, similar to that described
here.
Three code notes do the work:
'cLoopInit' sets things up and uses MarkA's nifty method to exclude certain key attributes with
replace().
Code:$MyTempString=;$MyTempList=;$LoopCtr=0;
$MyExportSet=$KeyAttributes.replace("citeinessay|citetype|nickname","");
$LoopSize=$MyExportSet.size;
action($Text("cLoop"));
'cLoop' uses the
.at() operator with the loop counter attribute to cycle through the items in list of key attributes. For each key attribute that has a value in that note (the
if(eval()) is testing to make sure it is not empty) a temporary string is populated with the attribute name and attribute value. Then this temporary string is added to a temporary list.
Code:if($LoopCtr<$LoopSize) {
$CurrListItem=$MyExportSet.at($LoopCtr);
if(eval('$'+$CurrListItem)){$MyTempString=$CurrListItem+"={"+eval('$'+$CurrListItem)+"}"}else{$MyTempString=;};
$MyTempList=$MyTempList+$MyTempString;
$LoopCtr=$LoopCtr+1;
action($Text("cLoop"));
}
else {
action($Text("cMakeReport"))
}
'cMakeReport' formats the temporary list with line breaks, adds some stuff before and after that I understand BibTex needs, replaces some values, and places the result in $Text, from where it can be viewed and exported. The effects can be seen dynamically by opening the 'test-reference' and inserting/changing values for the key attributes. (The text updates immediately if one File/Saves without closing the note.)
Code:$Text="@"+$citetype+"{"+$nickname+",
";
$Text=$Text+$MyTempList.format(",\n");
$Text=$Text.replace("URL","howpublished");
$Text=$Text+"
}";
In practice one would probably want to add a "guard attribute" with conditional or some other mechanism so that the loop doesn't continually run. Here, I've got a stamp that can turn off the rule.
Some complications/questions:
1) Should TB always evaluate 'year' and 'month' as -1 and 6? (see the
eval() in 'cLoop') That's what I got for the values when I had the attribute names as 'year' and 'month' even when no values had been entered for those two attributes in the current note. I got around the problem by renaming the attributes 'byear' and 'bmonth' but then had to add the
.replace() to 'cMakeReport' to change the names to what (presumably...I haven't used it) BibTex expects. (Am thinking problem may be because there should be an escaped $ within the second
eval() but not sure how to do that).
Edited: Adding '$'+ within eval() seems to work. Changed attribute names back to $year and $month.
2) If had attributes 'byear' and 'bmonth' could these be replaced by 'year' and 'month' in one replace()? I see aTbRef mentions multiple
back-references with
.replace() but don't know how that might work in this case.
As always, interested in suggestions/critique.