Modulo:Immagine multipla: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
r
Evita background-color: transparent, non funziona bene con la night mode. Impostando il valore a nil la proprietà viene omessa del tutto.
 
(15 versioni intermedie di 3 utenti non mostrate)
Riga 1:
--=====================================================
-- implements [[template:multiple image]]
-- Implementazione del [[template:Immagine multipla]]
-- Importato e modificato da
-- https://en.wikipedia.org/w/index.php?title=Module:Multiple_image&oldid=706647916
--=====================================================
 
local p = {}
 
--=====================================================
local function isnotempty(s)
-- Generazione del codice una singola cella contenente
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
-- un'immagine
end
--=====================================================
 
local function renderImageCell(image, width, height, link, alt, caption, textalign, istyle)
local root = mw.html.create('')
Line 16 ⟶ 21:
imagediv:addClass('thumbimage')
imagediv:cssText(istyle)
if( height ) then
imagediv:css('height', tostring(height) .. 'px')
imagediv:css('overflow', 'hidden')
end
imagediv:wikitext('[[file:' .. image .. widthstr .. linkstr .. altstr .. ']]')
if isnotempty(caption) then
local captiondiv = root:tag('div')
captiondiv:addClass('thumbcaption')
captiondiv:css('clear', 'left')
if isnotempty(textalign) then
captiondiv:css('text-align', textalign)
end
Line 33 ⟶ 38:
end
 
--=====================================================
local function getWidth(w1, w2)
-- Ritorna una tabella che contiene il numero di immagini
local w
-- per ogni riga.
if isnotempty(w1) then
-- pstr deve essere una stringa di numeri separati da qualunque
w = tonumber(w1)
--
elseif isnotempty(w2) then
--=====================================================
w = tonumber(w2)
end
return w or 200
end
 
local function getPerRow(pstr, ic)
-- split string into array using any non-digit as a dilimiter
Line 66 ⟶ 67:
end
 
--=====================================================
local function getImageNumbers(pargs)
-- Ritorna le dimensioni dell'immagine (larghezza x altezza)
-- Se queste non sono passate dai parametri tenta di recuperarle
-- dalle informazioni del file immagine, altrimenti ricade su
-- una larghezza pari a 200 e un'altezza pari alla larghezza
--=====================================================
 
local function get_dimensions(w, h, name)
local wr = w and tonumber(w)
local hr = h and tonumber(h)
if not(wr and hr) then
local page = mw.title.new(name, 'Media')
if not wr then
wr = page.file and page.file.width
end
if not hr then
hr = page.file and page.file.height
end
end
wr = wr or 200
hr = hr or wr
return wr, hr
end
 
