Parse field values to new tiddler(s)?

150 views
Skip to first unread message

Stallings

unread,
Jun 11, 2020, 9:57:54 AM6/11/20
to TiddlyWiki
I'd like to turn the value of a field ("bibtex-annote") into the content of multiple new tiddlers for later use/transclusion. Right now, I am able to copy the entire value to a single new tiddler with this button (adapted from here):

\define annoTags() [[$(currentTiddler)$]] Annotation
\define annoTiddler() $(currentTiddler)$


<$button>
<$action-sendmessage $message="tm-new-tiddler" title="New Annotation" tags=<<annoTags>> text={{!!bibtex-annote}}/>
Pull annotations
</$button>

This works, but obviously is incapable of parsing the value into multiple tiddlers. Here's the value:

Extracted Annotations (6/11/2020, 8:20:28 AM)"Infection-control strategies focused ... into this facility." (Arons et al 2020:1){\textbar}"The median ... (interquartile range, 3 to 5)." (Arons et al 2020:3){\textbar}

Which should return two tiddlers:
"Infection-control strategies focused ... into this facility." (Arons et al 2020:1)

"The median ... (interquartile range, 3 to 5)." (Arons et al 2020:3)

Where should I look for this kind of parsing? I'm currently using single-file TW5. I'm unsure about writing js, but can follow a guide/examples.

If I am way off, are there are other ways to import annotations from reference managers or BibTeX? I'm using Zotero with Zotfile at the moment.

Eric Shulman

unread,
Jun 11, 2020, 11:04:54 AM6/11/20
to TiddlyWiki
On Thursday, June 11, 2020 at 6:57:54 AM UTC-7, Stallings wrote:
I'd like to turn the value of a field ("bibtex-annote") into the content of multiple new tiddlers for later use/transclusion. Right now, I am able to copy the entire value to a single new tiddler with this button (adapted from here):
\define annoTags() [[$(currentTiddler)$]] Annotation
\define annoTiddler() $(currentTiddler)$
<$button>
<$action-sendmessage $message="tm-new-tiddler" title="New Annotation" tags=<<annoTags>> text={{!!bibtex-annote}}/>
Pull annotations
</$button>
This works, but obviously is incapable of parsing the value into multiple tiddlers. Here's the value:
Extracted Annotations (6/11/2020, 8:20:28 AM)"Infection-control strategies focused ... into this facility." (Arons et al 2020:1){\textbar}"The median ... (interquartile range, 3 to 5)." (Arons et al 2020:3){\textbar}
Which should return two tiddlers:
"Infection-control strategies focused ... into this facility." (Arons et al 2020:1)
"The median ... (interquartile range, 3 to 5)." (Arons et al 2020:3)

The $button widget can contain a $list widget that splits the existing "bibtex-annote" field into parts and then invokes the $action-sendmessage for each.  Something like this:

\define annoTags() [[$(currentTiddler)$]] Annotation

<$button>
   
<$list filter="[{!!bibtex-annote}split[{\textbar}]butlast[]]" variable="this-annote">
     
<$action-sendmessage $message="tm-new-tiddler" $param="New Annotation" tags=<<annoTags>> text=<<this-annote>>/>
   </
$list>
   
Pull annotations
</$button>

notes:
* the $list filter splits the bibtex-annote value on each occurrence of "{\textbar}".  note that the curly-braces in this case are literal text, and *NOT* a field reference syntax
* the resulting items do not include the split text (i.e., "{\textbar}" is automatically omitted)
* the $list includes a trailing blank item due to the final instance of "{\textbar}", so we use "butlast[]" to remove that blank item
* the $list uses variable="this-annote" to avoid changing the value of $(currentTiddler)$ (which you use in the annoTags() macro)
* the tm-new-tiddler uses $param="..." rather than title="..." to specify the title of the new tiddler.  This allows the action to automatically add a numeric suffix to the title (so that each new tiddler has a unique title)

Give it a try and see if it does as you intend.  Let me know how it goes.

enjoy,
-e

Stallings

unread,
Jun 11, 2020, 9:02:15 PM6/11/20
to TiddlyWiki
Works perfectly! Your explanations were invaluable, thank you.

