| 1 |
---luakeys.lua |
| 2 |
---Copyright 2021-2025 Josef Friedrich |
| 3 |
--- |
| 4 |
---This work may be distributed and/or modified under the |
| 5 |
---conditions of the LaTeX Project Public License, either version 1.3c |
| 6 |
---of this license or (at your option) any later version. |
| 7 |
---The latest version of this license is in |
| 8 |
---http://www.latex-project.org/lppl.txt |
| 9 |
---and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
---version 2008/05/04 or later. |
| 11 |
--- |
| 12 |
---This work has the LPPL maintenance status `maintained'. |
| 13 |
--- |
| 14 |
---The Current Maintainer of this work is Josef Friedrich. |
| 15 |
--- |
| 16 |
---This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex |
| 17 |
---luakeys-debug.sty and luakeys-debug.tex. |
| 18 |
----A key-value parser written with Lpeg. |
| 19 |
--- |
| 20 |
local lpeg = require('lpeg') |
| 21 |
|
| 22 |
if not tex then |
| 23 |
---Dummy functions for the tests. |
| 24 |
tex = { |
| 25 |
sp = function(input) |
| 26 |
return 1234567 |
| 27 |
end, |
| 28 |
} |
| 29 |
|
| 30 |
token = { |
| 31 |
set_macro = function(csname, content, global) |
| 32 |
end, |
| 33 |
} |
| 34 |
end |
| 35 |
|
| 36 |
--- |
| 37 |
local utils = (function() |
| 38 |
--- |
| 39 |
---True if a key string can be notated without square brackets. |
| 40 |
--- |
| 41 |
---@param identifer string |
| 42 |
--- |
| 43 |
---@return boolean |
| 44 |
local function is_lua_identifier(identifer) |
| 45 |
identifer = string.gsub(identifer, '_', '') |
| 46 |
return string.match(identifer, '^%w+$') ~= nil |
| 47 |
end |
| 48 |
|
| 49 |
--- |
| 50 |
---Split a string into lines. |
| 51 |
--- |
| 52 |
---@param content string The content to be split into individual lines. |
| 53 |
--- |
| 54 |
---@return string[] The individual lines as a array of strings. |
| 55 |
local function split_lines(content) |
| 56 |
local lines = {} |
| 57 |
for line in content:gmatch('[^\r\n]+') do |
| 58 |
table.insert(lines, line) |
| 59 |
end |
| 60 |
return lines |
| 61 |
end |
| 62 |
|
| 63 |
--- |
| 64 |
---Merge two tables into the first specified table. |
| 65 |
---The `merge_tables` function copies keys from the `source` table |
| 66 |
---to the `target` table. It returns the target table. |
| 67 |
--- |
| 68 |
---https://stackoverflow.com/a/1283608/10193818 |
| 69 |
--- |
| 70 |
---@param target table # The target table where all values are copied. |
| 71 |
---@param source table # The source table from which all values are copied. |
| 72 |
---@param overwrite? boolean # Overwrite the values in the target table if they are present (default true). |
| 73 |
--- |
| 74 |
---@return table target The modified target table. |
| 75 |
local function merge_tables(target, source, overwrite) |
| 76 |
if overwrite == nil then |
| 77 |
overwrite = true |
| 78 |
end |
| 79 |
for key, value in pairs(source) do |
| 80 |
if type(value) == 'table' and type(target[key] or false) == |
| 81 |
'table' then |
| 82 |
merge_tables(target[key] or {}, source[key] or {}, overwrite) |
| 83 |
elseif (not overwrite and target[key] == nil) or |
| 84 |
(overwrite and target[key] ~= value) then |
| 85 |
target[key] = value |
| 86 |
end |
| 87 |
end |
| 88 |
return target |
| 89 |
end |
| 90 |
|
| 91 |
--- |
| 92 |
---Clone a table, i.e. make a deep copy of the source table. |
| 93 |
--- |
| 94 |
---http://lua-users.org/wiki/CopyTable |
| 95 |
--- |
| 96 |
---@param source table # The source table to be cloned. |
| 97 |
--- |
| 98 |
---@return table # A deep copy of the source table. |
| 99 |
local function clone_table(source) |
| 100 |
local copy |
| 101 |
if type(source) == 'table' then |
| 102 |
copy = {} |
| 103 |
for orig_key, orig_value in next, source, nil do |
| 104 |
copy[clone_table(orig_key)] = clone_table(orig_value) |
| 105 |
end |
| 106 |
setmetatable(copy, clone_table(getmetatable(source))) |
| 107 |
else ---number, string, boolean, etc |
| 108 |
copy = source |
| 109 |
end |
| 110 |
return copy |
| 111 |
end |
| 112 |
|
| 113 |
--- |
| 114 |
---Remove an element from a table. |
| 115 |
--- |
| 116 |
---@param source table # The source table. |
| 117 |
---@param value any # The value to be removed from the table. |
| 118 |
--- |
| 119 |
---@return any|nil # If the value was found, then this value, otherwise nil. |
| 120 |
local function remove_from_table(source, value) |
| 121 |
for index, v in pairs(source) do |
| 122 |
if value == v then |
| 123 |
source[index] = nil |
| 124 |
return value |
| 125 |
end |
| 126 |
end |
| 127 |
end |
| 128 |
|
| 129 |
--- |
| 130 |
---Return the keys of a table as a sorted list (array like table). |
| 131 |
--- |
| 132 |
---@param source table # The source table. |
| 133 |
--- |
| 134 |
---@return table # An array table with the sorted key names. |
| 135 |
local function get_table_keys(source) |
| 136 |
local keys = {} |
| 137 |
for key in pairs(source) do |
| 138 |
table.insert(keys, key) |
| 139 |
end |
| 140 |
table.sort(keys, function(a, b) |
| 141 |
a = tostring(a) |
| 142 |
b = tostring(b) |
| 143 |
return a < b |
| 144 |
end) |
| 145 |
return keys |
| 146 |
end |
| 147 |
|
| 148 |
--- |
| 149 |
---Get the size of a table `{ one = 'one', 'two', 'three' }` = 3. |
| 150 |
--- |
| 151 |
---@param value any # A table or any input. |
| 152 |
--- |
| 153 |
---@return number # The size of the array like table. 0 if the input is no table or the table is empty. |
| 154 |
local function get_table_size(value) |
| 155 |
local count = 0 |
| 156 |
if type(value) == 'table' then |
| 157 |
for _ in pairs(value) do |
| 158 |
count = count + 1 |
| 159 |
end |
| 160 |
end |
| 161 |
return count |
| 162 |
end |
| 163 |
|
| 164 |
--- |
| 165 |
---Get the size of an array like table, for example `{ 'one', 'two', |
| 166 |
---'three' }` = 3. |
| 167 |
--- |
| 168 |
---@param value any # A table or any input. |
| 169 |
--- |
| 170 |
---@return number # The size of the array like table. 0 if the input is no table or the table is empty. |
| 171 |
local function get_array_size(value) |
| 172 |
local count = 0 |
| 173 |
if type(value) == 'table' then |
| 174 |
for _ in ipairs(value) do |
| 175 |
count = count + 1 |
| 176 |
end |
| 177 |
end |
| 178 |
return count |
| 179 |
end |
| 180 |
|
| 181 |
--- |
| 182 |
---Print a formatted string. |
| 183 |
--- |
| 184 |
---* `%d` or `%i`: Signed decimal integer |
| 185 |
---* `%u`: Unsigned decimal integer |
| 186 |
---* `%o`: Unsigned octal |
| 187 |
---* `%x`: Unsigned hexadecimal integer |
| 188 |
---* `%X`: Unsigned hexadecimal integer (uppercase) |
| 189 |
---* `%f`: Decimal floating point, lowercase |
| 190 |
---* `%e`: Scientific notation (mantissa/exponent), lowercase |
| 191 |
---* `%E`: Scientific notation (mantissa/exponent), uppercase |
| 192 |
---* `%g`: Use the shortest representation: %e or %f |
| 193 |
---* `%G`: Use the shortest representation: %E or %F |
| 194 |
---* `%a`: Hexadecimal floating point, lowercase |
| 195 |
---* `%A`: Hexadecimal floating point, uppercase |
| 196 |
---* `%c`: Character |
| 197 |
---* `%s`: String of characters |
| 198 |
---* `%p`: Pointer address b8000000 |
| 199 |
---* `%%`: A `%` followed by another `%` character will write a single `%` to the stream. |
| 200 |
---* `%q`: formats `booleans`, `nil`, `numbers`, and `strings` in a way that the result is a valid constant in Lua source code. |
| 201 |
--- |
| 202 |
---http://www.lua.org/source/5.3/lstrlib.c.html#str_format |
| 203 |
--- |
| 204 |
---@param format string # A string in the `printf` format |
| 205 |
---@param ... any # A sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string. |
| 206 |
local function tex_printf(format, ...) |
| 207 |
tex.print(string.format(format, ...)) |
| 208 |
end |
| 209 |
|
| 210 |
--- |
| 211 |
---Throw a single error message. |
| 212 |
--- |
| 213 |
---@param message string |
| 214 |
---@param help? table |
| 215 |
local function throw_error_message(message, help) |
| 216 |
if type(tex.error) == 'function' then |
| 217 |
tex.error(message, help) |
| 218 |
else |
| 219 |
error(message) |
| 220 |
end |
| 221 |
end |
| 222 |
|
| 223 |
--- |
| 224 |
---Throw an error by specifying an error code. |
| 225 |
--- |
| 226 |
---@param error_messages table |
| 227 |
---@param error_code string |
| 228 |
---@param args? table |
| 229 |
local function throw_error_code(error_messages, |
| 230 |
error_code, |
| 231 |
args) |
| 232 |
local template = error_messages[error_code] |
| 233 |
|
| 234 |
--- |
| 235 |
---@param message string |
| 236 |
---@param a table |
| 237 |
--- |
| 238 |
---@return string |
| 239 |
local function replace_args(message, a) |
| 240 |
for key, value in pairs(a) do |
| 241 |
if type(value) == 'table' then |
| 242 |
value = table.concat(value, ', ') |
| 243 |
end |
| 244 |
message = message:gsub('@' .. key, |
| 245 |
'“' .. tostring(value) .. '”') |
| 246 |
end |
| 247 |
return message |
| 248 |
end |
| 249 |
|
| 250 |
--- |
| 251 |
---@param list table |
| 252 |
---@param a table |
| 253 |
--- |
| 254 |
---@return table |
| 255 |
local function replace_args_in_list(list, a) |
| 256 |
for index, message in ipairs(list) do |
| 257 |
list[index] = replace_args(message, a) |
| 258 |
end |
| 259 |
return list |
| 260 |
end |
| 261 |
|
| 262 |
--- |
| 263 |
---@type string |
| 264 |
local message |
| 265 |
---@type table |
| 266 |
local help = {} |
| 267 |
|
| 268 |
if type(template) == 'table' then |
| 269 |
message = template[1] |
| 270 |
if args ~= nil then |
| 271 |
help = replace_args_in_list(template[2], args) |
| 272 |
else |
| 273 |
help = template[2] |
| 274 |
end |
| 275 |
else |
| 276 |
message = template |
| 277 |
end |
| 278 |
|
| 279 |
if args ~= nil then |
| 280 |
message = replace_args(message, args) |
| 281 |
end |
| 282 |
|
| 283 |
message = 'luakeys error [' .. error_code .. ']: ' .. message |
| 284 |
|
| 285 |
for _, help_message in ipairs({ |
| 286 |
'You may be able to find more help in the documentation:', |
| 287 |
'http://mirrors.ctan.org/macros/luatex/generic/luakeys/luakeys-doc.pdf', |
| 288 |
'Or ask a question in the issue tracker on Github:', |
| 289 |
'https://github.com/Josef-Friedrich/luakeys/issues', |
| 290 |
}) do |
| 291 |
table.insert(help, help_message) |
| 292 |
end |
| 293 |
|
| 294 |
throw_error_message(message, help) |
| 295 |
end |
| 296 |
|
| 297 |
local function visit_tree(tree, callback_func) |
| 298 |
if type(tree) ~= 'table' then |
| 299 |
throw_error_message( |
| 300 |
'Parameter “tree” has to be a table, got: ' .. |
| 301 |
tostring(tree)) |
| 302 |
end |
| 303 |
local function visit_tree_recursive(tree, |
| 304 |
current, |
| 305 |
result, |
| 306 |
depth, |
| 307 |
callback_func) |
| 308 |
for key, value in pairs(current) do |
| 309 |
if type(value) == 'table' then |
| 310 |
value = visit_tree_recursive(tree, value, {}, depth + 1, |
| 311 |
callback_func) |
| 312 |
end |
| 313 |
|
| 314 |
key, value = callback_func(key, value, depth, current, tree) |
| 315 |
|
| 316 |
if key ~= nil and value ~= nil then |
| 317 |
result[key] = value |
| 318 |
end |
| 319 |
end |
| 320 |
if next(result) ~= nil then |
| 321 |
return result |
| 322 |
end |
| 323 |
end |
| 324 |
|
| 325 |
local result = |
| 326 |
visit_tree_recursive(tree, tree, {}, 1, callback_func) |
| 327 |
|
| 328 |
if result == nil then |
| 329 |
return {} |
| 330 |
end |
| 331 |
return result |
| 332 |
end |
| 333 |
|
| 334 |
---@alias ColorName 'black'|'red'|'green'|'yellow'|'blue'|'magenta'|'cyan'|'white'|'reset' |
| 335 |
---@alias ColorMode 'bright'|'dim' |
| 336 |
|
| 337 |
--- |
| 338 |
---Small library to surround strings with ANSI color codes. |
| 339 |
-- |
| 340 |
---[SGR (Select Graphic Rendition) Parameters](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters) |
| 341 |
--- |
| 342 |
---__attributes__ |
| 343 |
--- |
| 344 |
---| color |code| |
| 345 |
---|------------|----| |
| 346 |
---| reset | 0 | |
| 347 |
---| clear | 0 | |
| 348 |
---| bright | 1 | |
| 349 |
---| dim | 2 | |
| 350 |
---| underscore | 4 | |
| 351 |
---| blink | 5 | |
| 352 |
---| reverse | 7 | |
| 353 |
---| hidden | 8 | |
| 354 |
--- |
| 355 |
---__foreground__ |
| 356 |
--- |
| 357 |
---| color |code| |
| 358 |
---|------------|----| |
| 359 |
---| black | 30 | |
| 360 |
---| red | 31 | |
| 361 |
---| green | 32 | |
| 362 |
---| yellow | 33 | |
| 363 |
---| blue | 34 | |
| 364 |
---| magenta | 35 | |
| 365 |
---| cyan | 36 | |
| 366 |
---| white | 37 | |
| 367 |
--- |
| 368 |
---__background__ |
| 369 |
--- |
| 370 |
---| color |code| |
| 371 |
---|------------|----| |
| 372 |
---| onblack | 40 | |
| 373 |
---| onred | 41 | |
| 374 |
---| ongreen | 42 | |
| 375 |
---| onyellow | 43 | |
| 376 |
---| onblue | 44 | |
| 377 |
---| onmagenta | 45 | |
| 378 |
---| oncyan | 46 | |
| 379 |
---| onwhite | 47 | |
| 380 |
--- |
| 381 |
---## Other ansi color modules |
| 382 |
--- |
| 383 |
---* ansicolors: |
| 384 |
--- [Github 143⋆](https://github.com/kikito/ansicolors.lua), |
| 385 |
--- [LuaRocks 945k](https://luarocks.org/modules/kikito/ansicolors) |
| 386 |
---* Lunacolors: [Github 12⋆](https://github.com/Rosettea/Lunacolors) |
| 387 |
local ansi_color = (function() |
| 388 |
--- |
| 389 |
---@param code integer |
| 390 |
--- |
| 391 |
---@return string |
| 392 |
local function format_color_code(code) |
| 393 |
return string.char(27) .. '[' .. tostring(code) .. 'm' |
| 394 |
end |
| 395 |
|
| 396 |
--- |
| 397 |
---@private |
| 398 |
--- |
| 399 |
---@param color ColorName # A color name. |
| 400 |
---@param mode? ColorMode |
| 401 |
---@param background? boolean # Colorize the background not the text. |
| 402 |
--- |
| 403 |
---@return string |
| 404 |
local function get_color_code(color, mode, background) |
| 405 |
local output = '' |
| 406 |
local code |
| 407 |
|
| 408 |
if mode == 'bright' then |
| 409 |
output = format_color_code(1) |
| 410 |
elseif mode == 'dim' then |
| 411 |
output = format_color_code(2) |
| 412 |
end |
| 413 |
|
| 414 |
if not background then |
| 415 |
if color == 'reset' then |
| 416 |
code = 0 |
| 417 |
elseif color == 'black' then |
| 418 |
code = 30 |
| 419 |
elseif color == 'red' then |
| 420 |
code = 31 |
| 421 |
elseif color == 'green' then |
| 422 |
code = 32 |
| 423 |
elseif color == 'yellow' then |
| 424 |
code = 33 |
| 425 |
elseif color == 'blue' then |
| 426 |
code = 34 |
| 427 |
elseif color == 'magenta' then |
| 428 |
code = 35 |
| 429 |
elseif color == 'cyan' then |
| 430 |
code = 36 |
| 431 |
elseif color == 'white' then |
| 432 |
code = 37 |
| 433 |
else |
| 434 |
code = 37 |
| 435 |
end |
| 436 |
else |
| 437 |
if color == 'black' then |
| 438 |
code = 40 |
| 439 |
elseif color == 'red' then |
| 440 |
code = 41 |
| 441 |
elseif color == 'green' then |
| 442 |
code = 42 |
| 443 |
elseif color == 'yellow' then |
| 444 |
code = 43 |
| 445 |
elseif color == 'blue' then |
| 446 |
code = 44 |
| 447 |
elseif color == 'magenta' then |
| 448 |
code = 45 |
| 449 |
elseif color == 'cyan' then |
| 450 |
code = 46 |
| 451 |
elseif color == 'white' then |
| 452 |
code = 47 |
| 453 |
else |
| 454 |
code = 40 |
| 455 |
end |
| 456 |
end |
| 457 |
return output .. format_color_code(code) |
| 458 |
end |
| 459 |
|
| 460 |
--- |
| 461 |
---@param text any |
| 462 |
---@param color ColorName # A color name. |
| 463 |
---@param mode? ColorMode |
| 464 |
---@param background? boolean # Colorize the background not the text. |
| 465 |
--- |
| 466 |
---@return string |
| 467 |
local function colorize(text, color, mode, background) |
| 468 |
return string.format('%s%s%s', |
| 469 |
get_color_code(color, mode, background), text, |
| 470 |
get_color_code('reset')) |
| 471 |
end |
| 472 |
|
| 473 |
return { |
| 474 |
colorize = colorize, |
| 475 |
|
| 476 |
--- |
| 477 |
---@param text any |
| 478 |
--- |
| 479 |
---@return string |
| 480 |
red = function(text) |
| 481 |
return colorize(text, 'red') |
| 482 |
end, |
| 483 |
|
| 484 |
--- |
| 485 |
---@param text any |
| 486 |
--- |
| 487 |
---@return string |
| 488 |
green = function(text) |
| 489 |
return colorize(text, 'green') |
| 490 |
end, |
| 491 |
|
| 492 |
---@return string |
| 493 |
yellow = function(text) |
| 494 |
return colorize(text, 'yellow') |
| 495 |
end, |
| 496 |
|
| 497 |
--- |
| 498 |
---@param text any |
| 499 |
--- |
| 500 |
---@return string |
| 501 |
blue = function(text) |
| 502 |
return colorize(text, 'blue') |
| 503 |
end, |
| 504 |
|
| 505 |
--- |
| 506 |
---@param text any |
| 507 |
--- |
| 508 |
---@return string |
| 509 |
magenta = function(text) |
| 510 |
return colorize(text, 'magenta') |
| 511 |
end, |
| 512 |
|
| 513 |
--- |
| 514 |
---@param text any |
| 515 |
--- |
| 516 |
---@return string |
| 517 |
cyan = function(text) |
| 518 |
return colorize(text, 'cyan') |
| 519 |
end, |
| 520 |
} |
| 521 |
end)() |
| 522 |
|
| 523 |
--- |
| 524 |
---A small logging library. |
| 525 |
--- |
| 526 |
---Log levels: |
| 527 |
--- |
| 528 |
---* 0: silent |
| 529 |
---* 1: error (red) |
| 530 |
---* 2: warn (yellow) |
| 531 |
---* 3: info (green) |
| 532 |
---* 4: verbose (blue) |
| 533 |
---* 5: debug (magenta) |
| 534 |
--- |
| 535 |
---## Other logging libraries: |
| 536 |
--- |
| 537 |
---* lualogging: |
| 538 |
--- [Github 59⋆](https://github.com/lunarmodules/lualogging), |
| 539 |
--- [LuaRocks 1000k](https://luarocks.org/modules/tieske/lualogging) |
| 540 |
---* lua-logger: |
| 541 |
--- [LuaRocks 0.7k](https://luarocks.org/modules/alissasquared/lua-logger) |
| 542 |
---* log.lua: |
| 543 |
--- [Github 331⋆](https://github.com/rxi/log.lua), |
| 544 |
--- [LuaRocks 0.3k](https://luarocks.org/modules/stephencathcart/log.lua) |
| 545 |
--- |
| 546 |
local log = (function() |
| 547 |
---@private |
| 548 |
local opts = { level = 0 } |
| 549 |
|
| 550 |
local function colorize_not(s) |
| 551 |
return s |
| 552 |
end |
| 553 |
|
| 554 |
local colorize = colorize_not |
| 555 |
|
| 556 |
---@private |
| 557 |
local function print_message(message, ...) |
| 558 |
local args = { ... } |
| 559 |
for index, value in ipairs(args) do |
| 560 |
args[index] = colorize(value) |
| 561 |
end |
| 562 |
print(string.format(message, table.unpack(args))) |
| 563 |
end |
| 564 |
|
| 565 |
--- |
| 566 |
---Set the log level. |
| 567 |
--- |
| 568 |
---@param level 0|'silent'|1|'error'|2|'warn'|3|'info'|4|'verbose'|5|'debug' |
| 569 |
local function set_log_level(level) |
| 570 |
if type(level) == 'string' then |
| 571 |
if level == 'silent' then |
| 572 |
opts.level = 0 |
| 573 |
elseif level == 'error' then |
| 574 |
opts.level = 1 |
| 575 |
elseif level == 'warn' then |
| 576 |
opts.level = 2 |
| 577 |
elseif level == 'info' then |
| 578 |
opts.level = 3 |
| 579 |
elseif level == 'verbose' then |
| 580 |
opts.level = 4 |
| 581 |
elseif level == 'debug' then |
| 582 |
opts.level = 5 |
| 583 |
else |
| 584 |
throw_error_message(string.format('Unknown log level: %s', |
| 585 |
level)) |
| 586 |
end |
| 587 |
else |
| 588 |
if level > 5 or level < 0 then |
| 589 |
throw_error_message(string.format( |
| 590 |
'Log level out of range 0-5: %s', level)) |
| 591 |
end |
| 592 |
opts.level = level |
| 593 |
end |
| 594 |
end |
| 595 |
|
| 596 |
--- |
| 597 |
---@return integer |
| 598 |
local function get_log_level() |
| 599 |
return opts.level |
| 600 |
end |
| 601 |
|
| 602 |
--- |
| 603 |
---Log at level 1 (error). |
| 604 |
--- |
| 605 |
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug). |
| 606 |
--- |
| 607 |
---@param message string |
| 608 |
---@param ... any |
| 609 |
local function error(message, ...) |
| 610 |
if opts.level >= 1 then |
| 611 |
colorize = ansi_color.red |
| 612 |
print_message(message, ...) |
| 613 |
colorize = colorize_not |
| 614 |
end |
| 615 |
end |
| 616 |
|
| 617 |
--- |
| 618 |
---Log at level 2 (warn). |
| 619 |
--- |
| 620 |
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug). |
| 621 |
--- |
| 622 |
---@param message string |
| 623 |
---@param ... any |
| 624 |
local function warn(message, ...) |
| 625 |
if opts.level >= 2 then |
| 626 |
colorize = ansi_color.yellow |
| 627 |
print_message(message, ...) |
| 628 |
colorize = colorize_not |
| 629 |
end |
| 630 |
end |
| 631 |
|
| 632 |
--- |
| 633 |
---Log at level 3 (info). |
| 634 |
--- |
| 635 |
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug). |
| 636 |
--- |
| 637 |
---@param message string |
| 638 |
---@param ... any |
| 639 |
local function info(message, ...) |
| 640 |
if opts.level >= 3 then |
| 641 |
colorize = ansi_color.green |
| 642 |
print_message(message, ...) |
| 643 |
colorize = colorize_not |
| 644 |
end |
| 645 |
end |
| 646 |
|
| 647 |
--- |
| 648 |
---Log at level 4 (verbose). |
| 649 |
--- |
| 650 |
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug). |
| 651 |
--- |
| 652 |
---@param message string |
| 653 |
---@param ... any |
| 654 |
local function verbose(message, ...) |
| 655 |
if opts.level >= 4 then |
| 656 |
colorize = ansi_color.blue |
| 657 |
print_message(message, ...) |
| 658 |
colorize = colorize_not |
| 659 |
end |
| 660 |
end |
| 661 |
|
| 662 |
--- |
| 663 |
---Log at level 5 (debug). |
| 664 |
--- |
| 665 |
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug). |
| 666 |
--- |
| 667 |
---@param message string |
| 668 |
---@param ... any |
| 669 |
local function debug(message, ...) |
| 670 |
if opts.level >= 5 then |
| 671 |
colorize = ansi_color.magenta |
| 672 |
print_message(message, ...) |
| 673 |
colorize = colorize_not |
| 674 |
end |
| 675 |
end |
| 676 |
|
| 677 |
return { |
| 678 |
set = set_log_level, |
| 679 |
get = get_log_level, |
| 680 |
error = error, |
| 681 |
warn = warn, |
| 682 |
info = info, |
| 683 |
verbose = verbose, |
| 684 |
debug = debug, |
| 685 |
} |
| 686 |
end)() |
| 687 |
|
| 688 |
return { |
| 689 |
is_lua_identifier = is_lua_identifier, |
| 690 |
split_lines = split_lines, |
| 691 |
merge_tables = merge_tables, |
| 692 |
clone_table = clone_table, |
| 693 |
remove_from_table = remove_from_table, |
| 694 |
get_table_keys = get_table_keys, |
| 695 |
get_table_size = get_table_size, |
| 696 |
get_array_size = get_array_size, |
| 697 |
visit_tree = visit_tree, |
| 698 |
tex_printf = tex_printf, |
| 699 |
throw_error_message = throw_error_message, |
| 700 |
throw_error_code = throw_error_code, |
| 701 |
ansi_color = ansi_color, |
| 702 |
log = log, |
| 703 |
} |
| 704 |
end)() |
| 705 |
|
| 706 |
--- |
| 707 |
---Convert back to strings |
| 708 |
---@section |
| 709 |
local visualizers = (function() |
| 710 |
--- |
| 711 |
---A collection of options to configure the `render` function. |
| 712 |
--- |
| 713 |
---This collection combines high level and low level options. |
| 714 |
---@class RenderConfiguration |
| 715 |
--- |
| 716 |
---High level options |
| 717 |
---@field style? 'tex'|'lua' # Render the input as a `lua` table or in the `tex` style, default `tex`. |
| 718 |
---@field inline? boolean # Render the input on one line without line breaks, default `true`. |
| 719 |
--- |
| 720 |
---Low level options |
| 721 |
---@field line_break? string # The character for a line break, for example, use `\n` for terminal output or `\par` for TeX rendering, default ``. |
| 722 |
---@field begin_table? string # The starting delimiter for a table, default `{`. |
| 723 |
---@field end_table? string # The final delimiter for a table, default `}`. |
| 724 |
---@field table_delimiters_first_depth? boolean # Whether table delimiters of the 1st level should be displayed. Instead of `{ key1,key2={value2} }` render `key1,key2={value2}`, default `false`. |
| 725 |
---@field indent? string # Characters used for indentation, default ``. |
| 726 |
---@field begin_key? string # The starting delimiter for a key, default `[`. |
| 727 |
---@field end_key? string # The final delimiter for a key, default `]`. |
| 728 |
---@field assignment? string # The symbol for the assignment operator, default `=`. |
| 729 |
---@field separator? string # The separator for the individual table elements, default `,`. |
| 730 |
---@field separator_last? boolean # Append a separator after the last element, default `false`. |
| 731 |
---@field quotation? string # The symbol that delimits a string, default `'`. |
| 732 |
---@field format_key? fun(key: unknown, conf: RenderConfiguration): string # A function that formats the key. |
| 733 |
---@field format_value? fun(value: unknown, conf: RenderConfiguration): string # A function that formats the value. |
| 734 |
|
| 735 |
--- |
| 736 |
---Render or serialize a Lua value into a string. |
| 737 |
--- |
| 738 |
---This function can be used to reverse the function `parse(kv_string)`. It takes a Lua value and converts this value |
| 739 |
---into a string. The keys of the resulting serialized table are sorted alpabetically. |
| 740 |
--- |
| 741 |
---Source: https://stackoverflow.com/a/54593224/10193818 |
| 742 |
--- |
| 743 |
---@param value unknown # A lua value to render. |
| 744 |
---@param config? RenderConfiguration # A collection of options to configure the `render` function. |
| 745 |
--- |
| 746 |
---@return string |
| 747 |
local function render(value, config) |
| 748 |
if config == nil then |
| 749 |
config = {} |
| 750 |
end |
| 751 |
|
| 752 |
---style=tex inline=true |
| 753 |
---@type RenderConfiguration |
| 754 |
local default_conf = { |
| 755 |
style = 'tex', |
| 756 |
inline = true, |
| 757 |
--- Low level |
| 758 |
line_break = '', |
| 759 |
begin_table = '{', |
| 760 |
end_table = '}', |
| 761 |
table_delimiters_first_depth = false, |
| 762 |
indent = '', |
| 763 |
begin_key = '[', |
| 764 |
end_key = ']', |
| 765 |
assignment = '=', |
| 766 |
separator = ',', |
| 767 |
separator_last = false, |
| 768 |
quotation = '"', |
| 769 |
format_key = function(key, conf) |
| 770 |
key = tostring(key) |
| 771 |
if string.find(key, ',') then |
| 772 |
return conf.quotation .. key .. conf.quotation |
| 773 |
end |
| 774 |
return key |
| 775 |
end, |
| 776 |
format_value = function(value, conf) |
| 777 |
value = tostring(value) |
| 778 |
if string.find(value, ',') then |
| 779 |
return conf.quotation .. value .. conf.quotation |
| 780 |
end |
| 781 |
return value |
| 782 |
end, |
| 783 |
} |
| 784 |
|
| 785 |
---@type RenderConfiguration |
| 786 |
local conf = utils.clone_table(config) |
| 787 |
utils.merge_tables(conf, default_conf, false) |
| 788 |
|
| 789 |
if conf.style == 'lua' then |
| 790 |
-- lua |
| 791 |
conf.quotation = '\'' |
| 792 |
conf.format_key = function(key, conf) |
| 793 |
if type(key) == 'string' and utils.is_lua_identifier(key) then |
| 794 |
return key |
| 795 |
end |
| 796 |
|
| 797 |
if type(key) == 'string' then |
| 798 |
key = conf.quotation .. tostring(key) .. conf.quotation |
| 799 |
end |
| 800 |
return conf.begin_key .. key .. conf.end_key |
| 801 |
end |
| 802 |
|
| 803 |
conf.format_value = function(value, opts) |
| 804 |
if type(value) == 'string' then |
| 805 |
value = opts.quotation .. value .. opts.quotation |
| 806 |
end |
| 807 |
return tostring(value) |
| 808 |
end |
| 809 |
conf.table_delimiters_first_depth = true |
| 810 |
end |
| 811 |
|
| 812 |
if not conf.inline then |
| 813 |
-- multiline |
| 814 |
conf.assignment = ' = ' |
| 815 |
conf.line_break = '\n' |
| 816 |
conf.indent = ' ' |
| 817 |
end |
| 818 |
|
| 819 |
-- Override the merged options with lower-level options from the function |
| 820 |
-- argument so that the entered options are also taken into account and are |
| 821 |
-- not overridden by the higher-level options |
| 822 |
|
| 823 |
local low_level_options = { |
| 824 |
'line_break', |
| 825 |
'begin_table', |
| 826 |
'end_table', |
| 827 |
'table_delimiters_first_depth', |
| 828 |
'indent', |
| 829 |
'begin_key', |
| 830 |
'end_key', |
| 831 |
'assignment', |
| 832 |
'separator', |
| 833 |
'separator_last', |
| 834 |
'quotation', |
| 835 |
'format_key', |
| 836 |
'format_value', |
| 837 |
} |
| 838 |
|
| 839 |
for _, option in ipairs(low_level_options) do |
| 840 |
if config[option] ~= nil then |
| 841 |
conf[option] = config[option] |
| 842 |
end |
| 843 |
end |
| 844 |
|
| 845 |
--- |
| 846 |
---@param input unknown |
| 847 |
---@param depth integer |
| 848 |
--- |
| 849 |
---@return string |
| 850 |
local function stringify(input, depth) |
| 851 |
local output = {} |
| 852 |
depth = depth or 0 |
| 853 |
|
| 854 |
local function add(depth, text) |
| 855 |
table.insert(output, string.rep(conf.indent, depth) .. text) |
| 856 |
end |
| 857 |
|
| 858 |
if type(input) ~= 'table' then |
| 859 |
return tostring(input) |
| 860 |
end |
| 861 |
|
| 862 |
local keys = utils.get_table_keys(input) |
| 863 |
local element_sum = utils.get_table_size(keys) |
| 864 |
local consecutive_numbers_counter = 1 |
| 865 |
local element_counter = 0 |
| 866 |
for _, key in pairs(keys) do |
| 867 |
element_counter = element_counter + 1 |
| 868 |
local value = input[key] |
| 869 |
if (key and type(key) == 'number' or type(key) == 'string') then |
| 870 |
local separator = conf.separator |
| 871 |
if not conf.separator_last and element_sum == element_counter then |
| 872 |
separator = '' |
| 873 |
end |
| 874 |
-- is array ... consecutive integers ... |
| 875 |
if type(key) == 'number' and consecutive_numbers_counter == |
| 876 |
key then |
| 877 |
consecutive_numbers_counter = |
| 878 |
consecutive_numbers_counter + 1 |
| 879 |
key = '' |
| 880 |
else |
| 881 |
key = conf.format_key(key, conf) |
| 882 |
key = key .. conf.assignment |
| 883 |
end |
| 884 |
|
| 885 |
if (type(value) == 'table') then |
| 886 |
if (next(value)) then |
| 887 |
add(depth, key .. conf.begin_table) |
| 888 |
add(0, stringify(value, depth + 1)) |
| 889 |
add(depth, conf.end_table .. separator); |
| 890 |
else |
| 891 |
add(depth, key .. conf.begin_table .. conf.end_table .. |
| 892 |
separator) |
| 893 |
end |
| 894 |
else |
| 895 |
value = conf.format_value(value, conf) |
| 896 |
add(depth, key .. value .. separator) |
| 897 |
end |
| 898 |
end |
| 899 |
end |
| 900 |
|
| 901 |
return table.concat(output, conf.line_break) |
| 902 |
end |
| 903 |
|
| 904 |
local begin_table = '' |
| 905 |
local end_table = '' |
| 906 |
|
| 907 |
if conf.table_delimiters_first_depth then |
| 908 |
begin_table = conf.begin_table |
| 909 |
end_table = conf.end_table |
| 910 |
end |
| 911 |
|
| 912 |
return begin_table .. conf.line_break .. stringify(value, 1) .. |
| 913 |
conf.line_break .. end_table |
| 914 |
end |
| 915 |
|
| 916 |
--- |
| 917 |
---Pretty print a Lua value to standard output (stdout). |
| 918 |
--- |
| 919 |
---It is a utility function that can be used to |
| 920 |
---debug and inspect the resulting Lua table of the function |
| 921 |
---`parse`. You have to compile your TeX document in a console to |
| 922 |
---see the terminal output. |
| 923 |
--- |
| 924 |
---@param value unknown # A value to be printed to standard output for debugging purposes. |
| 925 |
---@param config? RenderConfiguration # A collection of options to configure the `render` function. |
| 926 |
local function debug(value, config) |
| 927 |
if not config then |
| 928 |
config = { inline = false, table_delimiters_first_depth = true } |
| 929 |
end |
| 930 |
print('\n' .. render(value, config)) |
| 931 |
end |
| 932 |
|
| 933 |
return { render = render, debug = debug } |
| 934 |
end)() |
| 935 |
|
| 936 |
---@alias FormatKeyOperation 'lower'|'snake'|'upper' |
| 937 |
|
| 938 |
---@class OptionCollection |
| 939 |
---@field accumulated_result? table |
| 940 |
---@field assignment_operator? string # default `=` |
| 941 |
---@field convert_dimensions? boolean # default `false` |
| 942 |
---@field debug? boolean # default `false` |
| 943 |
---@field default? boolean # default `true` |
| 944 |
---@field defaults? table |
| 945 |
---@field defs? DefinitionCollection |
| 946 |
---@field false_aliases? table default `{ 'false', 'FALSE', 'False' }`, |
| 947 |
---@field format_keys? boolean|(FormatKeyOperation)[] # default `false`, |
| 948 |
---@field group_begin? string default `{`, |
| 949 |
---@field group_end? string default `}`, |
| 950 |
---@field hooks? HookCollection |
| 951 |
---@field invert_flag? string default `!` |
| 952 |
---@field list_separator? string default `,` |
| 953 |
---@field naked_as_value? boolean # default `false` |
| 954 |
---@field no_error? boolean # default `false` |
| 955 |
---@field quotation_begin? string `"` |
| 956 |
---@field quotation_end? string `"` |
| 957 |
---@field true_aliases? table `{ 'true', 'TRUE', 'True' }` |
| 958 |
---@field unpack? boolean # default `true` |
| 959 |
|
| 960 |
---@alias KeysHook fun(key: string, value: any, depth: integer, current: table, result: table): string, any |
| 961 |
---@alias ResultHook fun(result: table): nil |
| 962 |
|
| 963 |
---@class HookCollection |
| 964 |
---@field kv_string? fun(kv_string: string): string |
| 965 |
---@field keys_before_opts? KeysHook |
| 966 |
---@field result_before_opts? ResultHook |
| 967 |
---@field keys_before_def? KeysHook |
| 968 |
---@field result_before_def? ResultHook |
| 969 |
---@field keys? KeysHook |
| 970 |
---@field result? ResultHook |
| 971 |
|
| 972 |
---@alias ProcessFunction fun(value: any, input: table, result: table, unknown: table): any |
| 973 |
|
| 974 |
---@alias PickDataType 'string'|'number'|'dimension'|'integer'|'boolean'|'any' |
| 975 |
|
| 976 |
--- |
| 977 |
---A key-value pair definition |
| 978 |
---@class Definition |
| 979 |
---@field alias? string|table |
| 980 |
---@field always_present? boolean |
| 981 |
---@field choices? table |
| 982 |
---@field data_type? 'boolean'|'dimension'|'integer'|'number'|'string'|'list' |
| 983 |
---@field default? any |
| 984 |
---@field description? string |
| 985 |
---@field exclusive_group? string |
| 986 |
---@field l3_tl_set? string |
| 987 |
---@field macro? string |
| 988 |
---@field match? string |
| 989 |
---@field name? string |
| 990 |
---@field opposite_keys? table |
| 991 |
---@field pick? PickDataType|PickDataType[]|false |
| 992 |
---@field process? ProcessFunction |
| 993 |
---@field required? boolean |
| 994 |
---@field sub_keys? table<string, Definition> |
| 995 |
|
| 996 |
--- |
| 997 |
---A collection of key-value pair definitions. |
| 998 |
---@alias DefinitionCollection table<string|number, Definition> |
| 999 |
|
| 1000 |
local namespace = { |
| 1001 |
opts = { |
| 1002 |
accumulated_result = false, |
| 1003 |
assignment_operator = '=', |
| 1004 |
convert_dimensions = false, |
| 1005 |
debug = false, |
| 1006 |
default = true, |
| 1007 |
defaults = false, |
| 1008 |
defs = false, |
| 1009 |
false_aliases = { 'false', 'FALSE', 'False' }, |
| 1010 |
format_keys = false, |
| 1011 |
group_begin = '{', |
| 1012 |
group_end = '}', |
| 1013 |
hooks = {}, |
| 1014 |
invert_flag = '!', |
| 1015 |
list_separator = ',', |
| 1016 |
naked_as_value = false, |
| 1017 |
no_error = false, |
| 1018 |
quotation_begin = '"', |
| 1019 |
quotation_end = '"', |
| 1020 |
true_aliases = { 'true', 'TRUE', 'True' }, |
| 1021 |
unpack = true, |
| 1022 |
}, |
| 1023 |
|
| 1024 |
hooks = { |
| 1025 |
kv_string = true, |
| 1026 |
keys_before_opts = true, |
| 1027 |
result_before_opts = true, |
| 1028 |
keys_before_def = true, |
| 1029 |
result_before_def = true, |
| 1030 |
keys = true, |
| 1031 |
result = true, |
| 1032 |
}, |
| 1033 |
|
| 1034 |
attrs = { |
| 1035 |
alias = true, |
| 1036 |
always_present = true, |
| 1037 |
choices = true, |
| 1038 |
data_type = true, |
| 1039 |
default = true, |
| 1040 |
description = true, |
| 1041 |
exclusive_group = true, |
| 1042 |
l3_tl_set = true, |
| 1043 |
macro = true, |
| 1044 |
match = true, |
| 1045 |
name = true, |
| 1046 |
opposite_keys = true, |
| 1047 |
pick = true, |
| 1048 |
process = true, |
| 1049 |
required = true, |
| 1050 |
sub_keys = true, |
| 1051 |
}, |
| 1052 |
|
| 1053 |
error_messages = { |
| 1054 |
E001 = { |
| 1055 |
'Unknown parse option: @unknown!', |
| 1056 |
{ 'The available options are:', '@opt_names' }, |
| 1057 |
}, |
| 1058 |
E002 = { |
| 1059 |
'Unknown hook: @unknown!', |
| 1060 |
{ 'The available hooks are:', '@hook_names' }, |
| 1061 |
}, |
| 1062 |
E003 = 'Duplicate aliases @alias1 and @alias2 for key @key!', |
| 1063 |
E004 = 'The value @value does not exist in the choices: @choices', |
| 1064 |
E005 = 'Unknown data type: @unknown', |
| 1065 |
E006 = 'The value @value of the key @key could not be converted into the data type @data_type!', |
| 1066 |
E007 = 'The key @key belongs to the mutually exclusive group @exclusive_group and another key of the group named @another_key is already present!', |
| 1067 |
E008 = 'def.match has to be a string', |
| 1068 |
E009 = 'The value @value of the key @key does not match @match!', |
| 1069 |
|
| 1070 |
E011 = 'Wrong data type in the “pick” attribute: @unknown. Allowed are: @data_types.', |
| 1071 |
E012 = 'Missing required key @key!', |
| 1072 |
E013 = 'The key definition must be a table! Got @data_type for key @key.', |
| 1073 |
E014 = { |
| 1074 |
'Unknown definition attribute: @unknown', |
| 1075 |
{ 'The available attributes are:', '@attr_names' }, |
| 1076 |
}, |
| 1077 |
E015 = 'Key name couldn’t be detected!', |
| 1078 |
E017 = 'Unknown style to format keys: @unknown! Allowed styles are: @styles', |
| 1079 |
E018 = 'The option “format_keys” has to be a table not @data_type', |
| 1080 |
E019 = 'Unknown keys: @unknown', |
| 1081 |
|
| 1082 |
---Input / parsing error |
| 1083 |
E021 = 'Opposite key was specified more than once: @key!', |
| 1084 |
E020 = 'Both opposite keys were given: @true and @false!', |
| 1085 |
---Config error (wrong configuration of luakeys) |
| 1086 |
E010 = 'Usage: opposite_keys = { "true_key", "false_key" } or { [true] = "true_key", [false] = "false_key" } ', |
| 1087 |
E023 = { |
| 1088 |
'Don’t use this function from the global luakeys table. Create a new instance using e. g.: local lk = luakeys.new()', |
| 1089 |
{ |
| 1090 |
'This functions should not be used from the global luakeys table:', |
| 1091 |
'parse()', |
| 1092 |
'save()', |
| 1093 |
'get()', |
| 1094 |
}, |
| 1095 |
}, |
| 1096 |
}, |
| 1097 |
} |
| 1098 |
|
| 1099 |
--- |
| 1100 |
---Main entry point of the module. |
| 1101 |
--- |
| 1102 |
---The return value is intentional not documented so the Lua language server can figure out the types. |
| 1103 |
local function new() |
| 1104 |
---The default options. |
| 1105 |
---@type OptionCollection |
| 1106 |
local default_opts = utils.clone_table(namespace.opts) |
| 1107 |
|
| 1108 |
local error_messages = utils.clone_table(namespace.error_messages) |
| 1109 |
|
| 1110 |
--- |
| 1111 |
---@param error_code string |
| 1112 |
---@param args? table |
| 1113 |
local function throw_error(error_code, args) |
| 1114 |
utils.throw_error_code(error_messages, error_code, args) |
| 1115 |
end |
| 1116 |
|
| 1117 |
--- |
| 1118 |
---Normalize the parse options. |
| 1119 |
--- |
| 1120 |
---@param opts? OptionCollection|unknown # Options in a raw format. The table may be empty or some keys are not set. |
| 1121 |
--- |
| 1122 |
---@return OptionCollection |
| 1123 |
local function normalize_opts(opts) |
| 1124 |
if type(opts) ~= 'table' then |
| 1125 |
opts = {} |
| 1126 |
end |
| 1127 |
for key, _ in pairs(opts) do |
| 1128 |
if namespace.opts[key] == nil then |
| 1129 |
throw_error('E001', { |
| 1130 |
unknown = key, |
| 1131 |
opt_names = utils.get_table_keys(namespace.opts), |
| 1132 |
}) |
| 1133 |
end |
| 1134 |
end |
| 1135 |
local old_opts = opts |
| 1136 |
opts = {} |
| 1137 |
for name, _ in pairs(namespace.opts) do |
| 1138 |
if old_opts[name] ~= nil then |
| 1139 |
opts[name] = old_opts[name] |
| 1140 |
else |
| 1141 |
opts[name] = default_opts[name] |
| 1142 |
end |
| 1143 |
end |
| 1144 |
|
| 1145 |
for hook in pairs(opts.hooks) do |
| 1146 |
if namespace.hooks[hook] == nil then |
| 1147 |
throw_error('E002', { |
| 1148 |
unknown = hook, |
| 1149 |
hook_names = utils.get_table_keys(namespace.hooks), |
| 1150 |
}) |
| 1151 |
end |
| 1152 |
end |
| 1153 |
return opts |
| 1154 |
end |
| 1155 |
|
| 1156 |
local l3_code_cctab = 10 |
| 1157 |
|
| 1158 |
--- |
| 1159 |
---Parser / Lpeg related |
| 1160 |
---@section |
| 1161 |
|
| 1162 |
---Generate the PEG parser using Lpeg. |
| 1163 |
--- |
| 1164 |
---Explanations of some LPeg notation forms: |
| 1165 |
--- |
| 1166 |
---* `patt ^ 0` = `expression *` |
| 1167 |
---* `patt ^ 1` = `expression +` |
| 1168 |
---* `patt ^ -1` = `expression ?` |
| 1169 |
---* `patt1 * patt2` = `expression1 expression2`: Sequence |
| 1170 |
---* `patt1 + patt2` = `expression1 / expression2`: Ordered choice |
| 1171 |
--- |
| 1172 |
---* [TUGboat article: Parsing complex data formats in LuaTEX with LPEG](https://tug.or-g/TUGboat/tb40-2/tb125menke-Patterndf) |
| 1173 |
--- |
| 1174 |
---@param initial_rule string # The name of the first rule of the grammar table passed to the `lpeg.P(attern)` function (e. g. `list`, `number`). |
| 1175 |
---@param opts? table # Whether the dimensions should be converted to scaled points (by default `false`). |
| 1176 |
--- |
| 1177 |
---@return userdata # The parser. |
| 1178 |
local function generate_parser(initial_rule, opts) |
| 1179 |
if type(opts) ~= 'table' then |
| 1180 |
opts = normalize_opts(opts) |
| 1181 |
end |
| 1182 |
|
| 1183 |
local Variable = lpeg.V |
| 1184 |
local Pattern = lpeg.P |
| 1185 |
local Set = lpeg.S |
| 1186 |
local Range = lpeg.R |
| 1187 |
local CaptureGroup = lpeg.Cg |
| 1188 |
local CaptureFolding = lpeg.Cf |
| 1189 |
local CaptureTable = lpeg.Ct |
| 1190 |
local CaptureConstant = lpeg.Cc |
| 1191 |
local CaptureSimple = lpeg.C |
| 1192 |
|
| 1193 |
---Optional whitespace |
| 1194 |
local white_space = Set(' \t\n\r') |
| 1195 |
|
| 1196 |
---Match literal string surrounded by whitespace |
| 1197 |
local ws = function(match) |
| 1198 |
return white_space ^ 0 * Pattern(match) * white_space ^ 0 |
| 1199 |
end |
| 1200 |
|
| 1201 |
local line_up_pattern = function(patterns) |
| 1202 |
local result |
| 1203 |
for _, pattern in ipairs(patterns) do |
| 1204 |
if result == nil then |
| 1205 |
result = Pattern(pattern) |
| 1206 |
else |
| 1207 |
result = result + Pattern(pattern) |
| 1208 |
end |
| 1209 |
end |
| 1210 |
return result |
| 1211 |
end |
| 1212 |
|
| 1213 |
--- |
| 1214 |
---Convert a dimension to an normalized dimension string or an |
| 1215 |
---integer in the scaled points format. |
| 1216 |
--- |
| 1217 |
---@param input string |
| 1218 |
--- |
| 1219 |
---@return integer|string # A dimension as an integer or a dimension string. |
| 1220 |
local capture_dimension = function(input) |
| 1221 |
---Remove all whitespaces |
| 1222 |
input = input:gsub('%s+', '') |
| 1223 |
---Convert the unit string into lowercase. |
| 1224 |
input = input:lower() |
| 1225 |
if opts.convert_dimensions then |
| 1226 |
return tex.sp(input) |
| 1227 |
else |
| 1228 |
return input |
| 1229 |
end |
| 1230 |
end |
| 1231 |
|
| 1232 |
--- |
| 1233 |
---Add values to a table in two modes: |
| 1234 |
--- |
| 1235 |
---Key-value pair: |
| 1236 |
--- |
| 1237 |
---If `arg1` and `arg2` are not nil, then `arg1` is the key and `arg2` is the |
| 1238 |
---value of a new table entry. |
| 1239 |
--- |
| 1240 |
---Indexed value: |
| 1241 |
--- |
| 1242 |
---If `arg2` is nil, then `arg1` is the value and is added as an indexed |
| 1243 |
---(by an integer) value. |
| 1244 |
--- |
| 1245 |
---@param result table # The result table to which an additional key-value pair or value should to be added |
| 1246 |
---@param arg1 any # The key or the value. |
| 1247 |
---@param arg2? any # Always the value. |
| 1248 |
--- |
| 1249 |
---@return table # The result table to which an additional key-value pair or value has been added. |
| 1250 |
local add_to_table = function(result, arg1, arg2) |
| 1251 |
if arg2 == nil then |
| 1252 |
local index = #result + 1 |
| 1253 |
return rawset(result, index, arg1) |
| 1254 |
else |
| 1255 |
return rawset(result, arg1, arg2) |
| 1256 |
end |
| 1257 |
end |
| 1258 |
|
| 1259 |
-- LuaFormatter off |
| 1260 |
return Pattern({ |
| 1261 |
[1] = initial_rule, |
| 1262 |
|
| 1263 |
---list_item* |
| 1264 |
list = CaptureFolding( |
| 1265 |
CaptureTable('') * Variable('list_item') ^ 0, |
| 1266 |
add_to_table |
| 1267 |
), |
| 1268 |
|
| 1269 |
---'{' list '}' |
| 1270 |
list_container = |
| 1271 |
ws(opts.group_begin) * Variable('list') * ws(opts.group_end), |
| 1272 |
|
| 1273 |
---( list_container / key_value_pair / value ) ','? |
| 1274 |
list_item = |
| 1275 |
CaptureGroup( |
| 1276 |
Variable('list_container') + |
| 1277 |
Variable('key_value_pair') + |
| 1278 |
Variable('value') |
| 1279 |
) * ws(opts.list_separator) ^ -1, |
| 1280 |
|
| 1281 |
---key '=' (list_container / value) |
| 1282 |
key_value_pair = |
| 1283 |
(Variable('key') * ws(opts.assignment_operator)) * (Variable('list_container') + Variable('value')), |
| 1284 |
|
| 1285 |
---number / string_quoted / string_unquoted |
| 1286 |
key = |
| 1287 |
Variable('number') + |
| 1288 |
Variable('string_quoted') + |
| 1289 |
Variable('string_unquoted'), |
| 1290 |
|
| 1291 |
---boolean !value / dimension !value / number !value / string_quoted !value / string_unquoted |
| 1292 |
---!value -> Not-predicate -> * -Variable('value') |
| 1293 |
value = |
| 1294 |
Variable('boolean') * -Variable('value') + |
| 1295 |
Variable('dimension') * -Variable('value') + |
| 1296 |
Variable('number') * -Variable('value') + |
| 1297 |
Variable('string_quoted') * -Variable('value') + |
| 1298 |
Variable('string_unquoted'), |
| 1299 |
|
| 1300 |
---for is.boolean() |
| 1301 |
boolean_only = Variable('boolean') * -1, |
| 1302 |
|
| 1303 |
---boolean_true / boolean_false |
| 1304 |
boolean = |
| 1305 |
( |
| 1306 |
Variable('boolean_true') * CaptureConstant(true) + |
| 1307 |
Variable('boolean_false') * CaptureConstant(false) |
| 1308 |
), |
| 1309 |
|
| 1310 |
boolean_true = line_up_pattern(opts.true_aliases), |
| 1311 |
|
| 1312 |
boolean_false = line_up_pattern(opts.false_aliases), |
| 1313 |
|
| 1314 |
---for is.dimension() |
| 1315 |
dimension_only = Variable('dimension') * -1, |
| 1316 |
|
| 1317 |
dimension = ( |
| 1318 |
Variable('tex_number') * white_space ^ 0 * |
| 1319 |
Variable('unit') |
| 1320 |
) / capture_dimension, |
| 1321 |
|
| 1322 |
sign = Set('-+'), |
| 1323 |
|
| 1324 |
digit = Range('09'), |
| 1325 |
|
| 1326 |
integer = (Variable('sign') ^ -1) * white_space ^ 0 * (Variable('digit') ^ 1), |
| 1327 |
|
| 1328 |
fractional = (Pattern('.')) * (Variable('digit') ^ 1), |
| 1329 |
|
| 1330 |
---(integer fractional?) / (sign? white_space? fractional) |
| 1331 |
tex_number = (Variable('integer') * (Variable('fractional') ^ -1)) + |
| 1332 |
((Variable('sign') ^ -1) * white_space ^ 0 * Variable('fractional')), |
| 1333 |
|
| 1334 |
---for is.number() |
| 1335 |
number_only = Variable('number') * -1, |
| 1336 |
|
| 1337 |
---capture number |
| 1338 |
number = Variable('tex_number') / tonumber, |
| 1339 |
|
| 1340 |
---'bp' / 'BP' / 'cc' / etc. |
| 1341 |
---https://raw.githubusercontent.com/latex3/lualibs/master/lualibs-util-dim.lua |
| 1342 |
---https://github.com/TeX-Live/luatex/blob/51db1985f5500dafd2393aa2e403fefa57d3cb76/source/texk/web2c/luatexdir/lua/ltexlib.c#L434-L625 |
| 1343 |
unit = |
| 1344 |
Pattern('bp') + Pattern('BP') + |
| 1345 |
Pattern('cc') + Pattern('CC') + |
| 1346 |
Pattern('cm') + Pattern('CM') + |
| 1347 |
Pattern('dd') + Pattern('DD') + |
| 1348 |
Pattern('em') + Pattern('EM') + |
| 1349 |
Pattern('ex') + Pattern('EX') + |
| 1350 |
Pattern('in') + Pattern('IN') + |
| 1351 |
Pattern('mm') + Pattern('MM') + |
| 1352 |
Pattern('mu') + Pattern('MU') + |
| 1353 |
Pattern('nc') + Pattern('NC') + |
| 1354 |
Pattern('nd') + Pattern('ND') + |
| 1355 |
Pattern('pc') + Pattern('PC') + |
| 1356 |
Pattern('pt') + Pattern('PT') + |
| 1357 |
Pattern('px') + Pattern('PX') + |
| 1358 |
Pattern('sp') + Pattern('SP'), |
| 1359 |
|
| 1360 |
---'"' ('\"' / !'"')* '"' |
| 1361 |
string_quoted = |
| 1362 |
white_space ^ 0 * Pattern(opts.quotation_begin) * |
| 1363 |
CaptureSimple((Pattern('\\' .. opts.quotation_end) + 1 - Pattern(opts.quotation_end)) ^ 0) * |
| 1364 |
Pattern(opts.quotation_end) * white_space ^ 0, |
| 1365 |
|
| 1366 |
string_unquoted = |
| 1367 |
white_space ^ 0 * |
| 1368 |
CaptureSimple( |
| 1369 |
Variable('word_unquoted') ^ 1 * |
| 1370 |
(Set(' \t') ^ 1 * Variable('word_unquoted') ^ 1) ^ 0) * |
| 1371 |
white_space ^ 0, |
| 1372 |
|
| 1373 |
word_unquoted = (1 - white_space - Set( |
| 1374 |
opts.group_begin .. |
| 1375 |
opts.group_end .. |
| 1376 |
opts.assignment_operator .. |
| 1377 |
opts.list_separator)) ^ 1 |
| 1378 |
}) |
| 1379 |
-- LuaFormatter on |
| 1380 |
end |
| 1381 |
|
| 1382 |
local is = { |
| 1383 |
boolean = function(value) |
| 1384 |
if value == nil then |
| 1385 |
return false |
| 1386 |
end |
| 1387 |
if type(value) == 'boolean' then |
| 1388 |
return true |
| 1389 |
end |
| 1390 |
local parser = generate_parser('boolean_only') |
| 1391 |
local result = parser:match(tostring(value)) |
| 1392 |
return result ~= nil |
| 1393 |
end, |
| 1394 |
|
| 1395 |
dimension = function(value) |
| 1396 |
if value == nil then |
| 1397 |
return false |
| 1398 |
end |
| 1399 |
local parser = generate_parser('dimension_only') |
| 1400 |
local result = parser:match(tostring(value)) |
| 1401 |
return result ~= nil |
| 1402 |
end, |
| 1403 |
|
| 1404 |
integer = function(value) |
| 1405 |
local n = tonumber(value) |
| 1406 |
if n == nil then |
| 1407 |
return false |
| 1408 |
end |
| 1409 |
return n == math.floor(n) |
| 1410 |
end, |
| 1411 |
|
| 1412 |
number = function(value) |
| 1413 |
if value == nil then |
| 1414 |
return false |
| 1415 |
end |
| 1416 |
if type(value) == 'number' then |
| 1417 |
return true |
| 1418 |
end |
| 1419 |
local parser = generate_parser('number_only') |
| 1420 |
local result = parser:match(tostring(value)) |
| 1421 |
return result ~= nil |
| 1422 |
end, |
| 1423 |
|
| 1424 |
string = function(value) |
| 1425 |
return type(value) == 'string' |
| 1426 |
end, |
| 1427 |
|
| 1428 |
--- |
| 1429 |
---Check if the given value is a list or an array. |
| 1430 |
--- |
| 1431 |
---As we all know, there is no such thing as a list or array in Lua. |
| 1432 |
---A list is nothing more than a table that uses an ascending |
| 1433 |
---sequence of numbers as its keys. |
| 1434 |
--- |
| 1435 |
---@param value any # Any value to be checked. |
| 1436 |
--- |
| 1437 |
---@return boolean |
| 1438 |
list = function(value) |
| 1439 |
if type(value) ~= 'table' then |
| 1440 |
return false |
| 1441 |
end |
| 1442 |
|
| 1443 |
for k, _ in pairs(value) do |
| 1444 |
if type(k) ~= 'number' then |
| 1445 |
return false |
| 1446 |
end |
| 1447 |
end |
| 1448 |
return true |
| 1449 |
end, |
| 1450 |
|
| 1451 |
any = function(value) |
| 1452 |
return true |
| 1453 |
end, |
| 1454 |
} |
| 1455 |
|
| 1456 |
--- |
| 1457 |
---Apply the key-value-pair definitions (`defs`) on an input table in a |
| 1458 |
---recursive fashion. |
| 1459 |
--- |
| 1460 |
---@param defs table # A table containing all definitions. |
| 1461 |
---@param opts table # The parse options table. |
| 1462 |
---@param input table # The current input table. |
| 1463 |
---@param output table # The current output table. |
| 1464 |
---@param unknown table # Always the root unknown table. |
| 1465 |
---@param key_path table # An array of key names leading to the current |
| 1466 |
---@param input_root table # The root input table input and output table. |
| 1467 |
local function apply_definitions(defs, |
| 1468 |
opts, |
| 1469 |
input, |
| 1470 |
output, |
| 1471 |
unknown, |
| 1472 |
key_path, |
| 1473 |
input_root) |
| 1474 |
local exclusive_groups = {} |
| 1475 |
|
| 1476 |
local function add_to_key_path(key_path, key) |
| 1477 |
local new_key_path = {} |
| 1478 |
|
| 1479 |
for index, value in ipairs(key_path) do |
| 1480 |
new_key_path[index] = value |
| 1481 |
end |
| 1482 |
|
| 1483 |
table.insert(new_key_path, key) |
| 1484 |
return new_key_path |
| 1485 |
end |
| 1486 |
|
| 1487 |
local function get_default_value(def) |
| 1488 |
if def.default ~= nil then |
| 1489 |
return def.default |
| 1490 |
elseif opts ~= nil and opts.default ~= nil then |
| 1491 |
return opts.default |
| 1492 |
end |
| 1493 |
return true |
| 1494 |
end |
| 1495 |
|
| 1496 |
local function find_value(search_key, def) |
| 1497 |
if input[search_key] ~= nil then |
| 1498 |
local value = input[search_key] |
| 1499 |
input[search_key] = nil |
| 1500 |
return value |
| 1501 |
---naked keys: values with integer keys |
| 1502 |
elseif utils.remove_from_table(input, search_key) ~= nil then |
| 1503 |
return get_default_value(def) |
| 1504 |
end |
| 1505 |
end |
| 1506 |
|
| 1507 |
local apply = { |
| 1508 |
alias = function(value, key, def) |
| 1509 |
if type(def.alias) == 'string' then |
| 1510 |
def.alias = { def.alias } |
| 1511 |
end |
| 1512 |
local alias_value |
| 1513 |
local used_alias_key |
| 1514 |
---To get an error if the key and an alias is present |
| 1515 |
if value ~= nil then |
| 1516 |
alias_value = value |
| 1517 |
used_alias_key = key |
| 1518 |
end |
| 1519 |
for _, alias in ipairs(def.alias) do |
| 1520 |
local v = find_value(alias, def) |
| 1521 |
if v ~= nil then |
| 1522 |
if alias_value ~= nil then |
| 1523 |
throw_error('E003', { |
| 1524 |
alias1 = used_alias_key, |
| 1525 |
alias2 = alias, |
| 1526 |
key = key, |
| 1527 |
}) |
| 1528 |
end |
| 1529 |
used_alias_key = alias |
| 1530 |
alias_value = v |
| 1531 |
end |
| 1532 |
end |
| 1533 |
if alias_value ~= nil then |
| 1534 |
return alias_value |
| 1535 |
end |
| 1536 |
end, |
| 1537 |
|
| 1538 |
always_present = function(value, key, def) |
| 1539 |
if value == nil and def.always_present then |
| 1540 |
return get_default_value(def) |
| 1541 |
end |
| 1542 |
end, |
| 1543 |
|
| 1544 |
choices = function(value, key, def) |
| 1545 |
if value == nil then |
| 1546 |
return |
| 1547 |
end |
| 1548 |
if def.choices ~= nil and type(def.choices) == 'table' then |
| 1549 |
local is_in_choices = false |
| 1550 |
for _, choice in ipairs(def.choices) do |
| 1551 |
if value == choice then |
| 1552 |
is_in_choices = true |
| 1553 |
end |
| 1554 |
end |
| 1555 |
if not is_in_choices then |
| 1556 |
throw_error('E004', { value = value, choices = def.choices }) |
| 1557 |
end |
| 1558 |
end |
| 1559 |
end, |
| 1560 |
|
| 1561 |
data_type = function(value, key, def) |
| 1562 |
if value == nil then |
| 1563 |
return |
| 1564 |
end |
| 1565 |
if def.data_type ~= nil then |
| 1566 |
local converted |
| 1567 |
---boolean |
| 1568 |
if def.data_type == 'boolean' then |
| 1569 |
if value == 0 or value == '' or not value then |
| 1570 |
converted = false |
| 1571 |
else |
| 1572 |
converted = true |
| 1573 |
end |
| 1574 |
---dimension |
| 1575 |
elseif def.data_type == 'dimension' then |
| 1576 |
if is.dimension(value) then |
| 1577 |
converted = value |
| 1578 |
end |
| 1579 |
---integer |
| 1580 |
elseif def.data_type == 'integer' then |
| 1581 |
if is.number(value) then |
| 1582 |
local n = tonumber(value) |
| 1583 |
if type(n) == 'number' and n ~= nil then |
| 1584 |
converted = math.floor(n) |
| 1585 |
end |
| 1586 |
end |
| 1587 |
---number |
| 1588 |
elseif def.data_type == 'number' then |
| 1589 |
if is.number(value) then |
| 1590 |
converted = tonumber(value) |
| 1591 |
end |
| 1592 |
---string |
| 1593 |
elseif def.data_type == 'string' then |
| 1594 |
converted = tostring(value) |
| 1595 |
---list |
| 1596 |
elseif def.data_type == 'list' then |
| 1597 |
if is.list(value) then |
| 1598 |
converted = value |
| 1599 |
end |
| 1600 |
else |
| 1601 |
throw_error('E005', { data_type = def.data_type }) |
| 1602 |
end |
| 1603 |
if converted == nil then |
| 1604 |
throw_error('E006', { |
| 1605 |
value = value, |
| 1606 |
key = key, |
| 1607 |
data_type = def.data_type, |
| 1608 |
}) |
| 1609 |
else |
| 1610 |
return converted |
| 1611 |
end |
| 1612 |
end |
| 1613 |
end, |
| 1614 |
|
| 1615 |
exclusive_group = function(value, key, def) |
| 1616 |
if value == nil then |
| 1617 |
return |
| 1618 |
end |
| 1619 |
if def.exclusive_group ~= nil then |
| 1620 |
if exclusive_groups[def.exclusive_group] ~= nil then |
| 1621 |
throw_error('E007', { |
| 1622 |
key = key, |
| 1623 |
exclusive_group = def.exclusive_group, |
| 1624 |
another_key = exclusive_groups[def.exclusive_group], |
| 1625 |
}) |
| 1626 |
else |
| 1627 |
exclusive_groups[def.exclusive_group] = key |
| 1628 |
end |
| 1629 |
end |
| 1630 |
end, |
| 1631 |
|
| 1632 |
l3_tl_set = function(value, key, def) |
| 1633 |
if value == nil then |
| 1634 |
return |
| 1635 |
end |
| 1636 |
if def.l3_tl_set ~= nil then |
| 1637 |
tex.print(l3_code_cctab, |
| 1638 |
'\\tl_set:Nn \\g_' .. def.l3_tl_set .. '_tl') |
| 1639 |
tex.print('{' .. value .. '}') |
| 1640 |
end |
| 1641 |
end, |
| 1642 |
|
| 1643 |
macro = function(value, key, def) |
| 1644 |
if value == nil then |
| 1645 |
return |
| 1646 |
end |
| 1647 |
if def.macro ~= nil then |
| 1648 |
token.set_macro(def.macro, value, 'global') |
| 1649 |
end |
| 1650 |
end, |
| 1651 |
|
| 1652 |
match = function(value, key, def) |
| 1653 |
if value == nil then |
| 1654 |
return |
| 1655 |
end |
| 1656 |
if def.match ~= nil then |
| 1657 |
if type(def.match) ~= 'string' then |
| 1658 |
throw_error('E008') |
| 1659 |
end |
| 1660 |
local match = string.match(value, def.match) |
| 1661 |
if match == nil then |
| 1662 |
throw_error('E009', { |
| 1663 |
value = value, |
| 1664 |
key = key, |
| 1665 |
match = def.match:gsub('%%', '%%%%'), |
| 1666 |
}) |
| 1667 |
else |
| 1668 |
return match |
| 1669 |
end |
| 1670 |
end |
| 1671 |
end, |
| 1672 |
|
| 1673 |
opposite_keys = function(value, key, def) |
| 1674 |
if def.opposite_keys ~= nil then |
| 1675 |
local function get_value(key1, key2) |
| 1676 |
local opposite_name |
| 1677 |
if def.opposite_keys[key1] ~= nil then |
| 1678 |
opposite_name = def.opposite_keys[key1] |
| 1679 |
elseif def.opposite_keys[key2] ~= nil then |
| 1680 |
opposite_name = def.opposite_keys[key2] |
| 1681 |
end |
| 1682 |
return opposite_name |
| 1683 |
end |
| 1684 |
local true_key = get_value(true, 1) |
| 1685 |
local false_key = get_value(false, 2) |
| 1686 |
if true_key == nil or false_key == nil then |
| 1687 |
throw_error('E010') |
| 1688 |
end |
| 1689 |
|
| 1690 |
---@param v string |
| 1691 |
local function remove_values(v) |
| 1692 |
local count = 0 |
| 1693 |
while utils.remove_from_table(input, v) do |
| 1694 |
count = count + 1 |
| 1695 |
end |
| 1696 |
return count |
| 1697 |
end |
| 1698 |
|
| 1699 |
local true_count = remove_values(true_key) |
| 1700 |
local false_count = remove_values(false_key) |
| 1701 |
|
| 1702 |
if true_count > 1 then |
| 1703 |
throw_error('E021', { key = true_key }) |
| 1704 |
end |
| 1705 |
|
| 1706 |
if false_count > 1 then |
| 1707 |
throw_error('E021', { key = false_key }) |
| 1708 |
end |
| 1709 |
|
| 1710 |
if true_count > 0 and false_count > 0 then |
| 1711 |
throw_error('E020', |
| 1712 |
{ ['true'] = true_key, ['false'] = false_key }) |
| 1713 |
end |
| 1714 |
if true_count == 0 and false_count == 0 then |
| 1715 |
return |
| 1716 |
end |
| 1717 |
return true_count == 1 or false_count == 0 |
| 1718 |
end |
| 1719 |
end, |
| 1720 |
|
| 1721 |
process = function(value, key, def) |
| 1722 |
if value == nil then |
| 1723 |
return |
| 1724 |
end |
| 1725 |
if def.process ~= nil and type(def.process) == 'function' then |
| 1726 |
return def.process(value, input_root, output, unknown) |
| 1727 |
end |
| 1728 |
end, |
| 1729 |
|
| 1730 |
pick = function(value, key, def) |
| 1731 |
if def.pick then |
| 1732 |
local pick_types |
| 1733 |
|
| 1734 |
---Allow old deprecated attribut pick = true |
| 1735 |
if def.pick == true then |
| 1736 |
pick_types = { 'any' } |
| 1737 |
elseif type(def.pick) == 'table' then |
| 1738 |
pick_types = def.pick |
| 1739 |
else |
| 1740 |
pick_types = { def.pick } |
| 1741 |
end |
| 1742 |
|
| 1743 |
---Check if the pick attribute is valid |
| 1744 |
for _, pick_type in ipairs(pick_types) do |
| 1745 |
if type(pick_type) == 'string' and is[pick_type] == nil then |
| 1746 |
throw_error('E011', { |
| 1747 |
unknown = tostring(pick_type), |
| 1748 |
data_types = { |
| 1749 |
'any', |
| 1750 |
'boolean', |
| 1751 |
'dimension', |
| 1752 |
'integer', |
| 1753 |
'number', |
| 1754 |
'string', |
| 1755 |
}, |
| 1756 |
}) |
| 1757 |
end |
| 1758 |
end |
| 1759 |
|
| 1760 |
---The key has already a value. We leave the function at this |
| 1761 |
---point to be able to check the pick attribute for errors |
| 1762 |
---beforehand. |
| 1763 |
if value ~= nil then |
| 1764 |
return value |
| 1765 |
end |
| 1766 |
|
| 1767 |
for _, pick_type in ipairs(pick_types) do |
| 1768 |
for i, v in pairs(input) do |
| 1769 |
---We can not use ipairs here. `ipairs(t)` iterates up to the |
| 1770 |
---first absent index. Values are deleted from the `input` |
| 1771 |
---table. |
| 1772 |
if type(i) == 'number' then |
| 1773 |
local picked_value = nil |
| 1774 |
if is[pick_type](v) then |
| 1775 |
picked_value = v |
| 1776 |
elseif pick_type == 'string' and is.number(v) then |
| 1777 |
picked_value = tostring(v) |
| 1778 |
end |
| 1779 |
|
| 1780 |
if picked_value ~= nil then |
| 1781 |
input[i] = nil |
| 1782 |
return picked_value |
| 1783 |
end |
| 1784 |
end |
| 1785 |
end |
| 1786 |
end |
| 1787 |
end |
| 1788 |
end, |
| 1789 |
|
| 1790 |
required = function(value, key, def) |
| 1791 |
if def.required ~= nil and def.required and value == nil then |
| 1792 |
throw_error('E012', { key = key }) |
| 1793 |
end |
| 1794 |
end, |
| 1795 |
|
| 1796 |
sub_keys = function(value, key, def) |
| 1797 |
if def.sub_keys ~= nil then |
| 1798 |
local v |
| 1799 |
---To get keys defined with always_present |
| 1800 |
if value == nil then |
| 1801 |
v = {} |
| 1802 |
elseif type(value) == 'string' then |
| 1803 |
v = { value } |
| 1804 |
elseif type(value) == 'table' then |
| 1805 |
v = value |
| 1806 |
end |
| 1807 |
v = apply_definitions(def.sub_keys, opts, v, output[key], |
| 1808 |
unknown, add_to_key_path(key_path, key), input_root) |
| 1809 |
if utils.get_table_size(v) > 0 then |
| 1810 |
return v |
| 1811 |
end |
| 1812 |
end |
| 1813 |
end, |
| 1814 |
} |
| 1815 |
|
| 1816 |
---standalone values are removed. |
| 1817 |
---For some callbacks and the third return value of parse, we |
| 1818 |
---need an unchanged raw result from the parse function. |
| 1819 |
input = utils.clone_table(input) |
| 1820 |
if output == nil then |
| 1821 |
output = {} |
| 1822 |
end |
| 1823 |
if unknown == nil then |
| 1824 |
unknown = {} |
| 1825 |
end |
| 1826 |
if key_path == nil then |
| 1827 |
key_path = {} |
| 1828 |
end |
| 1829 |
|
| 1830 |
for index, def in pairs(defs) do |
| 1831 |
---Find key and def |
| 1832 |
local key |
| 1833 |
---`{ key1 = { }, key2 = { } }` |
| 1834 |
if type(def) == 'table' and def.name == nil and type(index) == |
| 1835 |
'string' then |
| 1836 |
key = index |
| 1837 |
---`{ { name = 'key1' }, { name = 'key2' } }` |
| 1838 |
elseif type(def) == 'table' and def.name ~= nil then |
| 1839 |
key = def.name |
| 1840 |
---Definitions as strings in an array: `{ 'key1', 'key2' }` |
| 1841 |
elseif type(index) == 'number' and type(def) == 'string' then |
| 1842 |
key = def |
| 1843 |
def = { default = get_default_value({}) } |
| 1844 |
end |
| 1845 |
|
| 1846 |
if type(def) ~= 'table' then |
| 1847 |
throw_error('E013', { data_type = tostring(def), key = index }) ---key is nil |
| 1848 |
end |
| 1849 |
|
| 1850 |
for attr, _ in pairs(def) do |
| 1851 |
if namespace.attrs[attr] == nil then |
| 1852 |
throw_error('E014', { |
| 1853 |
unknown = attr, |
| 1854 |
attr_names = utils.get_table_keys(namespace.attrs), |
| 1855 |
}) |
| 1856 |
end |
| 1857 |
end |
| 1858 |
|
| 1859 |
if key == nil then |
| 1860 |
throw_error('E015') |
| 1861 |
end |
| 1862 |
|
| 1863 |
local value = find_value(key, def) |
| 1864 |
|
| 1865 |
for _, def_opt in ipairs({ |
| 1866 |
'alias', |
| 1867 |
'opposite_keys', |
| 1868 |
'pick', |
| 1869 |
'always_present', |
| 1870 |
'required', |
| 1871 |
'data_type', |
| 1872 |
'choices', |
| 1873 |
'match', |
| 1874 |
'exclusive_group', |
| 1875 |
'macro', |
| 1876 |
'l3_tl_set', |
| 1877 |
'process', |
| 1878 |
'sub_keys', |
| 1879 |
}) do |
| 1880 |
if def[def_opt] ~= nil then |
| 1881 |
local tmp_value = apply[def_opt](value, key, def) |
| 1882 |
if tmp_value ~= nil then |
| 1883 |
value = tmp_value |
| 1884 |
end |
| 1885 |
end |
| 1886 |
end |
| 1887 |
|
| 1888 |
output[key] = value |
| 1889 |
end |
| 1890 |
|
| 1891 |
if utils.get_table_size(input) > 0 then |
| 1892 |
---Move to the current unknown table. |
| 1893 |
local current_unknown = unknown |
| 1894 |
for _, key in ipairs(key_path) do |
| 1895 |
if current_unknown[key] == nil then |
| 1896 |
current_unknown[key] = {} |
| 1897 |
end |
| 1898 |
current_unknown = current_unknown[key] |
| 1899 |
end |
| 1900 |
|
| 1901 |
---Copy all unknown key-value-pairs to the current unknown table. |
| 1902 |
for key, value in pairs(input) do |
| 1903 |
current_unknown[key] = value |
| 1904 |
end |
| 1905 |
end |
| 1906 |
|
| 1907 |
return output, unknown |
| 1908 |
end |
| 1909 |
|
| 1910 |
--- |
| 1911 |
---Parse a LaTeX/TeX style key-value string into a Lua table. |
| 1912 |
--- |
| 1913 |
---@param kv_string string # A string in the TeX/LaTeX style key-value format as described above. |
| 1914 |
---@param opts? OptionCollection # A table containing options. |
| 1915 |
--- |
| 1916 |
---@return table result # The final result of all individual parsing and normalization steps. |
| 1917 |
---@return table|nil unknown # A table with unknown, undefined key-value pairs. |
| 1918 |
---@return table raw # The unprocessed, raw result of the LPeg parser. |
| 1919 |
local function parse(kv_string, opts) |
| 1920 |
opts = normalize_opts(opts) |
| 1921 |
|
| 1922 |
local function log_result(caption, result) |
| 1923 |
utils.log.debug('%s: \n%s', caption, visualizers.render(result)) |
| 1924 |
end |
| 1925 |
|
| 1926 |
if kv_string == nil then |
| 1927 |
return {}, {}, {} |
| 1928 |
end |
| 1929 |
|
| 1930 |
if opts.debug then |
| 1931 |
utils.log.set('debug') |
| 1932 |
end |
| 1933 |
|
| 1934 |
utils.log.debug('kv_string: “%s”', kv_string) |
| 1935 |
|
| 1936 |
if type(opts.hooks.kv_string) == 'function' then |
| 1937 |
kv_string = opts.hooks.kv_string(kv_string) |
| 1938 |
end |
| 1939 |
|
| 1940 |
local result = generate_parser('list', opts):match(kv_string) |
| 1941 |
local raw = utils.clone_table(result) |
| 1942 |
|
| 1943 |
log_result('result after Lpeg Parsing', result) |
| 1944 |
|
| 1945 |
local function apply_hook(name) |
| 1946 |
if type(opts.hooks[name]) == 'function' then |
| 1947 |
if name:match('^keys') then |
| 1948 |
result = utils.visit_tree(result, opts.hooks[name]) |
| 1949 |
else |
| 1950 |
opts.hooks[name](result) |
| 1951 |
end |
| 1952 |
|
| 1953 |
if opts.debug then |
| 1954 |
print('After the execution of the hook: ' .. name) |
| 1955 |
visualizers.debug(result) |
| 1956 |
end |
| 1957 |
end |
| 1958 |
end |
| 1959 |
|
| 1960 |
local function apply_hooks(at) |
| 1961 |
if at ~= nil then |
| 1962 |
at = '_' .. at |
| 1963 |
else |
| 1964 |
at = '' |
| 1965 |
end |
| 1966 |
apply_hook('keys' .. at) |
| 1967 |
apply_hook('result' .. at) |
| 1968 |
end |
| 1969 |
|
| 1970 |
apply_hooks('before_opts') |
| 1971 |
|
| 1972 |
log_result('after hooks before_opts', result) |
| 1973 |
|
| 1974 |
--- |
| 1975 |
---Normalize the result table of the LPeg parser. This normalization |
| 1976 |
---tasks are performed on the raw input table coming directly from |
| 1977 |
---the PEG parser: |
| 1978 |
-- |
| 1979 |
---@param result table # The raw input table coming directly from the PEG parser |
| 1980 |
---@param opts table # Some options. |
| 1981 |
local function apply_opts(result, opts) |
| 1982 |
local callbacks = { |
| 1983 |
unpack = function(key, value) |
| 1984 |
if type(value) == 'table' and utils.get_array_size(value) == 1 and |
| 1985 |
utils.get_table_size(value) == 1 and type(value[1]) ~= |
| 1986 |
'table' then |
| 1987 |
return key, value[1] |
| 1988 |
end |
| 1989 |
return key, value |
| 1990 |
end, |
| 1991 |
|
| 1992 |
process_naked = function(key, value) |
| 1993 |
if type(key) == 'number' and type(value) == 'string' then |
| 1994 |
return value, opts.default |
| 1995 |
end |
| 1996 |
return key, value |
| 1997 |
end, |
| 1998 |
|
| 1999 |
format_key = function(key, value) |
| 2000 |
if type(key) == 'string' then |
| 2001 |
for _, style in ipairs(opts.format_keys) do |
| 2002 |
if style == 'lower' then |
| 2003 |
key = key:lower() |
| 2004 |
elseif style == 'snake' then |
| 2005 |
key = key:gsub('[^%w]+', '_') |
| 2006 |
elseif style == 'upper' then |
| 2007 |
key = key:upper() |
| 2008 |
else |
| 2009 |
throw_error('E017', { |
| 2010 |
unknown = style, |
| 2011 |
styles = { 'lower', 'snake', 'upper' }, |
| 2012 |
}) |
| 2013 |
end |
| 2014 |
end |
| 2015 |
end |
| 2016 |
return key, value |
| 2017 |
end, |
| 2018 |
|
| 2019 |
apply_invert_flag = function(key, value) |
| 2020 |
if type(key) == 'string' and key:find(opts.invert_flag) then |
| 2021 |
return key:gsub(opts.invert_flag, ''), not value |
| 2022 |
end |
| 2023 |
return key, value |
| 2024 |
end, |
| 2025 |
} |
| 2026 |
|
| 2027 |
if opts.unpack then |
| 2028 |
result = utils.visit_tree(result, callbacks.unpack) |
| 2029 |
end |
| 2030 |
|
| 2031 |
if not opts.naked_as_value and opts.defs == false then |
| 2032 |
result = utils.visit_tree(result, callbacks.process_naked) |
| 2033 |
end |
| 2034 |
|
| 2035 |
if opts.format_keys then |
| 2036 |
if type(opts.format_keys) ~= 'table' then |
| 2037 |
throw_error('E018', { data_type = type(opts.format_keys) }) |
| 2038 |
end |
| 2039 |
result = utils.visit_tree(result, callbacks.format_key) |
| 2040 |
end |
| 2041 |
|
| 2042 |
if opts.invert_flag then |
| 2043 |
result = utils.visit_tree(result, callbacks.apply_invert_flag) |
| 2044 |
end |
| 2045 |
|
| 2046 |
return result |
| 2047 |
end |
| 2048 |
result = apply_opts(result, opts) |
| 2049 |
|
| 2050 |
log_result('after apply opts', result) |
| 2051 |
|
| 2052 |
---All unknown keys are stored in this table |
| 2053 |
local unknown = nil |
| 2054 |
if type(opts.defs) == 'table' then |
| 2055 |
apply_hooks('before_defs') |
| 2056 |
result, unknown = apply_definitions(opts.defs, opts, result, {}, |
| 2057 |
{}, {}, utils.clone_table(result)) |
| 2058 |
end |
| 2059 |
|
| 2060 |
log_result('after apply_definitions', result) |
| 2061 |
|
| 2062 |
apply_hooks() |
| 2063 |
|
| 2064 |
if opts.defaults ~= nil and type(opts.defaults) == 'table' then |
| 2065 |
utils.merge_tables(result, opts.defaults, false) |
| 2066 |
end |
| 2067 |
|
| 2068 |
log_result('End result', result) |
| 2069 |
|
| 2070 |
if opts.accumulated_result ~= nil and type(opts.accumulated_result) == |
| 2071 |
'table' then |
| 2072 |
utils.merge_tables(opts.accumulated_result, result, true) |
| 2073 |
end |
| 2074 |
|
| 2075 |
---no_error |
| 2076 |
if not opts.no_error and type(unknown) == 'table' and |
| 2077 |
utils.get_table_size(unknown) > 0 then |
| 2078 |
throw_error('E019', { unknown = visualizers.render(unknown) }) |
| 2079 |
end |
| 2080 |
return result, unknown, raw |
| 2081 |
end |
| 2082 |
|
| 2083 |
--- |
| 2084 |
---Define a new `parse` function. |
| 2085 |
--- |
| 2086 |
---The `define` function returns a `parse` function. This created |
| 2087 |
---`parse` function is configured with the specified key-value |
| 2088 |
---defintions. |
| 2089 |
--- |
| 2090 |
---@param defs DefinitionCollection # A collection of key-value pair definitions. |
| 2091 |
---@param opts? OptionCollection # Options to configured the parse function with. |
| 2092 |
local function define(defs, opts) |
| 2093 |
return function(kv_string, inner_opts) |
| 2094 |
local options |
| 2095 |
|
| 2096 |
if inner_opts ~= nil and opts ~= nil then |
| 2097 |
options = utils.merge_tables(opts, inner_opts) |
| 2098 |
elseif inner_opts ~= nil then |
| 2099 |
options = inner_opts |
| 2100 |
elseif opts ~= nil then |
| 2101 |
options = opts |
| 2102 |
end |
| 2103 |
|
| 2104 |
if options == nil then |
| 2105 |
options = {} |
| 2106 |
end |
| 2107 |
|
| 2108 |
options.defs = defs |
| 2109 |
|
| 2110 |
return parse(kv_string, options) |
| 2111 |
end |
| 2112 |
end |
| 2113 |
|
| 2114 |
--- |
| 2115 |
---An array/list of key names or a mapping of source key names to target key names. |
| 2116 |
---@alias KeySpec table<integer|string, string> |
| 2117 |
|
| 2118 |
local DefinitionManager = (function() |
| 2119 |
--- |
| 2120 |
---The `DefinitionManager` class allows you to store key-value |
| 2121 |
---definitions in an object. The class provides a `parse` method |
| 2122 |
---that is configured with these definitions. |
| 2123 |
---@class DefinitionManager |
| 2124 |
---@field defs DefinitionCollection # A collection of key-value pair definitions. |
| 2125 |
DefinitionManager = {} |
| 2126 |
|
| 2127 |
---@private |
| 2128 |
DefinitionManager.__index = DefinitionManager |
| 2129 |
|
| 2130 |
--- |
| 2131 |
---Create a new instance of the `DefinitionManager` class. |
| 2132 |
--- |
| 2133 |
---@param defs DefinitionCollection # A collection of key-value pair definitions. |
| 2134 |
--- |
| 2135 |
---@return DefinitionManager manager # A new instance of the `DefinitionManager` class. |
| 2136 |
local function constructor(defs) |
| 2137 |
local manager = {} |
| 2138 |
for key, def in pairs(defs) do |
| 2139 |
if def.name ~= nil and type(key) == 'number' then |
| 2140 |
defs[def.name] = def |
| 2141 |
defs[key] = nil |
| 2142 |
end |
| 2143 |
end |
| 2144 |
setmetatable(manager, DefinitionManager) |
| 2145 |
manager.defs = defs |
| 2146 |
return manager |
| 2147 |
end |
| 2148 |
|
| 2149 |
--- |
| 2150 |
---Create a new instance of the `DefinitionManager` class. |
| 2151 |
--- |
| 2152 |
---@param defs DefinitionCollection # A collection of key-value pair definitions. |
| 2153 |
--- |
| 2154 |
---@return DefinitionManager manager # A new instance of the `DefinitionManager` class. |
| 2155 |
function DefinitionManager:new(defs) |
| 2156 |
return constructor(defs) |
| 2157 |
end |
| 2158 |
|
| 2159 |
--- |
| 2160 |
---Add a key-value pair definition under a specified key name to the |
| 2161 |
---definition collection. |
| 2162 |
--- |
| 2163 |
---@param key string # The name of key. |
| 2164 |
---@param def Definition # A key-value pair definition. |
| 2165 |
function DefinitionManager:set(key, def) |
| 2166 |
self.defs[key] = def |
| 2167 |
end |
| 2168 |
|
| 2169 |
--- |
| 2170 |
---Retrieve a key-value pair definition based on its key name. |
| 2171 |
--- |
| 2172 |
---@param key string # The name of key. |
| 2173 |
--- |
| 2174 |
---@return Definition def # A key-value pair definition. |
| 2175 |
function DefinitionManager:get(key) |
| 2176 |
return self.defs[key] |
| 2177 |
end |
| 2178 |
|
| 2179 |
--- |
| 2180 |
---Return all key names of the corresponding definitions as an array. |
| 2181 |
--- |
| 2182 |
---@return string[] key_names # key names of the corresponding definitions as an array. |
| 2183 |
function DefinitionManager:key_names() |
| 2184 |
return utils.get_table_keys(self.defs) |
| 2185 |
end |
| 2186 |
|
| 2187 |
--- |
| 2188 |
---Return all key names if the key specification is not provided. |
| 2189 |
--- |
| 2190 |
---@private |
| 2191 |
--- |
| 2192 |
---@param key_spec? KeySpec # An array/list of key names or a mapping of source key names to target key names. |
| 2193 |
--- |
| 2194 |
---@return KeySpec key_spec A collection of key-value pair definitions. |
| 2195 |
function DefinitionManager:normalize_key_spec(key_spec) |
| 2196 |
if key_spec == nil then |
| 2197 |
return utils.get_table_keys(self.defs) |
| 2198 |
end |
| 2199 |
return key_spec |
| 2200 |
end |
| 2201 |
|
| 2202 |
--- |
| 2203 |
---Include a subset of the key-value definitions in the return collection |
| 2204 |
---of definitions or, if `key_spec` is not specified, all definitions. |
| 2205 |
--- |
| 2206 |
---@param key_spec? KeySpec # An array/list of key names or a mapping of source key names to target key names. |
| 2207 |
---@param clone? boolean # Make a deep copy of the key-value pair definition tables. |
| 2208 |
--- |
| 2209 |
---@return DefinitionCollection defs A collection of key-value pair definitions. |
| 2210 |
function DefinitionManager:include(key_spec, clone) |
| 2211 |
local selection = {} |
| 2212 |
for key, value in pairs(self:normalize_key_spec(key_spec)) do |
| 2213 |
local src |
| 2214 |
local dest |
| 2215 |
if type(key) == 'number' then |
| 2216 |
src = value |
| 2217 |
dest = value |
| 2218 |
else |
| 2219 |
src = key |
| 2220 |
dest = value |
| 2221 |
end |
| 2222 |
if clone then |
| 2223 |
selection[dest] = utils.clone_table(self.defs[src]) |
| 2224 |
else |
| 2225 |
selection[dest] = self.defs[src] |
| 2226 |
end |
| 2227 |
end |
| 2228 |
return selection |
| 2229 |
end |
| 2230 |
|
| 2231 |
--- |
| 2232 |
---Exclude a subset of the key-value definitions or if `key_spec` is not specified |
| 2233 |
---return all definitions. |
| 2234 |
--- |
| 2235 |
---@param key_spec? KeySpec # An array/list of key names or a mapping of source key names to target key names. |
| 2236 |
---@param clone? boolean # Make a deep copy of the key-value pair definition tables. |
| 2237 |
--- |
| 2238 |
---@return DefinitionCollection defs A collection of key-value pair definitions. |
| 2239 |
function DefinitionManager:exclude(key_spec, clone) |
| 2240 |
local spec = {} |
| 2241 |
if key_spec == nil then |
| 2242 |
key_spec = {} |
| 2243 |
end |
| 2244 |
for key, value in pairs(key_spec) do |
| 2245 |
if type(key) == 'number' then |
| 2246 |
spec[value] = value |
| 2247 |
else |
| 2248 |
spec[key] = value |
| 2249 |
end |
| 2250 |
end |
| 2251 |
local selection = {} |
| 2252 |
for key, def in pairs(self.defs) do |
| 2253 |
if spec[key] == nil then |
| 2254 |
if clone then |
| 2255 |
selection[key] = utils.clone_table(def) |
| 2256 |
else |
| 2257 |
selection[key] = def |
| 2258 |
end |
| 2259 |
end |
| 2260 |
end |
| 2261 |
return selection |
| 2262 |
end |
| 2263 |
|
| 2264 |
---@class DefinitionManagerCloneOptions |
| 2265 |
---@field exclude? KeySpec |
| 2266 |
---@field include? KeySpec |
| 2267 |
|
| 2268 |
--- |
| 2269 |
---Create a new instance of the `DefinitionManager` class and add a deep copy of |
| 2270 |
---the key-value pair definitions to it. |
| 2271 |
--- |
| 2272 |
---@param opts? DefinitionManagerCloneOptions # Exclude or include some |
| 2273 |
--- key-value pair definitions. If `nil` all definitions are cloned. |
| 2274 |
--- |
| 2275 |
---@return DefinitionManager manager # A new instance of the `DefinitionManager` class. |
| 2276 |
function DefinitionManager:clone(opts) |
| 2277 |
if opts == nil then |
| 2278 |
opts = {} |
| 2279 |
end |
| 2280 |
if opts.exclude ~= nil then |
| 2281 |
return self:new(self:exclude(opts.exclude, true)) |
| 2282 |
end |
| 2283 |
if opts.include ~= nil then |
| 2284 |
return self:new(self:include(opts.include, true)) |
| 2285 |
end |
| 2286 |
return self:new(self:include(nil, true)) |
| 2287 |
end |
| 2288 |
|
| 2289 |
--- |
| 2290 |
---Define a new `parse` function. |
| 2291 |
--- |
| 2292 |
---The `define` method returns a `parse` function. This created |
| 2293 |
---`parse` function is configured with key-value |
| 2294 |
---definitions of this instance, or a subset if a key selection is specified. |
| 2295 |
--- |
| 2296 |
---@param key_selection? KeySpec A selection of key-value pair |
| 2297 |
--- definitions to include. If not specified all definitions are |
| 2298 |
--- used. |
| 2299 |
function DefinitionManager:define(key_selection) |
| 2300 |
return define(self:include(key_selection)) |
| 2301 |
end |
| 2302 |
|
| 2303 |
--- |
| 2304 |
---Parse a LaTeX/TeX style key-value string into a Lua table using |
| 2305 |
---all the definitions of this manager or a subset of the definitions. |
| 2306 |
--- |
| 2307 |
---@param key_selection? KeySpec A selection of key-value pair |
| 2308 |
--- definitions to include. If not specified all definitions are |
| 2309 |
--- used. |
| 2310 |
--- |
| 2311 |
---@return table result # The final result of all individual parsing and normalization steps. |
| 2312 |
---@return table|nil unknown # A table with unknown, undefined key-value pairs. |
| 2313 |
---@return table raw # The unprocessed, raw result of the LPeg parser. |
| 2314 |
function DefinitionManager:parse(kv_string, key_selection) |
| 2315 |
local d |
| 2316 |
if key_selection == nil then |
| 2317 |
d = self.defs |
| 2318 |
else |
| 2319 |
d = self:include(key_selection) |
| 2320 |
end |
| 2321 |
return parse(kv_string, { defs = d }) |
| 2322 |
end |
| 2323 |
|
| 2324 |
return constructor |
| 2325 |
end)() |
| 2326 |
|
| 2327 |
--- |
| 2328 |
---A table to store parsed key-value results. |
| 2329 |
local result_store = {} |
| 2330 |
|
| 2331 |
return { |
| 2332 |
new = new, |
| 2333 |
|
| 2334 |
version = { 0, 17, 0 }, |
| 2335 |
|
| 2336 |
parse = parse, |
| 2337 |
|
| 2338 |
define = define, |
| 2339 |
|
| 2340 |
DefinitionManager = DefinitionManager, |
| 2341 |
|
| 2342 |
---@see default_opts |
| 2343 |
opts = default_opts, |
| 2344 |
|
| 2345 |
error_messages = error_messages, |
| 2346 |
|
| 2347 |
---@see visualizers.render |
| 2348 |
render = visualizers.render, |
| 2349 |
|
| 2350 |
---@see visualizers.debug |
| 2351 |
debug = visualizers.debug, |
| 2352 |
|
| 2353 |
--- |
| 2354 |
---Save a result (a |
| 2355 |
---table from a previous run of `parse`) under an identifier. |
| 2356 |
---Therefore, it is not necessary to pollute the global namespace to |
| 2357 |
---store results for the later usage. |
| 2358 |
--- |
| 2359 |
---@param identifier string # The identifier under which the result is saved. |
| 2360 |
--- |
| 2361 |
---@param result table|any # A result to be stored and that was created by the key-value parser. |
| 2362 |
save = function(identifier, result) |
| 2363 |
result_store[identifier] = result |
| 2364 |
end, |
| 2365 |
|
| 2366 |
--- |
| 2367 |
---The function `get(identifier): table` retrieves a saved result |
| 2368 |
---from the result store. |
| 2369 |
--- |
| 2370 |
---@param identifier string # The identifier under which the result was saved. |
| 2371 |
--- |
| 2372 |
---@return table|any |
| 2373 |
get = function(identifier) |
| 2374 |
---if result_store[identifier] == nil then |
| 2375 |
--- throw_error('No stored result was found for the identifier \'' .. identifier .. '\'') |
| 2376 |
---end |
| 2377 |
return result_store[identifier] |
| 2378 |
end, |
| 2379 |
|
| 2380 |
is = is, |
| 2381 |
|
| 2382 |
utils = utils, |
| 2383 |
|
| 2384 |
--- |
| 2385 |
---Exported but intentionally undocumented functions |
| 2386 |
--- |
| 2387 |
|
| 2388 |
namespace = utils.clone_table(namespace), |
| 2389 |
|
| 2390 |
--- |
| 2391 |
---This function is used in the documentation. |
| 2392 |
--- |
| 2393 |
---@param from string # A key in the namespace table, either `opts`, `hook` or `attrs`. |
| 2394 |
print_names = function(from) |
| 2395 |
local names = utils.get_table_keys(namespace[from]) |
| 2396 |
tex.print(table.concat(names, ', ')) |
| 2397 |
end, |
| 2398 |
|
| 2399 |
print_default = function(from, name) |
| 2400 |
tex.print(tostring(namespace[from][name])) |
| 2401 |
end, |
| 2402 |
|
| 2403 |
print_error_messages = function() |
| 2404 |
local msgs = namespace.error_messages |
| 2405 |
local keys = utils.get_table_keys(namespace.error_messages) |
| 2406 |
for _, key in ipairs(keys) do |
| 2407 |
local msg = msgs[key] |
| 2408 |
---@type string |
| 2409 |
local msg_text |
| 2410 |
if type(msg) == 'table' then |
| 2411 |
msg_text = msg[1] |
| 2412 |
else |
| 2413 |
msg_text = msg |
| 2414 |
end |
| 2415 |
utils.tex_printf('\\item[\\texttt{%s}]: \\texttt{%s}', key, |
| 2416 |
msg_text) |
| 2417 |
end |
| 2418 |
end, |
| 2419 |
|
| 2420 |
--- |
| 2421 |
---Print the TeX markup for the command sequence `\luakeysdebug`. |
| 2422 |
---of the luakeys-debug package. |
| 2423 |
--- |
| 2424 |
---@param marg string # The mandatory argument of the `\luakeysdebug` command. |
| 2425 |
---@param oarg? string # The optional argument of the `\luakeysdebug` command. |
| 2426 |
---@param is_latex? boolean # If true, the markup is printed into verbatim environment. |
| 2427 |
print_debug = function(marg, oarg, is_latex) |
| 2428 |
local opts |
| 2429 |
if oarg then |
| 2430 |
opts = parse(oarg, { format_keys = { 'snake', 'lower' } }) |
| 2431 |
end |
| 2432 |
local result = parse(marg, opts) |
| 2433 |
visualizers.debug(result) |
| 2434 |
local rendered = visualizers.render(result, { |
| 2435 |
style = 'tex', |
| 2436 |
inline = false, |
| 2437 |
table_delimiters_first_depth = true, |
| 2438 |
}) |
| 2439 |
local lines = utils.split_lines(rendered) |
| 2440 |
if is_latex then |
| 2441 |
-- https://tug.org/TUGboat/tb32-1/tb100isambert.pdf |
| 2442 |
-- Not working in the LaTeX ltxdoc class! Why? |
| 2443 |
tex.print('\\begin{verbatim}') |
| 2444 |
tex.print(lines) |
| 2445 |
tex.print('\\end{verbatim}') |
| 2446 |
else |
| 2447 |
tex.print('\\bgroup\\parindent=0pt \\tt') |
| 2448 |
tex.print(31278, lines) |
| 2449 |
tex.print('\\egroup') |
| 2450 |
end |
| 2451 |
end, |
| 2452 |
|
| 2453 |
--- |
| 2454 |
---@param exported_table table |
| 2455 |
depublish_functions = function(exported_table) |
| 2456 |
local function warn_global_import() |
| 2457 |
throw_error('E023') |
| 2458 |
end |
| 2459 |
|
| 2460 |
exported_table.parse = warn_global_import |
| 2461 |
exported_table.define = warn_global_import |
| 2462 |
exported_table.save = warn_global_import |
| 2463 |
exported_table.get = warn_global_import |
| 2464 |
end, |
| 2465 |
} |
| 2466 |
end |
| 2467 |
|
| 2468 |
return new |