summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lualibs/lualibs.dtx
blob: 73c5aafcc1e8c8e8fa9722612046dd6b88890d5d (plain)
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
% \iffalse meta-comment
%
% Copyright (C) 2009--2021 by
%
%       PRAGMA ADE / ConTeXt Development Team
%       The LaTeX Project Team
%
% See ConTeXt's mreadme.pdf for the license.
%
% This work consists of the main source file lualibs.dtx
% and the derived files lualibs.lua, lualibs-basic.lua,
% and lualibs-extended.lua.
%
% Unpacking:
%    tex lualibs.dtx
%
% Documentation:
%    lualatex lualibs.dtx
%
%    The class ltxdoc loads the configuration file ltxdoc.cfg
%    if available. Here you can specify further options, e.g.
%    use A4 as paper format:
%       \PassOptionsToClass{a4paper}{article}
%
%
%
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation}
\Msg{* Package: lualibs 2021-05-20 v2.74 Lua additional functions.}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\let\MetaPrefix\relax

\preamble
This is a generated file.

Copyright (C) 2009--2021 by
        PRAGMA ADE / ConTeXt Development Team
        The LaTeX Project Team

See ConTeXt's mreadme.pdf for the license.

This work consists of the main source file lualibs.dtx
and the derived files lualibs.lua, lualibs-basic.lua,
and lualibs-extended.lua.

\endpreamble

% The following hacks are to generate a lua file with lua comments starting by
% -- instead of %%

\def\MetaPrefix{-- }

\def\luapostamble{%
  \MetaPrefix^^J%
  \MetaPrefix\space End of File `\outFileName'.%
}

\def\currentpostamble{\luapostamble}%

\generate{%
  \usedir{tex/luatex/lualibs}%
  \file{lualibs.lua}{\from{lualibs.dtx}{lualibs}}%
}

\generate{%
  \usedir{tex/luatex/lualibs}%
  \file{lualibs-basic.lua}{\from{lualibs.dtx}{basic}}%
}

\generate{%
  \usedir{tex/luatex/lualibs}%
  \file{lualibs-extended.lua}{\from{lualibs.dtx}{extended}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
\Msg{*     lualibs.lua, lualibs-basic.lua, lualibs-extended.lua}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{lualibs.drv}
  [2021/05/20 v2.74 Lua Libraries.]
\documentclass{ltxdoc}
\usepackage{fancyvrb,xspace}
\usepackage[x11names]{xcolor}
%
\def\primarycolor{DodgerBlue4}  %%-> rgb  16  78 139 | #104e8b
\def\secondarycolor{Goldenrod4} %%-> rgb 139 105 200 | #8b6914
%
\hypersetup{
   colorlinks=true,
    linkcolor=\primarycolor,
     urlcolor=\secondarycolor,
    citecolor=\primarycolor,
     pdftitle={The lualibs package},
   pdfsubject={Port of the ConTeXt Lua libraries},
    pdfauthor={Elie Roux & Philipp Gesang},
  pdfkeywords={luatex, lualatex, unicode, opentype}}

\newcommand*\email[1]{\href{mailto:#1}{#1}}
\usepackage{fontspec}
\setmainfont[
  Numbers=OldStyle,
  Ligatures=TeX,
]{Linux Libertine O}
\setmonofont [Ligatures=TeX,Scale=MatchLowercase] {Liberation Mono}
\setsansfont [Ligatures=TeX,Scale=MatchLowercase] {Iwona Medium}
\usepackage{metalogo}
\EnableCrossrefs
\CodelineIndex
\newcommand\TEX     {\TeX\xspace}
\newcommand\LATEX   {\LaTeX\xspace}
\newcommand\LUA     {Lua\xspace}
\newcommand\LUATEX  {\LuaTeX\xspace}
\newcommand\CONTEXT {Con\TeX t\xspace}

\def\definehighlight[#1][#2]%
 {\ifcsname #1\endcsname\else
    \expandafter\def\csname #1\endcsname%
      {\bgroup#2\csname #1_indeed\endcsname}
    \expandafter\def\csname #1_indeed\endcsname##1%
      {##1\egroup}%
  \fi}

\def\restoreunderscore{\catcode`\_=12\relax}

