[NTG-context] Re: LMTX: Command handling, Interface between TeX and Lua

2023-12-14 Thread Gerion Entrup
Am Mittwoch, 13. Dezember 2023, 21:57:35 CET schrieb Bruce Horrocks:
> 
> > On 13 Dec 2023, at 17:13, Gerion Entrup  wrote:
> > 
> > Is there a simple way to define a new command/environment and handle
> > the keyword arguments in Lua? For example defining a frontend command
> > like this:
> 
> I don't have the answer to your other questions but I do have an example for 
> this one.
> It's not mine - I'm fairly sure it came from this list but I can't find who 
> that was any more.
> 
> \startluacode
> userdata = userdata or { }
> 
> function userdata.mycommand(keywords, keyvals, str) 
> keyword_options = utilities.parsers.settings_to_array(keywords)
> named_values = utilities.parsers.settings_to_hash(keyvals)
> 
> context('First option = ' .. keyword_options[1])
> context('\\par')
> context('Color chosen = ' .. named_values['color'])
> context('\\par')
> context('Curly braces = ' .. str)
> context('\\par')
> end
> \stopluacode
> 
> \def\mycommand[#1][#2]#3{\ctxlua{
> userdata.mycommand('#1', '#2', [==[#3]==])}} 
> 
> \starttext
> \mycommand[top, inmargin, now][color=green, roof=gabled]{Anne of Green 
> Gables?}
> \stoptext

Thank you. That helps a lot already!

Gerion


signature.asc
Description: This is a digitally signed message part.
___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___


[NTG-context] Re: LMTX: Command handling, Interface between TeX and Lua

2023-12-13 Thread Bruce Horrocks

> On 13 Dec 2023, at 17:13, Gerion Entrup  wrote:
> 
> Is there a simple way to define a new command/environment and handle
> the keyword arguments in Lua? For example defining a frontend command
> like this:

I don't have the answer to your other questions but I do have an example for 
this one.
It's not mine - I'm fairly sure it came from this list but I can't find who 
that was any more.

\startluacode
userdata = userdata or { }

function userdata.mycommand(keywords, keyvals, str) 
keyword_options = utilities.parsers.settings_to_array(keywords)
named_values = utilities.parsers.settings_to_hash(keyvals)

context('First option = ' .. keyword_options[1])
context('\\par')
context('Color chosen = ' .. named_values['color'])
context('\\par')
context('Curly braces = ' .. str)
context('\\par')
end
\stopluacode

\def\mycommand[#1][#2]#3{\ctxlua{
userdata.mycommand('#1', '#2', [==[#3]==])}} 

\starttext
\mycommand[top, inmargin, now][color=green, roof=gabled]{Anne of Green Gables?}
\stoptext

—
Bruce Horrocks
Hampshire, UK

___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___