Modulo:Stemma

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

Modulo Lua che implementa il template {{Stemma con ornamenti comuni}}.

Ha una sottopagina di configurazione: Modulo:Stemma/Configurazione.


--[[
* Modulo che implementa il template "Stemma con ornamenti comuni".
* Ha sostituito tutte le ~180 sottopagine in "Categoria:Template ornamenti esteriori".
]]

require('strict')

local getArgs = require('Modulo:Arguments').getArgs
local cfg = mw.loadData('Modulo:Stemma/Configurazione')
local errorCategory = '[[Categoria:Voci con errori del modulo Stemma]]'

-- Error handler per xpcall, formatta l'errore.
--
-- @param {string} msg
-- @return {string}
local function errhandler(msg)
	local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
	return string.format('<span class="error">%s</span>%s', msg, cat)
end

-- =============================================================================
--                            Classe Stemma
-- =============================================================================

-- La classe Stemma è la classe principale del modulo.
-- Al suo interno ha un riferimento alla configurazione dell'ornamento richiesto,
-- allo scudo specificato e li restituisce in una tabella HTML opportunamente formattata.

local Stemma = {}

-- Costruttore della classe Stemma.
--
-- @param {table} args - gli argomenti passati al template
-- @return {table} un nuovo oggetto Stemma
function Stemma:new(args)
	local self = {}
 
	setmetatable(self, { __index = Stemma })

	-- ha 1 parametro posizionale e 3 con nome
	self.ornName = args[1]
	self.scudoFile = args.stemma
	self.align = args.align
	self.coef = tonumber(args.coef)

	-- ricerca l'ornamento
	self.orn = cfg.ornamenti[self.ornName]

	-- verifica argomenti
	if not self.orn then
		error(string.format('l\'ornamento %s non esiste', self.ornName or ''), 3)
	elseif args.coef and not self.coef then
		error('coef non è valido', 3)
	end

	self.coef = self.coef or 1
	self.ornFile = self.orn[1]
	self.ornSize = math.floor(self.orn[2] * self.coef)
	self.scudoSize = math.floor(self.orn[3] * self.coef)
	self.scudoTop = math.floor((self.orn[4] + 50) * self.coef)
	self.scudoLeft = math.floor((self.orn[5] + 50) * self.coef)
 
	return self
end

-- Restituisce una tabella HTML con l'ornamento e lo scudo richiesti.
--
-- @return {string}
function Stemma:getHTML()
	local tableStyle = {
		margin = '0',
		border = 'none',
		padding = '0',
		['background-color'] = 'transparent'
	}

	local root = mw.html.create('table')
	root
		:attr('border', '0')
		:attr('cellspacing', '0')
		:attr('cellpadding', '0')
		:attr('align', self.align)
		:css(tableStyle)
		:tag('tr')
			:tag('td')
				:tag('div')
					:css('position', 'relative')
					:wikitext(string.format('[[File:%s|%spx]]', self.ornFile, self.ornSize))
					:tag('div')
						:css('position', 'absolute')
						:css('border', 'none')
						:css('top', self.scudoTop .. 'px')
						:css('left', self.scudoLeft .. 'px')
						:tag('div')
							:css('position', 'absolute')
							:css('top', '0px')
							:css('left', '0px')
							:wikitext(string.format('[[File:%s|%spx]]', self.scudoFile or cfg.defaultScudo, self.scudoSize))

	return tostring(root)
end

-- =============================================================================
--                            Funzioni esportate
-- =============================================================================

local p = {}

-- Funzione per il template {{Stemma con ornamenti comuni}}.
function p.main(frame)
	return select(2, xpcall(function()
		return Stemma:new(getArgs(frame, { parentOnly = true })):getHTML()
	end, errhandler))
end

return p