Module:Image

local p = {} local rtable = require( 'Module:Table' )

-- "ext: gif, size:12" --> { ext = "gif", size = "12" } function p.splitParams( initialString ) local firstSplit = rtable.trimAll( mw.text.split( initialString, ',%s' ) ) local param = {} local allParams = {} for i, j in pairs( firstSplit ) do		if j:find( ':' ) then param = mw.text.split( j, ':' ) allParams[mw.text.trim( param[1] )] = mw.text.trim( param[2] ) else allParams[1] = j		end end return allParams end

-- "( ext: gif, size:12 )(name : Box)" --> { 1 = "ext: gif, size:12", 2 = "name : Box" } function p.splitImages( initialString ) local images = rtable.trimAll( rtable.trimAll( mw.text.split( initialString, '%(' ) ), '%)' ) local params = {} local i = 2  -- 1st box of 'images' is empty repeat params[i-1] = p.splitParams( images[i] ) i = i + 1 until not images[i] return params end

local function merge( userParam, default ) local param = {} for i, j in pairs( default ) do		param[i] = j	end for k, l in pairs( userParam ) do		param[k] = l	end return param end

function p.main( f ) local args = rtable.trimAll( rtable.getArgs( f ) ) if not args.params then error( 'Missing "params" parameter', 0 ) end local ret = '' for i, j in pairs( p.splitImages( args.params ) ) do ret = ret .. p.data( args.default and merge( args['image'..i] and merge( j, p.splitParams( args['image'..i] ) ) or j, p.splitParams( args.default ) ) or args['image'..i] and merge( j, p.splitParams( args['image'..i] ) ) or j ) end return ret end

function p.data( f ) local args = rtable.trimAll( rtable.getArgs( f ) ) local fileName = 'Image:' .. args.name .. '.' .. ( args.ext or 'png' ) return  .. math.ceil( ( args.width or mw.getCurrentFrame:callParserFunction( '#imgw', fileName ) ) * ( args.scale or 1 ) ) .. 'x' .. math.ceil( ( args.height or mw.getCurrentFrame:callParserFunction( '#imgh', fileName ) ) * ( args.scale or 1 ) ) .. 'px|' .. ( args.align or  ) .. '|link=' .. ( args.link or  ) .. '|' .. ( args.desc or  ) .. '' end

return p