Welcome, Guest. Please Login or Register
Tinderbox
  News:
Visit the Tinderbox Cookbook for tons of examples.
  HomeHelpSearchLoginRegister  
 
Pages: 1 2 
Send Topic Print
Export BibTex using $KeyAttibutes (Read 13243 times)
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Export BibTex using $KeyAttibutes
Sep 07th, 2011, 9:02am
 
I am looking to export a BibTex file. I have created attributes for each field that will be made for each citation in the final BibTex exported file. Each of those attributes has been put into $KeyAttributes.

I then have created a BibTex citation template that so far looks like this.

Code:
@^value($citetype)^{^value($nickname)^,
^comment(if a attribtue's string in key attributes is not empty enter here)^
^if(^value($KeyAttributes.empty)^)^
^endif^
^value($KeyAttributes.format("= {^value($KeyAttributes.at(*))^},\n"))^
} 



This is the result I have got so far(with a test entry)

Quote:
@InCollection{sep-dualism,
<!-- if a attribtue's string in key attributes is not empty enter here -->

citeinessay= {^value($KeyAttributes.at(*))^},
citetype= {^value($KeyAttributes.at(*))^},
nickname= {^value($KeyAttributes.at(*))^},
author= {^value($KeyAttributes.at(*))^},
editor= {^value($KeyAttributes.at(*))^},
booktitle= {^value($KeyAttributes.at(*))^},
title= {^value($KeyAttributes.at(*))^},
edition= {^value($KeyAttributes.at(*))^},
series= {^value($KeyAttributes.at(*))^},
pages= {^value($KeyAttributes.at(*))^},
month= {^value($KeyAttributes.at(*))^},
year= {^value($KeyAttributes.at(*))^},
URL= {^value($KeyAttributes.at(*))^},
publisher= {^value($KeyAttributes.at(*))^},
address= {^value($KeyAttributes.at(*))^},
note
}


It is nearly there but I can't get the actual attribute values for the attributes listed, in $KeyAttributes. So I need to find a way to get the listed attribute values and have them in the right field. I also need to exclude the three $citeinessay, $citetype and $nickname attributes, as they are for internal Tinderbox use.

NB: The ^if^ statement is for not exporting attributes that have no data. I will get that done as soon as getting the attributes values in the template is done.
Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #1 - Sep 7th, 2011, 1:49pm
 
OK, add a String $MyExportString and a Set $MyExportSet. Add a macro "export-list", and give it this code - note deliberate line break before ^endIf^:

Code:
^if(\$$1)^$1= {^value(\$$1)^},
^endIf^ 


Now add a template 'list-export' with this code:

Code:
@^value($citetype)^{^value($nickname)^,
^comment(if an attribute's string in $KeyAttributes is not empty enter here)^
^if($KeyAttributes)^^value($MyExportString)^
^endif^note
}
 


In the exporting container/agent, open HTML view and select the new export template and untick 'Markup text' (or code-set $HTMLMarkupText to false). Add the $Rule (line breaks for clarity here only):

$MyExportSet=$KeyAttributes.replace("citeinessay|citetype|nickname","");
$MyExportString=eval($MyExportSet.format('','eval(do("export-list","','"))+','')
);


N.B. $MyExportSet and $MyExportString should not be key attributes in any exported notes.

Output:

@InCollection{sep-dualism,
<!-- if an attribute's string in $KeyAttributes is not empty enter here -->
MyStringA= {Hello mother dear},MyNum= {5},MyNumA= {25},MySet= {ant;frog;dog},
note
}


The only bit not achieved is the line breaks after each attribute's entry which seems to be a problem in the evaluation of do() or the resolution of ^if()^. Still, I hope this moves things forward. I'm off on the road for a few days so can't progress this further just now.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Re: Export BibTex using $KeyAttibutes
Reply #2 - Sep 13th, 2011, 8:11am
 
Hi mark

thank you so much, hope you are having/had a good trip.

I added as you requested but the result I get is this

Quote:
@{,
<!-- if a attribtue's string in key attributes is not empty enter here -->

}


Huh

Help! Smiley
Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #3 - Sep 13th, 2011, 8:30am
 
Can you post a link to your test file?  It looks like the note being exported has no key attributes. My post used code copied from a working test. Not sure I kept the latter so it would help to see your file.

In your last example, I notice the output implies the note used to create it has no $citetype or $nickname. Are you exporting the right note?
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Re: Export BibTex using $KeyAttibutes
Reply #4 - Sep 13th, 2011, 11:10am
 
Sure no problem.

The only thing I can think off is that the note used to test has $HTMLDontExport on but I have an action that set's it to false.

I changed the document default to true, for not getting hundreds of files exporting by pressing Command-Shift-H. This may be the issue but I did that long before I was thinking about BibTex.
Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #5 - Sep 13th, 2011, 1:13pm
 
Link says 'file no longer shared'.

BTW, you can preview the export by selecting just the export container and then Cmd+Opt+H.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Re: Export BibTex using $KeyAttibutes
Reply #6 - Sep 14th, 2011, 3:40am
 
Sorry about that here is the link http://files.me.com/sebey/mvnfyo
Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #7 - Sep 14th, 2011, 5:36am
 
Ah, I see (this is why having a reference file helps!). In your "References" agent, move the Rule's code to the 'Action" box, so it's applied to the agent's children (i.e. the matched reference notes).

Your 'BiBtex' export template is set, sensibly, in the 'Reference' prototype. so when you open 'test-reference' in HTML view you should see the data as I do. I don't have/use BibTeX so can't advise on the layout of the export file.

To do:
  • Getting the per-attribute data to have line breaks after each item has defeated me. However, it may not matter to BibTeX. Unless Eastgate can point out an angle I've missed, getting line breaks may require tweaks the TB app.
  • I see $note' is a key attribute but also possibly intended to be hard coded as the last entry in the export. So you may need to add 'note' to the list of deletions from $MyExportSet and then amend the export template accordingly.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Re: Export BibTex using $KeyAttibutes
Reply #8 - Sep 16th, 2011, 7:33am
 
Mark thank you so much for your help! I just have a few questions about what the code does.

Quote:
^if(\$$1)^$1= {^value(\$$1)^},
^endIf^

What do those mean?

Code:
$MyExportSet=$KeyAttributes.replace("citeinessay|citetype|nickname","");
$MyExportString=eval($MyExportSet.format('','eval(do("export-list","','"))+','')
); 


Can you explain what this is doing?

Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #9 - Sep 16th, 2011, 9:48am
 
Note - do ensure to read the linked articles in this reply as they hold much of the fine detail of the answers you seek.

Quote:
^if(\$$1)^$1= {^value(\$$1)^},
^endIf^


Using do() or ^do()^call, the first parameter given is the name of the macro. The second and subsequent parameters form the first and subsequent data inputs passed the specified macro.

Within the macro, the inputs are referred to as $1, $@, etc. Wherever a $N is inserted the literal value of the input (i.e. the do() input value), thus above the $1 is inserting an attribute name passed to the macro. But, we need the overall result to be an attribute
reference (i.e. a $-prefixed name). In a macro a $ already has a special meaning, so to insert an actual dollar-sign character we 'escape' it with a back-slash (the red code above). So is input one is "SomeName" the macro code \$$1 returns the string "$SomeName". White space matters in this context; if the macro code were \$ $1 the string returned becomes "$ SomeName" - which isn't, of course, a valid attribute reference!

Quote:
$MyExportSet=$KeyAttributes.replace("citeinessay|citetype|nickname","");

$KeyAttributes is a Set. We are using the Set.replace() to create set that excludes the 3 attribute names before passing the result to $MyExportSet.  The replace string is a regular expression - description of the fine workings of these is a subject on its own; if you're new to regex, I'd read up the general subject via Google.

We need $MyExportSet as if we try to do the next step by nesting code (inserting the right side of the above where $MyExportSet is in the code below) it doesn't work. There are limits to how deeply TB will drill down into  code in terms of evaluating things.  There's no hard as fast limit - before you ask, you just need to experiment. Plus, when designing this sort of code, it's useful to have interim attribute values so you can check the result of each step is what you assumed it to be.

Quote:
$MyExportString=eval($MyExportSet.format('','eval(do("export-list","','"))+','')

);

This is a hack to get around the fact that action and export code lack a loop function, e.g. for(x, x < y; x=x+1) sort of thing. We need to turn every item in $MyExportSet into a discrete string.  OK, Set.format(), in its 4-argument syntax, lets us supply list-prefix/item-prefix/item-suffix/list-suffix for each item in the set. In fact, we only need list prefix/suffix but there's another road-block that I address with a macro. Each Set item's output needs to use the current list item value more than once. So, in the format() code's argument #2 and #3 we wrap the item name in an eval(do()) call, split between [2] and [3] like so: [eval(do(] and  [))]. For Set item "SomeAttribute" we pass in

eval(do("export-list","SomeName"))+

…representing #2[Set item value]#3, and receiving back:

eval(^if($SomeName)^SomeName= {^value($SomeName)^},
^endIf^)+


Great, we've now tricked format() into inserting each set item value not once, but three times, into the per-item output.

The outer wrapper takes the string+string+string+ output from format() and turns it into a single string.

Footnote. Why use ^code^ in the macro and not action code and evaluate before export. Well, it seems that's pushing eval() and macros further than they're currently (v5.9.3) designed to go - ergo the outstanding problem with TB no letting me get line returns where I want them in $ExportString. AS you may guess I explored a lot of blind alleys in getting to the above code (as the edges of action code aren't documented  - aTbRef is effectively what we know, or believe, is true).

Sorry for the long post. This stuff isn't simple (in execution) so please don't shoot the messenger if it's hard to follow! As said previously, try breaking the task into steps and check at each one that progress thus far is what you expect.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Sebastian Stephenson
Full Member
*
Offline



Posts: 100

Re: Export BibTex using $KeyAttibutes
Reply #10 - Sep 17th, 2011, 6:20am
 
Well I certainly was not expecting a one liner Wink

Mark this is fantastic, thank you.

I may try and see if could create an applescript that adds line breaks but maybe I should file a bug report or feature request to support line breaks in macros?

I only read it once and understand about 3/4 of it. So I will reread again to see if I can help in anyway. Regardless, this is huge step in the right direction, thank you.
Back to top
 
 
Sebastian Stephenson sebey9   IP Logged
Stéphane R
Full Member
*
Offline



Posts: 71

Re: Export BibTex using $KeyAttibutes
Reply #11 - Sep 17th, 2011, 9:02am
 
Sebastian,

Mark A.'s code looks very elaborate, and I would most likely understand it better if I saw it implemented.
Plus, I'm very interested in exporting BibTeX as well (to avoid redundancies with other bibliography programs).
Do you have a more recent version of your sample file that we could look at?

Thanks a bunch,
Stephane
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #12 - Sep 17th, 2011, 10:55am
 
@Stéphane, my test TBX is here (zipped TBX).

@Sebastian. 3/4? You're doing well Smiley, as it's not exactly shallow-end TB use. I've tried to avoid classing the missing line return issue as a 'bug' because it's only my assumption that it ought to be possible - I've not seen a definitive statement that it should work. Whatever, past experience is that these sort of edge cases get fixed in subsequent releases.
Back to top
 
« Last Edit: Sep 17th, 2011, 12:57pm by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v5
(TB consulting - email me)
WWW shoantel   IP Logged
Stéphane R
Full Member
*
Offline



Posts: 71

Re: Export BibTex using $KeyAttibutes
Reply #13 - Sep 17th, 2011, 12:02pm
 
Thanks very much Mark, but the link doesn't work for me
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 4572
Southsea, UK
Re: Export BibTex using $KeyAttibutes
Reply #14 - Sep 17th, 2011, 12:58pm
 
Bother, a typo in the URL. Now fixed.
Back to top
 
 

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