Leaguepedia | League of Legends Esports Wiki
Register
Advertisement

Documentation for this module may be created at Module:CargoDeclare/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_string = require("Module:StringUtil")
local util_table = require('Module:TableUtil')
local i18n = require('Module:i18nUtil')

local DOC_COLUMNS = { 'index', 'field', 'type', 'delimiter', 'desc' }

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.overwrite()
	-- doc page arg will be set when the declaring template is being transcluded
	if util_args.castAsBool(args.doc) then
		return p.doc(args)
	end
	if util_args.castAsBool(args.forgadget) then
		return p.forGadget(args)
	end
	return p.declare(args)
end

function p.declare(args)
	i18n.init('CargoDeclare')
	local tableName = args[1]
	local cargoFields = h.concatFieldsToArgs(h.getFields(tableName))
	return mw.getCurrentFrame():callParserFunction{
		name = ('#cargo_declare:_table=%s'):format(tableName),
		args = cargoFields
	}
end

function p.doc(args)
	i18n.init('CargoDeclare')
	local tableName = args[1]
	local fields = h.getFields(tableName)
	h.storeFields(fields)
	return h.makeDocTable(fields)
end

function p.forGadget(args)
	i18n.init('CargoDeclare')
	local cargoFields = h.getFields(args[1])
	return util_table.concat(cargoFields, ';!;!;', h.concatForJson)
end

function h.getFields(tableName)
	-- this should be called first thing by every top-level method
	local args = require('Module:CargoDeclare/' .. tableName)
	util_map.rowsInPlace(args, h.prepareArgs)
	return args
end

function h.prepareArgs(row)
	-- create any auto-generated description text needed
	if not row.sep then return end
	local desc = {
		row.desc ~= '' and row.desc,
		row.sep and i18n.default('sepSentence', row.sep)
	}
	row.desc = util_table.concat(desc, ' ')
end

function h.concatFieldsToArgs(fields)
	util_map.inPlace(fields, h.concatFieldToArg)
	return fields
end

function h.concatFieldToArg(field)
	return ('%s=%s'):format(field.field, field.type)
end

function h.storeFields(fields)
	if util_string.endsWith(mw.title.getCurrentTitle().text, 'doc') then return end
	for _, row in ipairs(fields) do
		util_cargo.store({
			_table = 'CargoFields',
			CargoTable = mw.title.getCurrentTitle().baseText,
			Name = row.field,
			FieldType = row.type,
			Description = row.desc,
		})
	end
end

function h.makeDocTable(fields)
	local output = mw.html.create('table')
		:addClass('wikitable')
	util_map.rowsInPlace(fields, h.formatForDocTable)
	util_html.printHeaderFromI18n(output, DOC_COLUMNS)
	util_html.printRowsByList(output, fields, DOC_COLUMNS)
	return output
end

function h.formatForDocTable(row)
	
end

function h.concatForJson(field)
	return ('%s:::%s'):format(field.field, field.desc)
end

return p
Advertisement