Debian Codegraph @

texk/texlive/linked_scripts/glossaries/makeglossaries-lite.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2 --[[
3 File : makeglossaries-lite.lua
4 Author : Nicola Talbot
5
6 Lua alternative to the makeglossaries Perl script.
7
8 Since Lua has limitations, this script isn't an exact
9 replacement to the Perl script. In particular the makeglossaries -d
10 switch isn't implemented in this Lua version.
11 This also doesn't provide the more detailed diagnostics that the Perl
12 version does nor does it attempt any language mappings. Since xindy
13 requires Perl, don't use this script if you want to use xindy. Instead
14 use the Perl makeglossaries script.
15
16 This file is distributed as part of the glossaries LaTeX package.
17 Copyright 2015-2025 Nicola L.C. Talbot
18 This work may be distributed and/or modified under the
19 conditions of the LaTeX Project Public License, either version 1.3
20 of this license or any later version.
21 The latest version of this license is in
22 http://www.latex-project.org/lppl.txt
23 and version 1.3 or later is part of all distributions of LaTeX
24 version 2005/12/01 or later.
25
26 This work has the LPPL maintenance status `maintained'.
27
28 This work consists of the files glossaries.dtx and glossaries.ins
29 and the derived files glossaries.sty, glossaries-prefix.sty,
30 glossary-hypernav.sty, glossary-inline.sty, glossary-list.sty,
31 glossary-long.sty, glossary-longbooktabs.sty, glossary-longragged.sty,
32 glossary-mcols.sty, glossary-super.sty, glossary-superragged.sty,
33 glossary-tree.sty, glossaries-compatible-207.sty,
34 glossaries-compatible-307.sty, glossaries-accsupp.sty,
35 glossaries-babel.sty, glossaries-polyglossia.sty, glossaries.l2h.
36 Also makeglossaries and makeglossaries-lite.lua.
37
38 History:
39 * 4.48 - 4.7:
40 - no change.
41 * 4.47:
42 - Added hybrid instructions if record option detected but not \makeglossaries
43 - Added extra info to error message on -d
44 - Fixed bug #183 that occurs when \@istfilename is missing
45 - Only add double-quotes if text contains any spaces which
46 may help address issue #129
47 * 4.40 - 4.46:
48 - no change.
49 * 4.39:
50 - corrected script name in version and help messages
51 * 4.38:
52 - no change.
53 * 4.37:
54 - no change.
55 * 4.36:
56 - fixed check for double-quotes (from \jobname when the file name
57 contains spaces).
58 * 4.35:
59 - no change.
60 * 4.34:
61 - added check for \glsxtr@resource
62 * 4.33:
63 - version number synchronized with glossaries.sty
64 * 1.3
65 - added check for \glsxtr@makeglossaries
66 * 1.2 (2016-05-27)
67 - added check for \@gls@extramakeindexopts
68 - added check for nil codepage
69 * 1.1
70 - changed first line from lua to texlua
71 --]]
72
73 thisversion = "4.7 (2025-05-14)"
74
75 quiet = false
76 dryrun = false
77
78 infile = nil
79 outfile = nil
80 styfile = nil
81 logfile = nil
82
83 isxindy = false
84
85 isbib2gls = false
86
87 xindylang = nil
88 xindyexec = "xindy"
89
90 makeindex_c = false
91 makeindex_g = false
92 letterorder = false
93 makeindex_r = false
94 makeindex_p = nil
95 makeindex_extra = nil
96 makeindex_m = "makeindex"
97
98 function version()
99
100 verYear = string.match(thisversion, "%d%d%d%d");
101
102 print(string.format("makeglossaries-lite version %s", thisversion))
103 print(string.format("Copyright (C) 2015-%s Nicola L C Talbot", verYear))
104 print("This material is subject to the LaTeX Project Public License.")
105 end
106
107 function help()
108 version()
109 print([[
110 Syntax : makeglossaries-lite [options] <filename>
111
112 For use with the glossaries package to pass relevant
113 files to makeindex or xindy.
114
115 <filename> Base name of glossary file(s). This should
116 be the name of your main LaTeX document without any
117 extension. If you do add an extension, only that
118 glossary file will be processed.
119
120 General Options:
121
122 -o <gls> Use <gls> as the output file.
123 (Don't use -o if you have more than one glossary.)
124 -s <sty> Employ <sty> as the style file.
125 -t <log> Employ <log> as the transcript file.
126 (Don't use -t if you have more than one glossary
127 or the transcripts will be overwritten.)
128 -q Quiet mode.
129 -l Letter ordering.
130 -n Print the command that would normally be executed,
131 but don't execute it (dry run).
132 --help Print this help message.
133 --version Print the version.
134
135 Xindy Options:
136
137 -L <language> Use <language>.
138 -x <file> Full path to xindy executable.
139 (Default assumes xindy is on the operating system's path.)
140
141 Makeindex Options:
142 (See makeindex documentation for further details on these options.)
143
144 -c Compress intermediate blanks.
145 -g Employ German word ordering.
146 -p <num> Set the starting page number to be <num>.
147 -r Disable implicit page range formation.
148 -m <file> Full path to makeindex executable.
149 (Default assumes makeindex is on the operating system's path.)
150
151 This is a light-weight Lua alternative to the makeglossaries Perl script.
152 If you want to use xindy, it's better to use the Perl makeglossaries version
153 instead.
154
155 ]])
156 end
157
158 function dorun(name, glg, gls, glo, language, codepage)
159
160 if isxindy then
161 doxindy(name, glg, gls, glo, language, codepage)
162 else
163 domakeindex(name, glg, gls, glo)
164 end
165
166 end
167
168 -- v4.47 only add double-quotes if text contains any spaces
169 function quote_if_spaced(str)
170
171 if string.find(str, " ") then
172 str = '"' .. str .. '"'
173 end
174
175 return str;
176
177 end
178
179 function doxindy(name, glg, gls, glo, language, codepage)
180
181 if codepage == nil
182 then
183 codepage = "utf8"
184 end
185
186 cmd = string.format('%s -I xindy -L %s -C %s -M %s -t %s -o %s',
187 quote_if_spaced(xindyexec), language, codepage, quote_if_spaced(styfile), quote_if_spaced(glg), quote_if_spaced(gls))
188
189 if letterorder then cmd = string.format('%s -M ord/letorder', cmd) end
190
191 if quiet then cmd = string.format('%s -q', cmd) end
192
193 cmd = string.format('%s %s', cmd, quote_if_spaced(glo))
194
195 if dryrun then
196
197 print(cmd)
198
199 else
200
201 assert(os.execute(cmd),
202 string.format("Failed to execute '%s'", cmd))
203
204 end
205
206 end
207
208 function domakeindex(name, glg, gls, glo)
209
210 cmd = quote_if_spaced(makeindex_m)
211
212 if makeindex_c then cmd = cmd .. " -c" end
213
214 if makeindex_g then cmd = cmd .. " -g" end
215
216 if letterorder then cmd = cmd .. " -l" end
217
218 if makeindex_extra then cmd = cmd .. " " .. makeindex_extra end
219
220 if quiet then cmd = cmd .. " -q" end
221
222 if glg ~= nil then cmd = string.format('%s -t %s', cmd, quote_if_spaced(glg)) end
223
224 if gls ~= nil then cmd = string.format('%s -o %s', cmd, quote_if_spaced(gls)) end
225
226 if makeindex_p ~= nil then
227 cmd = string.format("%s -p %s", cmd, makeindex_p)
228 end
229
230 if styfile ~= nil then
231 cmd = string.format('%s -s %s', cmd, quote_if_spaced(styfile))
232 end
233
234 cmd = string.format('%s %s', cmd, quote_if_spaced(glo))
235
236 if dryrun then
237 print(cmd)
238 else
239 assert(os.execute(cmd),
240 string.format("Failed to execute '%s'", cmd))
241 end
242
243 end
244
245 if #arg < 1
246 then
247 error("Syntax error: filename expected. Use --help for help.")
248 end
249
250 i = 1
251
252 while i <= #arg do
253
254 -- General Options
255 if arg[i] == "-q" then
256 quiet = true
257 elseif arg[i] == "-n"
258 then
259 dryrun = true
260 elseif arg[i] == "-o"
261 then
262 i = i + 1
263 if i > #arg then error("-o requires a filename") end
264 outfile = arg[i]
265 elseif arg[i] == "-s"
266 then
267 i = i + 1
268 if i > #arg then error("-s requires a filename") end
269 styfile = arg[i]
270 elseif arg[i] == "-t"
271 then
272 i = i + 1
273 if i > #arg then error("-t requires a filename") end
274 logfile = arg[i]
275 elseif arg[i] == "--version"
276 then
277 version()
278 os.exit()
279 elseif arg[i] == "--help"
280 then
281 help()
282 os.exit()
283 -- General options for the Perl version that aren't implemented by
284 -- this light-weight version:
285 elseif (arg[i] == "-Q") or (arg[i] == "-k")
286 then
287 print(string.format("Ignoring option '%s' (only available with the Perl version).", arg[i]))
288 elseif arg[i] == "-d"
289 then
290 error(string.format(
291 "The '%s' option isn't available for this light-weight version. \n(Lua doesn't natively provide a function to change directory.) \nYou will need to use the Perl version instead \nor just change directory before running this script.",
292 arg[i]))
293
294 -- Xindy Options
295 elseif arg[i] == "-L"
296 then
297 i = i + 1
298 if i > #arg then error("-L requires a language name") end
299 xindylang = arg[i]
300 elseif arg[i] == "-x"
301 then
302 i = i + 1
303 if i > #arg then error("-x requires a filename") end
304 xindyexec = arg[i]
305
306 -- Makeindex Options
307 elseif arg[i] == "-c"
308 then
309 makeindex_c = true
310 elseif arg[i] == "-g"
311 then
312 makeindex_g = true
313 elseif arg[i] == "-l"
314 then
315 letterorder = true
316 elseif arg[i] == "-r"
317 then
318 makeindex_r = true
319 elseif arg[i] == "-p"
320 then
321 i = i + 1
322 if i > #arg then error("-p requires a page number") end
323 makeindex_p = arg[i]
324 elseif arg[i] == "-m"
325 then
326 i = i + 1
327 if i > #arg then error("-m requires a filename") end
328 makeindex_m = arg[i]
329
330 -- Unknown Option
331 elseif string.sub(arg[i], 1, 1) == "-"
332 then
333 error(
334 string.format("Syntax error: unknown option '%s'. Use '--help' for help.",
335 arg[i]));
336
337 -- Input file
338 elseif infile == nil
339 then
340 infile = arg[i]
341 else
342 error("Syntax error: only one filename permitted");
343 end
344
345 i = i + 1
346 end
347
348 if not quiet then
349 print(string.format("makeglossaries.lua version %s", thisversion))
350 end
351
352 if infile == nil
353 then
354 error("Syntax error: missing filename")
355 end
356
357 i, j = string.find(infile, "%.%a*$")
358
359 ext = nil
360 inbase = infile
361
362 if i ~= nil
363 then
364 ext = string.sub(infile, i, j);
365
366 lext = string.lower(ext)
367
368 inbase = string.sub(infile, 1, i-1);
369
370 -- Just in case user has accidentally specified the aux or tex file
371 if lext == ".aux" or lext == ".tex"
372 then
373 ext = nil
374 infile = inbase
375 end
376 end
377
378 auxfile = inbase..".aux"
379
380 if not quiet then print(string.format("Parsing '%s'", auxfile)) end
381
382 assert(io.input(auxfile),
383 string.format("Unable to open '%s'", auxfile))
384
385 aux = io.read("*a")
386
387 if string.find(aux, "\\glsxtr@resource") ~= nil
388 then
389 isbib2gls = true
390 end
391
392 if styfile == nil
393 then
394
395 -- v4.36: corrected check for double-quotes
396
397 styfile = string.match(aux, "\\@istfilename{([^}]*)}")
398
399 -- v4.47: added check for null styfile (missing \@istfilename)
400 if styfile ~= nil
401 then
402 styfile = string.gsub(styfile, "\"", "");
403 end
404
405 if styfile == nil
406 then
407 if isbib2gls
408 then
409 -- v4.47 added extra hybrid instructions
410 error([[
411 No \@istfilename found but found \glsxtr@resource.
412 You need to run bib2gls not makeglossaries-lite.
413 If you have used record=alsoindex or record=hybrid
414 then add \makeglossaries to your preamble.
415 ]])
416 else
417 error([[
418 No \@istfilename found.
419 Did your LaTeX run fail?
420 Did your LaTeX run produce any output?
421 Did you remember to use \makeglossaries?
422 ]])
423 end
424 end
425 end
426
427 i = string.len(styfile)
428
429 if string.sub(styfile, i-3, i) == ".xdy"
430 then
431 isxindy = true
432 end
433
434 if not letterorder
435 then
436 if string.match(aux, "\\@glsorder{letter}") ~= nil
437 then
438 letterorder = true
439 end
440 end
441
442 makeindex_extra = string.match(aux, "\\@gls@extramakeindexopts{([^}]*%.?%a*)}")
443
444 if dryrun then print("Dry run mode. No commands will be executed.") end
445
446 onlyname = nil
447
448 glossaries = {}
449
450 for name, glg, gls, glo in
451 string.gmatch(aux, "\\@newglossary{([^}]+)}{([^}]+)}{([^}]+)}{([^}]+)}") do
452
453 if not quiet then
454 print(string.format("Found glossary type '%s' (%s,%s,%s)",
455 name, glg, gls, glo))
456 end
457
458 glossaries[name] = {}
459
460 glossaries[name].glg = glg
461 glossaries[name].gls = gls
462 glossaries[name].glo = glo
463
464 if "."..glo == ext then
465
466 onlyname = name
467
468 end
469
470 if isxindy then
471
472 if xindylang == nil then
473 glossaries[name].language = string.match(aux,
474 "\\@xdylanguage{"..name.."}{([^}]+)}");
475 else
476 glossaries[name].language = xindylang
477 end
478
479 glossaries[name].codepage = string.match(aux,
480 "\\@gls@codepage{"..name.."}{([^}]+)}");
481
482 end
483
484 end
485
486 onlytypes = string.match(aux, "\\glsxtr@makeglossaries{([^}]+)}")
487
488 if onlytypes ~= nil
489 then
490 if not quiet then
491 print(string.format("Only process subset: '%s'", onlytypes))
492 end
493
494 onlyglossaries = {}
495
496 for name in string.gmatch(onlytypes, '([^,]+)') do
497 onlyglossaries[name] = glossaries[name]
498 end
499
500 glossaries = onlyglossaries
501 end
502
503 if ext == nil
504 then
505
506 done = false
507
508 for name, value in pairs(glossaries) do
509
510 glg = value.glg
511 gls = value.gls
512 glo = value.glo
513
514 if logfile == nil then
515 glg = inbase .. "." .. glg
516 else
517 glg = logfile
518 end
519
520 if outfile == nil then
521 gls = inbase .. "." .. gls
522 else
523 gls = outfile
524 end
525
526 glo = infile .. "." .. glo
527
528 dorun(name, glg, gls, glo, value.language, value.codepage)
529
530 done = true
531 end
532
533 if not done then
534 error([[
535 No \@newglossary commands found in aux file.
536 Did you remember to use \makeglossaries?
537 Did you accidentally suppress the default glossary using "nomain"
538 and not provide an alternative glossary?
539 ]])
540 end
541
542 else
543
544 if onlyname == nil then
545
546 glo = infile
547 gls = outfile
548 glg = logfile
549
550 language = xindylang
551 codepage = 'utf8'
552
553 if language == nil then language = 'english' end
554
555 if gls == nil then gls = infile..".gls" end
556
557 else
558
559 value = glossaries[onlyname]
560
561 glg = value.glg
562 gls = value.gls
563 glo = infile
564
565 if logfile == nil then
566 glg = inbase .. "." .. glg
567 else
568 glg = logfile
569 end
570
571 if outfile == nil then
572 gls = inbase .. "." .. gls
573 else
574 gls = outfile
575 end
576
577 end
578
579 if codepage == nil then
580 codepage = 'utf8';
581 end
582
583 dorun(onlyname, glg, gls, glo, language, codepage)
584 end