Modulo:Infobox/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
test
Test parametro posizione
 
(20 versioni intermedie di 3 utenti non mostrate)
Riga 1:
-- Modulo per implementare le funzionalità di infobox
local p = {} -- per l'esportazione delle funzioni del modulo
require('Modulo:No globals')
local immagine_sinottico = require('Module:Immagine sinottico')._main
 
local args = {}-- variabile che contiene gli argomenti passati al template
local origArgs
local root -- radice del markup html
local dump = {}
 
local function checkList(valore)
 
-- Permette al software Mediawiki di gestire le liste # o *
local function is_defined(s)
local c = mw.ustring.sub(valore, 1, 1)
if s == nil or s == '' then
if c == '#' or c == '*' then
return false
valore = '<div>\n' .. valore .. '\n</div>'
end
return truevalore .. '\n'
end
 
local function getArgNums(...)
-------------------------------------------------------------------------------
-- Restituisce una lista che contiene il suffisso numerico di tutti gli argomenti
-- classe Infobox
-- che iniziano con il prefisso "prefix"
-------------------------------------------------------------------------------
-- Per esempio se nella lista argomenti sono valorizzati "Valore1, Valore2 e Valore4"
 
-- retistuirà la lista [1, 2, 4]
p.Infobox = {}
local prefixs = {...}
 
local nums = {}
function Infobox:new(args)
for k, _ in pairs(args) do
local self = {}
local num = nil
 
for _, candidate in ipairs(prefixs) do
setmetatable(self, { __index = Infobox })
num = ('' .. k):match('^' .. candidate .. '(%d+)$')
-- Crea l'albero html che rappresenta la tabella del sinottico
if num ~= nil then break end
self.stile_gruppi = args.stile_gruppi or ''
end
self.stile_etichette = args.stile_etichette or ''
if num then table.insert(nums, tonumber(num)) end
self.stile_valori = args.stile_valori or ''
if args.CreaTable == 'no' then
self.root = mw.html.create('')
else
self.root = mw.html.create('table')
self.root
:addClass(args.classe or 'sinottico')
:cssText(args.stile)
:attr('summary', args.sommario or 'Tabella sinottica che riassume i principali dati del soggetto' )
end
table.sort(nums)
self:_setupTableNode()
return selfnums
end
 
local function Infobox:tostringaddRow(rowArgs)
-- Aggiunge una riga alla tabella
return tostring(self.root)
-- Se rowArgs.gruppo non è nullo la considera come una riga di testata di gruppo
-- e ignora eventuali valorizzazioni di rowArgs.valore
if rowArgs.gruppo then
root
:tag('tr')
:addClass("sinottico_divisione")
:tag('th')
:attr('colspan', 2)
:cssText(rowArgs.stilegruppoN or args.StileGruppo or nil)
:wikitext(rowArgs.gruppo)
-- Altrimenti se rowArgs.valore non è nullo inserisce una riga dati, verificando
-- se esiste o meno la testata
elseif rowArgs.valore then
local row = root:tag('tr')
local dataCell
if rowArgs.nome then
row
:tag('th')
:cssText(rowArgs.stilenomeN or args.StileNome or nil)
:wikitext(rowArgs.nome)
dataCell = row:tag('td')
else
dataCell = row:tag('td')
:addClass('sinottico_testo_centrale')
:attr('colspan', 2)
end
dataCell
:addClass(rowArgs.classe or nil)
:cssText(rowArgs.stilevaloreN or args.StileValore or nil)
:wikitext(checkList(rowArgs.valore))
end
end
 
local function Infobox:sopratitolorenderTitle(text, options)
local suptitle = mw.html.create('')
if is_defined(text) then
if args.SopraTitolo then
self.subtitle = mw.html.create('')
suptitle
:tag('span')
:addClass('sinottico_sottotitolo')
:cssText(optionsargs.stile_sopratitoloStileSopraTitolo or nil)
:wikitext(textargs.SopraTitolo)
:done()
:tag('br'):done()
end
local subtitle = mw.html.create('')
return self
if args.SottoTitolo then
end
subtitle
 
