Jump to content

Module:Enumerate: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
frameOnly
Line 6: Line 6:
function p.main(frame)
function p.main(frame)
local args = getArgs(frame, {
local args = getArgs(frame, {
frameOnly = true,
trim = true
trim = true
})
})
Line 21: Line 22:


function p._main(frame, args)
function p._main(frame, args)
local prefix = args[1] or ""
local prefix = args[1] or args["prefix"] or ""
local suffix = args[2] or ""
local suffix = args[2] or args["suffix"] or ""
local parentArgs = frame:getParent() and getArgs(frame:getParent(), {
local parentArgs = frame:getParent() and getArgs(frame:getParent(), {
trim = true,
trim = true
removeBlanks = true
}) or args
}) or args
local finalOutput = ""
local finalOutput = ""
Line 33: Line 33:
local current = 1
local current = 1
local searching = true
local searching = true
mw.logObject(args)
mw.logObject(parentArgs)
while searching do
while searching do

Revision as of 15:32, 25 December 2021

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

return p