Modulo:Bandiera

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca

Modulo Lua che implementa le funzionalità dei Template:Bandiera e Template:Band dip.


--[[
* Modulo che implementa i template Bandiera e varianti.
]]

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
local mNazioni = require('Modulo:Nazioni')

-------------------------------------------------------------------------------
--						   Funzioni di utilità
-------------------------------------------------------------------------------

-- Error handler per xpcall, formatta l'errore
local function errhandler(msg)
	-- se necessita il backtrace usare:
	-- '<pre>' .. debug.traceback(msg) .. '</pre>'
	return string.format('<span style="color:red;">Errore: %s</span>', msg:match('.+:(.+)$'))
end

-- Aggiunge testo alla tabella "t", svolge anche la funzione di concatenatore
local function dumpText(t, ...)
	local arg = {...}
	for _, val in ipairs(arg) do
		table.insert(t, val)
	end
end

-------------------------------------------------------------------------------
--								classe Bandiera
-------------------------------------------------------------------------------

local Bandiera = {}

function Bandiera:new(args, dep)
	local self = {}

	setmetatable(self, { __index = Bandiera } )

	-- ha 2 parametri posizionali e 2 con nome
	self.nazName = args[1]
	self.labelType = dep and 'nome' or args[2]
	self.dim = args.dim
	self.lati = args.lati

	-- ricerca nazione
	self.naz = mNazioni._main( { self.nazName }, true )

	-- verifica argomenti
	if not self.naz then
		error('la nazionalità ' .. (self.nazName or '') .. ' non è valida')
	elseif self.dim and not self.dim:match('^%d+x?%d+$') then
		error('dim non è valido')
	elseif self.labelType and (self.labelType ~= 'nome' and self.labelType ~= 'sigla') then
		error('il parametro ' .. self.labelType .. ' non è valido')
	end

	-- eventuale bandiera dipendenza
	if dep and self.naz and self.naz.dip then
		self.bandDep = Bandiera:new({ self.naz.dip, 'nome' })
	end

	return self
end

function Bandiera:getWikiText()
	local t = {}

	if self.labelType then
		dumpText(t, '<span style="white-space:nowrap">')
	end

	if self.lati or self.naz.bl then
		dumpText(t, '<span style="margin:0px ', 
				 (self.lati or self.naz.bl), 'px 0px ',
				 (self.lati or self.naz.bl), 'px">')
	end

	dumpText(t, '[[File:', self.naz.band,
			 '|', self.naz.st,
			 (self.naz.sb and '' or '|border'),
			 '|', self.dim or '20x16', 'px|class=noviewer]]')

	if self.lati or self.naz.bl then
		dumpText(t, '</span>')
	end

	if self.labelType then
		if self.labelType == 'nome' then
			dumpText(t, '&nbsp;</span>[[',
					 (self.naz.wl and (self.naz.wl .. '|') or ''),
					 self.naz.st, ']]')
		elseif self.labelType == 'sigla' then
			dumpText(t, '&nbsp;</span>[[',
					 (self.naz.wl or self.naz.st), '|',
					 self.nazName, ']]')
		end
	end

	return (self.bandDep and (self.bandDep:getWikiText() .. '<br />&nbsp;&nbsp;&nbsp;') or '') ..
		   table.concat(t)
end

-------------------------------------------------------------------------------
--								  API
-------------------------------------------------------------------------------

local p = {}

-- Per utilizzare la classe Bandiera da un altro modulo
function p.getClass()
	return Bandiera
end

-- Entry-point per {{Band dip}}
function p.banddip(frame)
	return select(2, xpcall(function()
		return Bandiera:new(getArgs(frame, {parentOnly = true}), true):getWikiText()
	end, errhandler))
end

-- Entry-point per {{Bandiera}}
function p.main(frame)
	return select(2, xpcall(function()
		return Bandiera:new(getArgs(frame, {parentOnly = true})):getWikiText()
	end, errhandler))
end

return p