La documentació per a este mòdul pot ser creada en Módulo:Llista de cançons/doc

-- Module:Llista de cançons
-- Normativa: RACV (Normes del Puig) - L'Enciclopèdia en valencià
-- Suporta paràmetres en valencià, castellà i anglés

local p = {}

-- Funció per a netejar i comprovar text buit
local function trim(s)
    if s == nil then return nil end
    s = tostring(s):gsub('^%s+', ''):gsub('%s+$', '')
    if s == '' then return nil end
    return s
end

-- Funció per a ordenar numèricament les claus
local function obtenirNumeros(args)
    local nums = {}
    local vist = {}
    local prefixos = {'títul', 'título', 'title', 'duració', 'duración', 'length'}
    for k, v in pairs(args) do
        local strK = tostring(k)
        for _, pfx in ipairs(prefixos) do
            local num = strK:match('^' .. pfx .. '([1-9]%d*)$')
            if num then
                local n = tonumber(num)
                if not vist[n] then
                    vist[n] = true
                    table.insert(nums, n)
                end
            end
        end
    end
    table.sort(nums)
    return nums
end

function p.lista(frame)
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end

    local args = {}
    if origArgs then
        for k, v in pairs(origArgs) do
            local val = trim(v)
            if val then
                args[k] = val
            end
        end
    end
    -- Si es crida directament per frame en arguments propis
    if frame ~= mw.getCurrentFrame() and frame.args then
        for k, v in pairs(frame.args) do
            local val = trim(v)
            if val then
                args[k] = val
            end
        end
    end

    local text_previ = ''
    -- Crèdits generals
    local tot_escrit = args['all_writing'] or args['todas_escritas'] or args['totes_escrites']
    local tota_lletra = args['all_lyrics'] or args['todas_letras'] or args['totes_lletres']
    local tota_musica = args['all_music'] or args['toda_la_música'] or args['tota_la_música']

    if tot_escrit then
        text_previ = 'Totes les cançons escrites i compostes per ' .. tot_escrit .. '. '
    else
        if tota_lletra then
            text_previ = 'Totes les lletres escrites per ' .. tota_lletra
            if tota_musica then
                text_previ = text_previ .. ', tota la música composta per ' .. tota_musica .. '. '
            else
                text_previ = text_previ .. '. '
            end
        elseif tota_musica then
            text_previ = 'Tota la música composta per ' .. tota_musica .. '. '
        end
    end

    -- Determinació de columnes
    local has_lyrics = (args['lyrics_credits'] == 'yes' or args['créditos_letras'] == 'sí' or args['créditos_letras'] == 'si' or args['crèdits_lletra'] == 'sí' or args['crèdits_lletres'] == 'sí')
    local has_music = (args['music_credits'] == 'yes' or args['créditos_música'] == 'sí' or args['créditos_música'] == 'si' or args['crèdits_música'] == 'sí')
    local has_writers = (args['writing_credits'] == 'yes' or args['créditos_escritores'] == 'sí' or args['créditos_escritores'] == 'si' or args['crèdits_escritors'] == 'sí')
    local extra_col_title = args['extra_column'] or args['columna_extra']
    local has_extra = (extra_col_title ~= nil)

    local numeros = obtenirNumeros(args)

    -- Comprovar si hi ha alguna duració
    local has_durations = false
    local dur_total = args['duració_total'] or args['duración_total'] or args['total_length']
    if dur_total then
        has_durations = true
    else
        for _, n in ipairs(numeros) do
            if args['duració' .. n] or args['duración' .. n] or args['length' .. n] then
                has_durations = true
                break
            end
        end
    end

    -- Comptar columnes totals
    local total_cols = 2 -- Núm. i Títul
    if has_lyrics then total_cols = total_cols + 1 end
    if has_music then total_cols = total_cols + 1 end
    if has_writers then total_cols = total_cols + 1 end
    if has_extra then total_cols = total_cols + 1 end
    if has_durations then total_cols = total_cols + 1 end

    -- Ocultar / Plegar
    local ocultar = args['ocultar'] or args['amagar'] or args['collapsed']
    local is_collapsed = (ocultar == 'sí' or ocultar == 'si' or ocultar == 'yes' or ocultar == 'collapsed')

    local classes = 'wikitable mw-collapsible'
    if is_collapsed then
        classes = classes .. ' mw-collapsed'
    end

    -- Construcció de la taula en wikitext
    local out = {}
    if text_previ ~= '' then
        table.insert(out, "<div style=\"margin-bottom:6px; font-style:italic;\">" .. text_previ .. "</div>\n")
    end

    table.insert(out, '{| class="' .. classes .. '" style="width:100%; border:1px solid #a2a9b1; border-collapse:collapse; font-size:95%;"\n')

    -- Títul del disc / llista si existix
    local titol_llista = args['títul'] or args['título'] or args['title'] or args['headline']
    if titol_llista then
        table.insert(out, '! colspan="' .. total_cols .. '" style="background-color:#eaecf0; text-align:left; font-size:105%; padding:6px 10px;" | ' .. titol_llista .. '\n|-\n')
    end

    -- Capçaleres de columnes
    table.insert(out, '! scope="col" style="width:3em; text-align:center; background-color:#eaecf0;" | Núm.\n')
    table.insert(out, '! scope="col" style="text-align:left; background-color:#eaecf0;" | Títul\n')

    if has_lyrics then
        table.insert(out, '! scope="col" style="text-align:left; background-color:#eaecf0;" | Lletra\n')
    end
    if has_music then
        table.insert(out, '! scope="col" style="text-align:left; background-color:#eaecf0;" | Música\n')
    end
    if has_writers then
        table.insert(out, '! scope="col" style="text-align:left; background-color:#eaecf0;" | Escritor(s)\n')
    end
    if has_extra then
        table.insert(out, '! scope="col" style="text-align:left; background-color:#eaecf0;" | ' .. extra_col_title .. '\n')
    end
    if has_durations then
        table.insert(out, '! scope="col" style="width:5em; text-align:right; background-color:#eaecf0; padding-right:10px;" | Duració\n')
    end

    -- Files de cançons
    for _, n in ipairs(numeros) do
        local t = args['títul' .. n] or args['título' .. n] or args['title' .. n] or "''Sense títul''"
        local nota = args['nota' .. n] or args['notas' .. n] or args['note' .. n]
        local display_title = '«' .. t .. '»'
        if nota then
            display_title = display_title .. ' <span style="font-size:85%; color:#555;">(' .. nota .. ')</span>'
        end

        table.insert(out, '|-\n')
        table.insert(out, '| style="text-align:center;" | ' .. n .. '.\n')
        table.insert(out, '| ' .. display_title .. '\n')

        if has_lyrics then
            local lletra = args['lletra' .. n] or args['letra' .. n] or args['lyrics' .. n] or ''
            table.insert(out, '| ' .. lletra .. '\n')
        end
        if has_music then
            local musica = args['música' .. n] or args['musica' .. n] or args['music' .. n] or ''
            table.insert(out, '| ' .. musica .. '\n')
        end
        if has_writers then
            local escritor = args['escritor' .. n] or args['writer' .. n] or args['escritors' .. n] or ''
            table.insert(out, '| ' .. escritor .. '\n')
        end
        if has_extra then
            local extra = args['extra' .. n] or ''
            table.insert(out, '| ' .. extra .. '\n')
        end
        if has_durations then
            local dur = args['duració' .. n] or args['duración' .. n] or args['length' .. n] or ''
            table.insert(out, '| style="text-align:right; padding-right:10px;" | ' .. dur .. '\n')
        end
    end

    -- Fila de duració total si existix
    if dur_total then
        table.insert(out, '|-\n')
        table.insert(out, '| colspan="' .. total_cols .. '" style="text-align:right; font-weight:bold; background-color:#eaecf0; padding-right:10px;" | ' .. dur_total .. '\n')
    end

    table.insert(out, '|}')

    return table.concat(out)
end

return p