Module:CargoDeclare

local util_args = {} local util_map = {}

local bool_false = { ['false'] = true, ['no'] = true, [''] = true, ['0'] = true, ['nil'] = true } local bool_true = { ['true'] = true, ['yes'] = true }

local lang = mw.getLanguage('en')

function util_args.castAsBool(str) if type(str) == 'boolean' then return str end if not str or bool_false[lang:lc(str)] then return false end return true end

function util_map.inPlace(tbl, f, ...) for k, v in pairs(tbl) do		tbl[k] = f(v, ...) end return tbl end

function util_map.rowsInPlace(tbl, f, ...) for k, row in ipairs(tbl) do		f(row, ...) row.index = k	end return tbl end

local p = {} local h = {}

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

function p.main(frame) local args = require('Module:Arguments').getArgs(frame, {parentFirst=true}) -- 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 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 h.getFields(tableName) local args = require('Module:CargoDeclare/' .. tableName) util_map.rowsInPlace(args, h.prepareArgs) return args end

function h.prepareArgs(row) if not row.sep then return end local desc = { row.desc ~= '' and row.desc, row.sep and ('This field is a text representation of a list with separator %s'):format(row.sep) }	row.desc = 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) local tr = output:tag('tr') local columns = { ["desc"] = 'Description', ["field"] = 'Name', ["type"] = 'Type', ["index"] = "N", }	for i, COLUMN in ipairs(DOC_COLUMNS) do		tr:tag('th'):wikitext(columns[COLUMN]) end for _, row in ipairs(fields) do		tr = output:tag('tr') for _, COLUMN in ipairs(DOC_COLUMNS) do			tr:tag('td'):wikitext(row[COLUMN]) end end return output end

function h.formatForDocTable(row) end

return p