Module:Loop/template

From Liquipedia Dota 2 Wiki

Documentation (view - edit)

Module:Loop/template/doc


local Loop = {}

local function prepareForPreprocess(text)
	return mw.text.decode(mw.text.unstripNoWiki(text))
end

function Loop._loop(variable, fromStr, timesStr, wikitextToRun)
	local frame = mw.getCurrentFrame():getParent()
	frame = frame or mw.getCurrentFrame()

	local output = {}
	local cleanedText = prepareForPreprocess(wikitextToRun)

	local from, times, inc = tonumber(fromStr), tonumber(timesStr), 1
	if times < 0 then
		inc = -inc
	end

	mw.logObject(cleanedText, 'Run On')
	for i = from, from+times-1, inc do
		mw.ext.VariablesLua.vardefine(variable, i)
		table.insert(output, frame:preprocess(cleanedText))
	end
	
	mw.logObject(output, 'Output')

	return table.concat(output)
end

setmetatable(Loop, {
	__index = function(t, k)
		return function(frame)
			return Loop._loop(k, frame.args[1], frame.args[2], frame.args[3])
		end
	end
})

return Loop