Module:Infobox

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.

Copyable code to make a new infobox module:

local lang = mw.getLanguage('en')
local m_team = require('Module:Team')
local m_region = require('Module:Region')
local m_country = require('Module:Country')
local social = require('Module:Infobox/Social')
local teamhist = require('Module:Infobox/TeamHist')
local util_infobox = require('Module:InfoboxUtil')
local util_html = require('Module:HTMLUtil')
local util_cargo = require('Module:CargoUtil')
local util_dpl = require('Module:DPLUtil')
local util_table = require('Module:TableUtil')

local NOIMAGE = 'Unknown Infobox Image - Player.png'

local LAYOUT = {
	sections = {  },
	contents = {
		{  },
	},
	i18n = {}
}

function getProcessed(args)
	local tbl = {
		pagename = mw.title.getCurrentTitle().text,
	}
	return tbl
end

function processDisplay(args, processed)
	local tbl = {
		title = 
		image =
		notice =
	}
	return tbl
end

function processCargo(args, processed)
	local tbl = {}
	return tbl
end

function processCategories(args, processed)
	local tbl = {}
	return tbl
end

function processVariables(args, processed)
	local tbl = {}
	return tbl
end

local p = {}
function p.main(args)
	local processed = getProcessed(args)
	
	local settings = {
		lc = processed.lc,
		nocargo = mw.title.getCurrentTitle().nsText ~= '' or args.nocargo,
		nocat = mw.title.getCurrentTitle().nsText ~= '' or args['no-cat'],
	}
	
	return {
		display = processDisplay(args, processed),
		cargo = processCargo(args, processed),
		settings = settings,
		categories = processCategories(args, processed),
		variables = processVariables(args, processed),
		layout = LAYOUT,
	}
end

return p

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local i18n = require('Module:I18nUtil')
local lang = mw.getLanguage('en')
local CLASSES = {
	title = 'infobox-title',
	notice = 'infobox-notice'
}

local h = {}
local p = {}

function p.fromPreload(frame)
	local args = util_args.merge(true)
	local infoboxType = args.infoboxType
	local preload = require('Module:Infobox/' .. infoboxType)
	local data = preload.main(args)
	return p._main(data, infoboxType)
end

function p.fromArgs(frame)
	local args = util_args.merge(true)
	local data = {
		layout = h.layoutFromArgs(args),
		display = args,
		cargo = {},
		categories = {},
		settings = {},
		variables = {},
	}
	return p._main(data)
end

function p._main(data, infoboxType)
	if data.layout.i18nFile then
		i18n.initGlobalFromFile(data.layout.i18nFile)
		data.i18n = {}
	end
	h.setLC(data.settings.lc)
	h.setVariables(data.variables)
	h.storeCargo(data.settings.nocargo, data.cargo)
	h.mergeDynamicDisplayValuesIntoStatic(data, data.display)
	h.prepDisplayDataForPrinting(data.layout, data.display, infoboxType)
	local output = {
		h.getTabs(data.layout.tabs),
		tostring(h.printFinalInfobox(data.display)),
		h.setCategories(data.categories or {}, data.settings.nocat)
	}
	return table.concat(output,'')
end

function h.layoutFromArgs(args)
	local layout = {
		tabs = args.tabs,
		sections = util_text.split(args.sections),
		contents = {},
		i18n = {},
		classes = {},
	}
	for k, v in ipairs(layout.sections) do
		layout.contents[k] = util_text.split(args[v] or '')
		local section = layout.contents[k]
		
		layout.classes[v] = args[v .. '_class']
		local names = args[v .. '_names'] and util_text.split(args[v .. '_names'])
		for i, field in ipairs(section) do
			layout.i18n[field] = args[field .. '_name'] or names and names[i]
			layout.classes[field] = args[field .. '_class']
			if args[field .. '_style'] then
				section[field] = args[field .. '_style']
			end
		end
	end
	return layout
end

-- build infobox

function h.mergeDynamicDisplayValuesIntoStatic(data, display)
	if not data.layout.classes then data.layout.classes = {} end
	if not data.layout.i18n then data.layout.i18n = {} end
	util_table.merge(data.layout.classes, display.classes)
	util_table.merge(data.layout.i18n, display.names)
	display.names = data.layout.i18n
end

function h.prepDisplayDataForPrinting(layout, display, infoboxType)
	display.infoboxType = infoboxType
	display.lc = layout.lc
	for _, key in ipairs(h.getListOfNonemptySectionIndices(layout, display)) do
		display[#display+1] = {
			name = layout.sections[key],
			lines = h.getSectionContentsAndValues(layout.contents[key], display, layout.classes),
			class = layout.classes[key]
		}
	end
