Module:Loop

From pronounmail wiki
Revision as of 02:58, 18 May 2026 by Teeth (talk | contribs) (add error handling)
Jump to navigation Jump to search

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

--[[

This module is for generating looped content in templates and pages.

]]

local p = {}

--[[

Usage: {{#invoke|Loop|repi|i|10|number: i}}
]]
function p.repi( frame )
	local index_var = frame.args[1]
	local repetitions = tonumber( frame.args[2] )
	local text = frame.args[3];
	if not index_var then
		return p._error( 'function repi expects a string as first parameter, received "' .. ( frame.args[1] or '' ) .. '"' )
	end
	if not repetitions then
		return p._error( 'function repi expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' )
	end
	if not text then
		return p._error( 'function repi expects a string to repeat as third parameter, received "' .. ( frame.args[2] or '' ) .. '"' )
	end
	str = ''
	for i=1,repetitions do
		str = str .. string.gsub(text, index_var, i)
    end
	return str
end

--[[
Helper function to handle error messages.
]]
function p._error( error_str )
	local frame = mw.getCurrentFrame()
	local error_category = frame.args.error_category or 'Errors reported by Module Loop'
	local ignore_errors = frame.args.ignore_errors or false
	local no_category = frame.args.no_category or false

	if str._getBoolean(ignore_errors) then
		return ''
	end

	local error_str = '<strong class="error">Loop Module Error: ' .. error_str .. '</strong>'
	if error_category ~= '' and not str._getBoolean( no_category ) then
		error_str = '[[Category:' .. error_category .. ']]' .. error_str
	end

	return error_str
end


return p