Leaguepedia | League of Legends Esports Wiki
[checked revision][checked revision]
((via Mediawiker ST3))
((via Mediawiker ST3))
Line 94: Line 94:
 
function h.printNormalRow(tbl, label, content, class)
 
function h.printNormalRow(tbl, label, content, class)
 
if not content then return end
 
if not content then return end
require('Module:VarsUtil').logObject(I18N_INTERNAL_TABLE)
+
require('Module:VarsUtil').logObject(I18N_INTERNAL_TABLE or {})
 
tbl:tag('tr')
 
tbl:tag('tr')
 
:addClass(class)
 
:addClass(class)

Revision as of 12:35, 3 May 2019

Edit the documentation or categories for this module.

To make a new infobox module, subclass this.


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.i18nFile then
		i18n.initGlobalFromFile(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.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.printNormalRow(tbl, label, content, class)
	if not content then return end
	require('Module:VarsUtil').logObject(I18N_INTERNAL_TABLE or {})
	tbl:tag('tr')
		:addClass(class)
		:tag('td')
			:addClass('infobox-label')
			:wikitext(i18n.print(label) or label)
		:done()
		:tag('td')
			:wikitext(content)
		:done()
	:done()
	return
end
	
function h.printWideRow(tbl, content, class)
	if not content then return end
	tbl:tag('tr')
		:addClass(class or '')
		:tag('td')
			:attr('colspan','2')
			:addClass('infobox-wide')
			:wikitext(content)
	return
end

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)
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 = 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.getTitle(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.printCell(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.getTitle(displayData)
	if displayData.lc then
		return displayData.title:lower()
	end
	return displayData.title
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.printCell(tbl, line)
	if line.celltype == 'wide' then
		h.printWideRow(tbl, line.value, line.class)
	else
		h.printNormalRow(tbl, line.name, line.value, line.class)
	end
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