forked from Josef-Friedrich/luakeys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluakeys.lua
More file actions
2468 lines (2220 loc) · 71.2 KB
/
luakeys.lua
File metadata and controls
2468 lines (2220 loc) · 71.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---luakeys.lua
---Copyright 2021-2025 Josef Friedrich
---
---This work may be distributed and/or modified under the
---conditions of the LaTeX Project Public License, either version 1.3c
---of this license or (at your option) any later version.
---The latest version of this license is in
---http://www.latex-project.org/lppl.txt
---and version 1.3c or later is part of all distributions of LaTeX
---version 2008/05/04 or later.
---
---This work has the LPPL maintenance status `maintained'.
---
---The Current Maintainer of this work is Josef Friedrich.
---
---This work consists of the files luakeys.lua, luakeys.sty, luakeys.tex
---luakeys-debug.sty and luakeys-debug.tex.
----A key-value parser written with Lpeg.
---
local lpeg = require('lpeg')
if not tex then
---Dummy functions for the tests.
tex = {
sp = function(input)
return 1234567
end,
}
token = {
set_macro = function(csname, content, global)
end,
}
end
---
local utils = (function()
---
---True if a key string can be notated without square brackets.
---
---@param identifer string
---
---@return boolean
local function is_lua_identifier(identifer)
identifer = string.gsub(identifer, '_', '')
return string.match(identifer, '^%w+$') ~= nil
end
---
---Split a string into lines.
---
---@param content string The content to be split into individual lines.
---
---@return string[] The individual lines as a array of strings.
local function split_lines(content)
local lines = {}
for line in content:gmatch('[^\r\n]+') do
table.insert(lines, line)
end
return lines
end
---
---Merge two tables into the first specified table.
---The `merge_tables` function copies keys from the `source` table
---to the `target` table. It returns the target table.
---
---https://stackoverflow.com/a/1283608/10193818
---
---@param target table # The target table where all values are copied.
---@param source table # The source table from which all values are copied.
---@param overwrite? boolean # Overwrite the values in the target table if they are present (default true).
---
---@return table target The modified target table.
local function merge_tables(target, source, overwrite)
if overwrite == nil then
overwrite = true
end
for key, value in pairs(source) do
if type(value) == 'table' and type(target[key] or false) ==
'table' then
merge_tables(target[key] or {}, source[key] or {}, overwrite)
elseif (not overwrite and target[key] == nil) or
(overwrite and target[key] ~= value) then
target[key] = value
end
end
return target
end
---
---Clone a table, i.e. make a deep copy of the source table.
---
---http://lua-users.org/wiki/CopyTable
---
---@param source table # The source table to be cloned.
---
---@return table # A deep copy of the source table.
local function clone_table(source)
local copy
if type(source) == 'table' then
copy = {}
for orig_key, orig_value in next, source, nil do
copy[clone_table(orig_key)] = clone_table(orig_value)
end
setmetatable(copy, clone_table(getmetatable(source)))
else ---number, string, boolean, etc
copy = source
end
return copy
end
---
---Remove an element from a table.
---
---@param source table # The source table.
---@param value any # The value to be removed from the table.
---
---@return any|nil # If the value was found, then this value, otherwise nil.
local function remove_from_table(source, value)
for index, v in pairs(source) do
if value == v then
source[index] = nil
return value
end
end
end
---
---Return the keys of a table as a sorted list (array like table).
---
---@param source table # The source table.
---
---@return table # An array table with the sorted key names.
local function get_table_keys(source)
local keys = {}
for key in pairs(source) do
table.insert(keys, key)
end
table.sort(keys, function(a, b)
a = tostring(a)
b = tostring(b)
return a < b
end)
return keys
end
---
---Get the size of a table `{ one = 'one', 'two', 'three' }` = 3.
---
---@param value any # A table or any input.
---
---@return number # The size of the array like table. 0 if the input is no table or the table is empty.
local function get_table_size(value)
local count = 0
if type(value) == 'table' then
for _ in pairs(value) do
count = count + 1
end
end
return count
end
---
---Get the size of an array like table, for example `{ 'one', 'two',
---'three' }` = 3.
---
---@param value any # A table or any input.
---
---@return number # The size of the array like table. 0 if the input is no table or the table is empty.
local function get_array_size(value)
local count = 0
if type(value) == 'table' then
for _ in ipairs(value) do
count = count + 1
end
end
return count
end
---
---Print a formatted string.
---
---* `%d` or `%i`: Signed decimal integer
---* `%u`: Unsigned decimal integer
---* `%o`: Unsigned octal
---* `%x`: Unsigned hexadecimal integer
---* `%X`: Unsigned hexadecimal integer (uppercase)
---* `%f`: Decimal floating point, lowercase
---* `%e`: Scientific notation (mantissa/exponent), lowercase
---* `%E`: Scientific notation (mantissa/exponent), uppercase
---* `%g`: Use the shortest representation: %e or %f
---* `%G`: Use the shortest representation: %E or %F
---* `%a`: Hexadecimal floating point, lowercase
---* `%A`: Hexadecimal floating point, uppercase
---* `%c`: Character
---* `%s`: String of characters
---* `%p`: Pointer address b8000000
---* `%%`: A `%` followed by another `%` character will write a single `%` to the stream.
---* `%q`: formats `booleans`, `nil`, `numbers`, and `strings` in a way that the result is a valid constant in Lua source code.
---
---http://www.lua.org/source/5.3/lstrlib.c.html#str_format
---
---@param format string # A string in the `printf` format
---@param ... any # A sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string.
local function tex_printf(format, ...)
tex.print(string.format(format, ...))
end
---
---Throw a single error message.
---
---@param message string
---@param help? table
local function throw_error_message(message, help)
if type(tex.error) == 'function' then
tex.error(message, help)
else
error(message)
end
end
---
---Throw an error by specifying an error code.
---
---@param error_messages table
---@param error_code string
---@param args? table
local function throw_error_code(error_messages,
error_code,
args)
local template = error_messages[error_code]
---
---@param message string
---@param a table
---
---@return string
local function replace_args(message, a)
for key, value in pairs(a) do
if type(value) == 'table' then
value = table.concat(value, ', ')
end
message = message:gsub('@' .. key,
'“' .. tostring(value) .. '”')
end
return message
end
---
---@param list table
---@param a table
---
---@return table
local function replace_args_in_list(list, a)
for index, message in ipairs(list) do
list[index] = replace_args(message, a)
end
return list
end
---
---@type string
local message
---@type table
local help = {}
if type(template) == 'table' then
message = template[1]
if args ~= nil then
help = replace_args_in_list(template[2], args)
else
help = template[2]
end
else
message = template
end
if args ~= nil then
message = replace_args(message, args)
end
message = 'luakeys error [' .. error_code .. ']: ' .. message
for _, help_message in ipairs({
'You may be able to find more help in the documentation:',
'http://mirrors.ctan.org/macros/luatex/generic/luakeys/luakeys-doc.pdf',
'Or ask a question in the issue tracker on Github:',
'https://github.com/Josef-Friedrich/luakeys/issues',
}) do
table.insert(help, help_message)
end
throw_error_message(message, help)
end
local function visit_tree(tree, callback_func)
if type(tree) ~= 'table' then
throw_error_message(
'Parameter “tree” has to be a table, got: ' ..
tostring(tree))
end
local function visit_tree_recursive(tree,
current,
result,
depth,
callback_func)
for key, value in pairs(current) do
if type(value) == 'table' then
value = visit_tree_recursive(tree, value, {}, depth + 1,
callback_func)
end
key, value = callback_func(key, value, depth, current, tree)
if key ~= nil and value ~= nil then
result[key] = value
end
end
if next(result) ~= nil then
return result
end
end
local result =
visit_tree_recursive(tree, tree, {}, 1, callback_func)
if result == nil then
return {}
end
return result
end
---@alias ColorName 'black'|'red'|'green'|'yellow'|'blue'|'magenta'|'cyan'|'white'|'reset'
---@alias ColorMode 'bright'|'dim'
---
---Small library to surround strings with ANSI color codes.
--
---[SGR (Select Graphic Rendition) Parameters](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters)
---
---__attributes__
---
---| color |code|
---|------------|----|
---| reset | 0 |
---| clear | 0 |
---| bright | 1 |
---| dim | 2 |
---| underscore | 4 |
---| blink | 5 |
---| reverse | 7 |
---| hidden | 8 |
---
---__foreground__
---
---| color |code|
---|------------|----|
---| black | 30 |
---| red | 31 |
---| green | 32 |
---| yellow | 33 |
---| blue | 34 |
---| magenta | 35 |
---| cyan | 36 |
---| white | 37 |
---
---__background__
---
---| color |code|
---|------------|----|
---| onblack | 40 |
---| onred | 41 |
---| ongreen | 42 |
---| onyellow | 43 |
---| onblue | 44 |
---| onmagenta | 45 |
---| oncyan | 46 |
---| onwhite | 47 |
---
---## Other ansi color modules
---
---* ansicolors:
--- [Github 143⋆](https://github.com/kikito/ansicolors.lua),
--- [LuaRocks 945k](https://luarocks.org/modules/kikito/ansicolors)
---* Lunacolors: [Github 12⋆](https://github.com/Rosettea/Lunacolors)
local ansi_color = (function()
---
---@param code integer
---
---@return string
local function format_color_code(code)
return string.char(27) .. '[' .. tostring(code) .. 'm'
end
---
---@private
---
---@param color ColorName # A color name.
---@param mode? ColorMode
---@param background? boolean # Colorize the background not the text.
---
---@return string
local function get_color_code(color, mode, background)
local output = ''
local code
if mode == 'bright' then
output = format_color_code(1)
elseif mode == 'dim' then
output = format_color_code(2)
end
if not background then
if color == 'reset' then
code = 0
elseif color == 'black' then
code = 30
elseif color == 'red' then
code = 31
elseif color == 'green' then
code = 32
elseif color == 'yellow' then
code = 33
elseif color == 'blue' then
code = 34
elseif color == 'magenta' then
code = 35
elseif color == 'cyan' then
code = 36
elseif color == 'white' then
code = 37
else
code = 37
end
else
if color == 'black' then
code = 40
elseif color == 'red' then
code = 41
elseif color == 'green' then
code = 42
elseif color == 'yellow' then
code = 43
elseif color == 'blue' then
code = 44
elseif color == 'magenta' then
code = 45
elseif color == 'cyan' then
code = 46
elseif color == 'white' then
code = 47
else
code = 40
end
end
return output .. format_color_code(code)
end
---
---@param text any
---@param color ColorName # A color name.
---@param mode? ColorMode
---@param background? boolean # Colorize the background not the text.
---
---@return string
local function colorize(text, color, mode, background)
return string.format('%s%s%s',
get_color_code(color, mode, background), text,
get_color_code('reset'))
end
return {
colorize = colorize,
---
---@param text any
---
---@return string
red = function(text)
return colorize(text, 'red')
end,
---
---@param text any
---
---@return string
green = function(text)
return colorize(text, 'green')
end,
---@return string
yellow = function(text)
return colorize(text, 'yellow')
end,
---
---@param text any
---
---@return string
blue = function(text)
return colorize(text, 'blue')
end,
---
---@param text any
---
---@return string
magenta = function(text)
return colorize(text, 'magenta')
end,
---
---@param text any
---
---@return string
cyan = function(text)
return colorize(text, 'cyan')
end,
}
end)()
---
---A small logging library.
---
---Log levels:
---
---* 0: silent
---* 1: error (red)
---* 2: warn (yellow)
---* 3: info (green)
---* 4: verbose (blue)
---* 5: debug (magenta)
---
---## Other logging libraries:
---
---* lualogging:
--- [Github 59⋆](https://github.com/lunarmodules/lualogging),
--- [LuaRocks 1000k](https://luarocks.org/modules/tieske/lualogging)
---* lua-logger:
--- [LuaRocks 0.7k](https://luarocks.org/modules/alissasquared/lua-logger)
---* log.lua:
--- [Github 331⋆](https://github.com/rxi/log.lua),
--- [LuaRocks 0.3k](https://luarocks.org/modules/stephencathcart/log.lua)
---
local log = (function()
---@private
local opts = { level = 0 }
local function colorize_not(s)
return s
end
local colorize = colorize_not
---@private
local function print_message(message, ...)
local args = { ... }
for index, value in ipairs(args) do
args[index] = colorize(value)
end
print(string.format(message, table.unpack(args)))
end
---
---Set the log level.
---
---@param level 0|'silent'|1|'error'|2|'warn'|3|'info'|4|'verbose'|5|'debug'
local function set_log_level(level)
if type(level) == 'string' then
if level == 'silent' then
opts.level = 0
elseif level == 'error' then
opts.level = 1
elseif level == 'warn' then
opts.level = 2
elseif level == 'info' then
opts.level = 3
elseif level == 'verbose' then
opts.level = 4
elseif level == 'debug' then
opts.level = 5
else
throw_error_message(string.format('Unknown log level: %s',
level))
end
else
if level > 5 or level < 0 then
throw_error_message(string.format(
'Log level out of range 0-5: %s', level))
end
opts.level = level
end
end
---
---@return integer
local function get_log_level()
return opts.level
end
---
---Log at level 1 (error).
---
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug).
---
---@param message string
---@param ... any
local function error(message, ...)
if opts.level >= 1 then
colorize = ansi_color.red
print_message(message, ...)
colorize = colorize_not
end
end
---
---Log at level 2 (warn).
---
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug).
---
---@param message string
---@param ... any
local function warn(message, ...)
if opts.level >= 2 then
colorize = ansi_color.yellow
print_message(message, ...)
colorize = colorize_not
end
end
---
---Log at level 3 (info).
---
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug).
---
---@param message string
---@param ... any
local function info(message, ...)
if opts.level >= 3 then
colorize = ansi_color.green
print_message(message, ...)
colorize = colorize_not
end
end
---
---Log at level 4 (verbose).
---
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug).
---
---@param message string
---@param ... any
local function verbose(message, ...)
if opts.level >= 4 then
colorize = ansi_color.blue
print_message(message, ...)
colorize = colorize_not
end
end
---
---Log at level 5 (debug).
---
---The other log levels are: 0 (silent), 1 (error), 2 (warn), 3 (info), 4 (verbose), 5 (debug).
---
---@param message string
---@param ... any
local function debug(message, ...)
if opts.level >= 5 then
colorize = ansi_color.magenta
print_message(message, ...)
colorize = colorize_not
end
end
return {
set = set_log_level,
get = get_log_level,
error = error,
warn = warn,
info = info,
verbose = verbose,
debug = debug,
}
end)()
return {
is_lua_identifier = is_lua_identifier,
split_lines = split_lines,
merge_tables = merge_tables,
clone_table = clone_table,
remove_from_table = remove_from_table,
get_table_keys = get_table_keys,
get_table_size = get_table_size,
get_array_size = get_array_size,
visit_tree = visit_tree,
tex_printf = tex_printf,
throw_error_message = throw_error_message,
throw_error_code = throw_error_code,
ansi_color = ansi_color,
log = log,
}
end)()
---
---Convert back to strings
---@section
local visualizers = (function()
---
---A collection of options to configure the `render` function.
---
---This collection combines high level and low level options.
---@class RenderConfiguration
---
---High level options
---@field style? 'tex'|'lua' # Render the input as a `lua` table or in the `tex` style, default `tex`.
---@field inline? boolean # Render the input on one line without line breaks, default `true`.
---
---Low level options
---@field line_break? string # The character for a line break, for example, use `\n` for terminal output or `\par` for TeX rendering, default ``.
---@field begin_table? string # The starting delimiter for a table, default `{`.
---@field end_table? string # The final delimiter for a table, default `}`.
---@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`.
---@field indent? string # Characters used for indentation, default ``.
---@field begin_key? string # The starting delimiter for a key, default `[`.
---@field end_key? string # The final delimiter for a key, default `]`.
---@field assignment? string # The symbol for the assignment operator, default `=`.
---@field separator? string # The separator for the individual table elements, default `,`.
---@field separator_last? boolean # Append a separator after the last element, default `false`.
---@field quotation? string # The symbol that delimits a string, default `'`.
---@field format_key? fun(key: unknown, conf: RenderConfiguration): string # A function that formats the key.
---@field format_value? fun(value: unknown, conf: RenderConfiguration): string # A function that formats the value.
---
---Render or serialize a Lua value into a string.
---
---This function can be used to reverse the function `parse(kv_string)`. It takes a Lua value and converts this value
---into a string. The keys of the resulting serialized table are sorted alpabetically.
---
---Source: https://stackoverflow.com/a/54593224/10193818
---
---@param value unknown # A lua value to render.
---@param config? RenderConfiguration # A collection of options to configure the `render` function.
---
---@return string
local function render(value, config)
if config == nil then
config = {}
end
---style=tex inline=true
---@type RenderConfiguration
local default_conf = {
style = 'tex',
inline = true,
--- Low level
line_break = '',
begin_table = '{',
end_table = '}',
table_delimiters_first_depth = false,
indent = '',
begin_key = '[',
end_key = ']',
assignment = '=',
separator = ',',
separator_last = false,
quotation = '"',
format_key = function(key, conf)
key = tostring(key)
if string.find(key, ',') then
return conf.quotation .. key .. conf.quotation
end
return key
end,
format_value = function(value, conf)
value = tostring(value)
if string.find(value, ',') then
return conf.quotation .. value .. conf.quotation
end
return value
end,
}
---@type RenderConfiguration
local conf = utils.clone_table(config)
utils.merge_tables(conf, default_conf, false)
if conf.style == 'lua' then
-- lua
conf.quotation = '\''
conf.format_key = function(key, conf)
if type(key) == 'string' and utils.is_lua_identifier(key) then
return key
end
if type(key) == 'string' then
key = conf.quotation .. tostring(key) .. conf.quotation
end
return conf.begin_key .. key .. conf.end_key
end
conf.format_value = function(value, opts)
if type(value) == 'string' then
value = opts.quotation .. value .. opts.quotation
end
return tostring(value)
end
conf.table_delimiters_first_depth = true
end
if not conf.inline then
-- multiline
conf.assignment = ' = '
conf.line_break = '\n'
conf.indent = ' '
end
-- Override the merged options with lower-level options from the function
-- argument so that the entered options are also taken into account and are
-- not overridden by the higher-level options
local low_level_options = {
'line_break',
'begin_table',
'end_table',
'table_delimiters_first_depth',
'indent',
'begin_key',
'end_key',
'assignment',
'separator',
'separator_last',
'quotation',
'format_key',
'format_value',
}
for _, option in ipairs(low_level_options) do
if config[option] ~= nil then
conf[option] = config[option]
end
end
---
---@param input unknown
---@param depth integer
---
---@return string
local function stringify(input, depth)
local output = {}
depth = depth or 0
local function add(depth, text)
table.insert(output, string.rep(conf.indent, depth) .. text)
end
if type(input) ~= 'table' then
return tostring(input)
end
local keys = utils.get_table_keys(input)
local element_sum = utils.get_table_size(keys)
local consecutive_numbers_counter = 1
local element_counter = 0
for _, key in pairs(keys) do
element_counter = element_counter + 1
local value = input[key]
if (key and type(key) == 'number' or type(key) == 'string') then
local separator = conf.separator
if not conf.separator_last and element_sum == element_counter then
separator = ''
end
-- is array ... consecutive integers ...
if type(key) == 'number' and consecutive_numbers_counter ==
key then
consecutive_numbers_counter =
consecutive_numbers_counter + 1
key = ''
else
key = conf.format_key(key, conf)
key = key .. conf.assignment
end
if (type(value) == 'table') then
if (next(value)) then
add(depth, key .. conf.begin_table)
add(0, stringify(value, depth + 1))
add(depth, conf.end_table .. separator);
else
add(depth, key .. conf.begin_table .. conf.end_table ..
separator)
end
else
value = conf.format_value(value, conf)
add(depth, key .. value .. separator)
end
end
end
return table.concat(output, conf.line_break)
end
local begin_table = ''
local end_table = ''
if conf.table_delimiters_first_depth then
begin_table = conf.begin_table
end_table = conf.end_table
end
return begin_table .. conf.line_break .. stringify(value, 1) ..
conf.line_break .. end_table
end
---
---Pretty print a Lua value to standard output (stdout).
---
---It is a utility function that can be used to
---debug and inspect the resulting Lua table of the function
---`parse`. You have to compile your TeX document in a console to
---see the terminal output.
---
---@param value unknown # A value to be printed to standard output for debugging purposes.
---@param config? RenderConfiguration # A collection of options to configure the `render` function.
local function debug(value, config)
if not config then
config = { inline = false, table_delimiters_first_depth = true }
end
print('\n' .. render(value, config))
end
return { render = render, debug = debug }
end)()
---@alias FormatKeyOperation 'lower'|'snake'|'upper'
---@class OptionCollection
---@field accumulated_result? table
---@field assignment_operator? string # default `=`
---@field convert_dimensions? boolean # default `false`
---@field debug? boolean # default `false`
---@field default? boolean # default `true`
---@field defaults? table
---@field defs? DefinitionCollection
---@field false_aliases? table default `{ 'false', 'FALSE', 'False' }`,
---@field format_keys? boolean|(FormatKeyOperation)[] # default `false`,
---@field group_begin? string default `{`,
---@field group_end? string default `}`,
---@field hooks? HookCollection
---@field invert_flag? string default `!`
---@field list_separator? string default `,`
---@field naked_as_value? boolean # default `false`
---@field no_error? boolean # default `false`
---@field quotation_begin? string `"`
---@field quotation_end? string `"`
---@field true_aliases? table `{ 'true', 'TRUE', 'True' }`
---@field unpack? boolean # default `true`
---@alias KeysHook fun(key: string, value: any, depth: integer, current: table, result: table): string, any
---@alias ResultHook fun(result: table): nil
---@class HookCollection
---@field kv_string? fun(kv_string: string): string
---@field keys_before_opts? KeysHook
---@field result_before_opts? ResultHook
---@field keys_before_def? KeysHook
---@field result_before_def? ResultHook
---@field keys? KeysHook
---@field result? ResultHook
---@alias ProcessFunction fun(value: any, input: table, result: table, unknown: table): any
---@alias PickDataType 'string'|'number'|'dimension'|'integer'|'boolean'|'any'
---
---A key-value pair definition
---@class Definition
---@field alias? string|table
---@field always_present? boolean
---@field choices? table
---@field data_type? 'boolean'|'dimension'|'integer'|'number'|'string'|'list'
---@field default? any
---@field description? string
---@field exclusive_group? string
---@field l3_tl_set? string
---@field macro? string
---@field match? string
---@field name? string
---@field opposite_keys? table
---@field pick? PickDataType|PickDataType[]|false
---@field process? ProcessFunction
---@field required? boolean
---@field sub_keys? table<string, Definition>
---
---A collection of key-value pair definitions.
---@alias DefinitionCollection table<string|number, Definition>
local namespace = {