Modulo:Discendenza: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
ottimizzazione
Riga 1:
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local errorCategory = '[[Categoria:Errori di compilazione del template Discendenza]]'
 
local p = {}
Line 6 ⟶ 7:
local tabella = {}
local txt = {}
 
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
 
local function agg(t)
Line 15 ⟶ 21:
local resto = 0
local nx,px
-- conta elementi
local err = '-'
 
while (dati[n]) do n = n+1 end
n = n-1
-- crea pers dai dati
 
for m=4,n,4 do
nx = tonumber(dati[m-3])
Line 26 ⟶ 31:
if (px) then
if (pers[nx]) then
err = error(string.format('inseritoInserito id = <b>%d</b> per più elementi',nx))
else
pers[nx] = { padre = px, testo = dati[m-1], nota = dati[m], id = -1, x = -1, y = -1, sp = 0, figli = {} }
end
else
err = error(string.format('inseritoInserito id genitore = <b>%s</b> non numerico (id = <b>%d</b>)',dati[m-2],nx))
end
else
err = error(string.format('inseritoInserito id = <b>%s</b> non numerico',dati[m-3]))
end
resto = n-m
end
if (err == '-' and resto > 0) then
err = error(string.format('numeroNumero di dati %d non valido (elementi in più: %d)',n,resto))
end
return err
end
 
Line 133 ⟶ 137:
local tt = #pers[pid].figli
if (tt == 0) then
if (pers[pid].padre == -1 or pers[pid].id == 1) then
pers[pid].x = 0
elseif (pers[pid].id == 1) then
pers[pid].x = 0
else
Line 141 ⟶ 143:
end
elseif (tt == 1) then
if (pers[pid].padre == -1 or pers[pid].id == 1) then
pers[pid].x = pers[pers[pid].figli[1]].x
elseif (pers[pid].id == 1) then
pers[pid].x = pers[pers[pid].figli[1]].x
else
Riga 152:
else
local media = math.floor((pers[pers[pid].figli[1]].x + pers[pers[pid].figli[tt]].x)/2)
if (pers[pid].padre == -1 or pers[pid].id == 1) then
pers[pid].x = media
elseif (pers[pid].id == 1) then
pers[pid].x = media
else
Line 196 ⟶ 194:
local function mostraX(pid,allinea,largo,dida)
local xy = massimoXY(pid, {0, 0})
local p1,posx p2,= p3{}
local n1
local s1, s2, s3stx
local riga = {}
local xx, xp
if (allinea == 'destra') then
agg(string.format('<div class="floatright" style="width:'..largo..'px%dpx;padding:3px;background:#fff;border:1px solid #c8ccd1">', largo))
agg('<table cellpadding="1" cellspacing="0" border=0 style="border-collapse:separate;text-align:center;font-size:95%;line-height:110105%;margin:0px auto">')
else
agg('<table cellpadding="1" cellspacing="0" border=0 style="border-collapse:separate;text-align:center;font-size:95%;line-height:110105%;margin:10px auto">')
end
local lg = math.floor(100/(xy[1]+2))
if (lg == 0) then lg = 1 end
for n=1,xy[2] do
p1riga[1] = 0{}; p2riga[2] = 0{}; p3riga[3] = 0{}
posx[1] = 0; posx[2] = 0; posx[3] = 0
n1 = 0
if table.insert(n==riga[1) then s1 = ],string.format('<tr%s>',n>1 else s1 =and '<tr style="line-height:8px">' endor ''))
s2 = table.insert(riga[2],'<tr>')
if table.insert(riga[3],string.format('<tr%s>',n<xy[2]) then s3 =and '<tr style="line-height:8px">' else s3 =or '' end))
for _, v in pairs(tabella[n]) do
xx = pers[v].x
xp = pers[v].padre
if (n==1) then
 
for m=1,(xy[1]+2) do table.insert(riga[1],string.format('<td width=%d%%></td>',lg)) end
if (n>1) then
s1 = s1..'<td style="border-right:1px solid #000'
if (n1 == xp) then s1 = s1 ..';border-top:1px solid #000' end
n1 = xp
if (xx-p1>0) then s1 = s1..'" colspan="'..(xx+1-p1) end
p1 = xx + 1
s1 = s1 .. '">&nbsp;</td>'
else
forstx m=1,(xy[1]+2) do s1 = s1.string.format('<td widthstyle='..lg..'"border-right:1px solid #000%s%s">&nbsp;</td>' end,
n1==xp and ';border-top:1px solid #000' or '',
xx-posx[1]>0 and '" colspan="'..(xx+1-posx[1]) or ''
)
n1 = xp
posx[1] = xx + 1
table.insert(riga[1],stx)
end
 
