Debian Codegraph @

texk/texlive/linked_scripts/xindex/xindex.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2 -----------------------------------------------------------------------
3 -- FILE: xindex.lua
4 -- DESCRIPTION: create an index
5 -- REQUIREMENTS:
6 -- AUTHOR: Herbert Voß
7 -- LICENSE: LPPL 1.3
8 --
9 -----------------------------------------------------------------------
10
11 xindex = xindex or { }
12 local version = 1.04
13 xindex.version = version
14 --xindex.self = "xindex"
15
16 --[[doc--
17
18 xindex(1)
19
20 This file is provided under the terms of the LPPL v1.3 or
21 later as printed in full text in the manual (xindex.pdf).
22
23 \url{https://ctan.org/license/lppl1.3}.
24
25 Report bugs to
26
27 \url{https://gitlab.com/hvoss49/xindex/issues}.
28
29 --doc]]--
30
31 kpse.set_program_name("luatex")
32
33 local f = kpse.find_file("lualibs.lua")
34 --print ("filename "..f)
35
36 require("lualibs") -- all part of LuaTeX
37 require('unicode')
38 require('string')
39 require("lpeg")
40
41 local args = require ('xindex-lapp') [[
42 parameter handling
43 -q,--quiet
44 -h,--help
45 -v... Verbosity level; can be -v, -vv, -vvv
46 -V,--version
47 -a,--no_casesensitive
48 -b,--no_labels
49 -c,--config (default "")
50 -e,--escapechar (default ")
51 -f,--fix_hyperref
52 -g,--no_pagenumber
53 -i,--ignoreSpace
54 -k,--checklang
55 -l,--language (default en)
56 -n,--noheadings
57 -o,--output (default "")
58 -p,--prefix (default L)
59 -s,--use_stdin
60 <files...> (default stdin) .idx file(s)
61 ]]
62
63 --[[
64 No -v flag, v is just { false }. not args.v[1] is true, so vlevel becomes 0.
65 One -v flags, v is { true }
66 Two -v flags, v is { true, true }
67 Three -v flags, v is { true, true, true }
68 ]]
69
70 if args.version then
71 print("xindex version "..xindex.version)
72 os.exit()
73 end
74
75 vlevel = not args.v[1] and 0 or #args.v
76 not_quiet = not args["quiet"]
77
78 local luaVersion = _VERSION
79 if (luaVersion < "Lua 5.3") then
80 print("=========================================")
81 print("Sorry. but we need at least LuaTeX 1.09")
82 print("Leaving program xindex")
83 print("=========================================")
84 os.exit()
85 end
86
87 useStdInput = args["use_stdin"]
88
89 --local inspect = require 'inspect'
90 --print(inspect(args))
91
92 --[[
93 if args.h then
94 print(
95 Syntax: xinput [options] <file>
96 By default the Lua program "xindex" creates a so-called
97 .ind file, which has the same main filename as the input file
98 unless you are using the option "-o <output file>" There will
99 be no log file created.
100 )
101 end
102 ]]
103
104 require('xindex-baselib')
105
106 local nInFiles = #args.files
107
108 -- print(#args.files,args.files_name[1])
109
110 if not useStdInput and not args.files_name then
111 print("Use option -s for StdIn or define an input data file!")
112 os.exit()
113 end
114
115 if not useStdInput then
116 if vlevel == 3 then
117 print(tostring(nInFiles).." input file(s): ")
118 end
119 inFiles = {} --args.files as strings
120 for i = 1,nInFiles do
121 local file = args.files_name[i]
122 if not file_exists(file) then
123 if file_exists(file..".idx") then
124 inFiles[#inFiles+1] = file..".idx"
125 end
126 else
127 inFiles[#inFiles+1] = file
128 end
129 if vlevel == 3 then
130 print(file)
131 end
132 end
133 end
134
135 -- print ("Check Logfile:")
136
137 outfilename = ""
138 logfilename = ""
139
140 if args["output"] == '""' then
141 if not useStdInput then
142 if inFiles[1]:sub(inFiles[1]:len()-3,inFiles[1]:len()) == ".idx" then
143 outfilename = inFiles[1]:sub(1,inFiles[1]:len()-3).."ind"
144 if nInFiles > 1 then
145 logfilename = "xindex.ilg"
146 else
147 logfilename = inFiles[1]:sub(1,inFiles[1]:len()-3).."ilg"
148 end
149 else
150 outfilename = inFiles[1]..".ind"
151 if nInFiles > 1 then
152 logfilename = "xindex.ilg"
153 else
154 logfilename = inFiles[1]..".ilg"
155 end
156 end
157 else
158 outfilename = "xindex.ind"
159 logfilename = "xindex.ilg"
160 end
161 else
162 outfilename = args.output
163 if nInFiles > 1 or useStdInput then
164 logfilename = "xindex.ilg"
165 else
166 logfilename = outfilename:gsub('%p...','')..".ilg"
167 end
168 end
169
170 logFile = io.open(logfilename,"w+")
171 require('xindex-lib')
172 writeLog(2,"xindex v."..version.." (c) Herbert Voß\n",-1)
173 writeLog(1,"Verbose level = "..vlevel.."\n",1)
174 writeLog(2,"Logfile:"..logfilename.."\n",1)
175
176 writeLog(2,"Open outputfile "..outfilename,0)
177 outFile = io.open(outfilename,"w+")
178 writeLog(2," ... done\n",0)
179
180 if vlevel > 0 then
181 writeLog(1,"---------- parameter ----------\n",1)
182 for k,v in pairs(args) do
183 writeLog(1,tostring(k)..", "..tostring(v).."\n",1)
184 end
185 for k=1,#args.v do
186 writeLog(1,"v["..k.."]= "..tostring(args.v[k]).."\n",1)
187 end
188 writeLog(1,"---------- parameter ----------\n",1)
189 end
190
191 -- writeLog(2,"Using input file: "..inFile.."\n",0)
192
193 if args["ignoreSpace"] then
194 ignoreSpace = args["ignoreSpace"]
195 else
196 ignoreSpace = false
197 end
198 writeLog(2,"ignore space for sorting: "..tostring(ignoreSpace).."\n",-1)
199
200 labelPrefix = args.prefix
201 writeLog(2,"Label prefix: "..labelPrefix.."\n",-1)
202
203 writeLog(2,"Loading all config files ... \n",1)
204 writeLog(2,"Loading common config file ".."xindex-cfg-common\n",1) -- we need config for lang setting
205 Config_File_Common = kpse.find_file("xindex-cfg-common.lua")
206 cfg_common = require(Config_File_Common)
207
208 check_language = args["checklang"]
209 local aux_language = ""
210 if check_language then
211 print("check aux file for unknown language")
212 -- writeLog(2,'Check language in aux file\n',0)
213 -- \babel@aux{german}{} package babel
214 -- \selectlanguage *[variant=german,spelling=new,]{german} package polyglossia
215 local auxfile = inFiles[1]:split(".")[1]..".aux"
216 writeLog(2,auxfile.."\n",0)
217 local auxlines = read_lines_from(auxfile)
218 for line = 1,#auxlines do
219 local str = auxlines[line]
220 if string.find(str, "selectlanguage") then
221 str = str:match("{..+}$") -- get last word {language}
222 aux_language = str:sub(2,(#str-1))
223 break
224 else
225 if string.find(str, "babel@aux{") then
226 -- print("Babel gefunden: "..str)
227 str = str:match("{..+}$") -- get last word {language}
228 print("Babel: "..str)
229 aux_language = str:sub(2,(#str-3))
230 break
231 end
232 end
233 end
234 -- print(aux_language)
235 if #aux_language > 0 then
236 -- print("find language")
237 for i,lang in pairs(indexheader) do
238 for j = 3,#lang do
239 if lang[j] == aux_language then
240 language = i
241 end
242 end
243 end
244 else
245 language = "en"
246 end
247 print("Detected language: "..language)
248 else
249 if args["language"] then
250 language = string.lower(args["language"]:sub(1, 2))
251 else
252 language = "en"
253 end
254 end
255
256 writeLog(1,"Will use LUA-UCA\n",1)
257 ducet = require "lua-uca.lua-uca-ducet"
258 collator = require "lua-uca.lua-uca-collator"
259 languages = require "lua-uca.lua-uca-languages"
260 collator_obj = collator.new(ducet)
261
262 uca_config_file = "xindex-cfg-uca.lua" -- for additional language definition
263 writeLog(2,"Loading local UCA config file "..uca_config_file,0)
264 UCA_Config_File = kpse.find_file(uca_config_file)
265 uca_cfg = require(UCA_Config_File)
266 writeLog(2," ... done\n",0)
267
268 -- language name specified on the command line doesn't seem to be available
269 -- in the config file, so we just try to find it ourselves
270 for i, a in ipairs(arg) do
271 if a == "-l" or a=="--language" then
272 language = arg[i+1]
273 break
274 end
275 end
276
277 -- at last the user config
278
279 if args["config"] ~= '""' then
280 local user_config_file = "xindex-"..args["config"]..".lua"
281 print("Local config file is: "..user_config_file)
282 writeLog(2,"Loading local config file "..user_config_file,0)
283 if kpse.find_file(user_config_file) then
284 Config_File = kpse.find_file(user_config_file)
285 else
286 Config_File = ""
287 print("Cannot find config file with kpse.find_file!!")
288 end
289 print("\nLocal KPSE config file is: "..Config_File.."\n")
290 cfg = require(Config_File)
291 writeLog(2," ... done\n",0)
292 end
293
294 -- language can also be set in the config file
295
296 if languages[language] then
297 print("[Lua-UCA] Loading language: " .. language)
298 collator_obj = languages[language](collator_obj)
299 end
300 alphabet_lower_map = {} -- empty tables for UCA sorting
301 alphabet_upper_map = {}
302
303
304
305
306 --print("Sprache:"..language)
307
308 language = string.lower(language)
309 print("Used language: "..language)
310 writeLog(2,"Language = "..language.."\n",1)
311 if (indexheader[language] == nil) then
312 writeLog(2,'Corrected the unknown language "'..language..'" to "en"'.."\n",0)
313 language = "en"
314 end
315
316 index_header = indexheader[language]
317 if vlevel > 0 then for i=1,#index_header do writeLog(2,index_header[i].."\n",1) end end
318 if (folium[language] == nil) then
319 writeLog(2,'Corrected the unknown language "'..language..'" for page folium to "en"'.."\n",0)
320 page_folium = folium["en"]
321 else
322 page_folium = folium[language]
323 end
324
325
326 esc_char = args.escapechar
327 --print("Escape char "..esc_char)
328 esc_char2 = esc_char..esc_char
329 writeLog(2,"Escapechar = "..esc_char.."\n",1)
330 escape_chars = { -- by default " is the escape char
331 {esc_char2, '//escaped2//', esc_char },
332 {esc_char..'@', '//escapedat//', '@' },
333 {esc_char..'|', '//escapedvert//', '|' },
334 {esc_char..'!', '//escapedexcl//', '!' },
335 {'', '\\textbar', '|' },
336 {'', '\\braceLeft', '{' },
337 {'', '\\braceRight', '}' }
338 }
339
340 no_caseSensitive = args["no_casesensitive"]
341 if no_caseSensitive then
342 writeLog(1,"Sorting will be no case sensitive\n",1)
343 else
344 writeLog(1,"Sorting will be case sensitive\n",1)
345 end
346
347 no_headings = args["noheadings"]
348 if no_headings then
349 writeLog(1,"Output with NO headings between different first letter\n",1)
350 else
351 writeLog(1,"Output with headings between different first letter\n",1)
352 end
353
354 no_labels = args["no_labels"]
355 if no_headings then
356 writeLog(1,"Index without labels\n",1)
357 else
358 writeLog(1,"Index with labels\n",1)
359 end
360
361 show_pagenumber = not args["no_pagenumber"]
362 if not show_pagenumber then
363 writeLog(1,"Output with NO pagenumbers!\n",1)
364 end
365
366 fix_hyperref = args["fix_hyperref"]
367 if fix_hyperref then
368 writeLog(1,'fix hyperref with "|hyperpage -> \\textbar|hyperpage!\n',1)
369 end
370
371 writeLog(2,"Open outputfile "..outfilename,0)
372 outFile = io.open(outfilename,"w+")
373 writeLog(2,"... done\n",0)
374
375 writeLog(1,"Starting base file ... \n",2)
376
377 BaseRunFile = kpse.find_file("xindex-base.lua")
378 dofile(BaseRunFile)
379
380 logFile:close()
381