Module:CargoDeclare

From Porn Base Central
Jump to navigation Jump to search

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

local util_args = require('Module:ArgsUtil')
local util_html = require('Module:HtmlUtil')
local util_map = require('Module:MapUtil')
local util_table = require('Module:TableUtil')
local i18n = require('Module:i18nUtil')

local DOC_COLUMNS = { 'index', 'field', 'type', '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]
	return h.makeDocTable(h.getFields(tableName))
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.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