Jump to content

Module:Image array

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 20:39, 6 January 2014 (debug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- implements [[template:image array]]

local p = {}

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

local function arraycell( img, c, a, l, tc, t, w, h, m, bw, fs)
	local alt = ''
	local link = ''
	local text = t
	local endstr = ''
	local border = ''
	local font = ''

	if( isnotempty(a) ) then alt = 'alt=' .. a end
	if( isnotempty(l) ) then link = 'link=' .. l end
	if( isnotempty(tc) and not isnotempty(t)) then text = c	end
	if( m == 0 ) then endstr = '</tr><tr>' end
	if( bw > 0 ) then border = 'border:' .. tostring(bw) .. 'px #aaa solid' end
	if( isnotempty(fs) ) then font = 'font-size:' .. fs end
	
	return mw.ustring.format( '<td style="width:%dpx;%s">'
		..'<div style="vertical-align:middle; width:%dpx; height:%dpx; margin-left:auto; margin-right:auto;">'
		..'[[File:%s|%dx%dpx|%s|%s|%s]]</div>'
		..'<div style="vertical-align:middle; padding:1px;%s">%s</div></td>%s', 
		w, border, w, h, img, w, h, alt, link, text, font, c)
end

local function imagearray( frame )
	local args = frame:getParent().args
	local width = tonumber(args['width'] or '60')
	local height = tonumber(args['height'] or '70')
	local perrow = tonumber(args['perrow'] or '4')
	local text = args['text'] or ''
	local bw = tonumber(args['border-width'] or '0')
	local fs = args['font-size'] or ''
	
	local t = {}
	local tdebug = ''

	-- find all the nonempty image numbers
	local imagenums = {}
	for k, v in ipairs( args ) do
		tdebug = tdebug .. ' ' .. k
		local i = tonumber(k:match( '^%s*image([%d]+)%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			table.insert( imagenums, i )
		end
	end
	-- sort the image numbers
	table.sort(imagenums)
	
	-- start array
	table.insert(t, '<table style="border-collapse: collapse; text-align: center; font-size: smaller;'
		..'line-height:1.25em; margin:auto; width:' .. tostring(width*perrow) .. 'px;">\n<tr>\n')
	-- loop over the images
    for k, i in ipairs( imagenums ) do
    	i = tostring(i)
       	table.insert(t, arraycell( args['image' .. i], args['caption' .. i] or '', args['alt' .. i] or '', args['link' .. i] or '',
    		args['text'] or '', args['text' .. i] or '', width, height, math.fmod(k, perrow), bw, fs) )
	end
	-- end array	
    table.insert(t, '</tr>\n</table>')
--    return table.concat( t, '\n' )
	return tdebug
end
function p.array( frame )
    return imagearray( frame )
end
 
return p