Modulo:Stemma

Versione del 19 set 2016 alle 15:52 di Rotpunkt (discussione | contributi) (aggiornamento generale)
Info Istruzioni per l'uso
Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Stemma/man (modifica · cronologia)
Sandbox: Modulo:Stemma/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Stemma/test (modifica · cronologia · esegui)

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".
* Sostituisce tutte le ~180 sottopagine in "Categoria:Template ornamenti esteriori".
]]

require('Modulo:No globals')

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

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
-------------------------------------------------------------------------------

local 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 = tonumber(self.orn[2]) * self.coef
	self.scudoSize = tonumber(self.orn[3]) * self.coef
	self.scudoTop = (tonumber(self.orn[4]) + 50) * self.coef
	self.scudoLeft = (tonumber(self.orn[5]) + 50) * self.coef
 
	return self
end
 
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

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

local p = {}

-- Entry-point per {{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