Module:Image : Différence entre versions

De Binding of Isaac: Rebirth Wiki
Aller à : navigation, rechercher
(Erreur : calculs à effectuer sur scale et non args.scale)
(Why error is always on line 72 ?????????)
 
(67 révisions intermédiaires par le même utilisateur non affichées)
Ligne 2 : Ligne 2 :
 
local rtext = require( 'Module:Text' )
 
local rtext = require( 'Module:Text' )
  
-- Split parameters string, e.g.: "ext: gif, size:12" --> { ext = "gif", size = "12" }
+
--  "ext: gif, size:12" --> { ext = "gif", size = "12" }
 
local function splitParams( initialString )
 
local function splitParams( initialString )
 
local firstSplit = rtext.trimAll( mw.text.split( initialString, ',%s' ) )
 
local firstSplit = rtext.trimAll( mw.text.split( initialString, ',%s' ) )
Ligne 15 : Ligne 15 :
  
  
-- Split images string, e.g.: "(ext: gif, size:12 )(name:Box)" --> { 1 = "ext: gif, size:12", 2 = "name:Box" }
+
--  "( ext: gif, size:12 )(name : Box)" --> { 1 = "ext: gif, size:12", 2 = "name : Box" }
 
local function splitImages( initialString )
 
local function splitImages( initialString )
 
local images = rtext.trimAll( rtext.trimAll( mw.text.split( initialString, '%(' ) ), '%)' )
 
local images = rtext.trimAll( rtext.trimAll( mw.text.split( initialString, '%(' ) ), '%)' )
Ligne 40 : Ligne 40 :
  
  
function p.main( f, default )
+
function p.main( f )
 
local args = rtext.trimAll( rtext.getArgs( f ) )
 
local args = rtext.trimAll( rtext.getArgs( f ) )
 +
if not args.params then error( 'Missing "params" parameter', 0 ) end
 +
 
local ret = ''
 
local ret = ''
 
local userParams = splitImages( args.params )
 
local userParams = splitImages( args.params )
if args.image1 then
+
for i, j in pairs( userParams ) do
local params = {}
+
ret = ret .. p.data( args['image'..i] and merge( j, splitParams( args['image'..i] ) ) or j )
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
 
end
 +
 
return ret
 
return ret
 
end
 
end
Ligne 63 : Ligne 56 :
 
function p.data( f )
 
function p.data( f )
 
local args = rtext.trimAll( rtext.getArgs( f ) )
 
local args = rtext.trimAll( rtext.getArgs( f ) )
 +
args.type = args.type:gsub( '%s', '_' )
 +
if not ( args.type or args.width or args.height ) then error( 'Missing file size values. Add a value to "type", "width", or "height"', 0 ) end
 
 
local dataNameValue = { name=args.name, data=true }
+
local wikiName = rtext.link{ name=args.name, type=args.type, data=true }.wiki
if args.type then
+
local fileName = ''
dataNameValue.type = args.type
+
if args.fullname then
 +
fileName = args.fullname
 +
else
 +
fileName = fileName .. rtext.link{ name=args.name, type=args.type, data=true }.file
 +
fileName = ( args.pfx and ' ' .. ( args.pfx ):lower() or '' ) .. fileName .. ( args.sfx and ' ' .. ( args.sfx ):lower() or '' )
 
end
 
end
local dataName = rtext.link( dataNameValue ).clean
+
fileName = fileName .. '.' .. ( args.ext or 'png' )
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 }
+
local size = {}
if args.type then
+
if args.type and not ( args.width and args.height ) then
for i, j in pairs( mw.loadData( 'Module:Data' ).imageSize[args.type] ) do
+
for i, j in pairs( require( 'Module:Data' ).imageSize[args.type] ) do
 
if dataName == i then
 
if dataName == i then
 
if args.id then
 
if args.id then
Ligne 93 : Ligne 89 :
 
end
 
end
 
end
 
end
 +
size.width = args.width or size.width or mw.getCurrentFrame():callParserFunction( '#imgw', fileName )
 +
size.height = args.height or size.height or mw.getCurrentFrame():callParserFunction( '#imgh', fileName )
 
local scale = args.scale or '1'
 
local scale = args.scale or '1'
local fileSize = math.ceil( size.width * scale ) .. 'x' .. math.ceil( size.height * scale )
+
local fileSize = math.ceil( size.width * scale ) .. 'x' .. math.ceil( size.height * scale ) .. 'px'
 
 
--[[local fileAlign = args.align or 'center'
+
local fileAlign = args.align or 'center'
 
 
local fileLink = rtext.link({ name=args.name, data=true }).name .. '|' .. rtext.link({ name=args.name, data=true }).name]]
+
local fileLink = args.link and rtext.link{ name=args.name, data=true }.wiki .. '|' .. rtext.link{ name=args.name, data=true }.disp or ''
 
 
return size.width .. 'x' .. size.height--'[[Image:' .. fileName .. fileExt '|' .. fileSize .. '|' .. fileAlign .. '|link=' .. fileLink .. ']]'
+
return '[[Image:' .. fileName .. '|' .. fileSize .. '|' .. fileAlign .. '|link=' .. fileLink .. ']]'
 
end
 
end
  
 
return p
 
return p

Version actuelle datée du 31 octobre 2017 à 10:43

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' )

--  "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


--  "( 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 )
	local args = rtext.trimAll( rtext.getArgs( f ) )
	if not args.params then error( 'Missing "params" parameter', 0 ) end
	
	local ret = ''
	local userParams = splitImages( args.params )
	for i, j in pairs( userParams ) do
		ret = ret .. p.data( args['image'..i] and merge( j, splitParams( args['image'..i] ) ) or j )
	end
	
	return ret
end


function p.data( f )
	local args = rtext.trimAll( rtext.getArgs( f ) )
	args.type = args.type:gsub( '%s', '_' )
	if not ( args.type or args.width or args.height ) then error( 'Missing file size values. Add a value to "type", "width", or "height"', 0 ) end
	
	local wikiName = rtext.link{ name=args.name, type=args.type, data=true }.wiki
	local fileName = ''
	if args.fullname then
		fileName = args.fullname
	else
		fileName = fileName .. rtext.link{ name=args.name, type=args.type, data=true }.file
		fileName = ( args.pfx and ' ' .. ( args.pfx ):lower() or '' ) .. fileName .. ( args.sfx and ' ' .. ( args.sfx ):lower() or '' )
	end
	fileName = fileName .. '.' .. ( args.ext or 'png' )
	
	local size = {}
	if args.type and not ( args.width and args.height ) then
		for i, j in pairs( require( '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
	size.width = args.width or size.width or mw.getCurrentFrame():callParserFunction( '#imgw', fileName )
	size.height = args.height or size.height or mw.getCurrentFrame():callParserFunction( '#imgh', fileName )
	local scale = args.scale or '1'
	local fileSize = math.ceil( size.width * scale ) .. 'x' .. math.ceil( size.height * scale ) .. 'px'
	
	local fileAlign = args.align or 'center'
	
	local fileLink = args.link and rtext.link{ name=args.name, data=true }.wiki .. '|' .. rtext.link{ name=args.name, data=true }.disp or ''
	
	return '[[Image:' .. fileName .. '|' .. fileSize .. '|' .. fileAlign .. '|link=' .. fileLink .. ']]'
end

return p