Module:Infobox

From The Blockheads Wiki

Implements {{Infobox}}

Usage


local p = {}
function p.infobox( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local titleObject = mw.title.getCurrentTitle()
	local title = args.title or titleObject.baseText
	local subtitle = args.subtitle or ''
	local template = f:getParent():getTitle():lower():gsub( 'template:', '' ):gsub( 'infobox ', '' )
	local headerArea = ''
	
	local json = {
		images = {},
		invimages = {},
		rows = {}
	}
	
	local titleArea = args.titlearea
	if not titleArea then
		json.title = title
		if subtitle ~= '' then
			json.subtitle = subtitle
			subtitle = '<br><small class="infobox-subtitle">' .. subtitle .. '</small>'
		end
		titleArea = title .. subtitle
	end
	
	local imageArea = args.imagearea
	if not imageArea and imageArea ~= 'none' then
		local images = {}
		local invImages = {}
		local defaultImageSize = args.defaultimagesize or '150px'
		local defaultImageClass = args.defaultimageclass
		args.image1 = args.image1 or args.image or 'title'
		args.image1size = args.image1size or args.imagesize
		args.image1class = args.image1class or args.imageclass
		args.image1caption = args.image1caption or args.imagecaption or ''
		args.invimage1 = args.invimage1 or args.invimage or 'none'
		args.group1 = args.group1 or args.group
		args.group1size = args.group1size or args.groupsize
		args.group1class = args.group1class or args.groupclass
		args.group1caption = args.group1caption or args.groupcaption or ''
		
		local imgCount = {}
		local invImgCount = {}
		local groupCount = {}
		local groupImgList = {}
		for k, v in pairs( args ) do
			if type( k ) == 'string' then
				local image, num = k:match( '^(image)(%d+)$' )
				local invImage, invNum = k:match( '^(invimage)(%d+)$' )
				local group, groupNum = k:match( '^(group)(%d+)$' )
				local groupImg, groupImgNum = k:match( '^(%d+)-(%d+)$' )
				if v:lower() ~= 'none' then
					if image then
						table.insert( imgCount, tonumber( num ) )
					elseif invImage then
						table.insert( invImgCount, tonumber( invNum ) )
					elseif group then
						table.insert( groupCount, tonumber( groupNum ) )
						if not groupImgList['group' .. groupNum] then
							groupImgList['group' .. groupNum] = {}
						end
					elseif groupImg then
						if not groupImgList['group' .. groupImg] then
							groupImgList['group' .. groupImg] = {}
						end
						table.insert( groupImgList['group' .. groupImg], tonumber( groupImgNum ) )
					end
				end
			end
		end
		
		local animate
		if #groupCount > 0 then
			table.sort( groupCount )
			local tabber = {}
			for k, v in ipairs( groupCount ) do
				local group = args['group' .. v]
				local groupSize = args['group' .. v .. 'size'] or defaultImageSize
				local groupClass = args['group' .. v .. 'class'] or defaultImageClass
				local groupCaption = args['group' .. v .. 'caption'] or ''
				local groupImages = {}
				
				table.sort( groupImgList['group' .. v] )
				for _, w in ipairs( groupImgList['group' .. v] ) do
					local image = args[v .. '-' .. w]
					local size = args[v .. '-' .. w .. 'size'] or args['*-' .. w .. 'size'] or groupSize
					local class = args[v .. '-' .. w .. 'class'] or args['*-' .. w .. 'class'] or groupClass
					local caption = args[v .. '-' .. w .. 'caption'] or args['*-' .. w .. 'caption'] or ''
					
					if string.match( image, 'UNIQ%-%-gallery%-' ) then
						image = image
					elseif image:match( ';' ) then
						if not animate then
							animate = require( 'Module:Animate' ).animate
						end
						image = animate{ image, size, class = class }
					else
						json.images[#json.images + 1] = image
						local altText = image .. ': Infobox image for ' .. title .. ' the ' .. template .. ' in Minecraft'
						image = '[[File:' .. image .. '|' .. size .. '|class=' .. ( class or '' ) .. '|alt=' .. altText .. ']]'
					end
					
					if caption ~= '' then
						caption = '<div class="infobox-imagecaption">\n' .. caption .. '\n</div>'
					end
					
					table.insert( groupImages, '<div>' .. image .. caption .. '</div>' )
				end
					
				if groupCaption ~= '' then
					groupCaption = '<div class="infobox-imagecaption">\n' .. groupCaption .. '\n</div>'
				end
				
				table.insert( tabber, '|-|' .. group .. '=\n' .. table.concat( groupImages, '\n' ) .. groupCaption )
			end
			table.insert( images, '<div>' .. f:extensionTag( 'tabber', table.concat( tabber, '\n' ) ) .. '</div>' )
		end
		
		table.sort( imgCount )
		for k, v in ipairs( imgCount ) do
			local image = args['image' .. v]
			local size = args['image' .. v .. 'size'] or defaultImageSize
			local class = args['image' .. v .. 'class'] or defaultImageClass
			local caption = args['image' .. v .. 'caption'] or ''
			
			if image == 'title' then
				local imageTitle = mw.title.new( 'Media:' .. title .. '.png' )
				if #groupCount == 0 and imageTitle and imageTitle.exists then
					json.images[#json.images + 1] = title .. '.png'
					local altText = title .. '.png: Infobox image for ' .. title .. ' the ' .. template .. ' in Minecraft'
					image = '[[File:' .. title .. '.png|' .. size .. '|class=' .. ( class or '' ) .. '|alt=' .. altText .. ']]'
				else
					image = ''
				end
			--[=[
				elseif titleObject.namespace == 0 then
					image = '[[File:No image.svg|' .. size .. '|link=File:' .. title .. '.png|Upload ' .. title .. '.png]]'
				else
					image = '[[File:No image.svg|' .. size .. '|link=]]'
				end
			]=]
			elseif string.match( image, 'UNIQ%-%-gallery%-' ) then
				image = image
			elseif image:match( ';' ) then
				if not animate then
					animate = require( 'Module:Animate' ).animate
				end
				image = animate{ image, size, class = class }
			else
				json.images[#json.images + 1] = image
				local altText = image .. ': Infobox image for ' .. title .. ' the ' .. template .. ' in Minecraft'
				image = '[[File:' .. image .. '|' .. size .. '|class=' .. ( class or '' ) .. '|alt=' .. altText .. ']]'
			end
			
			if caption ~= '' then
				caption = '<div class="infobox-imagecaption">\n' .. caption .. '\n</div>'
			end
			
			if image ~= '' or caption ~= '' then
				table.insert( images, '<div>' .. image .. caption .. '</div>' )
			end
		end
		
		images = table.concat( images, '\n' )
		
		if #invImgCount > 0 then
			table.sort( invImgCount )
			local slot
			local invTitle = mw.title.new( 'Media:Invicon ' .. title .. '.png' )
			local invAliases = mw.loadData( 'Module:Inventory slot/Aliases' )
			for k, v in ipairs( invImgCount ) do
				local image = args['invimage' .. v]
				if image == 'title' then
					if invTitle and invTitle.exists or invAliases[title] then
						image = title
					else
						image = false
					end
				end
				
				if image == '----' then
					table.insert( invImages, '</div><div style="padding-top:.5em">' )
				elseif image then
					if not slot then
						slot = require( 'Module:Inventory slot' ).slot
					end
					json.invimages[#json.invimages + 1] = image
					table.insert( invImages, slot{ image, link = 'none' } )
				end
			end
			
			if slot and #invImages > 0 then
				invImages = '<div class="infobox-invimages"><div>' .. table.concat( invImages, '' ) .. '</div></div>'
			else
				invImages = ''
			end
		else
			invImages = ''
		end
		
		if images ~= '' or invImages ~= '' then
			imageArea = images .. '\n' .. invImages
		else
			imageArea = 'none'
		end
	end
	if imageArea and imageArea ~= 'none' then
		imageArea = '<div class="infobox-imagearea animated-container">' .. imageArea .. '</div>'
	else
		local groupArea = args.grouparea
		if groupArea then
			imageArea = groupArea
		else
			imageArea = ''
		end
	end
	
	local extraText = args.extratext
	
	if extraText and extraText ~= 'none' then
		json.extratext = extraText
		extraText = '<div class="infobox-extratext">'.. extraText ..'</div>'
	else
		extraText = ''
	end
	
	headerArea = imageArea.. '' ..extraText
	local footer = args.footer
	if footer then
		json.footer = footer
		footer = '| class="infobox-footer" colspan="2" | ' .. footer
	end
	
	local repl = function( label, field )
		json.rows[#json.rows + 1] = {
			label = mw.text.jsonDecode( label:gsub( '\n', '\\n' ) ),
			field = mw.text.jsonDecode( field:gsub( '\n', '\\n' ) )
		}
		return ''
	end
	args.rows = string.gsub( args.rows or '', '<code class="history%-json">{"label": (".-"), "field": (".-")}</code>\n', repl )
	
	local html = {
		'<div class="notaninfobox">',
			'<div class="bhwiki-header infobox-title">' .. titleArea .. '</div>',
			headerArea,
			'{| class="infobox-rows" cellspacing="1" cellpadding="4"',
			'|-',
			args.rows or '',
			footer or '',
			'|}',
		'</div>',
		f:callParserFunction( '#tag', { 'pre', class = 'history-json noexcerpt navigation-not-searchable',
			mw.text.jsonEncode( json, mw.text.JSON_PRETTY )
		} )
	}
	
	return table.concat( html, '\n' )
end

return p