Modulo:Stemma: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m aggiornato al funzionamento di mw.html:attr
m modulo no globals obsoleto
 
(3 versioni intermedie di un altro utente non mostrate)
Riga 1:
--[[
* Modulo perche implementareimplementa leil funzionalità ditemplate "Stemma con ornamenti comuni".
* Ha sostituito tutte le ~180 sottopagine in "Categoria:Template ornamenti esteriori".
*
* Traduce in lua:
* Template:Stemma_con_ornamenti_comuni
* Template:Stemma_con_ornamenti_comuni/parametro* (~180 sottopagine nella "Categoria:Template ornamenti esteriori")
]]
 
require('strict')
-- Configurazione
local cfg = mw.loadData("Modulo:Stemma/Configurazione")
 
local getArgs = require('Modulo:Arguments').getArgs
-------------------------------------------------------------------------------
local cfg = mw.loadData('Modulo:Stemma/Configurazione')
-- Funzioni di utilità
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 style=\"color:red;\">Errore: %s</span>", msg:match(".+:(.+)$"))
return string.format('<span class="error">%s</span>%s', msg, cat)
end
 
-- =============================================================================
-- Ritorna gli argomenti passati al modulo, eliminando quelli contenenti stringhe vuote
-- Classe Stemma
local function getArgs(frame)
-- =============================================================================
local args = {}
for k, v in pairs(frame:getParent().args) do
if v ~= "" then
args[k] = v
end
end
return args
end
 
-- La classe Stemma è la classe principale del modulo.
-------------------------------------------------------------------------------
-- Al suo interno ha un riferimento alla configurazione dell'ornamento richiesto,
-- classe Stemma
-- 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, })
__tostring = function(t) return self:__tostring() end,
__concat = function(t, t2) return tostring(t) .. tostring(t2) end })
-- 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"])
 
-- ha 1 parametro posizionale e 3 con nome
-- ricerca l'ornamento
self.ornornName = cfg.ornamentiargs[self.ornName1]
self.scudoFile = args.stemma
self.align = args.align
self.coef = tonumber(args.coef)
 
-- ricerca l'ornamento
-- verifica argomenti
self.orn = cfg.ornamenti[self.ornName]
if not self.orn then
error("l'ornamento " .. (self.ornName or "") .. " non è valido")
elseif args["coef"] and not self.coef then
error("coef non è valido")
end
 
-- verifica argomenti
self.coef = self.coef or 1
if not self.ornFileorn = self.orn[1]then
error(string.format('l\'ornamento %s non esiste', self.ornName or ''), 3)
self.ornSize = tonumber(self.orn[2]) * self.coef
elseif selfargs.scudoSizecoef =and tonumber(self.orn[3]) *not self.coef then
error('coef non è valido', 3)
self.scudoTop = (tonumber(self.orn[4]) + 50) * self.coef
end
self.scudoLeft = (tonumber(self.orn[5]) + 50) * self.coef
 
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
function Stemma:__tostring()
root = mw.html.create('')
root
:tag("table")
:attr("border", "0")
:attr("cellspacing", "0")
:attr("cellpadding", "0")
:attr("align", self.align)
:css("margin", "0")
:css("border", "none")
:css("padding", "0")
:css("background-color", "transparent")
:tag("tr")
:tag("td")
:tag("div")
:css("position", "relative")
:wikitext("[[File:" .. self.ornFile .. "|" .. self.ornSize .. "px]]")
: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("[[File:" .. (self.scudoFile or cfg.defaultScudo) ..
"|" .. self.scudoSize .. "px]]")
 
-- Restituisce una tabella HTML con l'ornamento e lo scudo richiesti.
return tostring(root)
--
-- @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 APIesportate
-- =============================================================================
-------------------------------------------------------------------------------
 
local p = {}
 
-- Entry-pointFunzione per il template {{Stemma con ornamenti comuni}}.
function p.stemmamain(frame)
return select(2, xpcall(function()
return Stemma:new(getArgs(frame, { parentOnly = true })) :getHTML()
end, errhandler))
end
 
return p