:tag('br'):done()
function Infobox:sottotitolo(text, options)
if is_defined(text) then
self.subtitle = mw.html.create('')
:tag('br')
:done()
:tag('span')
:addClass('sinottico_sottotitolo')
:cssText(optionsargs.stile_sottotitoloStileSottoTitolo or nil)
:wikitext(textargs.SottoTitolo)
end
if args.TitoloEst then
return self
root
end
:tag('caption')
 
:addClass('sinottico_testata')
 
:cssText(args.StileTitoloEst or nil)
function Infobox:titolo(title, options)
:node(suptitle)
self.root = nil
if self.options :wikitext(args.TitoloEst then)
:node(subtitle)
self.root
elseif args.TitoloInt then
:tag('caption')
root
:addClass('sinottico_testata' )
:css("font-weight", "bold")
:cssText(options.stile)
:node(options.sopratitolo, options)
:wikitext(title)
:node(options.sottotitolo, options)
else
self.root
:tag('tr')
:addClass('sinottico_testata')
:tag('th')
:attr('colspan', '2')
:node(options.sopratitolo, optionssuptitle)
:cssText(optionsargs.stileStileTitoloInt or nil)
:wikitext(titleargs.TitoloInt)
:node(options.sottotitolo, optionssubtitle)
end
return self
end
 
local function infobox:grupporenderImage(label, options)
if not is_defined(label)args.Immagine then return self end
local cell_immagine = mw.html.create('td')
if options.is_opzionale ~= nil then
cell_immagine
self.opzionale_label = label
:addClass('sinottico_testo_centrale ' .. (args.ClasseImmagine or ''))
self.opzionale_options = options
:attr('colspan', '2')
return self
:cssText(args.StileImmagine or nil)
:wikitext(args.Immagine)
if args.Didascalia then
cell_immagine
:tag('br', {selfClosing = true})
:done()
:tag('span')
:cssText(args.StileDidascalia or nil)
:wikitext(args.Didascalia)
end
root:tag('tr'):node(cell_immagine)
if self.opzionale_label ~= nil then
infobox:voidGroup()
end
self.root
:tag('tr')
:addClass("sinottico_divisione")
:tag('th')
:attr('colspan', 2)
:cssText(options.stile or self.stile_gruppi or '')
:wikitext(label)
return self
 
end
 
function infobox:annulla_gruppo()
self.opzionale_label = ''
self.opzionale_options = nil
return self
end
 
function infobox:valore(value, label, options)
if not is_defined(value) then return self end
if self.opzionale_label then
self:gruppo(self.opzionale_label, self.opzionale_options)
self:annulla_gruppo()
end
local row = self.root:tag('tr')
local dataCell
if label ~= nil then
row
:tag('th')
:cssText(options.stile_etichetta or self.stile_etichette or '')
:wikitext(label)
dataCell = row:tag('td')
else
dataCell = row:tag('td')
:attr('colspan', 2)
:css('text-align', 'center')
end
dataCell
:addClass(options.classe or '')
:cssText(args.stile_valore or self.stile_valori or '')
:wikitext(value)
return self
end
 
-------------------------------------------------------------------------------
-- fine classe Infobox
-------------------------------------------------------------------------------
 
local function getArgNums(...)
-- Restituisce una lista che contiene il suffisso numerico di tutti gli argomenti
-- che iniziano con il prefisso "prefix"
-- Per esempio se nella lista argomenti sono valorizzati "Valore1, Valore2 e Valore4"
-- retistuirà la lista [1, 2, 4]
local prefixs = {...}
local nums = {}
for k, _ in pairs(args) do
local num = nil
for _, candidate in ipairs(prefixs) do
num = ('' .. k):match('^' .. candidate .. '(%d+)$')
if num ~= nil then break end
end
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
 
