Module:Foreach

From para.wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Foreach/doc

local p = {}

function p.exec(frame)
	local args = frame:getParent().args
	if args[1] == "" then return "" end
	
	local tpl = args[2]
	local sep = args.sep or "%s*,%s*"
	local join = args.join or ", "
	local final = args.final or join
	local first = tonumber(args.first) or 1
	local last = tonumber(args.last) or 999
	
	local it = mw.text.gsplit(args[1], sep)
	local i = 1
	while i < first do
		it()
		i = i + 1
	end
	
	local out = {}
	for x in it do
		if i > last then
			break
		end
		i = i + 1
		out[#out + 1] = frame:expandTemplate{title=tpl, args={x}}
	end
	
	return mw.text.listToText(out, join, final)
end

return p