end

function h.getListOfNonemptySectionIndices(layout, display)
	local ret = {}
	for k, _ in ipairs(layout.sections) do
		if h.sectionIsNonempty(layout.contents[k], display) then
			ret[#ret+1] = k
		end
	end
	return ret
end

function h.sectionIsNonempty(contents, display)
	for k, v in ipairs(contents) do
		if display[v] then
			return true
		end
	end
	return false
end

function h.getSectionContentsAndValues(contents, display, classes)
	local ret = {}
	for _, paramName in ipairs(contents) do
		ret[#ret+1] = h.getOneSectionContentsAndValues(contents, display, classes, paramName)
	end
	return ret
end

function h.getOneSectionContentsAndValues(contents, display, classes, paramName)
	if not display[paramName] then return nil end
	return {
		name = display.names[paramName] or i18n.print(paramName) or paramName,
		value = display[paramName],
		class = classes[paramName],
		celltype = contents[paramName]
	}
end

function h.printFinalInfobox(displayData)
	local tbl = h.initializeTable(displayData)
	h.printHeading(tbl, displayData.notice, 'notice')
	h.printHeading(tbl, h.getTitleWithCasing(displayData), 'title')
	h.printImage(tbl, displayData)
	for k, section in ipairs(displayData) do
		h.printHeading(tbl, section.name, section.class)
		for _, line in ipairs(section.lines) do
			h.printRow(tbl, line)
		end
	end
	return tbl
end

function h.initializeTable(displayData)
	local tbl = mw.html.create('table')
		:addClass('infobox')
		:addClass(displayData.class)
	h.addInfoboxTypeClass(tbl, displayData.infoboxType)
	return tbl
end

function h.addInfoboxTypeClass(tbl, infoboxType)
	if not infoboxType then return end
	tbl:addClass('Infobox' .. infoboxType)
		:attr('id', 'infobox' .. infoboxType)
end

function h.getTitleWithCasing(displayData)
	if displayData.lc then
		return displayData.title:lower()
	end
	return displayData.title
end

function h.printHeading(tbl, content, class)
	if not content then return end
	tbl:tag('tr')
		:tag('th')
			:attr('colspan','2')
			:addClass(class and (CLASSES[class] or class) or '')
			:wikitext(i18n.print(content) or content)
		:done()
	:done()
	return
end

function h.printImage(tbl, data)
	if not data.image then return end
	local imageText = ('[[File:%s|center|%s]]'):format(data.image, data.imagesize or '220px')
	h.printWideRow(tbl, imageText, 'infobox-image')
	h.printWideRow(tbl, data.imagecaption, 'infobox-image-caption')
end

function h.printRow(tbl, line)
	local tr = tbl:tag('tr')
		:addClass(line.class)
	if line.celltype == 'wide' then
		h.printWideRow(tr, line.value)
	else
		h.printNormalRow(tr, line.name, line.value)
	end
end

function h.printWideRow(tr, content)
	if not content then return end
	tr:tag('td')
		:attr('colspan','2')
		:addClass('infobox-wide')
		:wikitext(content)
	return
end

function h.printNormalRow(tr, label, content)
	if not content then return end
	tr:tag('td')
		:addClass('infobox-label')
		:wikitext(label)
	:done()
	:tag('td')
		:wikitext(content)
	:done()
	return
end

function h.getKeyForHeading(contents, display)
	for k, v in ipairs(contents) do
		if display[v] then
			return k
		end
	end
end

function h.storeCargo(nocargo, data)
	if nocargo or not data then return end
	for _, tbl in ipairs(data) do
		util_cargo.store(tbl)
	end
	return
end

function h.getTabs(tabsTemplateTitle)
	if tabsTemplateTitle then
		local frame = mw.getCurrentFrame()
		return frame:expandTemplate{title = tabsTemplateTitle }
	end
	return ''
end

function h.setLC(lc)
	if not lc then return end
	local title = mw.title.getCurrentTitle().text
	local frame = mw.getCurrentFrame()
	frame:callParserFunction{ name = 'DISPLAYTITLE', args = lang:lcfirst(title) }
	return
end

function h.setVariables(data)
	if not data then return end
	util_table.removeFalseEntries(data)
	for k, v in pairs(data) do
		mw.getCurrentFrame():callParserFunction{
			name = '#vardefine:' .. k,
			args = { v }
		}
	end
	return
end

function h.setCategories(data, nocat)
	if nocat then
		return ''
	end
	util_table.removeFalseEntries(data)
	local tbl = {}
	for _, v in ipairs(data) do
		tbl[#tbl+1] = ("[[Category:%s]]"):format(v)
	end
	return table.concat(tbl, '')
end

return p