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 20:18, 14 April 2021 (Handle cases where for some reason someone doesn't invoke this in the template and instead invokes it directly, ultimately ruining the purpose of this module.). 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 getArgs = require('Module:Arguments').getArgs
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 mw.ustring.sub(target, 1, mw.ustring.len(prefix)) == prefix
end

function endswith(target, suffix)
	return mw.ustring.sub(target, -mw.ustring.len(suffix), -1) == suffix
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 suffix = args[2] or ""
	local parentArgs = frame:getParent() and getArgs(frame:getParent(), {
		trim = true,
		removeBlanks = false
	}) or args
	local finalOutput = ""
	
	local list = mw.html.create(yesno(args["ordered"]) and "ol" or "ul")
	
	local current = 1
	local searching = true
	
	mw.logObject(args)
	mw.logObject(parentArgs)
	
	while searching do
	    if parentArgs[prefix .. tostring(current) .. suffix] then
	    	mw.log(prefix .. tostring(current) .. suffix)
	    	mw.log(parentArgs[prefix .. tostring(current) .. suffix])
	    	list:node(
	    		mw.html.create("li")
	    		    :wikitext(parentArgs[prefix .. tostring(current) .. suffix])
    		)
	    	current = current + 1
	   	else
	   		searching = false
   		end
	end
	
	return current == 1 and "" or tostring(list)
end

return p