Jump to content

Module:Enumerate

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chlod (talk | contribs) at 18:09, 14 April 2021 (Forgot to export). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Enumerates a given parameter set from the invoking template as a bullet list.
local endswith = require("Module:String").endswith;
local yesno = require("Module:Yesno");
local p = {}

function p.main(frame)
	local args = getArgs(frame, {
		trim = true,
		removeBlanks = false
	})

    return p._main(frame, args)
end

function startswith(target, prefix)
	return string.sub(target, 1, string.len(prefix)) == prefix
end

function p._main(frame, args)
	if not args[1] then
		error("A parameter prefix to use was not found.")
	end
	
	local prefix = args[1]
	local toEnumerate = {}
	
	for param,_ in ipairs(args) do
		if startswith(param, prefix) then
			if args[2] and not endswith(param, args[2]) then
				toEnumerate.insert(param);
			elseif args[1] == nil then
				toEnumerate.insert(param);
			end
		end
	end
	
	local finalOutput = ""
	
	for param in toEnumerate do
		finalOutput = frame:preprocess(
			(yesno(args["ordered"]) and "#" or "*") .. args[param] .. "\n"
		)
	end
	
	return finalOutput
end

return p