if (xx-p2posx[2]>0) then
table.insert(riga[2],string.format('<td%s>&nbsp;</td>',
s2 = s2..'<td'
if ( xx-p2posx[2]>1) thenand s2 = s2..' colspan="'..(xx-p2posx[2])..'"' endor ''))
end
s2 = s2..'>&nbsp;</td>'
table.insert(riga[2],'<td colspan=2>'..pers[v].testo)
if (pers[v].nota == '-') then else table.insert(riga[2],
string.format('<br/><span style="font-size:90%%"><i>%s</i></span>',pers[v].nota))
end
table.insert(riga[2],'</td>')
s2 = s2..'<td colspan=2>'..pers[v].testo..'<br/>'
posx[2] = xx + 2
if (pers[v].nota == '-') then else s2 = s2..'<span style="font-size:90%"><i>'..pers[v].nota..'</i></span>' end
s2 = s2..'</td>'
p2 = xx + 2
 
if (n<xy[2]) then
if (#pers[v].figli > 0) then
s3 = s3table.insert(riga[3],string.format('<td style="border-right: 1px solid #000%s">&nbsp;</td>',
if ( xx-p3posx[3]>0) thenand s3 = s3..'" colspan="'..(xx+1-p3posx[3]) endor '')
p3 = xx + 1)
s3posx[3] = s3xx .. '">&nbsp;</td>'+ 1
end
end
end
 
s1 = s1.table.insert(riga[1],'</tr>')
s2 = s2.table.insert(riga[2],'</tr>')
if (n<xy[2]) then s3 = s3.table.insert(riga[3],'</tr>') end
 
agg(s1);aggtable.concat(s2riga[1]);agg(s3)
agg(table.concat(riga[2]))
agg(table.concat(riga[3]))
end
 
Line 312 ⟶ 316:
end
 
function p.main_discendenza(frameargs)
local capo = -1
local n1, n2
local errlato = args['-allinea'] or 'centro'
local larg = args['larghezza'] or '300'
local tipo = args['tipo'] or 'o'
local dida = args['didascalia'] or ''
dividi(args)
n1 = 0
for i, v in pairs(pers) do
n1 = n1+1
if (v.padre == -1) then
if (capo == -1) then
capo = i
else
error(string.format('Inseriti più elementi come capostipite (id = %d, %d)',capo,i))
end
else
if (v.padre == i) then
error(string.format('Inserito elemento come figlio di se stesso (id = %d)', i))
elseif (pers[v.padre]) then
table.insert(pers[v.padre].figli,i)
else
error(string.format('Inserito id genitore = %d non valido (id = %d)',v.padre,i))
end
end
end
 
if (capo == -1) then
error('Capostipite non definito')
else
n2 = organizza(capo, 1)
if (n1 == n2) then
if (tipo == 'v') then
return mostraY(capo)
elseif (tipo == 'o') then
calcolaX1(capo)
calcolaX2(capo)
calcolaX3(capo, 0)
return mostraX(capo, lato, larg, dida)
end
else
error('Inseriti elementi non collegati al capostipite')
end
end
end
 
function p.main(frame)
local args = getArgs(frame, {
valueFunc = function (key, value)
Line 330 ⟶ 378:
end
})
return p._discendenza(args)
local lato = args['allinea'] or 'centro'
local larg = args['larghezza'] or '300'
local tipo = args['tipo'] or 'o'
local dida = args['didascalia'] or ''
err = dividi(args)
if (err == '-') then
n1 = 0
for i, v in pairs(pers) do
n1 = n1+1
if (v.padre == -1) then
if (capo == -1) then
capo = i
else
err = string.format('inseriti più elementi come capostipite (id = <b>%d, %d</b>)',capo,i)
end
else
if (v.padre == i) then
err = string.format('inserito elemento come figlio di se stesso (id = <b>%d</b>)', i)
elseif (pers[v.padre]) then
table.insert(pers[v.padre].figli,i)
else
err = string.format('inserito id genitore = <b>%d</b> non valido (id = <b>%d</b>)',v.padre,i)
end
end
end
 
if (err == '-') then
if (capo == -1) then
err = 'capostipite non definito'
else
n2 = organizza(capo, 1)
if (n1 == n2) then
if (tipo == 'v') then
return mostraY(capo)
elseif (tipo == 'o') then
calcolaX1(capo)
calcolaX2(capo)
calcolaX3(capo, 0)
return mostraX(capo, lato, larg, dida)
end
else
err = 'inseriti elementi non collegati al capostipite'
end
end
end
end
return string.format('<p style="color:#b80000"><b>Template:Discendenza - Errore:</b> %s</p>[[Categoria:Errori di compilazione del template Discendenza]]',err)
end