Josef-Friedrich / luakeys

LuaTeX package to parse key value options in Lua only, like keyval, kvsetkeys, kvoptions, xkeyval, pgfkeys but in Lua

Home Page:https://www.ctan.org/pkg/luakeys

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: option on how to process standalone values

kalekje opened this issue · comments

I think a few of us might want an option on how to process standalone values. I think under the luakeys.parse options table, an option for standaloneastrue might be nice--instead of storing standalone values as integer-indexed values, I would like to see them as keys with a value set to true.

Ex: a,b,c,X=1,Y=2,Z=3 would produce a table like so:

Y	2
b	true
Z	3
c	true
X	1
a	true

I made a simple function to patch this up for my own use.

    local t_new = {}
    for k, v in pairs(t) do
        if type(k) == 'number' then
            t_new[v] = true
        else
            t_new[k] = v
        end
    end
    return t_new

I added a new option called standalone_as_key

I looked through the code and I think it's actually called standalone_as_true, something to keep in mind if you update the documentation. Thanks for adding this feature--I will have to test if it works for nested key-vals as well. To me, parsing key-val options as LaTeX options makes sense this way. But the default was is great if you want a Lua table (maybe you want to declare a table in TeX) because the syntax maps basically 1 to 1. Very nice!

Maybe standalone_as_key or even standalone_string_as_key is a better name for this option. standalone_as_true should work recursively.

Maybe the following new option (a callback function / hook) is also interesting for you:

luakeys.parse(
  'one,two,three={four}',
  {
    converter = function (key, value, depth, current_table, root_table)
      if type(key) == 'number' and type(value) == 'string' then
        return value, true -- the value is now key, true is now the new value
      end
      return key, value -- return key, value to change nothing
    end
  }
)

--> {one = true, two = true, three = {four = true}}

The feature still exists. Check out this lines:

luakeys/luakeys.lua

Lines 461 to 463 in 6d5e676

if options.converter ~= nil and type(options.converter) == 'function' then
parse_tree = visit_parse_tree(parse_tree, options.converter)
end

I think there was a misunderstanding. I have added the additional option converter that might be useful for you. I have implemented the option standalone_as_true as you requested.

luakeys/luakeys.lua

Lines 369 to 371 in 6d5e676

elseif type(key) == 'number' and type(value) == 'string' and options.standalone_as_true then
result[value] = true
else

describe('standalone as true', function()
it('true', function()
assert.are.same({one = true},
luakeys.parse('one', {standalone_as_true = true}))
end)
it('false', function()
assert.are.same({ 'one' },
luakeys.parse('one', {standalone_as_true = false}))
end)
end)
end)

However, the documentation is still missing

Breaking change in v0.6

Standalone values are now interpreted as naked keys by default and set to true. To get the old behavior back us { naked_as_value = true }