Vai al contenuto

Modulo:Infobox/sandbox

Da Wikipedia, l'enciclopedia libera.
Versione del 31 mag 2015 alle 21:21 di Moroboshi (discussione | contributi) (test semplificazione in seguito alla risoluzione del problema dei ref nel codice mediawiki)
-- Modulo per implementare le funzionalità di infobox
local p = {} -- per l'esportazione delle funzioni del modulo

local function getArgNums(args)
    -- Restituisce una lista che contiene il suffisso numerico  di tutti gli argomenti
    -- che iniziano con uno dei prefissi passati per argomento 
    -- Per esempio lase nella lista argomenti sono valorizzati "Valore1, Valore2 e Valore4"
    -- retistuirà la lista [1, 2, 4]
    local prefixs = {'Valore', 'GruppoOpzionale',  'Gruppo'}
    local nums = {}
    for k, _ in pairs(args) do
        local num = nil
        for _, candidate in ipairs(prefixs) do
            num = tostring(k):match('^' .. candidate .. '(%d+)$')
            if num ~= nil then break end
        end
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function addRow(root, args, rowArgs)
    -- Aggiunge una riga alla tabella
    -- Se rowArgs.gruppo non è nullo la considera come una riga di testata di gruppo
    -- e ignora eventuali valorizzazioni di rowArgs.valore
    if rowArgs.gruppo then
        root
            :tag('tr')
                :addClass("sinottico_divisione")
                :tag('th')
                    :attr('colspan', 2)
                    :cssText(args.StileGruppo or '')
                    :wikitext(rowArgs.gruppo)
    -- Altrimenti se rowArgs.valore non è nullo inserisce una riga dati, verificando
    -- se esiste o meno la testata
    elseif rowArgs.valore then
        local row = root:tag('tr')
        local dataCell
        if rowArgs.nome then
            row
                :tag('th')
                    :cssText(args.StileNome or '')
                    :wikitext(rowArgs.nome)
            dataCell = row:tag('td')
        else
            dataCell = row:tag('td')
                :attr('colspan', 2)
                :css('text-align', 'center')
        end
        dataCell
            :addClass(rowArgs.classe or '')
            :cssText(args.StileValore or '')
            :wikitext(rowArgs.valore)
    end
end

local function renderTitle(root, args)
    if args.TitoloEst then
        root
            :tag('caption')
            :addClass('sinottico_testata')
            :css("font-weight", "bold")
            :cssText(args.StileTitoloEst or '')
            :wikitext(args.TitoloEst)
    elseif args.TitoloInt then
        root
            :tag('tr')
            :addClass('sinottico_testata')
            :tag('th')
                :attr('colspan', '2')
                :cssText(args.StileTitoloInt or '')
                :wikitext(args.TitoloInt)
    end
end

local function renderImage(root, args)
    if not args.Immagine then return end
    root:tag('tr')
    local cell_immagine = root:tag('td')
    cell_immagine
        :addClass(args.ClasseImmagine or '')
        :attr('colspan', '2')
        :css('text-align', 'center')
        :cssText(args.StileImmagine or '')
        :wikitext(args.Immagine)
     if args.Didascalia then
        cell_immagine
            :tag('br', {selfClosing = true})
                :done()
            :tag('span')
            :cssText(args.StileDidascalia or '')
            :wikitext(args.Didascalia)
    end
end


local function renderRows(root, args)
    local rownums = getArgNums(args)
    for k, num in ipairs(rownums) do
        local skip = false
        if args['GruppoOpzionale' .. num] ~= nil then
            skip = true
            for j = k+1, #rownums do
                if args['Gruppo' .. rownums[j]] ~= nil or args['GruppoOpzionale' .. rownums[j]]~=nil then break end
                if args['Valore' .. rownums[j]] ~= nil then
                    skip = false
                    break
                end
            end
        end
        if not skip then
            addRow(root, args, {
                gruppo = args['GruppoOpzionale' .. num] or args['Gruppo' .. num],
                nome = args['Nome' .. num],
                valore = args['Valore' .. num],
                classe = args['Classe' .. num]
            })
        end
    end
end

local function renderLastRow(root, args)
    if not args.Ultima then return end
    root
        :tag('tr')
            :tag('td')
                :attr('colspan', '2')
                :addClass('sinottico_piede')
                :cssText(args.StileUltima or '')
                :wikitext(args.Ultima)
                :newline()
end

local function renderNavBar(root, args)
    if not args.NomeTemplate then return end
    root
        :tag('tr')
            :tag('td')
                :attr('colspan', '2')
                :css('text-align', 'right')
                :wikitext(mw.getCurrentFrame():expandTemplate({
                    title = 'Tnavbar',
                    args = { args.NomeTemplate }
                }))
end

function p.infobox(frame)
    local args = require('Module:Arguments').getArgs(frame, { wrappers = 'Template:Infobox' })

    -- Crea l'albero html che rappresenta la tabella del sinottico e restituisce il markup
    if args.InserisciTestata == 'no' then
        root = mw.html.create('')
    else
        root = mw.html.create('table')
        root
            :addClass('sinottico')
            :cssText(args.StileTabella or '')
        renderTitle(root, args)
        renderImage(root, args)
    end
    renderRows(root, args)
    if args.InserisciCoda ~= 'no' then
        renderLastRow(root, args)
        renderNavBar(root, args)
    end

    return tostring(root)
end

return p