Documentation for this module may be created at ਮੌਡਿਊਲ:Classement/doc

--[[
Lua module in order to work on sort keys
]]--
function getSortKey( str )
    str = mw.text.trim( mw.ustring.lower( removeAccentuation( str ) ), " «»\t\r\n\f'\"" )
    --Should be in decreasing order
    local wordsToRemove = { ["les[^%a]"]=3, ["le[^%a]"]=2, ["la[^%a]"]=2, ["des[^%a]"]=3, ["de[^%a]"]=2, ["du[^%a]"]=2, ["l'[%a]"]=2, ["d'[%a]"] = 2 }
    for key, value in next, wordsToRemove do
        if mw.ustring.find( str, "^" .. key ) then
            str = mw.ustring.sub( str, value + 1 )
        end
    end
    return str
end


function getSortKeyForName( firstName, lastName )
    --Should be in decreasing order
    local particles = { "von der ", "de las ", "de les ", "de los ", "das ", "del ", "dos ", "las ", "von ", "af ", "av ", "da ", "de ", "la ", "os ", "zu ", "d’", "d'" } -- c.f. http://fr.geneawiki.com/index.php/La_norme_AFNOR_NF_Z44-001
    local particle = ""
    local i = 1
    local length = table.getn( particles )

    while i <= length and particle == "" do
        if mw.ustring.find( lastName, "^" .. particles[i] ) then
            particle = particles[i]
            lastName = mw.ustring.sub( lastName, mw.ustring.len(particle) + 1 )
            break
        end
        i = i + 1
    end

    local key = removeAccentuation( lastName )
    if firstName ~= nil and firstName ~= "" then
        key = key .. ", " .. removeAccentuation( firstName )
    end
    if particle ~= "" then
        key = key .. " " .. removeAccentuation( particle )
    end

    return key
end

-- Only works for Basic Latin-based languages
function removeAccentuation( text )
    local list = { ["'"]="’", ["a"]="áàâãä", ["A"]="ÁÀÂÄ", ["ae"]="æ", ["Ae"]="Æ", ["c"]="ćç", ["C"]="ĆÇ", ["e"]="éèêëẽ", ["E"]="ÉÈÊË", ["g"]="ģ", ["i"]="íìîï", ["I"]="ÍÌÎÏ", ["l"]="ĺ", ["L"]="Ĺ", ["n"]="ń", ["N"]="Ń", ["oe"]="œ", ["0e"]="Œ", ["o"]="óòôöõ", ["O"]="ÓÒÔÖ", ["r"]="ŕ", ["R"]="Ŕ", ["s"]="ś", ["S"]="Ś", ["u"]="úùûü", ["U"]="ÚÙÛÜ", ["y"]="ý", ["Y"]="Ý", ["z"]="ź", ["Z"]="Ź" }
    for key, value in next,list do
       text = mw.ustring.gsub( text, "[" .. value .. "]", key )
    end
    return text
end


local p = {}

function p.getSortKey( frame )
    return getSortKey( frame.args[1] )
end

function p.getSortKeyForName( frame )
    return getSortKeyForName( frame.args[1], frame.args[2] )
end

function p.removeAccentuation( frame )
    return removeAccentuation( frame.args[1] )
end

return p