diff options
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua | 238 |
1 files changed, 57 insertions, 181 deletions
diff --git a/Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua b/Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua index cd88fef0c82..906ea00b699 100644 --- a/Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua +++ b/Master/texmf-dist/tex/lualatex/xindex/xindex-lib.lua @@ -5,11 +5,11 @@ -- AUTHOR: Herbert Voß -- LICENSE: LPPL1.3 -- --- $Id: xindex-lib.lua 4 2021-07-07 07:25:04Z hvoss $ +-- $Id: xindex-lib.lua 9 2021-08-13 19:42:48Z hvoss $ ----------------------------------------------------------------------- if not modules then modules = { } end modules ['xindex-lib'] = { - version = 0.30, + version = 0.31, comment = "main library to xindex.lua", author = "Herbert Voss", copyright = "Herbert Voss", @@ -112,6 +112,31 @@ end -- return table.concat(str) --end +-- get all lines from a file, returns an empty +-- list/table if the file does not exist +function read_lines_from(infile) + if not file_exists(infile) then return {} end + local lines = {} + local str + local c + for line in io.lines(infile) do + if line ~= "" then + if string.find(line,'\\indexentry',1,true) then + str = line:gsub('^\\%a+%s*{','{') -- remove "\\indexentry " + writeLog(1,"read_lines: str="..str.." ("..utf.sub(str,2,2)..")\n",2) + if getCharType(utf.sub(str,2,2)) == 0 then -- we have a symbol + lines[#lines + 1] = str:gsub('^{*','{ ') -- add a space before the symbol + else + lines[#lines + 1] = str + end + else + lines[#lines + 1] = "\\MACRO<<< "..line -- allow macros between entries + end + end + end + return lines +end + function checkVert(str) -- get the | part local vert = string.find(str,"|",1,true) if (vert) then @@ -147,59 +172,6 @@ function replaceAt(str) -- return "bar" from "foo@bar" return str:gsub('[^!|@]+@', '') end -function dofile(filename) - local file = kpse.find_file(filename) - local f = assert(loadfile(file)) - return f() -end - --- see if the file exists -function file_exists(file) -local f = io.open(file, "rb") -if f then f:close() end -return f ~= nil -end - --- get all lines from a file, returns an empty --- list/table if the file does not exist -function read_lines_from(infile) - if not file_exists(infile) then return {} end - local lines = {} - local str - local c - for line in io.lines(infile) do - if line ~= "" then - if string.find(line,'\\indexentry',1,true) then - str = line:gsub('^\\%a+%s*{','{') -- remove "\\indexentry " - writeLog(1,"read_lines: str="..str.." ("..utf.sub(str,2,2)..")\n",2) - if getCharType(utf.sub(str,2,2)) == 0 then -- we have a symbol - lines[#lines + 1] = str:gsub('^{*','{ ') -- add a space before the symbol - else - lines[#lines + 1] = str - end - else - lines[#lines + 1] = "\\MACRO<<< "..line -- allow macros between entries - end - end - end - return lines -end - -function array_concat(...) - local t = {} - for n = 1,select("#",...) do - local arg = select(n,...) - if type(arg)=="table" then - for _,v in ipairs(arg) do - t[#t+1] = v - end - else - t[#t+1] = arg - end - end - return t -end - function findSequences(a) -- look for 1,2,3,4, ... local b = {} local firstPage = a[1] -- first page in the sequence @@ -582,12 +554,13 @@ function UTFCompare(a,b) end end + function pageCompare(a,b) -- a = {{number=...,special=..},{...,...}} -- a["number"], b["number"] are something like "3" or "VI-17" or "9--31" writeLog(1,"pageCompare: "..a["number"].." "..b["number"].."\n",2) if (a["number"] == nil) or (b["number"] == nil) then return true end -- should be no nil here - local a0 = a["number"] -- can be numeric or alphanumeric - local b0 = b["number"] + local a0 = IntegerOrString(a["number"]) -- can be numeric or alpha or alphanumeric + local b0 = IntegerOrString(b["number"]) -- if pageNoPrefixDel ~= "" then ---- not active -- A = (a0:gsub(pageNoPrefixPattern,'')) -- B = (b0:gsub(pageNoPrefixPattern,'')) @@ -595,10 +568,20 @@ function pageCompare(a,b) -- a = {{number=...,special=..},{...,...}} -- b0 = tonumber(B) or romanToNumber(B) -- else if numericPage then +--[[ if (type(a0) == "number") and (type(b0) == "number") then return a0 < b0 end - if romanToNumber(a0) and (type(b0) == "number") then return true end -- ii < 2 - if (type(a0) == "number") and romanToNumber(b0) then return false end -- ii > 2 + if romanToNumber(tostring(a0)) and (type(b0) == "number") then return true end -- ii < 2 + if (type(a0) == "number") and romanToNumber(tostring(b0)) then return false end -- ii > 2 if romanToNumber(a0) and romanToNumber(b0) then return a0 < b0 end -- ii < iii +]] + if (type(a0) == "number") and (type(b0) == "number") then return a0 < b0 end -- 12 < 14 ist true + if (type(a0) == "number") then + if romanToNumber(b0) then return false else return true end -- 2, ii -> ii, 2; 2, A -> ok + end + if (type(b0) == "number") then + if romanToNumber(a0) then return true else return false end -- ii, 2 -> ok; A, 2 -> 2, A + end + if romanToNumber(tostring(a0)) and romanToNumber(tostring(b0)) then return a0 < b0 end -- ii < iii or A < B -- now we have some special page numbers A = tostring(a0) B = tostring(b0) @@ -722,14 +705,6 @@ function getPageList(v,hyperpage) end end - -function getPathFileExt(str) - local filepath = str:match("(.*[/\\])") - local filename = str:match("^.+/(.+)$") - local fileext = str:match("^.+(%..+)$") - return filepath,filename,fileext -end - function printList(Index,level) for k,v in pairs(Index) do if v["Entry"] then @@ -754,75 +729,6 @@ function getItem(str,n) end end -local romanMap = { - I = 1, - V = 5, - X = 10, - L = 50, - C = 100, - D = 500, - M = 1000, -} - -local numbers = { 1, 5, 10, 50, 100, 500, 1000 } -local romanchars = { "I", "V", "X", "L", "C", "D", "M" } -local RomanNumerals = { } - -function numberToRoman(s) - --s = tostring(s) - s = tonumber(s) - if not s or s ~= s then error"Unable to convert to number" end - if s == math.huge then error"Unable to convert infinity" end - s = math.floor(s) - if s <= 0 then return s end - local ret = "" - for i = #numbers, 1, -1 do - local num = numbers[i] - while s - num >= 0 and s > 0 do - ret = ret .. romanchars[i] - s = s - num - end - for j = 1, i - 1 do - local n2 = numbers[j] - if s - (num - n2) >= 0 and s < num and s > 0 and num - n2 ~= n2 then - ret = ret .. romanchars[j] .. romanchars[i] - s = s - (num - n2) - break - end - end - end - return ret -end - -function romanToNumber(s) --- if args_v then print("romanToNumber: "..tostring(s)) end --- if not s then return end - s = s:upper() - local ret = 0 - local i = 1 - while i <= s:len() do - local c = s:sub(i, i) - if c ~= " " then -- allow spaces - local m = romanMap[c] - if not m then return nil end --error("Unknown Roman Numeral '" .. c .. "'") - local next = s:sub(i + 1, i + 1) - local nextm = romanMap[next] - if next and nextm then - if nextm > m then - ret = ret + (nextm - m) - i = i + 1 - else - ret = ret + m - end - else - ret = ret + m - end - end - i = i + 1 - end - return ret -end - function commandEntry(line) return string.find(line,"\\MACRO<<<",1,true) end @@ -939,29 +845,6 @@ function getSortString(Entry) return data end -function writeLog(i, str, level) -- mode i 0->console; 1->logfile: 2->both - if not_quiet then - if level <= vlevel then - if (i ~= 1) or (i < 0) then io.write(tostring(str)) end - if i > 0 then logFile:write(tostring(str)) end - end - else - if level < 0 then - io.write(tostring(str)) - elseif level == 0 then - logFile:write(tostring(str)) - end - end -end - -function string:split() - local sep, fields = " ", {} - local pattern = string.format("([^%s]+)", sep) - self:gsub(pattern, function(c) fields[#fields+1] = c end) - return fields -end - - --\indexentry{hello@foo!world@foo!bar|bar}{60} @@ -973,28 +856,6 @@ end ]] -function shellsort(a) - local inc = math.ceil( #a / 2 ) - while inc > 0 do - for i = inc, #a do - local tmp = a[i] - local j = i - while j > inc and not UTFCompare(a[j-inc],tmp) do - a[j] = a[j-inc] - j = j - inc - end - a[j] = tmp - end - inc = math.floor( 0.5 + inc / 2.2 ) - end - return a -end - -function stripLeadingSpaces(str) - return str:gsub("^%s*(.-)%s*$", "%1") -end - - -- get Unicode category of the codepoint, if supported function getCategory(codepoints) local codepoint = codepoints[1] @@ -1045,3 +906,18 @@ function getSortChar(codepoints) end end +function writeLog(i, str, level) -- mode i 0->console; 1->logfile: 2->both + if not_quiet then + if level <= vlevel then + if (i ~= 1) or (i < 0) then io.write(tostring(str)) end + if i > 0 then logFile:write(tostring(str)) end + end + else + if level < 0 then + io.write(tostring(str)) + elseif level == 0 then + logFile:write(tostring(str)) + end + end +end + |