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
|
% \iffalse meta-comment
%
% Copyright 2009, 2010 by Élie Roux <elie.roux@telecom-bretagne.eu>
% Copyright 2010, 2011 by Manuel Pégourié-Gonnard <mpg@elzevir.fr>
%
% This work is under the CC0 license.
%
% This work consists of the main source file luatexbase-modutils.dtx
% and the derived files
% luatexbase-modutils.sty modutils.lua
% test-modutils-plain.tex test-modutils-latex.tex test-modutils.lua
%
% Unpacking:
% tex luatexbase-modutils.dtx
% Documentation:
% pdflatex luatexbase-modutils.dtx
%
%<*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
\keepsilent
\askforoverwritefalse
\let\MetaPrefix\relax
\preamble
See the aforementioned source file(s) for copyright and licensing information.
\endpreamble
\let\MetaPrefix\DoubleperCent
\generate{%
\usedir{tex/luatex/luatexbase}%
\file{luatexbase-modutils.sty}{\from{luatexbase-modutils.dtx}{texpackage}}%
}
\generate{%
\usedir{doc/luatex/luatexbase}%
\file{test-modutils-plain.tex}{\from{luatexbase-modutils.dtx}{testplain}}%
\file{test-modutils-latex.tex}{\from{luatexbase-modutils.dtx}{testlatex}}%
}
\def\MetaPrefix{-- }
\def\luapostamble{%
\MetaPrefix^^J%
\MetaPrefix\space End of File `\outFileName'.%
}
\def\currentpostamble{\luapostamble}%
\generate{%
\usedir{tex/luatex/luatexbase}%
\file{modutils.lua}{\from{luatexbase-modutils.dtx}{luamodule}}%
\usedir{doc/luatex/luatexbase}%
\file{test-modutils.lua}{\from{luatexbase-modutils.dtx}{testdummy}}%
}
\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* files into a directory searched by TeX:}
\Msg{*}
\Msg{* luatexbase-modutils.sty modutils.lua}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}
\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\documentclass{ltxdoc}
\input{lltxb-dtxstyle}
\begin{document}
\DocInput{luatexbase-modutils.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 \~}
%
% \pkdate{luatexbase-modutils}{v0.4 2011-05-24}
%
% \maketitle
%
% \begin{abstract}
% This package provides functions similar to \latex's |\usepackage| and
% |\ProvidesPackage| macros,\footnote{and their variants or synonyms such as
% \cs{documentclass} and \cs{RequirePackage} or \cs{ProvidesClass} and
% \cs{ProvidesFiles}} or more precisely the part of these macros that deals
% with identification and version checking (no attempt is done at implementing
% an option mechanism). It also provides functions for reporting errors and
% warnings in a standardised format.
% \end{abstract}
%
% \tableofcontents
%
% \section{Documentation}
%
% \subsection{Scope of this package}
%
% Lua's standard function |require()| is similar to \tex's |\input| primitive
% but is somehow more evolved in that it makes a few checks to avoid loading
% the same module twice. In the \tex world, this needs to be taken care of by
% macro packages; in the \latex world this is done by |\usepackage|.
%
% But |\usepackage| also takes care of many other things. Most notably, it
% implements a complex option system, and does some identification and version
% checking. The present package doesn't try to provide anything for options,
% but implements a system for identification and version checking similar to
% \latex's system.
%
% \medskip
%
% It is important to notice that Lua's standard function |module()| is
% completely orthogonal with the present package. It has nothing to do with
% identification and deals only with namespaces: more precisely, it
% modifies the current environment. So, you should continue to
% use it normally regardless of whether you chose to use this package's
% features for identification.
%
% It is recommended to always use |module()| or any other method that ensure
% the global name space remains clean. For example, you may heavily use the
% |local| keyword and explicitly qualify the name of every non-local symbol.
% Chapter 15 of \href{http://www.lua.org/pil/}{Programming in Lua, 1st ed.}
% discusses various methods for managing packages.
%
% \subsection{\tex macros}
%
% \begin{qcode}
% \cs{RequireLuaModule}\marg{name}\oarg{date}
% \end{qcode}
%
% The macro |\RequireLuaModule| is an interface to the Lua function
% |require_module|; it take the same arguments with the same meaning. The
% second argument is optional.
%
% \subsection{Lua functions}
%
% \begin{qcode}
% luatexbase.require_module({\meta{name}} [, \meta{required date}])
% \end{qcode}
%
% The function |luatexbase.require_module()| may be used as a replacement to
% |require()|. If only one argument is given, the only difference with
% |require()| is it checks that the module properly identifies itself (as
% explained below) with the same name.
%
% The second argument is optional; if used, it must be a
% string\footnote{Previous versions of the package supported floating-point
% version numbers as well, but it caused confusion with authors trying to use
% version strings such as |0.3a| and probably isn't worth the trouble.}
% containing a date in |YYYY//MM/DD| format which specifies the minimum
% version of the module required.
%
% \begin{qcode}
% luatexbase.provides_module(\meta{info})
% \end{qcode}
% This function is used by modules to identify themselves; the argument is a
% table containing information about the module. The required field |name|
% must contain the name of the module. It is recommended to provide a field
% |date| with the same format as above. Optional fields |version| (number or
% string) and |description| may be used if present. Other fields are ignored.
%
% If a date was required, then a warning is issued if the required date is
% strictly newer than the declared date (or if no date was declared). A list
% of loaded modules and their associated information is kept, and used to
% check the date without reloading the module (since |require()| won't reload
% it anyway) if a module is required several times.
%
% \begin{qcode}
% luatexbase.module_error(\meta{name}, \meta{message}, ...)
% luatexbase.module_warning(\meta{name}, \meta{message}, ...)
% luatexbase.module_info(\meta{name}, \meta{message}, ...)
% luatexbase.module_log(\meta{name}, \meta{message}, ...)
% \end{qcode}
% These functions are similar to \latex's |\PackageError|, |\PackageWarning|
% and |\PackageInfo| in the way they format the output. No automatic line
% breaking is done, you may still use |\n| as usual for that, and the name of
% the package will be prepended to each output line (except for |log| which is
% intended for short messages in a non-verbose format). The first argument
% is the name of the current module; the remaining arguments are passed to
% |string.format()|.
%
% Note that |module_error| raises an actual Lua error with |error()|, which
% currently means a call stack will be dumped. While this may not look pretty,
% at least it provides useful information for tracking the error down.
%
% \begin{qcode}
% local err, warn, info, log = luatexbase.errwarinf(\meta{name})
% local err, warn, info, log = luatexbase.provides_module(\meta{name})
% \end{qcode}
% Customised versions of the above commands maybe obtained by invoking
% |errwarinf()| and are also returned by |provides_module()|. They don't take
% the name of the module as their first argument any more, so that you don't
% need to repeat it all over the place. (Notice that |error| is the name of a
% standard Lua function, so you may want to avoid overwriting it, hence the
% use of |err| in the above example.)
%
% \subsection{Templates}
%
% Let me emphasize again that, while |luatexbase.require_module()| is meant to
% be used as a replacement for |require()|, the function
% |luatexbase.provides_module()| \emph{is not} a replacement for |module()|:
% they just don't do the same thing (declaring information vs changing the
% current name space).
%
% Now, here is how you module may begin:
% \begin{verbatim}
% local err, warn, info, log = luatexbase.provides_module({
% -- required
% name = 'mymodule',
% -- recommended
% date = '1970/01/01',
% version = 0.0, -- or version = '0.0a',
% description = 'a Lua module template',
% -- optional and ignored
% author = 'A. U. Thor',
% licence = 'LPPL v1.3+',
% })
%
% module('mynamespace', package.seeall)
% -- or any other method (see chapter 15 of PIL for examples)
% \end{verbatim}
%
% Alternatively, if you don't want to assume \pk{luatexbase-modutils} is
% loaded, you may load your module with:
% \begin{verbatim}
% (luatexbase.require_module or require)('mymodule')
% \end{verbatim}
% and begin your module's code with:
% \begin{verbatim}
% if luatexbase._provides_module then
% luatexbase.provides_module({
% -- required
% name = 'mymodule',
% -- recommended
% date = '1970/01/01',
% version = 0.0, -- or version = '0.0a',
% description = 'a Lua module template',
% -- optional and ignored
% author = 'A. U. Thor',
% licence = 'LPPL v1.3+',
% })
% end
%
% module('mynamespace', package.seeall)
% -- or any other method (see chapter 15 of PIL for examples)
%
% local function err(msg)
% -- etc.
% \end{verbatim}
%
% \section{Implementation}
%
% \subsection{\tex package}
%
% \begin{macrocode}
%<*texpackage>
% \end{macrocode}
%
% \subsubsection{Preliminaries}
%
% Catcode defenses and reload protection.
%
% \begin{macrocode}
\begingroup\catcode61\catcode48\catcode32=10\relax% = and space
\catcode123 1 % {
\catcode125 2 % }
\catcode 35 6 % #
\toks0\expandafter{\expandafter\endlinechar\the\endlinechar}%
\edef\x{\endlinechar13}%
\def\y#1 #2 {%
\toks0\expandafter{\the\toks0 \catcode#1 \the\catcode#1}%
\edef\x{\x \catcode#1 #2}}%
\y 13 5 % carriage return
\y 61 12 % =
\y 32 10 % space
\y 123 1 % {
\y 125 2 % }
\y 35 6 % #
\y 64 11 % @ (letter)
\y 10 12 % new line ^^J
\y 34 12 % "
\y 39 12 % '
\y 40 12 % (
\y 41 12 % )
\y 44 12 % ,
\y 45 12 % -
\y 46 12 % .
\y 47 12 % /
\y 58 12 % :
\y 91 12 % [
\y 93 12 % ]
\y 94 7 % ^
\y 95 8 % _
\y 96 12 % `
\toks0\expandafter{\the\toks0 \relax\noexpand\endinput}%
\edef\y#1{\noexpand\expandafter\endgroup%
\noexpand\ifx#1\relax \edef#1{\the\toks0}\x\relax%
\noexpand\else \noexpand\expandafter\noexpand\endinput%
\noexpand\fi}%
\expandafter\y\csname luatexbase@modutils@sty@endinput\endcsname%
% \end{macrocode}
%
% Package declaration.
%
% \begin{macrocode}
\begingroup
\expandafter\ifx\csname ProvidesPackage\endcsname\relax
\def\x#1[#2]{\immediate\write16{Package: #1 #2}}
\else
\let\x\ProvidesPackage
\fi
\expandafter\endgroup
\x{luatexbase-modutils}[2011/05/24 v0.4 Module utilities for LuaTeX]
% \end{macrocode}
%
% Make sure \luatex is used.
%
% \begin{macrocode}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
\input ifluatex.sty
\else
\RequirePackage{ifluatex}
\fi
\ifluatex\else
\begingroup
\expandafter\ifx\csname PackageError\endcsname\relax
\def\x#1#2#3{\begingroup \newlinechar10
\errhelp{#3}\errmessage{Package #1 error: #2}\endgroup}
\else
\let\x\PackageError
\fi
\expandafter\endgroup
\x{luatexbase-modutils}{LuaTeX is required for this package. Aborting.}{%
This package can only be used with the LuaTeX engine^^J%
(command `lualatex' or `luatex').^^J%
Package loading has been stopped to prevent additional errors.}
\expandafter\luatexbase@modutils@sty@endinput%
\fi
% \end{macrocode}
%
% Load \pk{luatexbase-loader} (hence \pk{luatexbase-compat}), require the
% supporting Lua module and make sure |luaescapestring| is available.
%
% \begin{macrocode}
\ifdefined\RequirePackage
\RequirePackage{luatexbase-loader}
\else
\input luatexbase-loader.sty
\fi
\luatexbase@directlua{require('luatexbase.modutils')}
\luatexbase@ensure@primitive{luaescapestring}
% \end{macrocode}
%
% \subsection{Auxiliary definitions}
%
% We need a version of |\@ifnextchar|. The definitions for the not-\latex
% case are stolen from \pk{ltxcmds} verbatim, only the prefix is changed.
%
% \begin{macrocode}
\ifdefined\kernel@ifnextchar
\let\lltxb@ifnextchar\kernel@ifnextchar
\else
\chardef\lltxb@zero0
\chardef\lltxb@two2
\long\def\lltxb@ifnextchar#1#2#3{%
\begingroup
\let\lltxb@CharToken= #1\relax
\toks\lltxb@zero{#2}%
\toks\lltxb@two{#3}%
\futurelet\lltxb@LetToken\lltxb@ifnextchar@
}
\def\lltxb@ifnextchar@{%
\ifx\lltxb@LetToken\lltxb@CharToken
\expandafter\endgroup\the\toks\expandafter\lltxb@zero
\else
\ifx\lltxb@LetToken\lltxb@SpaceToken
\expandafter\expandafter\expandafter\lltxb@@ifnextchar
\else
\expandafter\endgroup\the\toks
\expandafter\expandafter\expandafter\lltxb@two
\fi
\fi
}
\begingroup
\def\x#1{\endgroup
\def\lltxb@@ifnextchar#1{%
\futurelet\lltxb@LetToken\lltxb@ifnextchar@
}%
}%
\x{ }
\begingroup
\def\x#1{\endgroup
\let\lltxb@SpaceToken= #1%
}%
\x{ }
\fi
% \end{macrocode}
%
% \subsubsection{User macro}
%
% Interface to the Lua function for module loading. Avoid passing a second
% argument to the function if empty (most probably not specified).
%
% \begin{macrocode}
\def\RequireLuaModule#1{%
\lltxb@ifnextchar[{\lltxb@requirelua{#1}}{\lltxb@requirelua{#1}[]}}
\def\lltxb@requirelua#1[#2]{%
\luatexbase@directlua{luatexbase.require_module(
"\luatexluaescapestring{#1}"
\expandafter\ifx\expandafter\/\detokenize{#2}\/\else
, "\luatexluaescapestring{#2}"
\fi)}}
% \end{macrocode}
%
% \begin{macrocode}
\luatexbase@modutils@sty@endinput%
%</texpackage>
% \end{macrocode}
%
% \subsection{Lua module}
%
% \begin{macrocode}
%<*luamodule>
module("luatexbase", package.seeall)
% \end{macrocode}
%
% \subsection{Internal functions and data}
%
% Tables holding informations about the modules loaded and the versions
% required. Keys are module names and values are the info tables as passed
% to |provides_module()|.
%
% \begin{macrocode}
local modules = modules or {}
% \end{macrocode}
%
% Convert a date in YYYY/MM/DD format into a number.
%
% \begin{macrocode}
local function date_to_int(date)
local numbers = string.gsub(date, "(%d+)/(%d+)/(%d+)", "%1%2%3")
return tonumber(numbers)
end
% \end{macrocode}
%
% \subsubsection{Error, warning and info function for modules}
%
% Here are the reporting functions for the modules. An internal function is
% used for error messages, so that the calling level (last argument of
% |error()| remains constant using either |module_error()| or a custom
% version as returned by |errwarinf()|.
%
% \begin{macrocode}
local function msg_format(msg_type, mod_name, ...)
local cont = '('..mod_name..')' .. ('Module: '..msg_type):gsub('.', ' ')
return 'Module '..mod_name..' '..msg_type..': '
.. string.format(...):gsub('\n', '\n'..cont) .. '\n'
end
local function module_error_int(mod, ...)
error(msg_format('error', mod, ...), 3)
end
function module_error(mod, ...)
module_error_int(mod, ...)
end
% \end{macrocode}
%
% Split the lines explicitly in order not to depend on the value of
% |\newlinechar|.
%
% \begin{macrocode}
function module_warning(mod, ...)
for _, line in ipairs(msg_format('warning', mod, ...):explode('\n')) do
texio.write_nl(line)
end
end
function module_info(mod, ...)
for _, line in ipairs(msg_format('info', mod, ...):explode('\n')) do
texio.write_nl(line)
end
end
% \end{macrocode}
%
% No line splitting or advanced formating here.
%
% \begin{macrocode}
function module_log(mod, msg, ...)
texio.write_nl('log', mod..': '..msg:format(...))
end
% \end{macrocode}
%
% Produce custom versions of the reporting functions.
%
% \begin{macrocode}
function errwarinf(name)
return function(...) module_error_int(name, ...) end,
function(...) module_warning(name, ...) end,
function(...) module_info(name, ...) end,
function(...) module_log(name, ...) end
end
% \end{macrocode}
%
% For our own convenience, local functions for warning and errors in the
% present module.
%
% \begin{macrocode}
local err, warn = errwarinf('luatexbase.modutils')
% \end{macrocode}
%
% \subsubsection{module loading and providing functions}
%
% Load a module with mandatory name checking and optional version checking.
%
% \begin{macrocode}
function require_module(name, req_date)
require(name)
local info = modules[name]
if not info then
warn("module '%s' was not properly identified", name)
elseif info.version then
if not (info.date and date_to_int(info.date) > date_to_int(req_date))
then
warn("module '%s' required in version '%s'\n"
.. "but found in version '%s'", name, req_date, info.date)
end
end
end
% \end{macrocode}
%
% Provide identification information for a module. As a bonus, custom
% reporting functions are returned. No need to do any check here,
% everything done in |require_module()|.
%
% \begin{macrocode}
function provides_module(info)
if not (info and info.name) then
err('provides_module: missing information')
end
texio.write_nl('log', string.format("Lua module: %s %s %s %s\n",
info.name, info.date or '', info.version or '', info.description or ''))
modules[info.name] = info
return errwarinf(info.name)
end
% \end{macrocode}
%
% \begin{macrocode}
%</luamodule>
% \end{macrocode}
%
% \section{Test files}
%
% A dummy lua file for tests.
%
% \begin{macrocode}
%<*testdummy>
local err, warn, info, log = luatexbase.provides_module {
name = 'test-modutils',
date = '2000/01/01',
version = 1,
description = 'dummy test package',
}
info('It works!\nOh, rly?\nYeah rly!')
log("I'm a one-line info.")
%</testdummy>
% \end{macrocode}
%
% We just check that the package loads properly, under both LaTeX and Plain
% TeX, is able to load and identify the above dummy module.
%
% \begin{macrocode}
%<testplain>\input luatexbase-modutils.sty
%<testlatex>\RequirePackage{luatexbase-modutils}
%<*testplain,testlatex>
\RequireLuaModule{test-modutils}
\RequireLuaModule{test-modutils}[1970/01/01]
%</testplain,testlatex>
%<testplain>\bye
%<testlatex>\stop
% \end{macrocode}
%
% \Finale
\endinput
|