summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/glossaries/makeglossaries-lite.lua
blob: 860b4e54480585537082bc33668aff752d83238a (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
#!/usr/bin/env texlua
--[[
   File   : makeglossaries-lite.lua
   Author : Nicola Talbot
   
   Lua alternative to the makeglossaries Perl script.

   Since Lua has limitations, this script isn't an exact
   replacement to the Perl script. In particular the makeglossaries -d 
   switch isn't implemented in this Lua version.
   This also doesn't provide the more detailed diagnostics that the Perl
   version does nor does it attempt any language mappings. Since xindy
   requires Perl, don't use this script if you want to use xindy. Instead
   use the Perl makeglossaries script.
  
   This file is distributed as part of the glossaries LaTeX package.
   Copyright 2015-2022 Nicola L.C. Talbot
   This work may be distributed and/or modified under the
   conditions of the LaTeX Project Public License, either version 1.3
   of this license or any later version.
   The latest version of this license is in
     http://www.latex-project.org/lppl.txt
   and version 1.3 or later is part of all distributions of LaTeX
   version 2005/12/01 or later.
  
   This work has the LPPL maintenance status `maintained'.

   This work consists of the files glossaries.dtx and glossaries.ins 
   and the derived files glossaries.sty, glossaries-prefix.sty,
   glossary-hypernav.sty, glossary-inline.sty, glossary-list.sty, 
   glossary-long.sty, glossary-longbooktabs.sty, glossary-longragged.sty,
   glossary-mcols.sty, glossary-super.sty, glossary-superragged.sty, 
   glossary-tree.sty, glossaries-compatible-207.sty, 
   glossaries-compatible-307.sty, glossaries-accsupp.sty, 
   glossaries-babel.sty, glossaries-polyglossia.sty, glossaries.l2h.
   Also makeglossaries and makeglossaries-lite.lua.
  
   History:
   * 4.48 - 4.51:
     - no change.
   * 4.47:
     - Added hybrid instructions if record option detected but not \makeglossaries
     - Added extra info to error message on -d
     - Fixed bug #183 that occurs when \@istfilename is missing
     - Only add double-quotes if text contains any spaces which
       may help address issue #129
   * 4.40 - 4.46:
     - no change.
   * 4.39:
     - corrected script name in version and help messages
   * 4.38:
     - no change.
   * 4.37:
     - no change.
   * 4.36:
     - fixed check for double-quotes (from \jobname when the file name 
       contains spaces).
   * 4.35:
     - no change.
   * 4.34:
     - added check for \glsxtr@resource
   * 4.33:
     - version number synchronized with glossaries.sty
   * 1.3
     - added check for \glsxtr@makeglossaries
   * 1.2 (2016-05-27)
     - added check for \@gls@extramakeindexopts
     - added check for nil codepage
   * 1.1
     - changed first line from lua to texlua
--]]

thisversion = "4.51 (2022-11-02)"

quiet = false
dryrun = false

infile = nil
outfile = nil
styfile = nil
logfile = nil

isxindy = false

isbib2gls = false

xindylang = nil
xindyexec = "xindy"

makeindex_c = false
makeindex_g = false
letterorder = false
makeindex_r = false
makeindex_p = nil
makeindex_extra = nil
makeindex_m = "makeindex"

function version()

  verYear = string.match(thisversion, "%d%d%d%d");

  print(string.format("makeglossaries-lite version %s", thisversion))
  print(string.format("Copyright (C) 2015-%s Nicola L C Talbot", verYear))
  print("This material is subject to the LaTeX Project Public License.")
end

function help()
  version()
  print([[
Syntax : makeglossaries-lite [options] <filename>

For use with the glossaries package to pass relevant
files to makeindex or xindy.

<filename>	Base name of glossary file(s). This should
		be the name of your main LaTeX document without any
		extension. If you do add an extension, only that
		glossary file will be processed.

General Options:

-o <gls>	Use <gls> as the output file.
		(Don't use -o if you have more than one glossary.)
-s <sty>	Employ <sty> as the style file.
-t <log>	Employ <log> as the transcript file.
		(Don't use -t if you have more than one glossary
		or the transcripts will be overwritten.)
-q		Quiet mode.
-l		Letter ordering.
-n		Print the command that would normally be executed,
		but don't execute it (dry run).
--help		Print this help message.
--version	Print the version.

Xindy Options:

-L <language>	Use <language>.
-x <file>	Full path to xindy executable.
		(Default assumes xindy is on the operating system's path.)

Makeindex Options:
(See makeindex documentation for further details on these options.)

-c		Compress intermediate blanks.
-g		Employ German word ordering.
-p <num>	Set the starting page number to be <num>.
-r		Disable implicit page range formation.
-m <file>	Full path to makeindex executable.
		(Default assumes makeindex is on the operating system's path.)

This is a light-weight Lua alternative to the makeglossaries Perl script.
If you want to use xindy, it's better to use the Perl makeglossaries version
instead.

]])
end

function dorun(name, glg, gls, glo, language, codepage)

  if isxindy then
    doxindy(name, glg, gls, glo, language, codepage)
  else
    domakeindex(name, glg, gls, glo)
  end

end

-- v4.47 only add double-quotes if text contains any spaces
function quote_if_spaced(str)

   if string.find(str, " ") then
      str = '"' .. str .. '"' 
   end

   return str;

end

function doxindy(name, glg, gls, glo, language, codepage)

  if codepage == nil
  then
     codepage = "utf8"
  end

  cmd = string.format('%s -I xindy -L %s -C %s -M %s -t %s -o %s',
    quote_if_spaced(xindyexec), language, codepage, quote_if_spaced(styfile), quote_if_spaced(glg), quote_if_spaced(gls))

  if letterorder then cmd = string.format('%s -M ord/letorder', cmd) end

  if quiet then cmd = string.format('%s -q', cmd) end

  cmd = string.format('%s %s', cmd, quote_if_spaced(glo))

  if dryrun then

    print(cmd)

  else

    assert(os.execute(cmd), 
     string.format("Failed to execute '%s'", cmd))

  end

end

function domakeindex(name, glg, gls, glo)

  cmd = quote_if_spaced(makeindex_m)

  if makeindex_c then cmd = cmd .. " -c" end

  if makeindex_g then cmd = cmd .. " -g" end

  if letterorder then cmd = cmd .. " -l" end

  if makeindex_extra then cmd = cmd .. " " .. makeindex_extra end

  if quiet then cmd = cmd .. " -q" end

  if glg ~= nil then cmd = string.format('%s -t %s', cmd, quote_if_spaced(glg)) end

  if gls ~= nil then cmd = string.format('%s -o %s', cmd, quote_if_spaced(gls)) end

  if makeindex_p ~= nil then 
    cmd = string.format("%s -p %s", cmd, makeindex_p)
  end

  if styfile ~= nil then 
    cmd = string.format('%s -s %s', cmd, quote_if_spaced(styfile))
  end

  cmd = string.format('%s %s', cmd, quote_if_spaced(glo))

  if dryrun then
    print(cmd)
  else
    assert(os.execute(cmd), 
     string.format("Failed to execute '%s'", cmd))
  end

end

if #arg < 1
then
  error("Syntax error: filename expected. Use --help for help.")
end

i = 1

while i <= #arg do

-- General Options
  if arg[i] == "-q" then
    quiet = true
  elseif arg[i] == "-n"
  then
    dryrun = true
  elseif arg[i] == "-o"
  then
    i = i + 1
    if i > #arg then error("-o requires a filename") end
    outfile = arg[i]
  elseif arg[i] == "-s"
  then
    i = i + 1
    if i > #arg then error("-s requires a filename") end
    styfile = arg[i]
  elseif arg[i] == "-t"
  then
    i = i + 1
    if i > #arg then error("-t requires a filename") end
    logfile = arg[i]
  elseif arg[i] == "--version"
  then
    version()
    os.exit()
  elseif arg[i] == "--help"
  then
    help()
    os.exit()
-- General options for the Perl version that aren't implemented by
-- this light-weight version:
  elseif (arg[i] == "-Q") or (arg[i] == "-k")
  then
    print(string.format("Ignoring option '%s' (only available with the Perl version).", arg[i]))
  elseif arg[i] == "-d"
  then
    error(string.format(
      "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.",
      arg[i]))

-- Xindy Options
  elseif arg[i] == "-L"
  then
    i = i + 1
    if i > #arg then error("-L requires a language name") end
    xindylang = arg[i]
  elseif arg[i] == "-x"
  then
    i = i + 1
    if i > #arg then error("-x requires a filename") end
    xindyexec = arg[i]

-- Makeindex Options
  elseif arg[i] == "-c"
  then
    makeindex_c = true
  elseif arg[i] == "-g"
  then
    makeindex_g = true
  elseif arg[i] == "-l"
  then
    letterorder = true
  elseif arg[i] == "-r"
  then
    makeindex_r = true
  elseif arg[i] == "-p"
  then
    i = i + 1
    if i > #arg then error("-p requires a page number") end
    makeindex_p = arg[i]
  elseif arg[i] == "-m"
  then
    i = i + 1
    if i > #arg then error("-m requires a filename") end
    makeindex_m = arg[i]

-- Unknown Option
  elseif string.sub(arg[i], 1, 1) == "-"
  then
    error(
      string.format("Syntax error: unknown option '%s'. Use '--help' for help.",
                    arg[i]));

-- Input file
  elseif infile == nil
  then
    infile = arg[i]
  else
    error("Syntax error: only one filename permitted");
  end

  i = i + 1
end

if not quiet then
  print(string.format("makeglossaries.lua version %s", thisversion))
end

if infile == nil
then
  error("Syntax error: missing filename")
end

i, j = string.find(infile, "%.%a*$")

ext = nil
inbase = infile

if i ~= nil
then
   ext = string.sub(infile, i, j);

   lext = string.lower(ext)

   inbase = string.sub(infile, 1, i-1);

   -- Just in case user has accidentally specified the aux or tex file
   if lext == ".aux" or lext == ".tex"
   then
     ext = nil
     infile = inbase
   end
end

auxfile = inbase..".aux"

if not quiet then print(string.format("Parsing '%s'", auxfile)) end

assert(io.input(auxfile), 
  string.format("Unable to open '%s'", auxfile))

aux = io.read("*a")

if string.find(aux, "\\glsxtr@resource") ~= nil
then
  isbib2gls = true
end

if styfile == nil
then

-- v4.36: corrected check for double-quotes

  styfile = string.match(aux, "\\@istfilename{([^}]*)}")

-- v4.47: added check for null styfile (missing \@istfilename)
  if styfile ~= nil
  then
    styfile = string.gsub(styfile, "\"", "");
  end

  if styfile == nil
  then
    if isbib2gls
    then
-- v4.47 added extra hybrid instructions
       error([[
No \@istfilename found but found \glsxtr@resource.
You need to run bib2gls not makeglossaries-lite.
If you have used record=alsoindex or record=hybrid
then add \makeglossaries to your preamble.
  ]])
    else
       error([[
No \@istfilename found.
Did your LaTeX run fail?
Did your LaTeX run produce any output?
Did you remember to use \makeglossaries?
  ]])
    end
  end
end

i = string.len(styfile)

if string.sub(styfile, i-3, i) == ".xdy"
then
  isxindy = true
end

if not letterorder
then
  if string.match(aux, "\\@glsorder{letter}") ~= nil
  then
    letterorder = true
  end
end

makeindex_extra = string.match(aux, "\\@gls@extramakeindexopts{([^}]*%.?%a*)}")

if dryrun then print("Dry run mode. No commands will be executed.") end

onlyname = nil

glossaries = {}

for name, glg, gls, glo in 
  string.gmatch(aux, "\\@newglossary{([^}]+)}{([^}]+)}{([^}]+)}{([^}]+)}") do

  if not quiet then
    print(string.format("Found glossary type '%s' (%s,%s,%s)",
      name, glg, gls, glo))
  end

  glossaries[name] = {}

  glossaries[name].glg = glg
  glossaries[name].gls = gls
  glossaries[name].glo = glo

  if "."..glo == ext then

    onlyname = name

  end

  if isxindy then

    if xindylang == nil then
       glossaries[name].language = string.match(aux, 
         "\\@xdylanguage{"..name.."}{([^}]+)}");
    else
       glossaries[name].language = xindylang
    end

    glossaries[name].codepage = string.match(aux, 
      "\\@gls@codepage{"..name.."}{([^}]+)}");

  end

end

onlytypes = string.match(aux, "\\glsxtr@makeglossaries{([^}]+)}")

if onlytypes ~= nil
then
  if not quiet then
    print(string.format("Only process subset: '%s'", onlytypes))
  end

  onlyglossaries = {}

  for name in string.gmatch(onlytypes, '([^,]+)') do
     onlyglossaries[name] = glossaries[name]
  end

  glossaries = onlyglossaries
end

if ext == nil
then

  done = false

  for name, value in pairs(glossaries) do

    glg = value.glg
    gls = value.gls
    glo = value.glo

    if logfile == nil then
      glg = inbase .. "." .. glg
    else
      glg = logfile
    end

    if outfile == nil then
      gls = inbase .. "." .. gls
    else
      gls = outfile
    end

    glo = infile .. "." .. glo

    dorun(name, glg, gls, glo, value.language, value.codepage)

    done = true
  end

  if not done then
    error([[
No \@newglossary commands found in aux file.
Did you remember to use \makeglossaries?
Did you accidentally suppress the default glossary using "nomain"
and not provide an alternative glossary?
]])
  end

else

  if onlyname == nil then

     glo = infile
     gls = outfile
     glg = logfile

     language = xindylang
     codepage = 'utf8'

     if language == nil then language = 'english' end

     if gls == nil then gls = infile..".gls" end

  else

    value = glossaries[onlyname]

    glg = value.glg
    gls = value.gls
    glo = infile

    if logfile == nil then
      glg = inbase .. "." .. glg
    else
      glg = logfile
    end

    if outfile == nil then
      gls = inbase .. "." .. gls
    else
      gls = outfile
    end

  end

  if codepage == nil then
    codepage = 'utf8';
  end

  dorun(onlyname, glg, gls, glo, language, codepage)
end