Mòdul:Identificació ràdio

De la Viquipèdia, l'enciclopèdia lliure
local p = {} -- per l'esportazione delle funzioni del modulo

local args={} -- variabile che contiene gli argomenti passati al template

local icao_table = {
    A = {'Alfa', 'Alpha.svg'},
    B = {'Bravo', 'Bravo.svg'},
    C = {'Charlie', 'Charlie.svg'},
    D = {'Delta', 'Delta.svg'},
    E = {'Echo', 'Echo.svg'},
    F = {'Foxtrot', 'Foxtrot.svg'},
    G = {'Golf', 'Golf.svg'},
    H = {'Hotel', 'Hotel.svg'},
    I = {'India', 'India.svg'},
    J = {'Juliet', 'Juliet.svg'},
    K = {'Kilo', 'Kilo.svg'},
    L = {'Lima', 'Lima.svg'},
    M = {'Mike', 'Mike.svg'},
    N = {'November', 'November.svg'},
    O = {'Oscar', 'Oscar.svg'},
    P = {'Papa', 'Papa.svg'},
    Q = {'Quebec', 'Quebec.svg'},
    R = {'Romeo', 'Romeo.svg'},
    S = {'Sierra', 'Sierra.svg'},
    T = {'Tango', 'Tango.svg'},
    U = {'Uniform', 'Uniform.svg'},
    V = {'Victor', 'Victor.svg'},
    W = {'Whiskey', 'Whiskey flag.svg'},
    X = {'X-ray', 'Xray.svg'},
    Y = {'Yankee', 'Yankee.svg'},
    Z = {'Zulu', 'Zulu.svg'},
    ['1'] = {'One', 'ICS Pennant One.svg'},
    ['2'] = {'Two', 'ICS Pennant Two.svg'},
    ['3'] = {'Three', 'ICS Pennant Three.svg'},
    ['4'] = {'Four', 'ICS Pennant Four.svg'},
    ['5'] = {'Five', 'ICS Pennant Five.svg'},
    ['6'] = {'Six', 'ICS Pennant Six.svg'},
    ['7'] = {'Seven', 'ICS Pennant Seven.svg'},
    ['8'] = {'Eight', 'ICS Pennant Eight.svg'},
    ['9'] = {'Nine', 'ICS Pennant Niner.svg'},
    ['0'] = {'Zero', 'ICS Pennant Zero.svg'},
}

local parametri_nominati = { 'codice', 'compatto', 'dimensione'}

local function _identificativo_radio_compatto()
    -- Restituisce l'identificativo radio come seguenza compatta inlinea
    -- formata dalle lettere del codice seguita dalle bandiere.
    -- Esclude lettere non corrispondenti a una bandiera
    if not args.codice then return '' end
    root = mw.html.create('span')
    local codice = string.upper(args.codice)
    local extended_code = {}
    local flags = mw.html.create('')
    for letter in codice:gmatch(".") do
        if icao_table[letter] then
            flags
                :tag('span')
                    :css('padding', '0 2px 0')
                    :wikitext('[[File:' .. icao_table[letter][2] .. '|' .. tostring(args.dimensione)
                    .. 'px|' .. icao_table[letter][1] .. '|class=noviewer]]')
            table.insert(extended_code, letter)
        end
    end
    if #extended_code == 0 then return '' end
    root
        :wikitext(table.concat(extended_code) .. ' ')
        :node(flags)
    return tostring(root)
end

local function _identificativo_radio()
    -- Restituisce l'identificativo radio come tabella per l'inserimento
    -- nel template infobox nave (bandiere con sotto la lettera e ancora
    -- più sotto il codice per esteso)
    if not args.codice then return '' end
    root = mw.html.create('div')
    root
        :css('text-align', 'center')
    local codice = string.upper(args.codice)
    local extended_code = {}
    local inner_div = mw.html.create('div')
    for letter in codice:gmatch(".") do
        if icao_table[letter] then
            inner_div
                :tag('div')
                    :css('float', 'left')
                    :css('padding', '0 8px 0;')
                    :wikitext('[[File:' .. icao_table[letter][2] .. '|' .. tostring(args.dimensione) .. 'px|' .. icao_table[letter][1] .. '|class=noviewer]]')
                    :tag('br', {selfClosing=true})
                    :done()
                    :wikitext(letter)
            table.insert(extended_code, icao_table[letter][1])
        end
    end
    if #extended_code == 0 then return '' end
    inner_div
        :css('width', tostring((args.dimensione+16)*#extended_code+16).. 'px')
        :css('margin-left', 'auto')
        :css('margin-right', 'auto')
    root
        :node(inner_div)
        :tag('div')
            :css('font-size', '80%')
            :css('clear', 'both')
            :wikitext("(''" .. table.concat(extended_code, '-') .. "'')")
    return tostring(root)
end

function p.codice(frame)

    args['codice'] = frame.args[1]
    if frame.args[2] and frame.args[2] ~= '' then 
        args['compatto'] = true
    else
        args['compatto'] = false
    end
    args['dimensione'] = tonumber(frame.args[3]) or 15 
    if args.compatto then
        return _identificativo_radio_compatto()
    else
        return _identificativo_radio()
    end
end

return p