\definehighlight    [fileent][\ttfamily\restoreunderscore]         %% files, dirs
\definehighlight   [texmacro][\sffamily\itshape\textbackslash]     %% cs
\definehighlight   [luaident][\sffamily\itshape\restoreunderscore] %% lua identifiers
\definehighlight [identifier][\sffamily]                           %% names
\definehighlight     [abbrev][\rmfamily\scshape]                   %% acronyms
\definehighlight   [emphasis][\rmfamily\slshape]                   %% level 1 emph

\usepackage{titlesec}
\def\movecountertomargin#1{\llap{\rmfamily\upshape#1\hskip2em}}
\def\zeropoint{0pt}
%% we don’t have to grab attention like a child
\titleformat \part
             {\normalsize\bfseries}
             {\movecountertomargin\thepart} \zeropoint {}
\titleformat \section
             {\normalsize\scshape}%% no \word; life is full of disappointments
             {\movecountertomargin\thesection} \zeropoint {}
\titleformat \subsection
             {\small\itshape}
             {\movecountertomargin\thesubsection} \zeropoint {}
\renewcommand\partname{Part}%% gets rid of the stupid “file” heading

\usepackage{tocloft}
\renewcommand \cftpartfont   {\rmfamily\upshape}
\renewcommand \cftsecfont    {\rmfamily\upshape}
\renewcommand \cftsubsecfont {\rmfamily\upshape}
\setlength \cftbeforepartskip {1ex}
\setlength \cftbeforesecskip  {1ex}

\begin{document}
  \DocInput{lualibs.dtx}%
