Modulo:Partiti

Versione del 22 mag 2017 alle 12:37 di Moroboshi (discussione | contributi) (aggiungo aggancio per tabella colore_ombra)
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Partiti/man (modifica · cronologia)
Sandbox: Modulo:Partiti/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Partiti/test (modifica · cronologia · esegui)

Questo modulo serve a gestire i colori relativi ai partiti politici.


local cfg = mw.loadData( 'Modulo:Partiti/Configurazione' );
local getArgs = require('Module:Arguments').getArgs

-- ====================================================
-- corregge un valore di ritorno se questo inizia per #
-- vedi bug https://phabricator.wikimedia.org/T14974
-- =====================================================
local function fix_return(colore)
	if string.sub(colore,1,1) == '#' then
		colore = "#" .. string.sub(colore, 2, 7)
	end
	return colore
end

local function _colore(partito, default, ombra) 
	default = default or ''
	if partito == nil then return default end
	local partito_indice = cfg.alias[partito] or partito 
	local colore
	if ombra then
		colore =  cfg.colore_ombra[partito_indice] or default
	else
		colore = cfg.colore[partito_indice] or default
	end
	return  fix_return(colore)
end

local function colore(frame)
	local args = getArgs(frame)
	return _colore(args[1], args[2])
end

local function colore_ombra(frame)
	local args = getArgs(frame)
	return _colore(args[1], args[2], True)
end

local function _nome_corto(partito)
	if partito == nil then return '' end
	partito_indice = cfg.alias[partito] or partito 
	return cfg.nome_corto[partito_indice] or partito_indice
end

local function nome_corto(frame)
	local args = getArgs(frame)
	return _nome_corto(args[1])
end

local function color_table(frame)

	local color_t = mw.html.create('table'):addClass('wikitable')
	color_t:tag('tr'):tag('th'):attr('colspan', '2'):wikitext('Partito')
	local names = {}
	for k,v in pairs(cfg.colore) do
		names[#names+1] = k
	end
	table.sort(names)
	for _,name in ipairs(names) do
		color_t
			:tag('tr')
				:tag('td'):wikitext(' '):css('background-color', cfg.colore[name]):done()
				:tag('td'):wikitext('[[' .. name .. ']]'):done()
	end
	return color_t
end

return {
	colore = colore,
	_colore = _colore,
	nome_corto = nome_corto,
	_nome_corto = _nome_corto,
	color_table = color_table,
	colore_ombra = colore_ombra
}