-- Return a sorted array of the index of the parameter immagine1 .. immagine(n)
-- passed to the template. Index could be not consecutive but the gap between
-- different index must be five or less
local function getImageNumbers(args)
local imagenumbers = {}
local istart = 1
Line 74 ⟶ 102:
local found = false
for i=istart, iend do
if pargsargs['immagine' .. tostring(i)] then
imagenumbers[#imagenumbers+1] = i
found = true
Line 80 ⟶ 108:
end
if found then
istart = istartiend + istep1
iend = iend + istep
else
Line 90 ⟶ 118:
end
 
-- La funzione effettiva di creazione della galleria
local function renderMultipleImages(args, pargs)
function p._render(args)
 
local width = pargs(args['larghezza'] and tonumber(args['larghezza'])) or 200
local dir = pargsargs['direzione']
local align = pargs['allinea'] or args['allinea']
local capalign = pargsargs['allinea_didascalia']allinea or args['allinea_didascaliadidascalia'] --NEW
local totalwidth = pargs(args['larghezza_totalelarghezza totale'] orand tonumber(args['larghezza_totalelarghezza totale'])) or --NEW0
local imgstyle = pargsargs['stile_immagine']stile or args['stile_immagineimmagine']
local header = pargs['testata'] or pargsargs['testatatitolo']
local footer = pargsargs['pie_paginesotto']
local perrow = nil
local thumbclass = {
Line 114 ⟶ 143:
 
-- find all the nonempty images
local imagenumbers = getImageNumbers(pargsargs)
local imagecount = #imagenumbers
if imagecount == 0 then
Line 121 ⟶ 150:
-- create an array with the number of images per row
if dir == 'verticale' or dir == 'vertical' then
perrow = getPerRow('1', imagecount)
else
perrow = getPerRow(pargsargs['perrowper riga'], imagecount)
end
Line 134 ⟶ 163:
local widthmax = 0
local widthsum = {}
local heights = {}
local k = 0
for r=1,rowcount do
Line 139 ⟶ 169:
for c=1,perrow[r] do
k = k + 1
if( k <= imagecount ) then
local iw = args['larghezza' .. imagenumbers[k]]
if totalwidth > 0 then
widths[k] = getWidth(width, pargs['larghezza' .. i])
local h = args['altezza' .. imagenumbers[k]]
widths[k], heights[k] = get_dimensions(w, h, args['immagine' .. imagenumbers[k]])
else
widths[k] = (w and tonumber(w)) or width
end
widthsum[r] = widthsum[r] + widths[k]
end
Line 149 ⟶ 184:
 
-- if total_width has been specified, rescale the image widths
if totalwidth > 0 then
local heights = {}
if( isnotempty(totalwidth) ) then
totalwidth = tonumber(totalwidth)
widthmax = 0
local k = 0
Line 161 ⟶ 194:
for j=1,perrow[r] do
k = k + 1
if( k<= imagecount ) then
localif i = imagenumbers(heights[k] > 0) then
ar[j] = widths[k]/heights[k]
local h = tonumber( pargs['altezza' .. i] or '' ) or 0
if (h > 0) then
ar[j] = widths[k]/h
heights[k] = h
else
ar[j] = widths[k]/100
Line 178 ⟶ 208:
for j=1,perrow[r] do
k = k + 1
if( k<= imagecount ) then
local i = imagenumbers[k]
widths[k] = math.floor(ar[j]*ht + 0.5)
Line 193 ⟶ 223:
 
-- start building the array of images, if there are images
if( imagecount > 0 ) then
-- compute width of outer div
local bodywidth = 0
for r=1,rowcount do
if(local widthmaxbw == widthsum[r] + 4 * (perrow[r] - 1) then+ 2
bodywidth = widthmax + 4 * math.max(perrow[r]bodywidth, - 1bw) + 12
end
end
-- The body has a min-width of 100, which needs to be taken into account on specific widths
bodywidth = math.max( 100, bodywidth - 8);
 
-- crea il div esterno per la galleria di immagini
local bg = pargs['sfondo'] or ''
-- create the array of images
local root = mw.html.create('div')
root:addClass('thumb')
root:addClass('tmulti')
root:addClass(thumbclass[align] or 'tright')
-- crea il div interno
if( align == 'center' or align == 'centre' or align == 'centro') then
root:addClass('center')
end
if( pargs['margin_top'] or args['margin_top']) then
root:css('margin-top', pargs['margin_top'] or args['margin_top'])
end
if( pargs['margin_bottom'] or args['margin_bottom']) then
root:css('margin-bottom', pargs['margin_bottom'] or args['margin_bottom'])
end
if( bg ~= '' ) then
root:css('background-color', bg)
end
 
local div = root:tag('div')
div:addClass('thumbinner')
div:css('width', tostring(bodywidth+2) .. 'px')
:css('maxflex-widthdirection', tostring(bodywidth) .. 'pxrow')
if( bgalign ~== 'center' )or align == 'centre' or align == 'centro' then
div:css('background-colormargin', bg'0 auto')
end
-- addaggiunge theil headertitolo
if( isnotempty(header) ) then
div:tag('div')
:css('clear', 'both')
:css('fonttext-weightalign', args['boldallinea titolo'] or 'center')
:css('textbackground-aligncolor', pargsargs['allineamentosfondo titolo'] or 'center')
:css('background-color', pargs['sfondo titolo'] or 'transparent')
:wikitext(header)
end
Line 245 ⟶ 258:
for j=1,perrow[r] do
k = k + 1
if( k <= imagecount ) then
local imagediv = div:tag('div')
imagediv:addClass('tsingle')
if dir ~= 'vertical' then
imagediv:css('float', 'left')
end
if bg ~= '' then
imagediv:css('background-color', bg);
end
imagediv:css('margin', '1px')
local i = imagenumbers[k]
local img = pargsargs['immagine' .. i]
local w = widths[k]
imagediv:css('width', tostring(2 + w) .. 'px')
--:css('max-width', tostring(2 + w) .. 'px')
:css('padding-left', '0px')
:css('padding-right', '0px')
imagediv:wikitext(renderImageCell(img, w, heights[k],
pargsargs['collegamento' .. i], pargsargs['alt' .. i],
pargsargs['didascalia' .. i], capalign, imgstyle))
end
end
-- only float content gives a parent height:0, so add a clearing div
div:tag('div'):css('clear', 'left')
if dir ~= 'vertical' and dir ~= 'verticale' then
div:tag('div')
:css('clear', 'left')
end
end
-- add the footer
if( isnotempty(footer) ) then
div:tag('div')
:addClass('thumbcaption')
:css('clear', 'left')
:css('text-align', pargs['allineamento didascalia'] or args['allineamentoallinea didascaliasotto'] or 'left')
:css('background-color', pargsargs['sfondo didascaliasotto'] or 'transparent')
:wikitext(footer)
end
return tostring(root)
Line 285 ⟶ 294:
end
 
--interfaccia verso il template immagine multipla
function p.render( frame )
local getArgs = require('Module:Arguments').getArgs
--local pargs = getArgs(frame, {parentOnly=true})
local args = getArgs(frame, {frameOnly=true})
return renderMultipleImagesp._render( args, pargs )
end
--interfaccia verso template più specifici ({{Doppia immagine}}, {{tripla immagine}}...)
function p.render_frame( frame )
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame, {frameOnly=true})
return p._render( args )
end
 
return p