Documentația acestui modul poate fi creată la Modul:Multiple image/doc

-- implements [[template:multiple image]]
local p = {}

local autoscaledimages
local nonautoscaledimages

local function isnotempty(s)
	return s and tostring(s):match( '^%s*(.-)%s*$' ) ~= ''
end

local function getdimensions(s, w, h)
	if tonumber(w or '') and tonumber(h or '') then
		nonautoscaledimages = true
		return tonumber(w), tonumber(h)
	else
		local file = s and mw.title.new('Fișier:' .. s).file 
			or {width = 0, height = 0}
		w = tonumber(file.width or 0)
		h = tonumber(file.height or 0)
		autoscaledimages = true
		return w, h
	end
end

local function renderImageCell(image, width, height, link, alt, thumbtime, caption, textalign, istyle) 
	local root = mw.html.create('')
	
	local altstr = '|alt=' .. (alt or '')
	local linkstr = link and ('|link=' .. link) or ''
	local widthstr = '|' .. tostring(width) .. 'px'
	local thumbtimestr = ''

	if isnotempty( thumbtime ) then
		thumbtimestr = '|thumbtime=' .. thumbtime
	end
	
	local imagediv = root:tag('div')
	imagediv:addClass('thumbimage')
	imagediv:cssText(istyle)
	if( height ) then
		imagediv:css('height', tostring(height) .. 'px')
		imagediv:css('overflow', 'hidden')
	end
	imagediv:wikitext('[[Fișier:' .. image .. widthstr .. linkstr .. altstr .. thumbtimestr .. ']]')
	if isnotempty(caption) then
		local captiondiv = root:tag('div')
		captiondiv:addClass('thumbcaption')
		if isnotempty(textalign) then
			captiondiv:addClass('text-align-' .. textalign)
		end
		captiondiv:wikitext(caption)
	end
	return tostring(root)
end