\end{document}
%</driver>
% \fi
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{lualibs.drv}
%
% \title{The \identifier{lualibs} package}
% \date{2021/05/20 v2.74}
% \author{Élie Roux      · \email{elie.roux@telecom-bretagne.eu}\\
%         Philipp Gesang · \email{phg@phi-gamma.net}\\
%         The \LaTeX{} Project · \email{https://github.com/latex3/lualibs/}\\
%         }
%
% \maketitle
%
% \begin{abstract}
% Additional \LUA functions taken from the \verb|l-*| and \verb|util-*| files
% of \CONTEXT.
% For an introduction on this package (among others), please refer
% to the document \fileent{lualatex-doc.pdf}.
% \end{abstract}
%
% \tableofcontents
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \part{Package Description}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Overview}
%
% \LUA is a very minimal language, and it does only have a minimal standard
% library. The aim of this package is to provide an extended standard library,
% to be used by various \LUATEX packages. The code is specific to \LUATEX
% and depends on \LUATEX functions and modules not available in regular lua.
%
% \noindent The code is derived from \CONTEXT libraries.
%
% \section{Usage}
%
% You can either load the \identifier{lualibs} module, which will in turn load
% one of two sets of libraries provided by this package: |require("lualibs")|, or
% explicitly load the modules you need, e.g.: |require("lualibs-table")|,
% please note that some modules depend on others.
%
% \subsection{Loading Library Collections}
%
% The libraries are split into a \identifier{basic} and an
% \identifier{extended} collection.
% Though neither initialization time nor memory consumption will be noticably
% impacted,\footnote{%
%   Note that in terms of code this is only a small fraction of what
%   \CONTEXT loads with every run.
% }
% the \identifier{lualibs} package can skip loading of the latter on demand.
% The \luaident{config} table needs to be present prior to loading the
% package for this to work (in the future this may be achieved by an option of
% \texmacro{usepackage}) for \LATEX users).
% In the \verb|lualibs| field, set \verb|load_extended| to false:
%
% \begin{quote}\begin{verbatim}
%   \directlua{
%     --- 1) create config table safely
%     config                        = config or { }
%     config.lualibs                = config.lualibs or { }
%
%     --- 2) unset the load_extended entry
%     config.lualibs.load_extended  = false
%
%     --- 3) load the lualibs
%     require "lualibs"
%   }
% \end{verbatim}\end{quote}
%
% However, there is no guarantee that the \identifier{extended} set remains
% permanently excluded.
% Re-loading the package at a later point will cancel this option and possibly
% add the missing libraries.
%
% \subsection{Loading libraries Individually}
% In order to load the \fileent{table} module you would actually have to prepare
% it as follows:
%
% \begin{quote}\begin{verbatim}
%   require"lualibs-lua"
%   require"lualibs-lpeg"
%   require"lualibs-table"
% \end{verbatim}\end{quote}
%
% \noindent
% If your code is run by the \fileent{texlua} intepreter, you will need to
% initialize \luaident{kpse} library so that |require()| can find files
% under \abbrev{texmf} tree: |kpse.set_program_name("luatex")|.
%
% \section{Files}
%
% The \identifier{lualibs} bundle contains files from two \CONTEXT \LUA
% library categories: The generic auxiliary functions (original file prefix:
% |l-|) together form something close to a standard libary. Most of these are
% extensions of an existing namespace, like for instance |l-table.lua| which
% adds full-fledged serialization capabilities to the \LUA table library.
% They were imported under the \identifier{lualibs}-prefix and are contained
% in the \identifier{basic} collection.
% (For a list see table~\ref{tab:basic}.)
%
% \begin{table}[t]
%  \hrule
%  \centering
%  \caption{The \identifier{basic} set.}
%  \vskip1em
%  \begin{tabular}{l l l}
%   \identifier{lualibs} name & \CONTEXT name  & primary purpose                \\[1ex]
%   lualibs-lua.lua           & l-lua.lua      & compatibility                  \\
%   lualibs-package.lua       & l-package.lua  & \LUA file loaders              \\
%   lualibs-lpeg.lua          & l-lpeg.lua     & patterns                       \\
%   lualibs-function.lua      & l-function.lua & defines a dummy function       \\
%   lualibs-string.lua        & l-string.lua   & string manipulation            \\
%   lualibs-table.lua         & l-table.lua    & serialization, conversion      \\
%   lualibs-boolean.lua       & l-boolean.lua  & boolean converter              \\
%   lualibs-number.lua        & l-number.lua   & bit operations                 \\
%   lualibs-math.lua          & l-math.lua     & math functions                 \\
%   lualibs-io.lua            & l-io.lua       & reading and writing files      \\
%   lualibs-os.lua            & l-os.lua       & platform specific code         \\
%   lualibs-file.lua          & l-file.lua     & filesystem operations          \\
%   lualibs-gzip.lua          & l-gzip.lua     & wrapper for \identifier{lgzip} \\
%   lualibs-md5.lua           & l-md5.lua      & checksum functions             \\
%   lualibs-dir.lua           & l-dir.lua      & directory handling             \\
%   lualibs-unicode.lua       & l-unicode.lua  & utf and unicode                \\
%   lualibs-url.lua           & l-url.lua      & url handling                   \\
%   lualibs-set.lua           & l-set.lua      & sets                           \\[1ex]
%  \end{tabular}
%  \label{tab:basic}
%  \hrule
% \end{table}
%
% The \identifier{extended} category comprises a selection of files mostly from
% the utilities namespace (|util-|; cf. table~\ref{tab:extended}).
% Their purpose is more specific and at times quite low-level.
% Additionally, the file \fileent{trac-inf.lua} has been included because it is
% essential to some of the code loaded subsequently.
%
% \begin{table}[t]
%  \hrule
%  \centering
%  \caption{The \identifier{extended} set.}
%  \vskip1em
%  \begin{tabular}{l l l}
%   \identifier{lualibs} name & \CONTEXT name  & primary purpose                  \\[1ex]
%   lualibs-util-str.lua      & util-str.lua   & extra |string| functions         \\
%   lualibs-util-fil.lua      & util-fil.lua   & extra |file| functions           \\
%   lualibs-util-tab.lua      & util-tab.lua   & extra |table| functions          \\
%   lualibs-util-sto.lua      & util-sto.lua   & table allocation                 \\
%   lualibs-util-prs.lua      & util-sto.lua   & miscellaneous parsers            \\
%   lualibs-util-dim.lua      & util-dim.lua   & conversion between dimensions    \\
%   lualibs-trac-inf.lua      & trac-inf.lua   & timing, statistics               \\
%   lualibs-util-lua.lua      & util-lua.lua   & operations on bytecode           \\
%   lualibs-util-deb.lua      & util-deb.lua   & extra |debug| functionality      \\
%   lualibs-util-tpl.lua      & util-tpl.lua   & templating                       \\
%   lualibs-util-sta.lua      & util-sta.lua   & stacker (e.~g. for \abbrev{pdf}) \\
%   lualibs-util-jsn.lua      & util-jsn.lua   & conversion to and from json      \\[1ex]
%  \end{tabular}
%  \label{tab:extended}
%  \hrule
% \end{table}
%
% \section{Packaging}
%
% By default, \identifier{lualibs} will not load the libraries individually.
% Instead, it includes two \emphasis{merged packages} that have been compiled
% from the original files.
% This is achieved by means of \identifier{mtx-package}, a script for bundling
% \LUA code shipped with \CONTEXT.
% This concatenates the code of several \LUA files into a single file that is
% both easier to distribute and loading marginally faster.
% \identifier{mtx-package} ensures that the code from each file gets its
% own closure and strips newlines and comments, resulting in a smaller payload.
% Another package that relies on it heavily is the font loader as contained in
% \identifier{luaotfload} and \identifier{luatex-fonts}.
% \identifier{Luaotfload}, a port of the \CONTEXT fontloader for Plain and
% \LATEX, has a hard dependency on the functionality provided by the %
% \identifier{Lualibs} package. The packages should not be updated independently.
%
% If \CONTEXT is installed on the system, the merge files can be created
% by running:
%
% \begin{quote}\begin{verbatim}
%   mtxrun --script package --merge lualibs-basic.lua
%   mtxrun --script package --merge lualibs-extended.lua
% \end{verbatim}\end{quote}
%
% \noindent
% Of course there is a make target for that:
%
% \begin{quote}\begin{verbatim}
%   make merge
% \end{verbatim}\end{quote}
% \noindent
% will take care of assembling the packages from the files distributed with
% \identifier{lualibs}.
%
% For this to work, the syntax of the \LUA file needs to be well-formed:
% files that should be merged must be included via a function
% \verb|loadmodule()|.
% It doesn’t matter if the function actually does something; a dummy will
% suffice.
% Also, the argument to \verb|loadmodule()| must be wrapped in parentheses.
% This rule is quite convenient, actually, since it allows excluding files
% from the merge while still using \verb|loadmodule()| consistently.
%
% \begin{quote}\begin{verbatim}
%   ...
%   loadmodule("my-lua-file.lua") -- <= will be merged
%   loadmodule('my-2nd-file.lua') -- <= will be merged
%   loadmodule "my-3rd-file.lua"  -- <= will be ignored
%   ...
% \end{verbatim}\end{quote}
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \part{\texorpdfstring{\fileent{lualibs.lua}}{lualibs.lua}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \iffalse
%<*lualibs>
% \fi
%    \begin{macrocode}
lualibs = lualibs or { }

lualibs.module_info = {
  name          = "lualibs",
  version       = "2.74",       --TAGVERSION
    date        = "2021-05-20", --TAGDATE
  description   = "ConTeXt Lua standard libraries.",
  author        = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux & Philipp Gesang",
  copyright     = "PRAGMA ADE / ConTeXt Development Team",
  license       = "See ConTeXt's mreadme.pdf for the license",
}

%    \end{macrocode}
%   The behavior of the lualibs can be configured to some extent.
%   \begin{itemize}
%     \item Based on the parameter \verb|lualibs.prefer_merged|, the
%           libraries can be loaded via the included merged packages or
%           the individual files.
%     \item Two classes of libraries are distinguished, mainly because
%           of a similar distinction in \CONTEXT, but also to make
%           loading of the less fundamental functionality optional.
%           While the “basic” collection is always loaded, the
%           configuration setting \verb|lualibs.load_extended| triggers
%           inclusion of the extended collection.
%     \item Verbosity can be increased via the \verb|verbose| switch.
%   \end{itemize}
%
%    \begin{macrocode}

config           = config or { }
config.lualibs   = config.lualibs or { }

if config.lualibs.prefer_merged ~= nil then
  lualibs.prefer_merged = config.lualibs.prefer_merged
else
  lualibs.prefer_merged = true
end

if config.lualibs.load_extended ~= nil then
  lualibs.load_extended = config.lualibs.load_extended
else
  lualibs.load_extended = true
end

if config.lualibs.verbose ~= nil then
  config.lualibs.verbose = config.lualibs.verbose
else
  config.lualibs.verbose = false
end

%    \end{macrocode}
%     The lualibs may be loaded in scripts.
%     To account for the different environment, fallbacks for
%     the luatexbase facilities are installed.
%
%    \begin{macrocode}

local dofile          = dofile
local kpsefind_file   = kpse.find_file
local stringformat    = string.format
local texiowrite_nl   = texio.write_nl

local find_file, error, warn, info
do
  local _error, _warn, _info
  if luatexbase and luatexbase.provides_module then
    _error, _warn, _info = luatexbase.provides_module(lualibs.module_info)
  else
    _error, _warn, _info = texiowrite_nl, texiowrite_nl, texiowrite_nl
  end

  if lualibs.verbose then
    error, warn, info = _error, _warn, _info
  else
    local dummylogger = function ( ) end
    error, warn, info = _error, dummylogger, dummylogger
  end
  lualibs.error, lualibs.warn, lualibs.info = error, warn, info
end

if status.kpse_used == 0 then
 kpse.set_program_name("luatex")
end

find_file = kpsefind_file

%    \end{macrocode}
%     The lualibs load a merged package by default.
%     In order to create one of these, the meta file that includes the
%     libraries must satisfy certain assumptions \verb|mtx-package| makes
%     about the coding style.
%     Most important is that the functions that indicates which files
%     to include must go by the name \verb|loadmodule()|.
%     For this reason we define a \verb|loadmodule()| function as a
%     wrapper around \verb|dofile()|.
%
%    \begin{macrocode}

local loadmodule = loadmodule or function (name, t)
  if not t then t = "library" end
  local filepath  = find_file(name, "lua")
  if not filepath or filepath == "" then
    warn(stringformat("Could not locate %s “%s”.", t, name))
    return false
  end
  dofile(filepath)
  return true
end

lualibs.loadmodule = loadmodule

%    \end{macrocode}
%     The separation of the “basic” from the “extended” sets coincides
%     with the split into luat-bas.mkiv and luat-lib.mkiv.
%
%    \begin{macrocode}

if lualibs.basic_loaded        ~= true
or config.lualibs.force_reload == true
then
  loadmodule"lualibs-basic.lua"
  loadmodule"lualibs-compat.lua" --- restore stuff gone since v1.*
end

if  lualibs.load_extended       == true
and lualibs.extended_loaded     ~= true
or  config.lualibs.force_reload == true
then
  loadmodule"lualibs-extended.lua"
end

--- This restores the default of loading everything should a package
--- have requested otherwise. Will be gone once there is a canonical
--- interface for parameterized loading of libraries.
config.lualibs.load_extended = true

-- vim:tw=71:sw=2:ts=2:expandtab

%    \end{macrocode}
%
% \iffalse
%</lualibs>
% \fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \part{\texorpdfstring{\fileent{lualibs-basic.lua}}{lualibs-basic.lua}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \iffalse
%<*basic>
% \fi
%    \begin{macrocode}
lualibs                 = lualibs or { }
local info              = lualibs.info
local loadmodule        = lualibs.loadmodule

local lualibs_basic_module = {
  name          = "lualibs-basic",
  version       = "2.74",       --TAGVERSION
  date          = "2021-05-20", --TAGDATE
  description   = "ConTeXt Lua libraries -- basic collection.",
  author        = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux & Philipp Gesang",
  copyright     = "PRAGMA ADE / ConTeXt Development Team",
  license       = "See ConTeXt's mreadme.pdf for the license",
}

local loaded = false --- track success of package loading

if lualibs.prefer_merged then
  info"Loading merged package for collection “basic”."
  loaded = loadmodule('lualibs-basic-merged.lua')
else
  info"Ignoring merged packages."
  info"Falling back to individual libraries from collection “basic”."
end

%    \end{macrocode}
%     \verb|mtx-package| expects the files to be included by
%     \verb|loadmodule|.
%     If run on this file, it will create \verb|lualibs-basic-merged.lua|
%     from all the files mentioned in the next block.
%
%    \begin{macrocode}

if loaded == false then
  loadmodule("lualibs-lua.lua")
  loadmodule("lualibs-package.lua")
  loadmodule("lualibs-lpeg.lua")
  loadmodule("lualibs-function.lua")
  loadmodule("lualibs-string.lua")
  loadmodule("lualibs-table.lua")
  loadmodule("lualibs-boolean.lua")
  loadmodule("lualibs-number.lua")
  loadmodule("lualibs-math.lua")
  loadmodule("lualibs-io.lua")
  loadmodule("lualibs-os.lua")
  loadmodule("lualibs-file.lua")
  loadmodule("lualibs-gzip.lua")
  loadmodule("lualibs-md5.lua")
  loadmodule("lualibs-dir.lua")
  loadmodule("lualibs-unicode.lua")
  loadmodule("lualibs-url.lua")
  loadmodule("lualibs-set.lua")
end

lualibs.basic_loaded = true
-- vim:tw=71:sw=2:ts=2:expandtab

%    \end{macrocode}
%
% \iffalse
%</basic>
% \fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \part{\texorpdfstring{\fileent{lualibs-extended.lua}}{lualibs-extended.lua}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \iffalse
%<*extended>
% \fi
%    \begin{macrocode}
lualibs = lualibs or { }

%    \end{macrocode}
%     Loading the \emph{extended} set requires a tad more effort, but
%     it’s well invested.
%
%     Since we only want the functionality, we have to simulate parts
%     of a running \CONTEXT environment, above all logging, that some
%     of the more involved libraries cannot be loaded without.
%     Also, one utility file cannot be packaged because it returns a
%     table which would preclude loading of later code.
%     Thus, we remove it from the natural loading chain (it is not
%     critical) and append it at the end.
%
%    \begin{macrocode}

local lualibs_extended_module = {
  name          = "lualibs-extended",
  version       = "2.74",       --TAGVERSION
  date          = "2021-05-20", --TAGDATE
  description   = "ConTeXt Lua libraries -- extended collection.",
  author        = "Hans Hagen, PRAGMA-ADE, Hasselt NL & Elie Roux & Philipp Gesang",
  copyright     = "PRAGMA ADE / ConTeXt Development Team",
  license       = "See ConTeXt's mreadme.pdf for the license",
}


local stringformat     = string.format
local loadmodule       = lualibs.loadmodule
local texiowrite       = texio.write
local texiowrite_nl    = texio.write_nl

%    \end{macrocode}
%     Here we define some functions that fake the elaborate
%     logging/tracking mechanism Context provides.
%
%    \begin{macrocode}

local error, logger, mklog
if luatexbase and luatexbase.provides_module then
  --- TODO test how those work out when running tex
  local __error,_,_,__logger =
    luatexbase.provides_module(lualibs_extended_module)
  error  = __error
  logger = __logger
  mklog = function ( ) return logger end
else
  mklog = function (t)
    local prefix = stringformat("[%s] ", t)
    return function (...)
      texiowrite_nl(prefix)
      texiowrite   (stringformat(...))
    end
  end
  error  = mklog"ERROR"
  logger = mklog"INFO"
end

local info = lualibs.info

%    \end{macrocode}
%     We temporarily put our own global table in place and restore
%     whatever we overloaded afterwards.
%
%     \CONTEXT\ modules each have a custom logging mechanism that can be
%     enabled for debugging.
%     In order to fake the presence of this facility we need to define at
%     least the function \verb|logs.reporter|.
%     For now it’s sufficient to make it a reference to \verb|mklog| as
%     defined above.
%
%    \begin{macrocode}

local dummy_function = function ( ) end
local newline        = function ( ) texiowrite_nl"" end

local fake_logs = function (name)
  return {
    name     = name,
    enable   = dummy_function,
    disable  = dummy_function,
    reporter = mklog,
    newline  = newline
  }
end

local fake_trackers = function (name)
  return {
    name     = name,
    enable   = dummy_function,
    disable  = dummy_function,
    register = mklog,
    newline  = newline,
  }
end

local backup_store = { }

local fake_context = function ( )
  if logs     then backup_store.logs     = logs     end
  if trackers then backup_store.trackers = trackers end
  logs     = fake_logs"logs"
  trackers = fake_trackers"trackers"
end


%    \end{macrocode}
% Restore a backed up logger if appropriate.
%
%    \begin{macrocode}
local unfake_context = function ( )
  if backup_store then
    local bl, bt = backup_store.logs, backup_store.trackers
    if bl   then logs     = bl   end
    if bt   then trackers = bt   end
  end
end

fake_context()

local loaded = false
if lualibs.prefer_merged then
  info"Loading merged package for collection “extended”."
  loaded = loadmodule('lualibs-extended-merged.lua')
else
  info"Ignoring merged packages."
  info"Falling back to individual libraries from collection “extended”."
end

if loaded == false then
  loadmodule("lualibs-util-str.lua")--- string formatters (fast)
  loadmodule("lualibs-util-fil.lua")--- extra file helpers
  loadmodule("lualibs-util-tab.lua")--- extended table operations
  loadmodule("lualibs-util-sto.lua")--- storage (hash allocation)
  ----------("lualibs-util-pck.lua")---!packers; necessary?
  ----------("lualibs-util-seq.lua")---!sequencers (function chaining)
  ----------("lualibs-util-mrg.lua")---!only relevant in mtx-package
  loadmodule("lualibs-util-prs.lua")--- miscellaneous parsers; cool. cool cool cool
  ----------("lualibs-util-fmt.lua")---!column formatter (rarely used)
  loadmodule("lualibs-util-dim.lua")--- conversions between dimensions
  loadmodule("lualibs-util-jsn.lua")--- JSON parser

  ----------("lualibs-trac-set.lua")---!generalization of trackers
  ----------("lualibs-trac-log.lua")---!logging
  loadmodule("lualibs-trac-inf.lua")--- timing/statistics
  loadmodule("lualibs-util-lua.lua")--- operations on lua bytecode
  loadmodule("lualibs-util-deb.lua")--- extra debugging
  loadmodule("lualibs-util-tpl.lua")--- templating
  loadmodule("lualibs-util-sta.lua")--- stacker (for writing pdf)
end

unfake_context() --- TODO check if this works at runtime

lualibs.extended_loaded = true
-- vim:tw=71:sw=2:ts=2:expandtab

%    \end{macrocode}
%
% \iffalse
%</extended>
% \fi
%
% \Finale
\endinput