Modulo:Stemma

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 per implementare le funzionalità di "Stemma con ornamenti comuni"
*
* Traduce in lua:
* Template:Stemma_con_ornamenti_comuni
* Template:Stemma_con_ornamenti_comuni/parametro* (~180 sottopagine nella "Categoria:Template ornamenti esteriori")
]]
-- Moduli
local htmlBuilder = require("Modulo:HtmlBuilder")
-- Configurazione
local cfg = mw.loadData("Modulo:Stemma/Configurazione")
-------------------------------------------------------------------------------
-- Funzioni di utilità
-------------------------------------------------------------------------------
-- Error handler per xpcall, formatta l'errore
local function errhandler(msg)
return string.format("<span style=\"color:red;\">Errore: %s</span>", msg:match(".+:(.+)$"))
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
-------------------------------------------------------------------------------
local 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"])
-- ricerca l'ornamento
self.orn = cfg.ornamenti[self.ornName]
-- verifica argomenti
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
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:__tostring()
root = htmlBuilder.create()
root
.tag("table")
.attr("border", "0")
.attr("cellspacing", "0")
.attr("cellpadding", "0")
.attr(self.align and "align" or nil, 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]]")
return tostring(root)
end
-------------------------------------------------------------------------------
-- API
-------------------------------------------------------------------------------
local p = {}
-- Entry-point per {{Stemma con ornamenti comuni}}
function p.stemma(frame)
return select(2, xpcall(function() return Stemma:new(getArgs(frame)) end, errhandler))
end
return p