Jump to content

Module:Multiple image

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 15:13, 23 April 2014 (start module for multiple images). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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

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

local function renderImageCell(image, width, link, alt, caption) 
	local root = mw.html.create('')
	
	alt = '|alt=' .. (alt or '')
	link = link and ('|link=' .. link) or ''
	
	local div1 = root:tag('div')
	div1:addClass('thumbimage')
	div1:wikitext('[[file:' .. image .. '|' .. tostring(width) .. 'px' .. link .. alt .. ']]')
	if isnotempty(caption) then
		local div2 = root:tag('div')
		div2:addClass('thumbcaption')
		div2:css('clear', 'left')
		div2:wikitext(caption)
	end
	return tostring(root)
end

local function imagesortfun(a,b)
	return (a[1] < b[1])
end

local function renderMultipleImages(frame)
	local args = frame:getParent().args
	local defaultwidth = 200
	local width = args['width'] or ''
	local dir = args['direction'] or ''
	local thumbclass = {
		["left"] = 'tleft',
		["none"] = 'tnone',
		["center"] = 'tnone',
		["centre"] = 'tnone',
		["right"] = 'tright'
		}

	-- find all the nonempty images and corresponding widths
	local images = {}
	local imagecount = 0
	local widthmax = 0
	local widthsum = 0
	for k, v in pairs( args ) do
		local i = tonumber(tostring(k):match( '^%s*image([%d]+)%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			local w = defaultwidth
			if isnotempty( width ) then
				w = tonumber(width)
			elseif isnotempty( args['width' .. tostring(i)] ) then
				w = tonumber(args['width' .. tostring(i)])
			end
			widthmax = math.max(widthmax, w)
			widthsum = widthsum + w
			table.insert( images, {i, v, } )
			imagecount = imagecount + 1
		end
	end
	-- sort the images by image number
	table.sort(images, imagesortfun)
	if( imagecount > 0 ) then
		local bodywidth = 0
		local bg = args['background color'] or ''
		-- create the array of images
		local root = mw.html.create('div')
		root:addClass('thumb')
		root:addClass(thumbclass(args['align'] or '') or 'tright')
		if( dir == 'vertical') then
			bodywidth = widthmax + 12
		else
			bodywidth = widthsum + 4 * (math.max(numimages,2) - 2) + 17
		end
		root:css('width', tostring(bodywidth) .. 'px')
		if( align == 'center' or align == 'centre' ) then
			root:css('margin', '0 auto')
		end
		if( bg ~= '' ) then
			root:css('background-color', bg)
		end
		local div = root:tag('div')
		div:addClass('thumbinner')
		if( bg ~= '' ) then
			div:css('background-color', bg)
		end
		if( isnotempty(args['header']) ) then
			div:tag('div')
				:css('clear', 'both')
				:css('font-weight', 'bold')
				:css('text-align', args['header_align'] or 'center')
				:css('background-color', args['header_background'] or 'transparent')
				:wikitext(args['header'])
		end
		for k=1,imagecount do
			imagediv = div:tag('div')
			if dir == 'horizontal' then
				imagediv:css('float', 'left')
			end
			imagediv:css('margin', '1px')
			local imageinfo = images[k]
			local i = imageinfo[1]
			local img = imageinfo[2]
			local w = imageinfo[3]
			imagediv:css('width', tostring(2 + w) .. 'px')
			imagediv:wikitext(renderImageCell(img, w, args['link' .. i], args['alt' .. i], args['caption' .. i]))
		end

		if( isnotempty(args['footer']) ) then
			class="thumbcaption" style="clear: left; text-align: {{{footer_align|left}}}; background: {{{footer_background|transparent}}}"
			div:tag('div')
				:addClass('thumbcaption')
				:css('clear', 'left')
				:css('text-align', args['footer_align'] or 'left')
				:css('background-color', args['footer_background'] or 'transparent')
				:wikitext(args['footer'])
		end
	end
	
end