* Modifying bibliography entries using lua
@ 2018-07-23 16:42 Stanislav Sokolenko
2018-07-23 18:40 ` Hans Hagen
0 siblings, 1 reply; 6+ messages in thread
From: Stanislav Sokolenko @ 2018-07-23 16:42 UTC (permalink / raw)
To: mailing list for ConTeXt users
Dear list,
I'm struggling to output bibliography entries into lua for string
modification (like making a particular author name bold). In effect, I
just need something like the following:
\starttexdefinition btx:customauthor
\startluacode
local text = context.btxflush('author')
-- tex.sprint(text) -- fails because text remains nil
\stopluacode
\stoptexdefinition
It's clear that btxflux is the wrong function for this as it writes the
contents to file rather than returning a variable in lua... Is there a
way to directly access what btxflush is writing through the publications
table or other means? I've gone over the source code but I can't
decipher what btxflush is actually doing.
Thanks,
Stan
Longer MNWE:
\stopluacode
\startbuffer[ref]
@article{solo,
author = {Lastname, Firstname},
title = {Solo work},
journal = {Journal},
year = {2000},
month = {1},
volume = {1}
}
\stopbuffer
\usebtxdataset[ref][ref.buffer]
\setupbtx[dataset=ref]
\definebtxrendering[ref][dataset=ref]
\setupbtx[default:cite]
[alternative=authoryear,
etallimit=1]
\starttexdefinition btx:customauthor
\startluacode
local text = context.btxflush('author')
-- tex.sprint(text) -- text remains nill
\stopluacode
\stoptexdefinition
\startsetups btx:default:list:article
\texdefinition{btx:customauthor}
\texdefinition{btx:default:title}
\texdefinition{btx:default:journal}
\texdefinition{btx:default:year}
\removeunwantedspaces
\removepunctuation
\btxperiod
\stopsetups
\starttext
Citations: \cite[solo]
\startsubject[title=Bibliography]
\placelistofpublications[ref][method=dataset]
\stopsection
\stoptext
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Modifying bibliography entries using lua
2018-07-23 16:42 Modifying bibliography entries using lua Stanislav Sokolenko
@ 2018-07-23 18:40 ` Hans Hagen
2018-07-23 20:17 ` Stanislav Sokolenko
0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2018-07-23 18:40 UTC (permalink / raw)
To: mailing list for ConTeXt users, Stanislav Sokolenko
On 7/23/2018 6:42 PM, Stanislav Sokolenko wrote:
> Dear list,
>
> I'm struggling to output bibliography entries into lua for string
> modification (like making a particular author name bold). In effect, I
> just need something like the following:
>
> \starttexdefinition btx:customauthor
> \startluacode
> local text = context.btxflush('author')
> -- tex.sprint(text) -- fails because text remains nil
> \stopluacode
> \stoptexdefinition
>
> It's clear that btxflux is the wrong function for this as it writes the
> contents to file rather than returning a variable in lua... Is there a
> way to directly access what btxflush is writing through the publications
> table or other means? I've gone over the source code but I can't
> decipher what btxflush is actually doing.
This is not for the fainthearted so here we go
\startluacode
function document.MyBoldPub(set,tag)
-- local a = publications.getfield(set,tag,"author")
-- inspect(a)
-- local c = publications.getcasted(set,tag,"author")
-- inspect(c)
if c[1].surnames[1] == "Myname" then
context.bold(function()
context.btxflush('author')
end )
else
context.btxflush('author')
end
end
\stopluacode
\starttexdefinition btx:customauthor
\ctxluacode{document.MyBoldPub("\currentbtxdataset","\currentbtxtag")}
\stoptexdefinition
try to avoid tex.print cum suis, use the proper context.* interface instead
> Thanks,
>
> Stan
>
> Longer MNWE:
>
> \stopluacode
>
> \startbuffer[ref]
> @article{solo,
> author = {Lastname, Firstname},
> title = {Solo work},
> journal = {Journal},
> year = {2000},
> month = {1},
> volume = {1}
> }
> \stopbuffer
>
> \usebtxdataset[ref][ref.buffer]
> \setupbtx[dataset=ref]
>
> \definebtxrendering[ref][dataset=ref]
> \setupbtx[default:cite]
> [alternative=authoryear,
> etallimit=1]
>
>
> \starttexdefinition btx:customauthor
> \startluacode
> local text = context.btxflush('author')
> -- tex.sprint(text) -- text remains nill
> \stopluacode
> \stoptexdefinition
>
> \startsetups btx:default:list:article
> \texdefinition{btx:customauthor}
> \texdefinition{btx:default:title}
> \texdefinition{btx:default:journal}
> \texdefinition{btx:default:year}
> \removeunwantedspaces
> \removepunctuation
> \btxperiod
> \stopsetups
>
> \starttext
>
> Citations: \cite[solo]
>
> \startsubject[title=Bibliography]
> \placelistofpublications[ref][method=dataset]
> \stopsection
>
> \stoptext
>
>
> ___________________________________________________________________________________
>
> If your question is of interest to others as well, please add an entry
> to the Wiki!
>
> maillist : ntg-context@ntg.nl /
> http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage : http://www.pragma-ade.nl / http://context.aanhet.net
> archive : https://bitbucket.org/phg/context-mirror/commits/
> wiki : http://contextgarden.net
> ___________________________________________________________________________________
--
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Modifying bibliography entries using lua
2018-07-23 18:40 ` Hans Hagen
@ 2018-07-23 20:17 ` Stanislav Sokolenko
2018-07-23 21:32 ` Hans Hagen
0 siblings, 1 reply; 6+ messages in thread
From: Stanislav Sokolenko @ 2018-07-23 20:17 UTC (permalink / raw)
To: Hans Hagen, mailing list for ConTeXt users
Thanks Hans, that's really helpful!
> This is not for the fainthearted so here we go
>
> \startluacode
> function document.MyBoldPub(set,tag)
> -- local a = publications.getfield(set,tag,"author")
> -- inspect(a)
> -- local c = publications.getcasted(set,tag,"author")
> -- inspect(c)
> if c[1].surnames[1] == "Myname" then
> context.bold(function()
> context.btxflush('author')
> end )
> else
> context.btxflush('author')
> end
> end
> \stopluacode
So if I wanted to bold one specific author in a reference of many, I can
just loop over publications.getcasted(...), and print the appropriate
names, initials, etc...
That said -- is there any way to call the appropriate authorconversion
routine based on the \setupbtx settings? I found the
publications.authorhashers method table, which seems to do most of this
work, but I'm not sure how to pick the appropriate method and ensure
that the correct separators are used.
Stan
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Modifying bibliography entries using lua
2018-07-23 20:17 ` Stanislav Sokolenko
@ 2018-07-23 21:32 ` Hans Hagen
2018-07-24 14:41 ` Stanislav Sokolenko
0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2018-07-23 21:32 UTC (permalink / raw)
To: Stanislav Sokolenko, mailing list for ConTeXt users
On 7/23/2018 10:17 PM, Stanislav Sokolenko wrote:
> Thanks Hans, that's really helpful!
>
>> This is not for the fainthearted so here we go
>>
>> \startluacode
>> function document.MyBoldPub(set,tag)
>> -- local a = publications.getfield(set,tag,"author")
>> -- inspect(a)
>> -- local c = publications.getcasted(set,tag,"author")
>> -- inspect(c)
>> if c[1].surnames[1] == "Myname" then
>> context.bold(function()
>> context.btxflush('author')
>> end )
>> else
>> context.btxflush('author')
>> end
>> end
>> \stopluacode
>
> So if I wanted to bold one specific author in a reference of many, I can
> just loop over publications.getcasted(...), and print the appropriate
> names, initials, etc...
>
> That said -- is there any way to call the appropriate authorconversion
> routine based on the \setupbtx settings? I found the
> publications.authorhashers method table, which seems to do most of this
> work, but I'm not sure how to pick the appropriate method and ensure
> that the correct separators are used.
Best use the setups provided than reinvent the logic.
\startluacode
function document.CheckMeB(set,tag,aut)
local c = publications.getcasted(set,tag,"author")
if c[aut].surnames[1] == "Foo" then
context("\\bf")
end
end
\stopluacode
\startsetups btx:list:author:normalshort
\fastsetup{btx:list:author:concat}
\begingroup
\ctxluacode{document.CheckMeB("\currentbtxdataset","\currentbtxtag",\number\currentbtxauthorindex)}
\ifx\currentbtxinitials\empty \else
\currentbtxinitials
\btxparameter{separator:initials}
\fi
\ifx\currentbtxvons\empty \else
\currentbtxvons
\ifx\currentbtxsurnames\empty \else
\btxparameter{separator:vons}
\fi
\fi
\ifx\currentbtxsurnames\empty \else
\currentbtxsurnames
\ifx\currentbtxjuniors\empty \else
\btxparameter{separator:juniors}
\currentbtxjuniors
\fi
\fi
\endgroup
\fastsetup{btx:list:author:others}
\stopsetups
Or something similar (there are several wasy to do this).
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Modifying bibliography entries using lua
2018-07-23 21:32 ` Hans Hagen
@ 2018-07-24 14:41 ` Stanislav Sokolenko
2018-07-24 16:29 ` Hans Hagen
0 siblings, 1 reply; 6+ messages in thread
From: Stanislav Sokolenko @ 2018-07-24 14:41 UTC (permalink / raw)
To: Hans Hagen, mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 3019 bytes --]
That's perfect, thank you!
On 2018-07-23 06:32 PM, Hans Hagen wrote:
> \startsetups btx:list:author:normalshort
> \fastsetup{btx:list:author:concat}
> \begingroup
>
> \ctxluacode{document.CheckMeB("\currentbtxdataset","\currentbtxtag",\number\currentbtxauthorindex)}
>
> \ifx\currentbtxinitials\empty \else
> \currentbtxinitials
> \btxparameter{separator:initials}
> \fi
> \ifx\currentbtxvons\empty \else
> \currentbtxvons
> \ifx\currentbtxsurnames\empty \else
> \btxparameter{separator:vons}
> \fi
> \fi
> \ifx\currentbtxsurnames\empty \else
> \currentbtxsurnames
> \ifx\currentbtxjuniors\empty \else
> \btxparameter{separator:juniors}
> \currentbtxjuniors
> \fi
> \fi
> \endgroup
> \fastsetup{btx:list:author:others}
> \stopsetups
I went with the following little extension to match and make bold
multiple names:
|\startbuffer[ref]@article{solo, author ={MyLast, MyFirst}, title ={Solo
work}, journal ={Journal}, year ={2000}, month ={1}, volume
={1}}@article{co, author ={OtherLast, OtherFirst and MyLast, MyFirst and
CoLast, CoFirst}, title ={Joint work}, journal ={Journal}, month ={1},
year ={2000}, volume
={1}}\stopbuffer\usebtxdataset[ref][ref.buffer]\setupbtx[dataset=ref]\definebtxrendering[ref][dataset=ref]\setupbtx[default:cite][alternative=authoryear,
etallimit=1, authorconversion=normalshort]\startluacodeBoldNames
={{firstnames ="MyFirst", surnames ="MyLast"}, {initials ="C", surnames
="CoLast"}, }function document.CompareNames(reference, targets)-- Loop
over targets for _, target in pairs(targets)do -- Loop over fields for
key, value in pairs(target)do if reference[key]==nil then break end
full_entry ='' for _, part in ipairs(reference[key])do full_entry
=full_entry .. ' ' .. part end full_entry =string.sub(full_entry, 2)if
full_entry ==value then return(true)end end end return(false)end
function document.BoldNames(set, tag, aut)local c
=publications.getcasted(set, tag, "author")if
document.CompareNames(c[aut], BoldNames)then context("\\bf")end end
\stopluacode\startsetupsbtx:list:author:normalshort
\fastsetup{btx:list:author:concat}\begingroup\ctxluacode{document.BoldNames("\currentbtxdataset","\currentbtxtag",\number\currentbtxauthorindex)}\ifx\currentbtxinitials\empty\else\currentbtxinitials\btxparameter{separator:initials}\fi\ifx\currentbtxvons\empty\else\currentbtxvons\ifx\currentbtxsurnames\empty\else\btxparameter{separator:vons}\fi\fi\ifx\currentbtxsurnames\empty\else\currentbtxsurnames\ifx\currentbtxjuniors\empty\else\btxparameter{separator:juniors}\currentbtxjuniors\fi\fi\endgroup\fastsetup{btx:list:author:others}\stopsetups\starttextCitations:
\cite[solo]\cite[co]\startsubject[title=Bibliography]\placelistofpublications[ref][method=dataset]\stopsection\stoptext|
Cheers,
Stan
[-- Attachment #1.2: Type: text/html, Size: 12807 bytes --]
[-- Attachment #2: Type: text/plain, Size: 492 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Modifying bibliography entries using lua
2018-07-24 14:41 ` Stanislav Sokolenko
@ 2018-07-24 16:29 ` Hans Hagen
0 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2018-07-24 16:29 UTC (permalink / raw)
To: Stanislav Sokolenko, mailing list for ConTeXt users
On 7/24/2018 4:41 PM, Stanislav Sokolenko wrote:
> I went with the following little extension to match and make bold
> multiple names:
ok.
(this document.CompareNames makes a good candidate for a 'how to improve
your lua code' tutorial ... maybe something for the ctx meeting)
I didn't check what it does but ... for instance
> BoldNames = {
> full_entry = ''
use locals!
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-07-24 16:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-23 16:42 Modifying bibliography entries using lua Stanislav Sokolenko
2018-07-23 18:40 ` Hans Hagen
2018-07-23 20:17 ` Stanislav Sokolenko
2018-07-23 21:32 ` Hans Hagen
2018-07-24 14:41 ` Stanislav Sokolenko
2018-07-24 16:29 ` Hans Hagen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).