Module:FirstLetter: Difference between revisions

From TANGOWIKI-TITAF
No edit summary
No edit summary
 
Line 1: Line 1:
local p = {}
local p = {}


function p.from(frame)
-- Normalize and clean up input
     local input = frame.args[1] or ''
local function normalize(str)
     input = mw.text.trim(input)
    str = mw.text.trim(str or "")
    str = mw.ustring.upper(str)
    str = mw.ustring.gsub(str, '[ÁÀÄÂ]', 'A')
     str = mw.ustring.gsub(str, '[ÉÈËÊ]', 'E')
    str = mw.ustring.gsub(str, '[ÍÌÏÎ]', 'I')
    str = mw.ustring.gsub(str, '[ÓÒÖÔ]', 'O')
    str = mw.ustring.gsub(str, '[ÚÙÜÛ]', 'U')
     str = mw.ustring.gsub(str, '[Ñ]', 'N')
    return str
end


    -- Normalize accents
-- Get first letter
    input = mw.ustring.gsub(input, '[ÁÀÄÂ]', 'A')
function p.letter(frame)
     input = mw.ustring.gsub(input, '[ÉÈËÊ]', 'E')
     local input = normalize(frame.args[1])
    input = mw.ustring.gsub(input, '[ÍÌÏÎ]', 'I')
     return mw.ustring.sub(input, 1, 1)
    input = mw.ustring.gsub(input, '[ÓÒÖÔ]', 'O')
end
     input = mw.ustring.gsub(input, '[ÚÙÜÛ]', 'U')
    input = mw.ustring.gsub(input, '[Ñ]', 'N')


    local first = mw.ustring.upper(mw.ustring.sub(input, 1, 1))
-- Get letter block
 
function p.block(frame)
    if mw.ustring.find("ABCD", first) then return "A–D" end
     local input = normalize(frame.args[1])
     if mw.ustring.find("EFGH", first) then return "E–H" end
     local first = mw.ustring.sub(input, 1, 1)
    if mw.ustring.find("IJKL", first) then return "I–L" end
     if mw.ustring.find("MNOP", first) then return "M–P" end
    if mw.ustring.find("QRST", first) then return "Q–T" end
    if mw.ustring.find("UVWXYZ", first) then return "U–Z" end


    if first:match("[A-D]") then return "A–D" end
    if first:match("[E-H]") then return "E–H" end
    if first:match("[I-L]") then return "I–L" end
    if first:match("[M-P]") then return "M–P" end
    if first:match("[Q-T]") then return "Q–T" end
    if first:match("[U-Z]") then return "U–Z" end
     return "Other"
     return "Other"
end
end


return p
return p

Latest revision as of 07:53, 12 May 2025

Documentation for this module may be created at Module:FirstLetter/doc

local p = {}

-- Normalize and clean up input
local function normalize(str)
    str = mw.text.trim(str or "")
    str = mw.ustring.upper(str)
    str = mw.ustring.gsub(str, '[ÁÀÄÂ]', 'A')
    str = mw.ustring.gsub(str, '[ÉÈËÊ]', 'E')
    str = mw.ustring.gsub(str, '[ÍÌÏÎ]', 'I')
    str = mw.ustring.gsub(str, '[ÓÒÖÔ]', 'O')
    str = mw.ustring.gsub(str, '[ÚÙÜÛ]', 'U')
    str = mw.ustring.gsub(str, '[Ñ]', 'N')
    return str
end

-- Get first letter
function p.letter(frame)
    local input = normalize(frame.args[1])
    return mw.ustring.sub(input, 1, 1)
end

-- Get letter block
function p.block(frame)
    local input = normalize(frame.args[1])
    local first = mw.ustring.sub(input, 1, 1)

    if first:match("[A-D]") then return "A–D" end
    if first:match("[E-H]") then return "E–H" end
    if first:match("[I-L]") then return "I–L" end
    if first:match("[M-P]") then return "M–P" end
    if first:match("[Q-T]") then return "Q–T" end
    if first:match("[U-Z]") then return "U–Z" end
    return "Other"
end

return p