وحدة:Classement

من ويكي مصدر، المكتبة الحرة
--[[
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 = { "ال", "أبو ", "أم ", "ابن " } -- c.f. http://fr.geneawiki.com/index.php/La_norme_AFNOR_NF_Z44-001
    local particle = ""
    local i = 1
    local length = #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 = { ["'"]="’", ["ا"]="أإآ", ["و"]="ؤ", ["ي"]="ئ", ["ة"]="ت"}
    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