Leaguepedia | League of Legends Esports Wiki
Advertisement
Leaguepedia | League of Legends Esports Wiki

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require("Module:CargoUtil")
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_sort = require("Module:SortUtil")
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')
local SORTKEY
local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge()
	h.processArgs(args)
	local names = h.getNames(args)
	names.vars = h.getVarsArray(names.vars)
	util_sort.dictByKeys(names.data, 'sortkey', true)
	return h.makeOutput(names)
end

function h.processArgs(args)
	SORTKEY = args.sortkey
end

function h.getNames(args)
	local rawNamesSheet = require(('Module:%snames'):format(args[1]))
	local names = { vars = {}, data = {} }
	for input, output in pairs(rawNamesSheet) do
		h.addPairToNames(names, input, output)
	end
	return names
end

function h.addPairToNames(names, input, output)
	if type(output) == 'table' then
		h.initializeInput(names, input, input, h.getSortkey(input, output))
		for k, v in pairs(output) do
			names.vars[k] = true -- for the heading of the overall table
			names.data[input].vars[k] = v
		end
	elseif type(output) == 'string' then
		h.initializeInput(names, input, output)
	end
end

function h.getSortkey(input, output)
	if not SORTKEY then return input end
	return output[SORTKEY]
end

function h.initializeInput(names, input, output, sortkey)
	if not names.data[output] then
		util_table.push(names.data, output)
		names.data[output] = { inputs = {}, vars = {} }
	end
	if sortkey then
		names.data[output].sortkey = sortkey
		names.data[output].output = output
	end
	util_table.push(names.data[output].inputs, tostring(input))
end

function h.getVarsArray(vars)
	local ret = {}
	for var, _ in pairs(vars) do
		ret[#ret+1] = var
	end
	table.sort(ret)
	return ret
end

function h.makeOutput(names)
	local output = mw.html.create('table')
		:addClass('wikitable')
		:addClass('hoverable-rows')
	h.printHeading(output, names.vars)
	h.printData(output, names.data, names.vars)
	return output
end

function h.printHeading(output, vars)
	-- no i18n here so can't use util_html
	local tr = output:tag('tr')
	tr:tag('th'):wikitext('Output')
	tr:tag('th'):wikitext('Inputs')
	for _, v in ipairs(vars) do
		tr:tag('th'):wikitext(v)
	end
end

function h.printData(output, data, vars)
	for _, v in ipairs(data) do
		local tr = output:tag('tr')
		h.printDataRow(tr, data[v], vars)
	end
end

function h.printDataRow(tr, dataRow, vars)
	table.sort(dataRow.inputs)
	tr:tag('th'):wikitext(dataRow.output)
	tr:tag('td'):wikitext(util_table.concat(dataRow.inputs, ', ', h.printOneInput))
	for _, v in ipairs(vars) do
		valToPrint = dataRow.vars[v]
		if valToPrint ~= nil then valToPrint = tostring(valToPrint) end
		tr:tag('td'):wikitext(valToPrint or "")
	end
end

function h.printOneInput(input)
	return (('<code>%s</code>'):format(input))
end

return p
Advertisement