Module:Media

Jump to navigation Jump to search

Documentation for this module may be created at Module:Media/doc

-- This is essentially a wrapper Module for Module:Tabs to give further functions to tabs in infoboxes using Template:Media
-- Allows for assigning which specific Media tab is selected first and will be able to give a media's full display name as a hover element
local getArgument = require('Module:Arguments').getArgument

local p = {}

function p.Main(frame)
	local oArgs = frame:getParent().args
	local args = {}
	for k, v in pairs(oArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	local maxArgs = table.maxn(args)
	local i = 0
	local compareActive
	local mediaArgs = {}
	local SEOimage
	local fileType
	local output
	
	if args['type'] ~= nil then -- Used for auto caption
		fileType = args['type']:lower()
	else
		fileType = 'artwork'
	end
	if args['active'] ~= nil then
		compareActive = args['active']
	end
	repeat
		if args[i+1] and args[i+2] then
			local caption = fileType .. ' from '
			local mediaType
			mediaArgs[i+1] = args[i+2]
			mediaArgs[i+2] = '[[File:' .. args[i+1] .. '|270x350px]]'
			
			-- Creates the Caption (will add manual override)
			local positionPlus = string.find(mediaArgs[i+1], "+")
			if positionPlus then
				local positionFirstSym = string.find(mediaArgs[i+1], "[%-%+%~]") -- Gets the position of the first symbol
				local positionEnd = string.find(mediaArgs[i+1], "[%-%~]", positionPlus + 1) or string.len(mediaArgs[i+1]) + 1 -- Gets the position of the symbol after +
				
				local getType -- Carried over from Module:Link for assessing whether a given media type exists
				local abbr = string.sub(mediaArgs[i+1], 1, positionFirstSym - 1):lower()
				
				mediaType = string.sub(mediaArgs[i+1], positionPlus + 1, positionEnd - 1):lower()
				if mediaType == 'tv' or mediaType == 'ova' then
					mediaType = mediaType:upper()
				else
					mediaType = string.sub(mediaType, 1, 1):upper() .. string.sub(mediaType, 2)
				end
				if mw.title.makeTitle('Module', 'Data/Media/' .. mediaType).exists == true then
					getType = mw.loadData('Module:Data/Media/' .. mediaType)
					if getType.media[abbr] then
						caption = caption .. frame:expandTemplate{title = 'Link', args = { mediaType, abbr .. '+' } }
					else
						caption = caption .. "''unknown media''[[Category:Pages with links to unknown media]]"
					end
				else
					caption = caption .. "''unknown media type''[[Category:Pages with links to unknown media]]"
				end
				mediaArgs[i+1] = string.sub(mediaArgs[i+1], 1, positionPlus - 1) .. string.sub(mediaArgs[i+1], positionEnd)
			else
				local positionEnd = string.find(mediaArgs[i+1], "[%-%~]") or string.len(mediaArgs[i+1]) + 1
				local getType = mw.loadData('Module:Data/Media/Game')
				local abbr = string.sub(mediaArgs[i+1], 1, positionEnd - 1):lower()
				if getType.media[abbr] then
						caption = caption .. frame:expandTemplate{title = 'Link', args = { 'Game', abbr } }
					else
						caption = caption .. "''unknown media''[[Category:Pages with links to unknown media]]"
				end
			end
			
			local positionTilde = string.find(mediaArgs[i+1], "~")
			if positionTilde then
				positionEnd = string.find(mediaArgs[i+1], "[%-]", positionTilde + 1) or string.len(mediaArgs[i+1]) + 1  -- Gets the position of the symbol after ~
				caption = string.sub(mediaArgs[i+1], positionTilde + 1, positionTilde + 1):upper() .. string.sub(mediaArgs[i+1], positionTilde + 2, positionEnd - 1)
				mediaArgs[i+1] = string.sub(mediaArgs[i+1], 1, positionTilde - 1) .. string.sub(mediaArgs[i+1], positionEnd)
			end
			
			local positionMinus = string.find(mediaArgs[i+1], "-")
			if positionMinus then
				local positionEnd = string.len(mediaArgs[i+1]) -- No symbols with meaning will be left at the point of this section
				local priorText = string.sub(mediaArgs[i+1], positionMinus + 1, positionEnd)
				if positionTilde == nil then
					caption = priorText .. ' ' .. caption
				end
				-- Might be a more efficient way to format this section if it weren't embedded in positionMinus tree
				if mediaType then
					mediaArgs[i+1] = string.sub(mediaArgs[i+1], 1, positionMinus - 1) .. ' ' .. tostring(mw.html.create('small'):wikitext( '(' .. mediaType .. ')' )) .. ' ' .. tostring(mw.html.create('small'):wikitext(priorText))
				else
					mediaArgs[i+1] = string.sub(mediaArgs[i+1], 1, positionMinus - 1) .. ' ' .. tostring(mw.html.create('small'):wikitext(priorText))
				end
			else
				caption = string.sub(caption, 1, 1):upper() .. string.sub(caption, 2)
				if mediaType then
					mediaArgs[i+1] = mediaArgs[i+1] .. ' ' .. tostring(mw.html.create('small'):wikitext( '(' .. mediaType .. ')' ))
				end
			end
			
			mediaArgs[i+2] = mediaArgs[i+2] .. '\n\n' .. caption
			
			-- Compares tab to what is set as active and overwrites SEO image and sets the default tab if so
			if args[i+2] == compareActive then
				mediaArgs['active'] = (i+2) * 0.5
				SEOimage = args[i+1]
			end
		end
		-- Sets the first tab to SEO image at first; is overwritten by subsequent loop if active is set
		if i == 0 then
			SEOimage = args[1]
		end
		i = i + 2
	until i+1 > maxArgs
	mw.ext.seo.set({ image = SEOimage })
	-- If there is only one instance, then do not render as tabs
	if maxArgs <= 3 then
		output = mediaArgs[2]
	else
		output = frame:expandTemplate{title = 'Tabs', args = mediaArgs}
	end
	return output
end

return p