Module:Args Utility

From Caves of Qud Wiki
Jump to navigation Jump to search

Taken from LoL's ArgsUtil module.


local util_map = require('Module:Map Utility')
local util_text = require('Module:Text Utility')
local util_table = require('Module:Table Utility')

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

local lang = mw.getLanguage('en')

local p = {}

function p.norm(v)
	if not v then
		return false
	end
	local lc = lang:lc(v)
	if bool_false[lc] then
		return false
	elseif bool_true[lc] then
		return true
	end
	return v
end

function p.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 p.isDefined(str)
	if not str then return false end
	return true
end

function p.boolToStringYN(str)
	if str then return 'Yes' end
	return 'No'
end

function p.boolToStringTF(str)
	if str then return 'True' end
	return 'False'
end

function p.nilToFalse(val)
	-- casts nil as false
	-- does not change anything else
	-- used if needing to have false but non-nil values in a table
	-- for ipairs, util_table.removeFalseEntries, etc
	return not not val and val
end

function p.lookupVars(str, lookup_tbl, skipdefault)
	-- for rolenames etc. if a default table is supplied, this will be
	-- returned with priority over lookup.DEFAULT in the case of no match
	local vars = str and lookup_tbl[lang:lc(str)]
	if not vars then
		if skipdefault then
			return nil
		end
		return lookup_tbl.DEFAULT
	end
	if type(vars) == 'string' then
		vars = lookup_tbl[vars]
		if type(vars) == 'string' then
			error(('Error in lookup module for input %s'):format(str))
		end
	end
	return vars
end

function p.merge(norm)
	local f = mw.getCurrentFrame()
	local origArgs = f.args
	local parentArgs = f:getParent().args

	local args = {}
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	return args
end

function p.mergeKeepEmpty()
	local f = mw.getCurrentFrame()
	local origArgs = f.args
	local parentArgs = f:getParent().args

	local args = {}
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		args[k] = v
	end
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		args[k] = v
	end
	
	return args
end

function p.overwrite(norm)
	local f = mw.getCurrentFrame()
	local origArgs = f.args
	local parentArgs = f:getParent().args

	local args = {}
	
	for k, v in pairs(parentArgs) do
		v = mw.text.trim(v)
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	for k, v in pairs(origArgs) do
		v = mw.text.trim(tostring(v))
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	
	return args
end

function p.original(norm)
	local args = {}
	for k, v in pairs(mw.getCurrentFrame().args) do
		v = mw.text.trim(tostring(v))
		if norm and v ~= '' or not norm then
			args[k] = v
		end
	end
	return args
end

function p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
	if not max then max = -1 end
	local i = 1
	local tbl = {}
	if args[argname] and not disallowUnnamedFirst then
		tbl[1] = args[argname]
		i = 2
	end
	while args[argname .. i] or i <= max do
		tbl[i] = args[argname .. i]
		i = i + 1
	end
	if not next(tbl) then
		return nil
	end
	return tbl
end

function p.numberedArgsToList(args, argname, disallowUnnamedFirst, max, sep, removeEmpty)
	local tbl = p.numberedArgsToTable(args, argname, disallowUnnamedFirst, max)
	if removeEmpty then
		util_table.removeFalseEntries(tbl, max)
	elseif max then
		for k = 1, max do
			tbl[k] = tbl[k] or ''
		end
	end
	return table.concat(tbl, sep or ',')
end

function p.ifArg(arg, display)
	if not arg then
		return false
	end
	return display
end

function p.splitArgs(input, fieldlist, sep)
	if not input or input == '' then return end
	sep = sep or '%s*;;;%s*'
	local result = {}
	local inputTbl = util_text.split(input,sep)
	for i, v in ipairs(fieldlist) do
		if not inputTbl[i] then
			error(('Missing parameter %s - maybe wrong child template?'):format(v))
		end
		if inputTbl[i] ~= '' then
			result[v] = inputTbl[i]
		end
	end
	return result
end

function p.splitNamedArgs(input, sep)
	if not input or input == '' then return end
	sep = sep or '%s*;;;%s*'
	local result = {}
	local inputTbl = util_text.split(input,sep)
	for i, v in ipairs(inputTbl) do
		a, b = v:match('([^=]+)%s*=%s*(.+)')
		if a then
			result[a] = b
		end
	end
	return result
end

function p.splitArgsArray(input, fieldlist, outersep, innersep)
	if not input or input == '' then return {} end
	outersep = outersep or '%s*:::%s*'
	local ret = util_map.split(input, outersep, p.splitArgs, fieldlist, innersep)
	return ret
end

function p.strOrTitle(str)
	return str or mw.title.getCurrentTitle().rootText
end

return p