After a good amount of trial-and-error, I now have a system of three tiddlers (example site so no one's working in the dark): 

1. "Annotation Parser" containing your code above

2. "Annotation Filter", tagged with $:/tags/ViewTemplate (I have no concept of why this works) containing the filter below and transcluding the parser
<$list filter="[is[current]!field:bibtex-annote[]]">
{{||Annotation Parser}}
</$list>

and 3. any tiddler containing the field "bibtex-entry-type," with the transcluded button.

I tried to remove "Extracted Annotations (6/7/2020, 1:57:43 PM)" from the beginning of the string (that bit always ends with "AM)" or "PM)" with

<$list filter="[{!!bibtex-annote}allafter[M)]split[{\textbar}]butlast[]]" variable="this-annote">

but that didn't work, I assume because it was expecting an item in a list, rather than a regular expression. Nesting didn't work, either:

<$list filter="[{!!bibtex-annote}regexp[allafter[M)]]split[{\textbar}]butlast[]]" variable="this-annote">

but that was just a shot in the dark.

Eric Shulman

unread,
Jun 11, 2020, 10:00:38 PM6/11/20
to TiddlyWiki
On Thursday, June 11, 2020 at 6:02:15 PM UTC-7, Stallings wrote:
I tried to remove "Extracted Annotations (6/7/2020, 1:57:43 PM)" from the beginning of the string (that bit always ends with "AM)" or "PM)" with
<$list filter="[{!!bibtex-annote}allafter[M)]split[{\textbar}]butlast[]]" variable="this-annote">
but that didn't work, I assume because it was expecting an item in a list, rather than a regular expression. Nesting didn't work, either:
<$list filter="[{!!bibtex-annote}regexp[allafter[M)]]split[{\textbar}]butlast[]]" variable="this-annote">
but that was just a shot in the dark.

Here's an updated "Annotation Parser" that removed the leading text:
\define annoTitle() $(currentTiddler)$ Annotation

\define annoTags() [[$(currentTiddler)$]] Annotation

<$button>

   
<$set name="trimmed-annote" filter="[{!!bibtex-annote}split[M)]]" select="1">
   
<$list filter="[<trimmed-annote>split[{\textbar}]butlast[]]" variable="this-annote">
     
<$action-sendmessage $message="tm-new-tiddler" $param=<<annoTitle>> tags=<<annoTags>> text=<<this-annote>>/>
   </
$list>
   
</$set>
   Pull annotations
</
$button>

notes:
* the $set widget uses a filter to split the bibtext-annote content at the "M)", producing a list of two items.  It then discards the leading "Extracted Annotations (date, time)" content by selecting item 1 from the list (the *second* item, because lists are 0-based)
* the $list then uses the <trimmed-annote> variable as input, rather than the previous bibtext-annote field value
* I also changed the tm-new-tiddler $param value from a literal "New Annotation" to a macro that constructs the new title by appending "Annotation" to the current tiddler title.  Thus, when parsing "Arons et al 2020" from your example, you will get three new tiddlers, entitled "Arons et al 2020 Annotation", "Arons et al 2020 Annotation 1" and "Arons et al 2020 Annotation 2".

again, give it a try and let me know how it goes...

enjoy,
-e

Eric Shulman

unread,
Jun 11, 2020, 10:09:14 PM6/11/20
to TiddlyWiki
Here's yet another version that combines the $set and $list parsing into one:

\define annoTitle() $(currentTiddler)$ Annotation
\define annoTags() [[$(currentTiddler)$]] Annotation

<$button>

   
<$list filter="[{!!bibtex-annote}split[M)]butfirst[]split[{\textbar}]butlast[]]" variable="this-annote">

     
<$action-sendmessage $message="tm-new-tiddler" $param=<<annoTitle>> tags=<<annoTags>> text=<<this-annote>>/>
   </
$list>

   
Pull annotations
</$button>

notes:
* the filter first splits on "M)" and then discards the leading content using the butfirst[] operator
* the rest of the content is then split on "{\textbar}" as before

enjoy,
-e

Stallings

unread,
Jun 12, 2020, 7:13:37 PM6/12/20
to TiddlyWiki
That's it! Again, thank you for clarifying the steps. Documentation on https://tiddlywiki.com/ can be difficult to navigate if I don't know what I'm looking for.
Reply all
Reply to author
Forward
0 new messages