We have begun migration of most wikis to our new host, Amazon Web Services. All but the top 100 wikis are in read-only mode until the migration is complete. Estimated completion: TBD -- Update: Fri 10/20 11pm UTC - We have encountered some unexpected issues. Our staff is working around the clock to complete this process as quickly as possible. Thank you for your patience.

Module:Image

De Binding of Isaac: Rebirth Wiki
Révision datée du 12 octobre 2017 à 04:46 par Derugon (discussion | contributions) (Erreur : concaténation imparfaire)
Aller à : navigation, rechercher
Missing No. Icon.png
Ce module est lié au projet Modèles.
Ne l'utilisez pas sur des pages du wiki non-liées à ce projet tant que celui-ci n'est pas terminé.



local p = {}
local rtext = require( 'Module:Text' )

-- Split parameters string,  e.g.: "ext: gif, size:12" --> { ext = "gif", size = "12" }
local function splitParams( initialString )
	local firstSplit = rtext.trimAll( mw.text.split( initialString, ',%s' ) )
	local param = {}
	local allParam = {}
	for i, j in pairs( firstSplit ) do
		param = rtext.trimAll( mw.text.split( j, ':' ) )
		allParam[param[1]] = param[2]
	end
	return allParam
end


-- Split images string,  e.g.: "(ext: gif, size:12 )(name:Box)" --> { 1 = "ext: gif, size:12", 2 = "name:Box" }
local function splitImages( initialString )
	local images = rtext.trimAll( rtext.trimAll( mw.text.split( initialString, '%(' ) ), '%)' )
	local params = {}
	local i = 2   -- 1st box of 'images' is empty
	repeat
		params[i-1] = 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, default )
	local args = rtext.trimAll( rtext.getArgs( f ) )
	local ret = ''
	local userParams = splitImages( args.params )
	if args.image1 then
		local params = {}
		local i = 1
		repeat
			params[i] = splitParams( args['image'..i] )
			i = i + 1
		until not args['image'..i]
		for k, l in pairs( userParams ) do
			ret = ret .. p.data( merge( l, params[k] ) )
		end
	--[[else
		ret = ret .. p.data( allParam )]]
	end
	return ret
end


function p.data( f )
	local args = rtext.trimAll( rtext.getArgs( f ) )
	
	local dataNameValue = { name=args.name, data=true }
	if args.type then
		 dataNameValue.type = args.type
	end
	local dataName = rtext.link( dataNameValue ).clean
	local fileName = args.fullname or ( args.pfx or '' ):lower() .. ' ' .. dataName .. ' ' .. ( args.sfx or '' ):lower()
	
	local fileExt = '.' .. ( args.ext or 'png' )
	
	local size = { width = args.width, height = args.height }
	if args.type then
		for i, j in pairs( mw.loadData( 'Module:Data' ).imageSize[args.type] ) do
			if dataName == i then
				if args.id then
					for k, l in pairs( j ) do
						if args.id:lower() == k then
							size.width = l[1]
							size.height = l[2]
							break
						end
					end
				else
					size.width = j[1][1]
					size.height = j[1][2]
				end
				break
			end
		end
	end
	local scale = args.scale or '1'
	local fileSize = math.ceil( size.width * scale ) .. 'x' .. math.ceil( size.height * scale )
	
	local fileAlign = args.align or 'center'
	
	--local fileLink = rtext.link({ name=args.name, data=true }).name .. '|' .. rtext.link({ name=args.name, data=true }).name
	
	return '[[Image:' .. fileName .. fileExt .. '|' .. fileSize .. '|' .. fileAlign .. '|link=' --[[.. fileLink]] .. ']]'
end

return p