This module implements the reference template {{R:he:HWSSRoot}}
.
local p = {}
local list = require("Module:R:he:HWSSRoot/data")
local hebrew_letters = {
= "ʾ",
= "B",
= "G",
= "D",
= "H",
= "W",
= "Z",
= "X",
= "7",
= "Y",
"] = "K",
= "L",
"] = "M",
"] = "N",
= "S",
= "&",
"] = "P",
"] = "C",
= "Q",
= "R",
= "ʃ",
= "T",
}
local function cleanup(value)
return value:gsub("/.", ""):gsub("%(.*", ""):gsub("$", ""):gsub(" ", " "):gsub("(.)%1$","%1"):gsub("(.)%1 ","%1 ")
end
function p.hebrew_to_normalized_latin(term)
local normalized = term:gsub("שׁ" ,"ש"):gsub("שׂ", "ש"):gsub("־", "")
for k, v in pairs(hebrew_letters) do
normalized = mw.ustring.gsub(normalized, k, v)
end
return cleanup(normalized)
end
function p.latin_to_hebrew_for_sorting(term)
local normalized = term
for k, v in pairs(hebrew_letters) do
if mw.ustring.find(k, "%[") then
normalized = mw.ustring.gsub(normalized, v, mw.ustring.sub(k, 2, 2))
else
normalized = mw.ustring.gsub(normalized, v, k)
end
end
return normalized
end
function p.simple_latin(frame)
return hebrew_to_normalized_latin(frame.args)
end
function p.page_number(frame)
local value = p.hebrew_to_normalized_latin(frame.args)
value = p.latin_to_hebrew_for_sorting(value)
local low = 1
local high = #list
local mid = 0
while high - low > 1 do
mid = math.floor((low+high)/2)
local midval = p.latin_to_hebrew_for_sorting(cleanup(list))
if midval == value then return 78 + mid
elseif midval < value then low = mid
elseif midval > value then high = mid - 1
end
end
if p.latin_to_hebrew_for_sorting(list) <= value then return 78 + high
else return 78 + low
end
end
return p