local function getWidth(w1, w2)
	local w
	if isnotempty(w1) then
		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
	local pr
	if type(pstr) ~= number then
		pr = mw.text.split(tostring(pstr or ''), '[^%d][^%d]*')
	-- if split failed, assume a single row
		if (#pr < 1) then
			pr = {tostring(ic)}
		end
	else
		pr = {pstr}
	end
	-- convert the array of strings to an array of numbers,
	-- adding any implied/missing numbers at the end of the array
	local r = 1
	local thisrow = tonumber(pr[1] or ic) or ic
	local prownum = {}
	while( ic > 0 ) do
		prownum[r] = thisrow
		ic = ic - thisrow
		r = r + 1
		-- use the previous if the next is missing and 
		-- make sure we don't overstep the number of images
		thisrow = math.min(tonumber(pr[r] or thisrow) or ic, ic)
	end
	return prownum
end

local function renderMultipleImages(frame)
	local pargs = frame:getParent().args
	local args = frame.args
	local geometry = {}
	
	geometry.width = pargs['width'] or ''
	geometry.dir = pargs['direction'] or ''
	geometry.align = pargs['align'] or args['align']
	geometry.capalign = pargs['caption_align'] or args['caption_align'] or ''
	geometry.totalwidth = pargs['total_width'] or args['total_width'] or ''

	local text = {}
	text.header = args['header'] or args['title'] or pargs['header'] or pargs['title'] or ''
	text.footer = args['footer'] or pargs['footer'] or ''
	
	local styling = {}
	styling.imgstyle = pargs['image_style'] or args['image_style']
	styling.imagegap = tonumber(pargs['image_gap'] or '1') or 1

	styling.bgcolor = pargs['background color']
	styling.marginTop = pargs['margin_top'] or args['margin_top']
	styling.marginBottom = pargs['margin_bottom'] or args['margin_bottom']
	styling.headerAlign = pargs['header_align']
	styling.headerBackground = pargs['header_background']
	styling.footerAlign = pargs['footer_align'] or args['footer_align']
	styling.footerBackground = pargs['footer_background']
	styling.border = pargs['border'] or args['border'] or ''
	geometry.align = geometry.align or (styling.border == 'infobox' and 'center' or '')

	-- find all the nonempty images
	local imagenumbers = {}
	local imagecount = 0
	for k, v in pairs( pargs ) do
		local i = tonumber(tostring(k):match( '^%s*image([%d]+)%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			table.insert( imagenumbers, i)
			imagecount = imagecount + 1
		end
	end

	-- sort the imagenumbers
	table.sort(imagenumbers)
	
	local images = {}
	for _,eachImageNumber in ipairs(imagenumbers) do
		local thisImg = {}
		thisImg.image = args['image' .. eachImageNumber] or pargs['image' .. eachImageNumber]
		thisImg.width = args['width' .. eachImageNumber] or pargs['width' .. eachImageNumber]
		thisImg.height = args['height' .. eachImageNumber] or pargs['height' .. eachImageNumber]
		thisImg.link = args['link' .. eachImageNumber] or pargs['link' .. eachImageNumber]
		thisImg.alt = args['alt' .. eachImageNumber] or pargs['alt' .. eachImageNumber]
		thisImg.thumbtime = args['thumbtime' .. eachImageNumber] or pargs['thumbtime' .. eachImageNumber]
		thisImg.caption = args['caption' .. eachImageNumber] or pargs['caption' .. eachImageNumber]
		table.insert(images, thisImg)
	end
	
	geometry.perrow = pargs['perrow']

	return p.renderImages(images, text, geometry, styling)
end

function p.renderImages(images, text, geometry, styling)
	local actualPerrow = nil

	actualPerrow = getPerRow(geometry and geometry.dir == 'vertical' and '1' or geometry and geometry.perrow, #images)

	-- compute the number of rows
	local rowcount = #actualPerrow
	local totalwidth = geometry and geometry.totalwidth

	-- store the image widths and compute row widths and maximum row width
	local heights = {}
	local widths = {}
	local widthmax = 0
	local widthsum = {}
	local k = 0
	for r=1,rowcount do
		widthsum[r] = 0
		for c=1,actualPerrow[r] do
			k = k + 1
			if( k <= #images ) then
				if( isnotempty(totalwidth) ) then
					widths[k], heights[k] = getdimensions(images[k].image, images[k].width, images[k].height)
				else
					widths[k] = getWidth(geometry.width, images[k].width)
				end
				widthsum[r] = widthsum[r] + widths[k]
			end
		end
		widthmax = math.max(widthmax, widthsum[r])
	end

	local imagegap = styling and styling.imagegap or 1
	-- make sure the gap is non-negative
	if imagegap < 0 then imagegap = 0 end
	
	-- if total_width has been specified, rescale the image widths
	if( isnotempty(totalwidth) ) then
		totalwidth = tonumber(totalwidth)
		widthmax = 0
		local k = 0
		for r=1,rowcount do
			local koffset = k
			local tw = totalwidth - (3 + imagegap) * (actualPerrow[r] - 1) - 12
			local ar = {}
			local arsum = 0
			for j=1,actualPerrow[r] do
				k = k + 1
				if( k <= #images ) then
					local h = heights[k] or 0
					if (h > 0) then
						ar[j] = widths[k]/h
						heights[k] = h
					else
						ar[j] = widths[k]/100
					end
					arsum = arsum + ar[j]
				end
			end
			local ht = tw/arsum
			local ws = 0
			k = koffset
			for j=1,actualPerrow[r] do
				k = k + 1
				if( k <= #images ) then
					widths[k] = math.floor(ar[j]*ht + 0.5)
					ws = ws + widths[k]
					if heights[k] then
						heights[k] = math.floor(ht)
					end
				end
			end
			widthsum[r] = ws
			widthmax = math.max(widthmax, widthsum[r])
		end
	end	

	-- start building the array of images, if there are images
	if( #images > 0 ) then
		-- compute width of outer div
		local bodywidth = 0
		for r=1,rowcount do
			if( widthmax == widthsum[r] ) then
				bodywidth = widthmax + (3 + imagegap) * (actualPerrow[r] - 1) + 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);

		local bg = styling and styling.bgcolor or ''
		local thumbclass = {
			["left"] = 'tleft',
			["none"] = 'tnone',
			["center"] = 'tnone',
			["centre"] = 'tnone',
			["right"] = 'tright'
		}
		-- create the array of images
		local root = mw.html.create('div')
		root:addClass('thumb')
		root:addClass('tmulti')

		root:addClass(thumbclass[geometry and geometry.align or 'right'] or 'tright')
		
		if( geometry and (geometry.align == 'center' or geometry.align == 'centre' )) then
			root:addClass('center')
		end
		if( bg ~= '' ) then
			root:css('background-color', bg)
		end

		local div = root:tag('div')
		div:addClass('thumbinner')
		div:css('width', tostring(bodywidth) .. 'px')
			:css('max-width', tostring(bodywidth) .. 'px')
		if( bg ~= '' ) then
			div:css('background-color', bg)
		end
		if(styling and (styling.border == 'infobox' or styling.border == 'none')) then
			div:css('border', 'none')
		end
		
		-- add the header
		if( isnotempty(text and text.header) ) then
			div:tag('div')
				:addClass('trow')
				:tag('div')
					:addClass('theader')
					:css('text-align', styling.headerAlign or 'center')
					:css('background-color', styling.headerBackground or 'transparent')
				:wikitext(text.header)
		end
		-- loop through the images
		local k = 0
		for r=1,rowcount do
			local rowdiv = div:tag('div'):addClass('trow');
			for j=1,actualPerrow[r] do
				k = k + 1
				if( k <= #images ) then
					local imagediv = rowdiv:tag('div')
					imagediv:addClass('tsingle')
					if bg ~= '' then
						imagediv:css('background-color', bg);
					end
					if ((imagegap > 1) and (j < actualPerrow[r])) then
						imagediv:css('margin-right', tostring(imagegap) .. 'px')
					end
					local img = images[k].image
					local w = widths[k]
					imagediv:css('width', tostring(2 + w) .. 'px')
						:css('max-width', tostring(2 + w) .. 'px')
					imagediv:wikitext(renderImageCell(img, w, heights[k], 
						images[k].link, images[k].alt,
						images[k].thumbtime, images[k].caption, geometry and geometry.capalign, styling and styling.imgstyle))
				end
			end
		end
		-- add the footer
		if( isnotempty(text and text.footer) ) then
			local falign = mw.ustring.lower(styling and styling.footerAlign or 'left')
			falign = (falign == 'centre') and 'center' or falign
			
			div:tag('div')
				:addClass('trow')
				:tag('div')
					:addClass('thumbcaption' .. (falign == 'center' and '-center' or ''))
					:css('text-align', (falign ~= 'left') and falign or nil)
					:css('background-color', styling and styling.footerBackground or 'transparent')
					:css('color', 'var(--color-base, #000)')
					:wikitext(text.footer)
		end
		return mw.getCurrentFrame():extensionTag {name = 'templatestyles', args = {src = 'Multiple image/styles.css', wrapper = ".tmulti"}} .. tostring(root)
	end
	return ''
end

function p.render( frame )
	autoscaledimages = false
	nonautoscaledimages = false
	
	return renderMultipleImages( frame )
		.. (autoscaledimages and '[[Categorie:Pagini care utilizează imagini multiple scalate automat]]' or '')
		.. (nonautoscaledimages and '[[Categorie:Pagini care utilizează imagini multiple scalate manual]]' or '')
end
 
return p