* Default values for key-value macros
@ 2021-10-10 12:01 noib3 via ntg-context
2021-10-10 15:42 ` Hans Hagen via ntg-context
0 siblings, 1 reply; 2+ messages in thread
From: noib3 via ntg-context @ 2021-10-10 12:01 UTC (permalink / raw)
To: ntg-context; +Cc: noib3
[-- Attachment #1.1: Type: text/plain, Size: 1285 bytes --]
The way I've been defining key-value macros is to first pass them to Lua,
handle the parsing there thanks to `utilities.parsers.settings_to_hash` and
then assign the resulting Lua table to ConTeXt variables.
This worked fine up to now, but I'm having problems with the following MWE:
```
\def\style#1[#2] {
\ctxlua{userdata.style([==[#2]==])}
\placestyle
}
\startluacode
userdata = userdata or {}
userdata.style = function(keyvals)
local style = {
color = nil,
text = nil,
}
args = utilities.parsers.settings_to_hash(keyvals)
for k, v in pairs(args) do
style[k] = v
end
context.setvariables({'style'}, style)
end
\stopluacode
\define\placestyle{
\doiftext
{\getvariable{style}{text}}
{\color[\getvariable{style}{color}]{\getvariable{style}{text}}}
}
\starttext
\style[
text={Some red text},
color=red,
]
\style[
color=blue,
]
\stoptext
```
Here I assign a default value of `nil` to every key, and then check if the
`text` variable is empty before printing it. I would expect the second
macro not to print anything since its `text` should be nil, instead both
macros print 'Some red text', the first one in red and the second one in
blue.
I have attached a screenshot of the output.
What am I doing wrong?
[-- Attachment #1.2: Type: text/html, Size: 1623 bytes --]
[-- Attachment #2: 2021-10-10_14-00.png --]
[-- Type: image/png, Size: 8371 bytes --]
[-- Attachment #3: Type: text/plain, Size: 493 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] 2+ messages in thread
* Re: Default values for key-value macros
2021-10-10 12:01 Default values for key-value macros noib3 via ntg-context
@ 2021-10-10 15:42 ` Hans Hagen via ntg-context
0 siblings, 0 replies; 2+ messages in thread
From: Hans Hagen via ntg-context @ 2021-10-10 15:42 UTC (permalink / raw)
To: mailing list for ConTeXt users; +Cc: Hans Hagen
On 10/10/2021 2:01 PM, noib3 via ntg-context wrote:
> The way I've been defining key-value macros is to first pass them to
> Lua, handle the parsing there thanks to
> `utilities.parsers.settings_to_hash` and then assign the resulting Lua
> table to ConTeXt variables.
>
> This worked fine up to now, but I'm having problems with the following MWE:
>
> ```
> \def\style#1[#2] {
> \ctxlua{userdata.style([==[#2]==])}
> \placestyle
> }
here you overload a core macro
> \startluacode
> userdata = userdata or {}
>
> userdata.style = function(keyvals)
> local style = {
> color = nil,
> text = nil,
> }
> args = utilities.parsers.settings_to_hash(keyvals)
use "local args" here ^
> for k, v in pairs(args) do
> style[k] = v
> end
> context.setvariables({'style'}, style)
> end
> \stopluacode
>
> \define\placestyle{
> \doiftext
> {\getvariable{style}{text}}
> {\color[\getvariable{style}{color}]{\getvariable{style}{text}}}
> }
>
> \starttext
>
> \style[
> text={Some red text},
> color=red,
> ]
>
> \style[
> color=blue,
> ]
>
> \stoptext
> ```
>
> Here I assign a default value of `nil` to every key, and then check if
> the `text` variable is empty before printing it. I would expect the
> second macro not to print anything since its `text` should be nil,
> instead both macros print 'Some red text', the first one in red and the
> second one in blue.
context.setvariables
like any setting, it accumulates unless you group
> I have attached a screenshot of the output.
>
> What am I doing wrong?
local style = {
color = "",
text = "",
}
or you can go fancy with:
\startluacode
interfaces.implement {
name = "MyStyle",
public = true,
protected = true,
arguments = "hash",
actions = function(a)
if a.text and a.text ~= "" then
if a.color and a.color ~= "" then
context.color( { a.color }, a.text)
else
context(a.text)
end
end
end
}
\stopluacode
\MyStyle[
text={Some blue text},
color=blue,
]
\MyStyle[
color=blue,
]
\MyStyle[
text={test \em test},
]
-----------------------------------------------------------------
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] 2+ messages in thread
end of thread, other threads:[~2021-10-10 15:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-10 12:01 Default values for key-value macros noib3 via ntg-context
2021-10-10 15:42 ` Hans Hagen via ntg-context
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).