diff options
Diffstat (limited to 'indexing/xindex/lualatex/xindex-lib.lua')
-rw-r--r-- | indexing/xindex/lualatex/xindex-lib.lua | 88 |
1 files changed, 55 insertions, 33 deletions
diff --git a/indexing/xindex/lualatex/xindex-lib.lua b/indexing/xindex/lualatex/xindex-lib.lua index 8e480866c8..720d473d53 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.22, + version = 0.23, comment = "main library to xindex.lua", author = "Herbert Voss", copyright = "Herbert Voss", @@ -569,43 +569,15 @@ function GenerateSortKeys(Index) 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 - +function UTFCompare(a,b) local A = a["SortKey"] local B = b["SortKey"] - writeLog(1,"UTFCompare: A--B "..A.."--"..B.."\n",2) --- print(A,B) ---[[ - if A == B then -- same entry, use also page number - Apage = string.format("%09s",a["pages"][1]["number"]) - Bpage = string.format("%09s",b["pages"][1]["number"]) - A = string.format("%-90s",A)..Apage - B = string.format("%-90s",B)..Bpage - end - 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 + if use_UCA then + return collator_obj:compare_strings(A,B) else - Apage = string.format("%09s",a["pages"][1]["number"]) - Bpage = string.format("%09s",b["pages"][1]["number"]) + return A<B end - A = string.format("%-90s",A)..Apage - B = string.format("%-90s",B)..Bpage -]] - return A < B end function pageCompare(a,b) -- a = {{number=...,special=..},{...,...}} @@ -1012,3 +984,53 @@ function stripLeadingSpaces(str) end +-- get Unicode category of the codepoint, if supported +function getCategory(codepoints) + local codepoint = codepoints[1] + local category = get_category(codepoint) + if category == "Nd" then + return "digits" + elseif category:match("^L") then + local block = binary_range_search(codepoint, unicode_blocks) or {} + return block[3] or "other" + end + return "other" +end + +-- insert the index entry to the categories subtable +function categorize(codepoints, entry, categories) + local subcategories = categories.categories + -- return category name from the Unicode block of the first character + local category = getCategory(codepoints) + -- if the categories doesn't use this code block, categorize it as other + local used = subcategories[category] and category or "other" + table.insert(subcategories[used], entry) +end + +-- convert categories back to index list +function uncategorize(ordering_categories) + local newlist = {} + -- make new index order based on defined categories + for _, category in ipairs(ordering_categories.order) do + for _,entry in ipairs(ordering_categories.categories[category]) do + newlist[#newlist+1] = entry + end + end + return newlist +end + +function getSortChar(codepoints) + if #codepoints > 0 then + local codes, pos = collator_obj:get_lowest_char(codepoints, 1) + if not codes then + -- if the first character in the sort key doesn't return letter + -- continue until we find some + table.remove(codepoints, 1) + return getSortChar(codepoints) + end + local sort_char = utf8.char(table.unpack(codes)) + -- print unicode category of the first char + return upper(sort_char) -- use unicode.utf8.upper to make the char uppercase + end +end + |