Modulo:Discendenza/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
use require('strict') instead of require('Module:No globals')
 
(41 versioni intermedie di 2 utenti non mostrate)
Riga 1:
require('Module:No globalsstrict')
local getArgs = require('Module:Arguments').getArgs
local errorCategory = '[[Categoria:Errori di compilazione del template Discendenza]]'
local mwHtml = getmetatable( mw.html.create() ).__index
 
function mwHtml:attrIf( cond, name, value )
if cond then
return self:attr( name, value )
else
return self
end
end
 
function mwHtml:cssIf( cond, name, value )
if cond then
return self:css( name, value )
else
return self
end
end
 
function mwHtml:wikitextIf( cond, value1, value2 )
if cond then
return self:wikitext( value1 )
elseif not (value2 == nil) then
return self:wikitext( value2 )
else
return self
end
end
 
local p = {}
local pers = {}
local tabella = {}
 
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 dividi(dati)
Line 10 ⟶ 43:
local resto = 0
local nx,px
local err = '-'
 
while (dati[n]) do n = n+1 end
n = n-1
 
for m=4,n,4 do
nx = tonumber(dati[m-3])
Line 21 ⟶ 51:
if (px) then
if (pers[nx]) then
err = error(string.format('Inserito id = %d utilizzato per elementipiù diversielementi',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('Inserito id genitore = %s non numerico per(id elemento= %d)',dati[m-2],nx))
end
else
err = error(string.format('Inserito id elemento= %s 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 128 ⟶ 157:
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 136 ⟶ 163:
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
Line 147 ⟶ 172:
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 189 ⟶ 212:
end
 
local function mostramostraX(pid,allinea,largo,dida)
local xyposx = massimoXY(pid, {0, 0})
local p1, p2, p3
local n1
local s1, s2, s3stx
local riga = {}
local xx, xp
local stileDiv = { ['width'] = largo..'px', ['padding'] = '3px', ['background'] = '#FFF', ['border'] = '1px solid #C8CCD1' }
local ss = ''
local stileTabella = { ['border-collapse'] = 'separate', ['text-align'] = 'center', ['font-size'] = '95%', ['line-height'] = '105%', ['margin'] = '0px auto !important', }
if (allinea == 'destra') then
local xy = massimoXY(pid, {0, 0})
ss = '<div class="floatright" style="width:'..largo..'px;padding:3px;background:#fff;border:1px solid #c8ccd1"><table cellpadding="1" cellspacing="0" border=0 style="text-align:center;font-size:95%;line-height:110%;margin:0px auto">'
else
ss = '<center><table cellpadding="1" cellspacing="0" border=0 style="text-align:center;font-size:95%;line-height:110%;margin:10px auto">'
end
local lg = math.floor(100/(xy[1]+2))
if (lg == 0) then lg = 1 end
 
local bDiv = mw.html.create('div')
if (allinea == 'destra') then
bDiv:css(stileDiv):addClass('floatright')
stileTabella['margin'] = '10px auto'
end
local bTabella = mw.html.create('table')
:css(stileTabella)
:attr({['cellpadding']='1',['cellspacing']='0',['border']='0'})
for n=1,xy[2] do
local riga1 = mw.html.create('tr')
p1 = 0; p2 = 0; p3 = 0
local riga2 = mw.html.create('tr')
local riga3 = mw.html.create('tr')
posx[1] = 0; posx[2] = 0; posx[3] = 0
n1 = 0
if (n==>1) then s1 = riga1:css('<tr>' else s1 = '<tr style="line-height:','8px">') end
if (n<xy[2]) then riga3:css('line-height','8px') end
s2 = '<tr>'
if (n<xy[2]) then s3 = '<tr style="line-height:8px">' else s3 = '' 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
s1 = s1..'<td style="border-right:1px solid #000'
riga1:node(mw.html.create('td'):css('width',lg..'%'))
if (n1 == xp) then s1 = s1 ..';border-top:1px solid #000' end
n1 = xpend
if (xx-p1>0) then s1 = s1..'" colspan="'..(xx+1-p1) end
p1 = xx + 1
s1 = s1 .. '">&nbsp;</td>'
else
riga1:node(mw.html.create('td')
for m=1,(xy[1]+2) do s1 = s1..'<td width='..lg..'%></td>' end
:css('border-right','1px solid #000')
:cssIf(n1 == xp,'border-top','1px solid #000')
:attrIf(xx-posx[1]>0,'colspan',xx+1-posx[1])
)
n1 = xp
posx[1] = xx + 1
end
 
if (xx-p2posx[2]>0) then
s2 = s2riga2:node(mw.html.create('<td')
if :attrIf(xx-p2posx[2]>1) then s2 = s2..,' colspan="'..(,xx-p2posx[2])..'"' end
)
s2 = s2..'>&nbsp;</td>'
end
riga2:node(mw.html.create('td')
s2 = s2..'<td colspan=2>'..pers[v].testo..'<br/>'
:attr('colspan','2')
if (pers[v].nota == '-') then else s2 = s2..'<span style="font-size:90%"><i>'..pers[v].nota..'</i></span>' end
:wikitextIf(pers[v].nota=='-', pers[v].testo, string.format('%s<br/><span style="font-size:90%%"><i>%s</i></span>',pers[v].testo,pers[v].nota))
s2 = s2..'</td>'
p2 = xx + 2)
posx[2] = xx + 2
 
if (n<xy[2]) then
if (#pers[v].figli > 0) then
riga3:node(mw.html.create('td')
s3 = s3..'<td style="border-right: 1px solid #000'
:css('border-right','1px solid #000')
if (xx-p3>0) then s3 = s3..'" colspan="'..(xx+1-p3) end
:attrIf(xx-posx[3]>0,'colspan',xx+1-posx[3])
p3 = xx + 1
)
s3 = s3 .. '">&nbsp;</td>'
posx[3] = xx + 1
end
end
end
 
bTabella:node(riga1):node(riga2):node(riga3)
s1 = s1..'</tr>'
end
s2 = s2..'</tr>'
if (n<xy[2]) then s3 = s3..'</tr>' end
 
bDiv:node(bTabella)
ss = ss..s1..s2..s3
if (allinea == 'destra' and not (dida=='')) then
bDiv:node(mw.html.create('p')
:css({['font-size'] = '87%', ['font-style'] = 'normal', ['border-top'] = '1px solid #c8ccd1', ['margin'] = '8px 2px 3px'})
:wikitext(dida)
)
end
return tostring(bDiv)
end
 
local function calcolaY(pid, t)
ss = ss..'</table>'
if (allineapers[pid].y ==> 'destra't) then sst = sspers[pid]..'</div>' else ss = ss..'</center>'y end
for _, v in pairs(pers[pid].figli) do
t = calcolaY(v,t)
pers[pid].sp = pers[pid].sp + 1 + pers[v].sp
end
return t
end
 
local function mostraY(pid)
return ss
local bTabella = mw.html.create('table')
:attr({['cellpadding']='0',['cellspacing']='0',['border']='0'})
:css({['border-collapse']='separate',['text-align']='left',['margin']='10px 0 10px 16px'})
 
local function mostraY2(pid, a)
if (pers[pid].padre > -1) then
local riga1 = mw.html.create('tr')
local riga2 = mw.html.create('tr')
local spd = pers[pers[pid].padre].sp
if (pers[pid].id == 1 and pers[pers[pid].padre].padre > -1) then
riga1:node(mw.html.create('td')
:attr('rowspan',2*spd))
riga1:node(mw.html.create('td')
:attr('rowspan',2*spd)
:cssIf(pers[pers[pid].padre].id < #pers[pers[pers[pid].padre].padre].figli,'border-left','1px solid #666')
)
end
riga1
:node(mw.html.create('td')
:css('width','6px'))
:node(mw.html.create('td')
:css({['border-left']='1px solid #666',['border-bottom']='1px solid #666',['width']='10px',['line-height']='3px',['height']='12px'}))
:node(mw.html.create('td')
:attr({['colspan']=2*a-1, ['rowspan']=2})
:css('padding', '0px 3px 2px 1px')
:wikitextIf(pers[pid].nota=='', pers[pid].testo, pers[pid].testo..' - '..pers[pid].nota))
riga2
:node(mw.html.create('td'))
:node(mw.html.create('td')
:css({['line-height']='8px',['line-height']='3px',['height']='12px'})
:cssIf(pers[pid].id < #pers[pers[pid].padre].figli,'border-left','1px solid #666'))
bTabella:node(riga1):node(riga2)
else
bTabella:node(
mw.html.create('tr')
:node(mw.html.create('td')
:attr('colspan',2*a-1)
:css('padding','0px 0px 2px 2px')
:wikitextIf(pers[pid].nota=='',pers[pid].testo,pers[pid].testo..' - '..pers[pid].nota)
)
)
end
if (pers[pid].sp > 0) then
for _, v in pairs(pers[pid].figli) do
mostraY2(v,a-1)
end
end
end
 
mostraY2(pid,calcolaY(pid,0))
function p.main(frame)
return tostring(bTabella)
end
 
function p._discendenza(args)
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.discendenza(frame)
local args = getArgs(frame, {
valueFunc = function (key, value)
Line 275 ⟶ 418:
end
})
return p._discendenza(args)
local lato = args['allinea'] or 'centro'
local larg = args['larghezza'] or '300'
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('più elementi (id: %d, %d) indicati come capostipite',capo,i)
end
else
if (v.padre == i) then
err = string.format('elemento con id %d indicato come figlio di se stesso', i)
elseif (pers[v.padre]) then
table.insert(pers[v.padre].figli,i)
else
err = string.format('id genitore %d non valido in elemento con id %d',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
calcolaX1(capo)
calcolaX2(capo)
calcolaX3(capo, 0)
else
err = 'alcuni elementi non sono collegati al capostipite'
end
end
end
end
if (err == '-') then
return mostra(capo, lato, larg)
else
return string.format('<p style="color:#b80000"><b>Template:Discendenza - Errore:</b> %s</p>[[Categoria:Errori di compilazione del template Discendenza]]',err)
end
end