summaryrefslogtreecommitdiff
path: root/indexing/xindex/lualatex/xindex-lib.lua
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-10-29 03:00:39 +0000
committerNorbert Preining <norbert@preining.info>2019-10-29 03:00:39 +0000
commit97f5151099967e6c823d640a90d87bca92c44035 (patch)
tree685caa49488599f87d0caa4e972833b2263d8567 /indexing/xindex/lualatex/xindex-lib.lua
parent25a647b97ef526aefcc75269dd19c46dbe3ae4c1 (diff)
CTAN sync 201910290300
Diffstat (limited to 'indexing/xindex/lualatex/xindex-lib.lua')
-rw-r--r--indexing/xindex/lualatex/xindex-lib.lua132
1 files changed, 79 insertions, 53 deletions
diff --git a/indexing/xindex/lualatex/xindex-lib.lua b/indexing/xindex/lualatex/xindex-lib.lua
index 8720d3db1b..59193f1d28 100644
--- a/indexing/xindex/lualatex/xindex-lib.lua
+++ b/indexing/xindex/lualatex/xindex-lib.lua
@@ -7,7 +7,7 @@
-----------------------------------------------------------------------
if not modules then modules = { } end modules ['xindex-lib'] = {
- version = 0.16,
+ version = 0.18,
comment = "main library to xindex.lua",
author = "Herbert Voss",
copyright = "Herbert Voss",
@@ -19,16 +19,24 @@ local escapechar2 = "\"\""
local Letters = "ÄÖÜäöüabcdefghijklmnopqrstuvwxyzßABCDEFGHIJKLMNOPQRSTUVWXYZ"
local Digits = "0123456789"
--- Looks up the character `character´ in the alphabet and returns its 'normalization' for sorting
-local function get_normalized_char( CharList, character )
+function CreateCharListMap(CharList)
+ local map = {}
for i, alphabet_entry in ipairs(CharList) do
- for _, alphabet_char in ipairs(alphabet_entry) do
- if character == alphabet_char then
- return alphabet_entry[1]
+ local first_char
+ for j, alphabet_char in ipairs(alphabet_entry) do
+ if j == 1 then
+ first_char = alphabet_char
+ else
+ map[alphabet_char] = first_char
end
end
end
- return character
+ return map
+end
+
+-- Looks up the character `character´ in the alphabet and returns its 'normalization' for sorting
+local function get_normalized_char( CharListMap, character )
+ return CharListMap[character] or character
end
function Lower(strOrig) -- return UTF string.lower
@@ -47,57 +55,60 @@ end
function NormalizedLower(strOrig) -- return normalized UTF string.lower (ä -> a)
local str = ""
for i=1, utf.len(strOrig) do
- local c = get_normalized_char(alphabet_lower,utf.sub(strOrig,i,i))
+ local c = get_normalized_char(alphabet_lower_map,utf.sub(strOrig,i,i))
str = str..string.lower(c)
end
return str
end
function NormalizedUpper(strOrig) -- return normalized UTF string.upper (ä -> A)
- local str = ""
+ local str = {}
local c
for i=1, utf.len(strOrig) do
- c = get_normalized_char(alphabet_lower,utf.sub(strOrig,i,i))
--- str = str..string.upper(c)
- str = str..c
+ c = get_normalized_char(alphabet_lower_map,utf.sub(strOrig,i,i))
+ str[#str + 1] = c
end
- strOrig = str
- str=""
+ strOrig = table.concat(str)
+ str = {}
for i=1, utf.len(strOrig) do
- c = get_normalized_char(alphabet_upper,utf.sub(strOrig,i,i))
+ c = get_normalized_char(alphabet_upper_map,utf.sub(strOrig,i,i))
c = NormalizedUppercase(c)
- str = str..c
+ str[#str + 1] = c
end
- return str
+ return table.concat(str)
end
function NormalizedUppercase(chr) -- return normalized UTF uppercase letter
- for i, alphabet_entry in ipairs(alphabet_uppercase) do
- if chr == alphabet_entry[1] then
- return alphabet_entry[2]
- end
- end
- return chr
+ return alphabet_uppercase_map[chr] or chr
end
function NormalizedChars(strOrig) -- return normalized UTF string (ä -> a) with first as upper case
- local c = string.upper(get_normalized_char(alphabet_lower,utf.sub(strOrig,1,1)))
- local str = c
- for i=2, utf.len(strOrig) do
- c = get_normalized_char(alphabet_lower,utf.sub(strOrig,i,i))
- str = str..c
- end
- local strOrigTrue = strOrig
- strOrig = str
- c = string.upper(get_normalized_char(alphabet_upper,utf.sub(strOrig,1,1)))
- str = c
- for i=2, utf.len(strOrig) do
- c = get_normalized_char(alphabet_upper,utf.sub(strOrig,i,i))
- str = str..c
- end
+ local c = get_normalized_char(alphabet_lower_map,utf.sub(strOrig,1,1))
+ c = get_normalized_char(alphabet_upper_map, c)
+ c = string.upper(c)
+ local str = { c }
+ for ch in string.utfcharacters(utf.sub(strOrig, 2)) do
+ c = get_normalized_char(alphabet_lower_map, ch)
+ c = get_normalized_char(alphabet_upper_map, c)
+ str[#str + 1] = c
+ end
+ return table.concat(str)
+end
+
+-- local strOrigTrue = strOrig
+-- strOrig = table.concat(str)
+-- c = string.upper(get_normalized_char(alphabet_upper,utf.sub(strOrig,1,1)))
+-- str = { c }
+-- for i=2, utf.len(strOrig) do
+-- c = get_normalized_char(alphabet_upper,utf.sub(strOrig,i,i))
+-- str[#str + 1] = c
+-- end
+-- for ch in string.utfcharacters(strOrig, 2) do
+-- str[#str + 1] = get_normalized_char(alphabet_upper, ch)
+-- end
-- print(strOrigTrue.." -> "..str)
- return str
-end
+-- return table.concat(str)
+--end
function checkVert(str) -- get the | part
local vert = string.find(str,"|",1,true)
@@ -524,34 +535,42 @@ end
-- str = "\indexentry {foo}{bar}"
-- first, second = str:match('{([^}]*)}%s*{([^}]*)}') -> foo, bar
-function UTFCompare(a,b)
--- a, b are something like \indexentry{foo}{bar}
--- writeLog(1,"UTFCompare: "..a["Entry"]..", "..a["pages"][1]["number"].." - "..b["Entry"]..", "..b["pages"][1]["number"].."\n",2)
--- k = k + 1
--- if (k % 50) == 0 then writeLog(1,".",1) end
- local A,B,Apage,Bpage
+function GenerateSortKey(a)
+ local A, Apage
if numericPage then
if tonumber(a["pages"][1]["number"]) then
Apage = string.format("%09d",a["pages"][1]["number"])
else
Apage = string.format("%09d",romanToNumber(a["pages"][1]["number"]))
end
- if tonumber(b["pages"][1]["number"]) then
- Bpage = string.format("%09d",b["pages"][1]["number"])
- else
- Bpage = string.format("%09d",romanToNumber(b["pages"][1]["number"]))
- end
else
Apage = string.format("%09s",a["pages"][1]["number"])
- Bpage = string.format("%09s",b["pages"][1]["number"])
end
if no_caseSensitive then
A = NormalizedUpper(getSortString(a["Entry"].." "..Apage..a["pages"][1]["special"]):gsub('!','')) -- replace! by empty
- B = NormalizedUpper(getSortString(b["Entry"].." "..Bpage..b["pages"][1]["special"]):gsub('!',''))
+ a["SortKey"] = A
else
A = NormalizedChars(getSortString(a["Entry"].." "..Apage..a["pages"][1]["special"]):gsub('!','')) -- replace! by empty
- B = NormalizedChars(getSortString(b["Entry"].." "..Bpage..b["pages"][1]["special"]):gsub('!',''))
+ a["SortKey"] = A
end
+ -- return A
+end
+
+function GenerateSortKeys(Index)
+ for _, entry in ipairs(Index) do
+ GenerateSortKey(entry)
+ end
+end
+
+function UTFCompare(a,b)
+-- a, b are something like \indexentry{foo}{bar}
+-- writeLog(1,"UTFCompare: "..a["Entry"]..", "..a["pages"][1]["number"].." - "..b["Entry"]..", "..b["pages"][1]["number"].."\n",2)
+-- k = k + 1
+-- if (k % 50) == 0 then writeLog(1,".",1) end
+
+ local A = a["SortKey"]
+ local B = b["SortKey"]
+
writeLog(1,"UTFCompare: A--B "..A.."--"..B.."\n",2)
-- print(A,B)
--[[
@@ -907,7 +926,13 @@ local match = function(expr)
return Ct(idx):match(expr)
end
+local SortStringCache = {}
function getSortString(Entry)
+ local sortString = SortStringCache[Entry]
+ if sortString ~= nil then
+ return sortString
+ end
+
local t = match(Entry)
-- require 'xindex-pretty'.dump(t) -- only for internal dump
local data = t[1]
@@ -920,6 +945,7 @@ function getSortString(Entry)
data = data..t[i]
end
end
+ SortStringCache[Entry] = data
return data
end