Module:Esperanto Translation/Informujo

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
local p = {}
function p.informujo( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local titleObject = mw.title.getCurrentTitle()
	local title = args.titolo or titleObject.subpageText
	local template = f:getParent():getTitle():lower():gsub( 'minecraft wiki:project/esperanto translation/ŝablono:', '' )
	local headerArea = ''
	local filename = require( 'Module:Esperanto Translation/Nomoj' ).dosiernomo_el_nomo
	
	local colon = title:find(':')
	if colon then title = title:sub( colon + 1 ) end
	
	local json = {
		images = {},
		invimages = {},
		rows = {}
	}
	
	local imageArea = args.bildoareo
	if not imageArea and imageArea ~= 'ne' then
		local images = {}
		local invImages = {}
		local defaultImageSize = args["defaŭltabildogrando"] or '150px'
		local defaultImageClass = args["defaŭltabildoklaso"]
		args.bildo1 = args.bildo1 or args.bildo or 'titolo'
		args.bildo1grando = args.bildo1grando or args.bildogrando
		args.bildo1klaso = args.bildo1klaso or args.bildoklaso
		args.bildo1titolo = args.bildo1titolo or args.bildotitolo or ''
		args.ujbildo1 = args.ujbildo1 or args.ujbildo or 'ne'
		args.grupo1 = args.grupo1 or args.grupo
		args.grupo1grando = args.grupo1grando or args.grupogrando
		args.grupo1klaso = args.grupo1klaso or args.grupoklaso
		args.grupo1titolo = args.grupo1titolo or args.grupotitolo 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( '^(bildo)(%d+)$' )
				local invImage, invNum = k:match( '^(ujbildo)(%d+)$' )
				local group, groupNum = k:match( '^(grupo)(%d+)$' )
				local groupImg, groupImgNum = k:match( '^(%d+)-(%d+)$' )
				if v:lower() ~= 'ne' 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['grupo' .. groupNum] then
							groupImgList['grupo' .. groupNum] = {}
						end
					elseif groupImg then
						if not groupImgList['grupo' .. groupImg] then
							groupImgList['grupo' .. groupImg] = {}
						end
						table.insert( groupImgList['grupo' .. 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['grupo' .. v]
				local groupSize = args['grupo' .. v .. 'grando'] or defaultImageSize
				local groupClass = args['grupo' .. v .. 'klaso'] or defaultImageClass
				local groupCaption = args['grupo' .. v .. 'titolo'] or ''
				local groupImages = {}
				
				table.sort( groupImgList['grupo' .. v] )
				for _, w in ipairs( groupImgList['grupo' .. v] ) do
					local image = args[v .. '-' .. w]
					local size = args[v .. '-' .. w .. 'grando'] or groupSize
					local class = args[v .. '-' .. w .. 'klaso'] or groupClass
					local caption = args[v .. '-' .. w .. 'titolo'] 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 .. ': Informuja bildo por la ' .. template .. ' «' .. title .. '» el 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['bildo' .. v]
			local size = args['bildo' .. v .. 'grando'] or defaultImageSize
			local class = args['bildo' .. v .. 'klaso'] or defaultImageClass
			local caption = args['bildo' .. v .. 'titolo'] or ''
			
			if image == 'titolo' then
				local imageName = title
				local imageTitle = mw.title.new( 'Media:' .. imageName .. '.png' )
				if not imageTitle.exists then
					imageName = filename( title )
					imageTitle = mw.title.new( 'Media:' .. imageName .. '.png' )
				end
				if #groupCount == 0 and imageTitle and imageTitle.exists then
					json.images[#json.images + 1] = imageName .. '.png'
					local altText = imageName .. '.png: Informuja bildo por la ' .. template .. ' «' .. title .. '» el Minecraft'
					image = '[[File:' .. imageName .. '.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 .. ': Informuja bildo por la ' .. template .. ' «' .. title .. '» el 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( images, '<div>' .. image .. caption .. '</div>' )
		end
		
		images = table.concat( images, '\n' )
		
		if #invImgCount > 0 then
			table.sort( invImgCount )
			local slot
			local invName = title
			local invTitle = mw.title.new( 'Media:Invicon ' .. invName .. '.png' )
			if not invTitle.exists then
				invName = filename( title )
				invTitle = mw.title.new( 'Media:Invicon ' .. invName .. '.png' )
			end
			local invAliases = mw.loadData( 'Module:Esperanto Translation/Objektujo/Alinomoj' )
			for k, v in ipairs( invImgCount ) do
				local image = args['ujbildo' .. v]
				if image == 'titolo' 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:Esperanto Translation/Objektujo' ).ujo
					end
					json.invimages[#json.invimages + 1] = image
					table.insert( invImages, slot{ image, link = 'ne' } )
				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 = 'ne'
		end
	end
	if imageArea and imageArea ~= 'ne' then
		imageArea = '<div class="infobox-imagearea animated-container">' .. imageArea .. '</div>'
	else
		local groupArea = args.grupareo
		if groupArea then
			imageArea = groupArea
		else
			imageArea = ''
		end
	end
	
	local extraText = args.kromteksto
	
	if extraText and extraText ~= 'ne' then
		json.extratext = extraText
		extraText = '<div class="infobox-extratext">'.. extraText ..'</div>'
	else
		extraText = ''
	end
	
	headerArea = imageArea.. '' ..extraText
	local footer = args.malsuprateksto
	if footer then
		json.footer = footer
		footer = '| class="infobox-footer" colspan="2" | ' .. footer
	end
	
	json.title = title
	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.horizontaloj = string.gsub( args.horizontaloj or '', '<code class="history%-json">{"label": (".-"), "field": (".-")}</code>\n', repl )
	
	local html = {
		'<div class="notaninfobox">',
			'<div class="mcwiki-header infobox-title">' .. title .. '</div>',
			headerArea,
			'{| class="infobox-rows" cellspacing="1" cellpadding="4"',
			'|-',
			args.horizontaloj 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