Module:Enumerate

MyWikiBiz, Author Your Legacy — Wednesday December 25, 2024
Jump to navigationJump to search

Template:Module rating

Usage

This module is meant to be invoked inside of a template, since it requires the argument list of the calling template.

This module allows the enumeration of a set of parameters with a given prefix. If the prefix was notes, the parameters notes1, notes2, notes3, and so on will be enumerated in a unordered list until it sees the first null parameter.

The prefix is defined by the first argument passed into the invocation.

Template:X35
{{#invoke:{{BASEPAGENAME}}|main|notes}}
Any page
{{X35
| notes1 = Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
| notes2 = Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
| notes3 = Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
| notes4 = Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
}}
Output
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  • Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  • Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  • Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

The entries will cut off in the event that the parameters are out of order.

Template:X35
{{#invoke:{{BASEPAGENAME}}|main|notes}}
Any page
{{X35
| notes1 = Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
| notes2 = Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
| notes3 = 
| notes4 = Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
}}
Output
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  • Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Do note that Template:T is sandbox template and does not actually call this module.

Examples



-- 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 = true
	})

    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 = true
	}) 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