local function renderRows()
local rownums = getArgNums('Valore', 'GruppoOpzionale', 'Gruppo', 'Nodo')
for k, num in ipairs(rownums) do
local skip = false
if args['GruppoOpzionale' .. num] ~= nil then
skip = true
if args['GruppoOpzionale' .. num] ~= '$fine' then
for j = k+1, #rownums do
Infobox:gruppo(args['GruppoOpzionale' .. num], {opzionale='s',
classe =if args['ClasseGruppo' .. numrownums[j]], stile ~= nil or args['GruppoStileGruppoOpzionale' .. numrownums[j]})]~=nil then break end
if args['Valore' .. rownums[j]] ~= nil or args['Nodo' .. rownums[j]] ~= nil then
else
skip = false
Infobox:annulla_gruppo()
break
end
end
elseend
Infobox:valore(if args['ValoreNodo' .. num], args['Nome'~= ..nil num])then
root:wikitext(args['Nodo' .. num])
skip = true
end
if not skip and args['GruppoOpzionale' .. num] ~= '$fine' then
addRow({
gruppo = args['GruppoOpzionale' .. num] or args['Gruppo' .. num],
nome = args['Nome' .. num],
valore = args['Valore' .. num],
classe = args['Classe' .. num],
stilegruppoN = args['GruppoStile' .. num],
stilenomeN = args['NomeStile' .. num],
stilevaloreN = args['ValoreStile' .. num]
})
end
end
end
 
local function renderLastRow()
if not args.Ultima then return end
root
:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('sinottico_piede')
:cssText(args.StileUltima or nil)
:wikitext(args.Ultima)
:newline()
end
 
local function renderNavBar()
if not args.NomeTemplate then return end
root
:tag('tr')
:tag('td')
:addClass('sinottico_piede2 noprint nomobile metadata')
:attr('colspan', '2')
:wikitext(mw.getCurrentFrame():expandTemplate({
title = 'Link sinottico',
args = args.LinkWikidata and
{ args.NomeTemplate } or
{ args.NomeTemplate, nowd = 1 }
}))
end
 
local function _infobox()
-- Crea l'albero html che rappresenta la tabella del sinottico e restituisce il markup
if args.CreaTable == 'no' or args.Posizione == 'corpo' or args.Posizione == 'coda' then
Infobox = infobox.create({sommario = args.summary, stile_gruppi = args.StileGruppo,
root = mw.html.create('')
stile_valori = args.StileValore, stile_etichette = args.StileEtichetta })
local titolo, stile_titolo
if args.TitoloEst then
titolo = args.TitoloEst
stile = args.StileTitoloEst
else
root = mw.html.create('table')
titolo = args.TitoloInt
root
stile = args.StileTitoloInt
:addClass('infobox sinottico')
:cssText(args.StileTabella or nil)
:attr('summary', args.Summary or 'Tabella sinottica che riassume i principali dati del soggetto')
end
renderTitle()
Infobox:titolo( titolo, nil , { stile = stile, sottotitolo=args.SottoTitolo, stile_sottotitolo=args.StileSottoTitolo,
renderImage()
sopratitolo = args.SopraTitolo, stile_sopratitolo = args.SopraTitolo })
renderRows()
if args.Immagine then
renderLastRow()
local args_immagine =
renderNavBar()
Infobox:Valore(immagine_sinottico({ args.Immagine, args.Didascalia, stile_valore = args.StileImmagine,
compatibile = 's'}))
local res = tostring(root)
end
if args.Posizione == 'coda' then
renderRows(Infobox)
res = res .. '</table>';
Infobox:valore(args.Ultima, nil , {classe_valore='sinottico_piede', stile_valore = args.StileUltima })
ifelseif args.NomeTemplatePosizione == 'testa' then
res = mw.ustring.gsub( res, '</table>$', '')
local txt_nav = mw.getCurrentFrame():expandTemplate({
title = 'Link sinottico',
args = args.LinkWikidata and
{ args.NomeTemplate } or
{ args.NomeTemplate, nowd = 1 }
})
Infobox:valore(txt_nav, nil , {classe_valore='sinottico_piede2 noprint metadata'})
end
return tostring(root)res
end
 
Riga 325 ⟶ 318:
preprocessSingleArg('StileValore')
preprocessArgs({
{prefix = 'Nodo'},
{prefix = 'Gruppo', depend={'GruppoStile'}},
{prefix = 'GruppoOpzionale', depend={'GruppoStile'}},
{prefix = 'Valore', depend={'Nome', 'Classe', 'NomeStile', 'ValoreStile'}},
}, 50)
preprocessSingleArg('Ultima')
Riga 334 ⟶ 328:
preprocessSingleArg('LinkWikidata')
preprocessSingleArg('CreaTable')
preprocessSingleArg('Posizione')
preprocessSingleArg('Summary')
return _infobox()