Module:For loop: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius
m Protected Module:ForLoop: High-risk Lua module ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))
meta>Mr. Stradivarius
add User:Frietjes's fix for positional arguments specified with pv, switch to Module:Arguments, don't use a global args table, reorder the functions to make more sense, and switch indentation to tabs
Line 1: Line 1:
-- This module implements {{for loop}}.
local getArgs = require('Module:Arguments').getArgs
p = {}
p = {}
 
local args
function p.main(frame)
local args = getArgs(frame, {
local function callTemplate(template, targs)
trim = false,
    return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
removeBlanks = false
})
return p._main(args)
end
end
 
local function getArgNums(prefix, suffix)
function p._main(args)
    -- Returns a table containing the numbers of the arguments that exist
local template = args['call'] or 'void'
    -- for the specified prefix and suffix.
local variableParam = args.pv
    local nums = {}
variableParam = tonumber(variableParam) or variableParam or 1 -- fix for positional parameters
    for k, v in pairs(args) do
local variableValPrefix = args.prefix or ''
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)' .. suffix .. '$')
local variableValPostfix = args.postfix or ''
        if num then table.insert(nums, tonumber(num)) end
local sep = args[1] or ''
    end
local constantArgs = p.getConstants(args)
    table.sort(nums)
local variableVals = p.getVariableVals(args)
    return nums
 
local result = ''
for i, v in ipairs(variableVals) do
v = mw.text.trim(v) -- trim whitespace
local targs = constantArgs
targs[variableParam] = variableValPrefix .. v .. variableValPostfix
local expandedTemplate = p.callTemplate(template, targs)
result = result .. expandedTemplate
if variableVals[i + 1] then
result = result .. sep
end
end
return result
end
end
 
local function getConstants()
function p.getConstants(args)
    local constantArgNums = getArgNums('pc', 'n')
local constantArgNums = p.getArgNums(args, 'pc', 'n')
    local constantArgs = {}
local constantArgs = {}
    for _, v in ipairs(constantArgNums) do
for _, num in ipairs(constantArgNums) do
        local keyArgName = 'pc' .. tostring(v) .. 'n'
local keyArg = 'pc' .. tostring(num) .. 'n'
        local valArgName = 'pc' .. tostring(v) .. 'v'
local valArg = 'pc' .. tostring(num) .. 'v'
        constantArgs[args[keyArgName]] = args[valArgName]
local key = args[keyArg]
    end
key = tonumber(key) or key
    return constantArgs
local value = args[valArg]
constantArgs[key] = value
end
return constantArgs
end
end
 
local function getVariableVals()
function p.getVariableVals(args)
    local variableVals = {}
local variableVals = {}
    for i, v in ipairs(args) do
for i, v in ipairs(args) do
        if i ~= 1 then
if i ~= 1 then
            variableVals[i - 1] = v
variableVals[i - 1] = v
        end
end
    end
end
    return variableVals
return variableVals
end
end
 
local function _main()
function p.getArgNums(args, prefix, suffix)
    local template = args['call'] or 'void'
-- Returns a table containing the numbers of the arguments that exist
    local variableParam = args.pv or 1
-- for the specified prefix and suffix.
    local variableValPrefix = args.prefix or ''
local nums = {}
    local variableValPostfix = args.postfix or ''
local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'
    local sep = args[1] or ''
for k, v in pairs(args) do
    local constantArgs = getConstants()
local num = tostring(k):match(pattern)
    local variableVals = getVariableVals()
if num then
nums[#nums + 1] = tonumber(num)
    local result = ''
end
    for i, v in ipairs(variableVals) do
end
        v = mw.text.trim(v) -- trim whitespace
table.sort(nums)
        local targs = constantArgs
return nums
        targs[variableParam] = variableValPrefix .. v .. variableValPostfix
        result = result .. tostring(callTemplate(template, targs))
        if variableVals[i + 1] then
            result = result .. sep
        end
    end
    return result
end
end
 
function p.main(frame)
function p.callTemplate(template, targs)
    -- If called via #invoke, use the args passed into the invoking template.
return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
        for k, v in pairs(frame.args) do
            args = frame.args
            break
        end
    else
        args = frame
    end
    return _main()
end
end
 
return p
return p

Revision as of 03:33, 18 January 2014

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

-- This module implements {{for loop}}.

local getArgs = require('Module:Arguments').getArgs

p = {}

function p.main(frame)
	local args = getArgs(frame, {
		trim = false,
		removeBlanks = false
	})
	return p._main(args)
end

function p._main(args)
	local template = args['call'] or 'void'
	local variableParam = args.pv
	variableParam = tonumber(variableParam) or variableParam or 1 -- fix for positional parameters
	local variableValPrefix = args.prefix or ''
	local variableValPostfix = args.postfix or ''
	local sep = args[1] or ''
	local constantArgs = p.getConstants(args)
	local variableVals = p.getVariableVals(args)

	local result = ''
	for i, v in ipairs(variableVals) do
		v = mw.text.trim(v) -- trim whitespace
		local targs = constantArgs
		targs[variableParam] = variableValPrefix .. v .. variableValPostfix
		local expandedTemplate = p.callTemplate(template, targs)
		result = result .. expandedTemplate
		if variableVals[i + 1] then
			result = result .. sep
		end
	end
	return result
end

function p.getConstants(args)
	local constantArgNums = p.getArgNums(args, 'pc', 'n')
	local constantArgs = {}
	for _, num in ipairs(constantArgNums) do
		local keyArg = 'pc' .. tostring(num) .. 'n'
		local valArg = 'pc' .. tostring(num) .. 'v'
		local key = args[keyArg]
		key = tonumber(key) or key
		local value = args[valArg]
		constantArgs[key] = value
	end
	return constantArgs
end

function p.getVariableVals(args)
	local variableVals = {}
	for i, v in ipairs(args) do
		if i ~= 1 then
			variableVals[i - 1] = v
		end
	end
	return variableVals
end

function p.getArgNums(args, prefix, suffix)
	-- Returns a table containing the numbers of the arguments that exist
	-- for the specified prefix and suffix.
	local nums = {}
	local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'
	for k, v in pairs(args) do
		local num = tostring(k):match(pattern)
		if num then
			nums[#nums + 1] = tonumber(num)
		end
	end
	table.sort(nums)
	return nums
end

function p.callTemplate(template, targs)
	return mw.getCurrentFrame():expandTemplate{title = template, args = targs}
end

return p