summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/xindex/xindex-HAdW.lua
blob: a45bd744332851f07df492b0fbc5bfaf81d2482b (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
-----------------------------------------------------------------------
--         FILE:  xindex-HAdW.lua
--  DESCRIPTION:  configuration file for xindex.lua
-- REQUIREMENTS:  
--       AUTHOR:  Herbert Voß
--      LICENSE:  LPPL1.3
-----------------------------------------------------------------------
--
-- configuration for index files of the Heidelberger Akademie der Wissenschaften

if not modules then modules = { } end modules ['xindex-HAdW'] = {
      version = 0.05,
      comment = "configuration to xindex.lua",
       author = "Herbert Voss",
    copyright = "Herbert Voss",
      license = "LPPL 1.3"
}

--local version = "0.01"

itemPageDelimiter = ","     -- Hello, 14
compressPages     = true    -- something like 12--15, instaead of 12,13,14,15. the |( ... |) syntax is still valid
fCompress	  = true    -- 3f -> page 3, 4 and 3ff -> page 3, 4, 5
minCompress       = 3       -- 14--17 or 
numericPage       = false   -- for non numerical pagenumbers, like "VI-17"
sublabels         = {"", "-\\,", "--\\,", "---\\,"} -- for the (sub(sub(sub-items  first one is for item
pageNoPrefixDel   = ""     -- a delimiter for page numbers like "VI-17"
indexOpening      = ""     -- commands after \begin{theindex}



--[[
    Each character's position in this array-like table determines its 'priority'.
    Several characters in the same slot have the same 'priority'.
]]
alphabet_lower = { --   for sorting
    { ' ' },  -- only for internal tests
    { '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' }
}
alphabet_upper = { -- for sorting
    { ' ' },
    { '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' }
}


--\indexentry{Ackers, Carolus}{VII/1-715}
--\indexentry{Bremen!Adalbert I. von, Erzbischof}{VII/2/1-948}
--\indexentry{Bremen!Adalbert I. von, Erzbischof}{VII/1-50}

function specialCompressPageList(pages)
  if (#pages <= 1) or (pages[1]["number"] == "") then 
    pages[1]["number"] = pages[1]["number"]:gsub('-',' ')
    return pages 
  end  -- only one pageno
--[[ 
modify pagelist
sortPages = {{ origin = { number=VII/1-715, special="" }, 
               sort   = 07/1-00715 },
             {...}
            }
]]
  local sortPages = {}
  local roman 
  local volume
  local page
  local i
  for i=1,#pages do
    roman = string.gsub(pages[i]["number"],'%U*','') -- only uppercase to catch VII123f (folium pages)
    roman = string.format("%03d",tonumber(romanToNumber(roman)))
    volume = string.gsub(pages[i]["number"],'%a*','')
    if volume then volume = volume:gsub('-%d*','') end
    page = string.gsub(pages[i]["number"],'.*-','')
    page = string.format("%5s",page)
--print(roman..volume.." "..page)
    sortPages[#sortPages+1] = {
      origin = pages[i],
      sort = roman..volume.." "..page }  -- no minus between Roman/Volume and first page
   end
-- sort the page list  
  table.sort(sortPages, function(a,b) return a["sort"] < b["sort"] end )
  local Pages = {}
  for i=1,#sortPages do    -- use the sorted origin table
    Pages[#Pages+1] = sortPages[i]["origin"]
  end
--  writeLog(1,print(getRawPagesP(Pages)),2)
-- test if two or more pages in the list
  if #Pages == 2 then  -- only two pages
    local page1 = string.gsub(Pages[1]["number"],'%A*','')  -- get prefix1
    local page2 = string.gsub(Pages[2]["number"],'%A*','')  -- ger prefix2
    if page1 == page2 then  -- same prefix
      page1 = string.gsub(Pages[1]["number"],'.*-','') -- get page1
      page2 = string.gsub(Pages[2]["number"],'.*-','') -- get page2
      if (page2-page1) == 1 then                       -- pagediff = 1
        Pages[1]["number"] = Pages[1]["number"].."f"   
        Pages[2] = nil                                 -- remove second page
        return Pages 
      else  -- page difference > 1
        pages[1]["number"] = pages[1]["number"]:gsub('-',' ')
        Pages[2]["number"] = string.gsub(Pages[2]["number"],'.*-','') -- use only number -> same prefix
        return Pages  -- Pages[1] is the same
      end
    else
      pages[1]["number"] = pages[1]["number"]:gsub('-',' ')
      pages[2]["number"] = pages[2]["number"]:gsub('-',' ')
      return Pages  -- different prefix -> simple return of the two pages
    end
  else   -- more than two pages
    local prefixList = {}
    local lastPrefix = ""
    local currentPrefix
    for i=1,#Pages do  -- create the list of different prefixes, eg {VI, VI/2/1, VI/2/2}
      currentPrefix, j = string.gsub(Pages[i]["number"],'-.*','')
      if currentPrefix ~= lastPrefix then
        prefixList[#prefixList+1] = currentPrefix
        lastPrefix = currentPrefix
      end
    end
    pages = {}
    for j = 1,#prefixList do
      lastPrefix = prefixList[j]
      i = 1
      local subPageList = {}
      while (i <= #Pages) do
        currentPrefix, _ = string.gsub(Pages[i]["number"],'-.*','')
        if (lastPrefix == currentPrefix) then
          subPageList[#subPageList+1] = { number = string.gsub(Pages[i]["number"],'.*-',''), special = Pages[i]["special"] }
        end
          i = i +1
      end
      sortPages = compressPageList(subPageList)
      -- no minus between Roman/Volume and first page
      pages[#pages+1] = { number = lastPrefix.." "..sortPages[1]["number"], special = sortPages[1]["special"] }
      for i=2,#sortPages do
        pages[#pages+1] = sortPages[i]
      end
    end
    return pages
  end
end

function replaceRoman(r)
  local i = romanToNumber(r)
  if i then return "//"..string.format("%03d",i).."//"
       else return r
  end
end

function SORTprehook(data)  -- replace roman with algebraic, eg Karl IX -> Karl // 9//
--  writeLog(1,require 'xindex-pretty'.dump(data),0)   -- only for internal dump
  local entry
  local elements = {}
  for i=1,#data do
    entry = data[i]["Entry"]
    elements = entry:split()
    local number = romanToNumber(elements[#elements])
    if number then
      local strNr = string.format("%03d",tostring(number))
      elements[#elements] = "//"..strNr.."//"
      entry = ""
      for j=1,#elements do entry=entry.." "..elements[j] end
    else 
      entry:gsub("!(.-)%p",replaceRoman)
    end
    data[i]["Entry"] = entry
  end
  return data
end

function SORTposthook(data)  -- the other way round as prehook
  local entry
  local elements = {}
  for i=1,#data do
    entry = data[i]["Entry"]
    if entry:find("//") then 
       data[i]["Entry"] = entry:gsub("//(.-)//",numberToRoman)
    end
  end
  return data
end