| 1 |
--% Kale Ewasiuk ([email protected]) |
| 2 |
--% 2025-02-14 |
| 3 |
--% Copyright (C) 2025 Kale Ewasiuk |
| 4 |
--% |
| 5 |
--% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
--% of this software and associated documentation files (the "Software"), to deal |
| 7 |
--% in the Software without restriction, including without limitation the rights |
| 8 |
--% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
--% copies of the Software, and to permit persons to whom the Software is |
| 10 |
--% furnished to do so, subject to the following conditions: |
| 11 |
--% |
| 12 |
--% The above copyright notice and this permission notice shall be included in |
| 13 |
--% all copies or substantial portions of the Software. |
| 14 |
--% |
| 15 |
--% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 16 |
--% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 |
--% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 18 |
--% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 |
--% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 20 |
--% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
--% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
--% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 23 |
--% OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
|
| 26 |
local luatbls = {} |
| 27 |
|
| 28 |
luatbls._luakeys = require'luakeys'() |
| 29 |
|
| 30 |
luatbls._rec_tbl = '' |
| 31 |
luatbls._rec_tbl_opts = {} |
| 32 |
|
| 33 |
luatbls._xysep = '%s+' -- spaces separate x-y coordinates |
| 34 |
luatbls._tblv = '<v>' |
| 35 |
luatbls._tblk = '<k>' |
| 36 |
luatbls._cstemp = 'ltbl<t><k>' |
| 37 |
luatbls._debug = false |
| 38 |
|
| 39 |
function luatbls._dprint(s1, s2) |
| 40 |
if luatbls._debug then |
| 41 |
penlight.wrth(s1, s1) |
| 42 |
end |
| 43 |
end |
| 44 |
|
| 45 |
|
| 46 |
setmetatable(luatbls, {__call=function(t,s)return t._get_tbl_item(s) end}) |
| 47 |
|
| 48 |
function luatbls._get_tbl_name(s) |
| 49 |
s = s:strip() |
| 50 |
if s == '' then |
| 51 |
return luatbls._rec_tbl |
| 52 |
end |
| 53 |
for _, delim in ipairs{'.', '/', '|'} do |
| 54 |
s = s:split(delim)[1] -- if tbl reference had a . | or /, an indexer was used |
| 55 |
end |
| 56 |
if luatbls[s] == nil then |
| 57 |
local validtbls = penlight.List(penlight.tablex.kkeys(luatbls)):filter(function(s) return not s:startswith('_') end):join(', ') |
| 58 |
penlight.tex.pkgerror('luatbls', 'Tried to access undefined tbl: "'..s..'". Valid tbls are: '..validtbls) |
| 59 |
return luatbls._rec_tbl |
| 60 |
end |
| 61 |
return s |
| 62 |
end |
| 63 |
|
| 64 |
function luatbls._get_tbl(s) |
| 65 |
s = luatbls._get_tbl_name(s) |
| 66 |
return luatbls[s] |
| 67 |
end |
| 68 |
|
| 69 |
function luatbls._get_tbl_index(s, undec) |
| 70 |
undec = undec or false -- flag for allowing undeclared indexing |
| 71 |
local tbl = '' |
| 72 |
local key = '' |
| 73 |
local s_raw = s |
| 74 |
if s:find('%.') then |
| 75 |
local tt = s:split('.') |
| 76 |
tbl = tt[1] |
| 77 |
key = tt[2] |
| 78 |
elseif s:find('/') then |
| 79 |
local tt = s:split('/') |
| 80 |
tbl = tt[1] |
| 81 |
if tbl == '' then tbl = luatbls._rec_tbl end |
| 82 |
key = tonumber(tonumber(tt[2])) |
| 83 |
if key < 0 then key = #luatbls[tbl]+1+key end |
| 84 |
else |
| 85 |
tbl = luatbls._rec_tbl |
| 86 |
key = tonumber(s) or s |
| 87 |
if type(key) == 'number' and key < 0 then key = #luatbls[tbl]+1+key end |
| 88 |
end |
| 89 |
if tbl == '' then tbl = luatbls._rec_tbl end |
| 90 |
|
| 91 |
if (luatbls[tbl] == nil) or ((not undec) and (luatbls[tbl][key] == nil)) then |
| 92 |
penlight.tex.pkgerror('luatbls', 'Invalid tbl index attempt using: "'..s_raw..'". We tried to use tbl="' ..tbl..'" and key="'..key..'"'.. |
| 93 |
'Note that "|" is forbidden here. The recent table is: '..luatbls._rec_tbl) |
| 94 |
end |
| 95 |
return tbl, key |
| 96 |
end |
| 97 |
|
| 98 |
function luatbls._get_tbl_seq(s) |
| 99 |
local tblseq = s:split('|') |
| 100 |
local tbl = nil |
| 101 |
local seq = nil |
| 102 |
if #tblseq == 1 then |
| 103 |
tbl = luatbls._get_tbl_name('') |
| 104 |
seq = tblseq[1] |
| 105 |
if seq == '' then seq = ':,*' end |
| 106 |
elseif #tblseq == 2 then |
| 107 |
tbl = luatbls._get_tbl_name(tblseq[1]) |
| 108 |
seq = tblseq[2] |
| 109 |
if seq == '' then seq = ':,*' end |
| 110 |
end |
| 111 |
return tbl, seq |
| 112 |
end |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
function luatbls._get_tbl_item(s, p) -- get item with string, p means print value |
| 117 |
p = p or false |
| 118 |
local tbl, key = luatbls._get_tbl_index(s) |
| 119 |
local itm = luatbls[tbl][key] |
| 120 |
if p then |
| 121 |
tex.sprint(tostring(itm)) |
| 122 |
end |
| 123 |
return itm |
| 124 |
end |
| 125 |
|
| 126 |
|
| 127 |
function luatbls._set_tbl_item(s, v) |
| 128 |
tbl, key = luatbls._get_tbl_index(s) |
| 129 |
luatbls[tbl][key] = v |
| 130 |
end |
| 131 |
|
| 132 |
function luatbls._check_recent_tbl_undefault() |
| 133 |
local undefaults = {} |
| 134 |
if luatbls._rec_tbl_opts ~= nil then |
| 135 |
local defaults = penlight.tablex.union( |
| 136 |
luatbls._rec_tbl_opts.defs or {}, |
| 137 |
luatbls._rec_tbl_opts.defaults or {} |
| 138 |
) |
| 139 |
for k, v in pairs(luatbls[luatbls._rec_tbl]) do |
| 140 |
if defaults[k] == nil then |
| 141 |
undefaults[#undefaults+1] = k |
| 142 |
end |
| 143 |
end |
| 144 |
if penlight.hasval(undefaults) then |
| 145 |
penlight.tex.pkgerror('luatbls', |
| 146 |
'Invalid keys passed to tbl keyval: ' .. (', '):join(undefaults) .. |
| 147 |
' ; choices are: ' .. (', '):join(penlight.tablex.keys(defaults)) |
| 148 |
) |
| 149 |
end |
| 150 |
end |
| 151 |
end |
| 152 |
|
| 153 |
function luatbls._check_choices(k, csv) |
| 154 |
local csv = penlight.List(luatbls._luakeys.parse(csv,{naked_as_value=true})) |
| 155 |
local v = luatbls._get_tbl_item(k) |
| 156 |
if not csv:contains(v) then |
| 157 |
penlight.tex.pkgerror('luatbls', 'Invalid choice "'..v..'" given to tbl.key "'..k..'". Allowed choices are: '.. |
| 158 |
(', '):join(csv)) |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
|
| 163 |
function luatbls._make_alpha_key(k) |
| 164 |
if tonumber(k) ~= nil then |
| 165 |
k = penlight.Char(tonumber(k)) |
| 166 |
end |
| 167 |
return k |
| 168 |
end |
| 169 |
|
| 170 |
function luatbls._make_def_name(t, k, temp) |
| 171 |
if temp == penlight.tex.xNoValue then |
| 172 |
temp = luatbls._cstemp |
| 173 |
end |
| 174 |
k = luatbls._make_alpha_key(k) |
| 175 |
return temp:gsub('<t>',t):gsub('<k>',k) |
| 176 |
end |
| 177 |
|
| 178 |
function luatbls._def_tbl(ind, def, g) |
| 179 |
local _tbl, _key = luatbls._get_tbl_index(ind) |
| 180 |
def = luatbls._make_def_name(_tbl, _key, def) |
| 181 |
luatbls._def_tbl_one(luatbls[_tbl][_key], def, g) |
| 182 |
end |
| 183 |
|
| 184 |
|
| 185 |
function luatbls._def_tbl_some(Ind, def, g) |
| 186 |
for t, k, v in luatbls._iter_tbls_vals(Ind) do |
| 187 |
local newdef = luatbls._make_def_name(t, k, def) |
| 188 |
luatbls._def_tbl_one(v, newdef, g) |
| 189 |
end |
| 190 |
end |
| 191 |
|
| 192 |
|
| 193 |
function luatbls._def_tbl_one(v, cs, g) |
| 194 |
if type(v) == 'table' then |
| 195 |
for kk, vv in pairs(v) do |
| 196 |
token.set_macro(cs..luatbls._make_alpha_key(kk), tostring(vv), g) |
| 197 |
end |
| 198 |
else |
| 199 |
token.set_macro(cs, tostring(v), g) |
| 200 |
end |
| 201 |
end |
| 202 |
|
| 203 |
function luatbls._def_tbl_coords(ind, def) |
| 204 |
local tbl, key = luatbls._get_tbl_index(ind) |
| 205 |
local str = luatbls[tbl][key] |
| 206 |
def = luatbls._make_def_name(tbl, key, def) |
| 207 |
local x, y = str:strip():splitv(luatbls._xysep) |
| 208 |
if (not penlight.hasval(x)) or (not penlight.hasval(y)) then |
| 209 |
penlight.tex.pkgerror('luatbls', '_def_tbl_coords function could not parse coordiantes given as "'..str..'" ensure two numbers separated by space are given!', '', true) |
| 210 |
end |
| 211 |
token.set_macro(def..'x', tostring(x)) |
| 212 |
token.set_macro(def..'y', tostring(y)) |
| 213 |
end |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
function luatbls._make_one_toggle(def, v, g) |
| 218 |
tex.sprint(g..'\\providetoggle{'..def..'}') |
| 219 |
tex.sprint(g..'\\toggle'..tostring(v)..'{'..def..'}') |
| 220 |
end |
| 221 |
|
| 222 |
function luatbls._make_toggle_tbl(ind, def, g) |
| 223 |
g = g or '' |
| 224 |
local t, k = luatbls._get_tbl_index(ind) |
| 225 |
local v = luatbls[t][k] |
| 226 |
def = luatbls._make_def_name(t, k, def) |
| 227 |
luatbls._make_one_toggle(def, penlight.hasval(v), g) |
| 228 |
end |
| 229 |
|
| 230 |
function luatbls._make_toggles_tbl(Ind, def, g) |
| 231 |
g = g or '' |
| 232 |
for t, k, v in luatbls._iter_tbls_vals(Ind) do |
| 233 |
if type(v) == 'boolean' then |
| 234 |
local newdef = luatbls._make_def_name(t, k, def) |
| 235 |
luatbls._make_one_toggle(newdef, v, g) |
| 236 |
end |
| 237 |
end |
| 238 |
end |
| 239 |
|
| 240 |
|
| 241 |
function luatbls._make_one_length(def, v, g) |
| 242 |
tex.sprint(g..'\\providenewlength{\\'..def..'}') |
| 243 |
tex.sprint(g..'\\deflength{\\'..def..'}{'..v..'}') |
| 244 |
end |
| 245 |
|
| 246 |
function luatbls._make_length_tbl(ind, def, g) |
| 247 |
g = g or '' |
| 248 |
local t, k = luatbls._get_tbl_index(ind) |
| 249 |
local v = luatbls[t][k] |
| 250 |
if type(v) == 'number' then |
| 251 |
v = tostring(v)..'sp' |
| 252 |
end |
| 253 |
local def = luatbls._make_def_name(t, k, def) |
| 254 |
luatbls._make_one_length(def, v, g) |
| 255 |
end |
| 256 |
|
| 257 |
function luatbls._make_lengths_tbl(Ind, def, g) |
| 258 |
g = g or '' |
| 259 |
for t, k, v in luatbls._iter_tbls_vals(Ind) do |
| 260 |
if type(v) == 'number' then |
| 261 |
v = tostring(v)..'sp' |
| 262 |
end |
| 263 |
if v:istexdim() then |
| 264 |
local newdef = luatbls._make_def_name(t, k, def) |
| 265 |
luatbls._make_one_length(newdef, v, g) |
| 266 |
end |
| 267 |
end |
| 268 |
end |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
function luatbls._for_tbl_prt(k, v,cmd) |
| 273 |
local cmd_new = cmd:gsub(luatbls._tblv, tostring(v)):gsub(luatbls._tblk, tostring(k)):gsub('(\\%w+) ', '%1') -- for some reason a space gets added to \cs, maybe |
| 274 |
luatbls._dprint(cmd_new, '_for_tbl replacement') |
| 275 |
tex.sprint(cmd_new) |
| 276 |
end |
| 277 |
|
| 278 |
function luatbls._for_tbl(Ind, cmd) |
| 279 |
for t, k, v in luatbls._iter_tbls_vals(Ind) do |
| 280 |
luatbls._for_tbl_prt(k, v,cmd) |
| 281 |
end |
| 282 |
end |
| 283 |
|
| 284 |
function luatbls._for_tbl_e(tbl, cmd) |
| 285 |
for k, v in pairs(tbl) do |
| 286 |
luatbls._for_tbl_prt(k, v,cmd) |
| 287 |
end |
| 288 |
end |
| 289 |
|
| 290 |
|
| 291 |
function luatbls._iter_tbls_vals(s) |
| 292 |
if s:find('|') or ((s:find('%.') == nil) and (s:find('/') == nil)) then |
| 293 |
local tbl, seq = luatbls._get_tbl_seq(s) |
| 294 |
local keyval = {} |
| 295 |
for key, val in penlight.seq.tbltrain(luatbls._get_tbl(tbl), seq) do |
| 296 |
-- todo this should check validity of keys for sequences |
| 297 |
keyval[#keyval+1] = {key, val} |
| 298 |
end |
| 299 |
luatbls._dprint(keyval, 'luatbls._iter_tbls_vals is iterating through tbl: '..tbl..' with sequence: '..seq) |
| 300 |
local i = 0 |
| 301 |
return function() |
| 302 |
i = i + 1 |
| 303 |
if i <= #keyval then |
| 304 |
return tbl, keyval[i][1], keyval[i][2] |
| 305 |
end |
| 306 |
end |
| 307 |
else |
| 308 |
local tbl, key = luatbls._get_tbl_index(s) |
| 309 |
local val = luatbls[tbl][key] |
| 310 |
luatbls._dprint(keyval, 'luatbls._iter_tbls_vals is using tbl: '..tbl..' with key: '..key) |
| 311 |
local i = 1 |
| 312 |
return function() |
| 313 |
if i == 1 then |
| 314 |
i = i + 1 -- only return the single value |
| 315 |
return tbl, key, val |
| 316 |
end |
| 317 |
end |
| 318 |
end |
| 319 |
end |
| 320 |
|
| 321 |
function luatbls._make_args(s, key, val) |
| 322 |
local args = {val} |
| 323 |
if s == nil then |
| 324 |
return args |
| 325 |
end |
| 326 |
s = s:split(')')[1] |
| 327 |
args = luatbls._luakeys.parse(s, {naked_as_value=true}) |
| 328 |
for i, v in ipairs(args) do |
| 329 |
if v == luatbls._tblv then |
| 330 |
args[i] = val |
| 331 |
elseif v == luatbls._tblk then |
| 332 |
args[i] = key |
| 333 |
end |
| 334 |
end |
| 335 |
return args |
| 336 |
end |
| 337 |
|
| 338 |
function luatbls._make_func(f, key, val) |
| 339 |
f = f:strip() |
| 340 |
fargs = f:split('(') |
| 341 |
f = fargs[1] |
| 342 |
args = luatbls._make_args(fargs[2], key, val) |
| 343 |
if f:startswith(':') then |
| 344 |
f = f:sub(2,-1) |
| 345 |
if type(val) == 'string' then |
| 346 |
f = string[f] |
| 347 |
elseif type(val) == 'table' then |
| 348 |
f = penlight.tablex[f] |
| 349 |
end |
| 350 |
else |
| 351 |
f = penlight._Gdot(f) |
| 352 |
end |
| 353 |
--luatbls._dprint() -- todo more debug printing |
| 354 |
return f, args |
| 355 |
end |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
function luatbls._make_newtbl(tblind, newtbl) |
| 360 |
if tblind == '' then tblind = luatbls._rec_tbl..'|' end |
| 361 |
if newtbl ~= '' then -- determine if new tbl is needed |
| 362 |
local ogtbl = luatbls._get_tbl_name(tblind) |
| 363 |
luatbls[newtbl] = penlight.tablex.deepcopy(luatbls[ogtbl]) |
| 364 |
luatbls._rec_tbl = newtbl |
| 365 |
tblind, _ = string.gsub(tblind, ogtbl, newtbl, 1) |
| 366 |
end |
| 367 |
return tblind |
| 368 |
end |
| 369 |
|
| 370 |
function luatbls._apply_tbl(tblind, func, newtbl) |
| 371 |
tblind = luatbls._make_newtbl(tblind, newtbl) |
| 372 |
for _, f in pairs(func:split('|')) do |
| 373 |
for tbl, key, val in luatbls._iter_tbls_vals(tblind) do |
| 374 |
local thefunc, args = luatbls._make_func(f, key, val) |
| 375 |
if thefunc == nil then |
| 376 |
penlight.tex.pkgerror('luatbls', 'Tried to apply function: "'..f..'" to tbl value. It yielded no function. Ensure : is used for self-methods') |
| 377 |
end |
| 378 |
luatbls[tbl][key] = thefunc(penlight.utils.unpack(args)) |
| 379 |
end |
| 380 |
end |
| 381 |
end |
| 382 |
|
| 383 |
return luatbls |