Modulo:Stemma: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m aggiornato al funzionamento di mw.html:attr
aggiornamento generale
Riga 1:
--[[
* Modulo perche implementareimplementa leil funzionalità ditemplate "Stemma con ornamenti comuni".
* Sostituisce 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('Modulo:No globals')
-- 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
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
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
 
-------------------------------------------------------------------------------
-- classe Stemma
-------------------------------------------------------------------------------
 
Riga 39 ⟶ 22:
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 = 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:__tostringgetHTML()
local tableStyle = {
root = mw.html.create('')
margin = '0',
root
border = 'none',
:tag("table")
padding = '0',
:attr("border", "0")
['background-color'] = 'transparent'
: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]]")
 
local root = mw.html.create('table')
return tostring(root)
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
-- API
-------------------------------------------------------------------------------
 
Riga 110 ⟶ 93:
 
-- Entry-point per {{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