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
|
---
--- This is file `luaindex.lua',
--- generated with the docstrip utility.
---
--- The original source files were:
---
--- luaindex.dtx (with options: `lua')
---
--- Copyright (c) 2011 by Markus Kohm <komascript(at)gmx.info>
---
--- This file was generated from file(s) of luaindex distribution.
--- --------------------------------------------------------------
---
--- This work may be distributed and/or modified under the conditions of
--- the LaTeX Project Public License, version 1.3c of the license.
--- The latest version of this license is in
--- http://www.latex-project.org/lppl.txt
--- and version 1.3c or later is part of all distributions of LaTeX
--- version 2005/12/01 or later and of this work.
---
--- This work has the LPPL maintenance status "maintained".
---
--- The Current Maintainer and author of this work is Markus Kohm.
---
--- This file may only be distributed together with the files listed in
--- `luaindex.dtx'. You may however distribute the files listed in
--- `luaindex.dtx' without this file.
---
--- NOTE: THIS IS AN ALPHA-VERSION!
---
module("koma.luaindex", package.seeall)
local indexes = {}
function newindex( indexname )
indexes[indexname]={ presortreplaces = {},
sortorderbychar = {} }
end
function sortorder( indexname, sortorder )
local i, value
local index = indexes[indexname]
if index == nil then
tex.error( "Unknown index `" .. indexname .. "'",
{ "You've tried to add a new sortorder to an index, but there's no index with the",
"given name.",
"You should define the index using lua function ",
" `koma.luaindex.newindex(\"" .. indexname .. "\")'",
"before."
}
)
else
if type(sortorder) == "string" then
local value
i = 1
repeat
value = unicode.utf8.sub( sortorder, i, i )
if value then
index.sortorderbychar[value] = i
end
i = i + 1
until value == ""
else -- should be table
for i, value in ipairs( sortorder ) do
index.sortorderbychar[value] = i
end
end
end
end
function presortreplace( indexname, pass, pattern, replace )
local n
local index = indexes[indexname]
if index == nil then
tex.error( "Unknown index `" .. indexname .. "'",
{ "You've tried to add a new presort-replace to an index, but there's no index",
"with the given name.",
"You should define the index using lua function ",
" `koma.luaindex.newindex(\"" .. indexname .. "\")'",
"before."
}
)
else
for n = table.maxn(index.presortreplaces), pass, 1 do
if ( index.presortreplaces[n] == nil ) then
index.presortreplaces[n] = {}
end
end
index.presortreplaces[pass][pattern]=replace
end
end
local function getclass( utfc )
local i
for i in unicode.utf8.gmatch( utfc, "%n" ) do
return 2
end
for i in unicode.utf8.gmatch( utfc, "%a" ) do
return 3
end
return 1
end
local function do_presortreplaces( srcstr, presortreplace )
if presortreplace then
local pat, rep
for pat, rep in pairs( presortreplace ) do
srcstr = unicode.utf8.gsub( srcstr, pat, rep )
end
end
return srcstr
end
local function printsubindex( level, index, presortreplace_zero )
local i,t,n,p,l
local group=""
local class=-1
local item="\\"
for l = 1, level, 1 do
item = item .. "sub"
end
item = item .. "item "
for i,t in ipairs( index ) do
if ( level == 0 ) then
local sort=do_presortreplaces( t["sort"], presortreplace_zero )
local firstchar=unicode.utf8.upper( unicode.utf8.sub( sort, 1, 1 ) )
if ( firstchar ~= group ) then
local newclass
newclass=getclass( firstchar )
if ( newclass == 1 and class ~= newclass ) then
tex.print( "\\indexgroup{\\symbolsname}" )
elseif ( newclass == 3 ) then
tex.print( "\\indexgroup{" .. firstchar .. "}" )
elseif ( newclass == 2 and class ~= newclass ) then
tex.print( "\\indexgroup{\\numbersname}" )
end
group=firstchar
class=newclass
end
end
tex.sprint( item, t["value"] )
if t["pages"] then
tex.sprint( "\\indexpagenumbers{" )
for n,p in ipairs( t["pages"] ) do
tex.sprint( "\\indexpagenumber{", p, "}" )
end
tex.print( "}" )
end
if t["subindex"] then
printsubindex( level+1, t["subindex"], presortreplaces_zero )
end
end
end
function printindex( indexname )
local index=indexes[indexname]
if index == nil then
tex.error( "Unknown index `" .. indexname .. "'",
{ "You've tried to print an index, but there's no index with the",
"given name.",
"You should define the index using lua function ",
" `koma.luaindex.newindex(\"" .. indexname .. "\")'",
"before."
}
)
else
print( "Index: \"" .. indexname .. "\" with " .. table.maxn( index ) .. " level-0-entries" )
tex.print( "\\begin{theindex}" )
printsubindex(0,indexes[indexname],indexes[indexname].presortreplaces[0])
tex.print( "\\end{theindex}" )
end
end
local function getsubclass( utfc )
local i
for i in unicode.utf8.gmatch( utfc, "%l" ) do
return 1
end
for i in unicode.utf8.gmatch( utfc, "%u" ) do
return 2
end
for i in unicode.utf8.gmatch( utfc, "%c" ) do
return 1
end
for i in unicode.utf8.gmatch( utfc, "%s" ) do
return 2
end
for i in unicode.utf8.gmatch( utfc, "%p" ) do
return 3
end
for i in unicode.utf8.gmatch( utfc, "%n" ) do
return 4
end
return 10 -- unkown is the biggest sub class
end
local function do_strcmp( first, second, sortorderbychar )
local secondtable = string.explode( second, "" )
local firstutf
local n = 1
for firstutf in string.utfcharacters( first ) do
local secondutf = unicode.utf8.sub( second, n, n )
n = n + 1;
if firstutf then
if secondutf ~= "" then
if firstutf ~= secondutf then
local firstn, secondn
if sortorderbychar then
firstn = sortorderbychar[firstutf]
secondn = sortorderbychar[secondutf]
end
if firstn and secondn then
if firstn < secondn then
return -1
elseif firstn > secondn then
return 1
end
else
local firstclass = getclass( firstutf )
local secondclass = getclass( secondutf )
if firstclass < secondclass then
return -1
elseif firstclass == secondclass then
local firstsubclass = getsubclass( firstutf)
local secondsubclass = getsubclass( secondutf )
if firstsubclass < secondsubclass then
return -1
elseif firstsubclass == secondsubclass then
if firstutf < secondutf then
return -1
else
return 1
end
else
return 1
end
else
return 1
end
end
end
else
return 1
end
else
if secondutf ~= "" then
return -1
else
return 0 -- This should never happen!
end
end
end
if unicode.utf8.sub( second, n, n ) ~= "" then
return -1
else
return 0
end
end
local function do_indexcmp( firstsort, secondsort,
presortreplaces, sortorderbychar )
local pass = 0
local ncmp = 0
repeat
if presortreplaces and presortreplaces[pass] then
firstsort = do_presortreplaces( firstsort, presortreplaces[pass] )
secondsort = do_presortreplaces( secondsort, presortreplaces[pass] )
end
pass = pass + 1
ncmp = do_strcmp( firstsort, secondsort, sortorderbychar )
until ( ncmp ~= 0 ) or ( pass > table.maxn( presortreplaces ) )
return ncmp
end
local function subinsert( index, presortreplaces, sortorderbychar,
pagestring, sortvalue, outputvalue, ... )
local min = 1
local max = table.maxn(index)
local updown = 0
local n = math.ceil(( min + max ) / 2)
while min <= max do
updown = do_indexcmp( sortvalue, index[n].sort,
presortreplaces, sortorderbychar )
if updown == 0 then
if outputvalue == index[n].value then
if ( ... ) then
if ( index[n].subindex == nil ) then
index[n].subindex = {}
end
subinsert( index[n].subindex, presortreplaces, sortorderbychar,
pagestring, ... )
else
local i, p
for i, p in ipairs( index[n].pages ) do
if pagestring == p then
return
end
end
table.insert( index[n].pages, pagestring )
end
return
else
repeat
n = n + 1
if n <= max then
updown = do_indexcmp( sortvalue, index[min].sort,
presortreplaces, sortorderbychar )
end
until n > max or updown ~= 0
min = n
max = n-1
end
elseif updown > 0 then
min = n+1
else
max = n-1
end
n = math.ceil(( min + max ) / 2)
end
if ( ... ) then
table.insert( index, n,
{ sort=sortvalue, value=outputvalue, subindex={} } )
subinsert( index[n].subindex, presortreplaces, sortorderbychar,
pagestring, ... )
else
table.insert( index, n,
{ sort=sortvalue, value=outputvalue, pages={pagestring} } )
end
end
function insert( indexname, pagestring, sortvalue, outputvalue, ... )
local index=indexes[indexname]
subinsert( index, index.presortreplaces, index.sortorderbychar,
pagestring, sortvalue, outputvalue, ... )
end
function removeentries( indexname )
local p = indexes[indexname].presortreplaces
local s = indexes[indexname].sortorderbychar
indexes[indexname]={ presortreplaces = p,
sortorderbychar = s }
end
|