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
___________________________________________________________________________________