24,539
ARTICLES
ON THIS WIKI
ON THIS WIKI
Module:For
Documentation for this module may be created at Module:For/doc
local p = {}
function esc(x)
return (x:gsub('%%', '%%%%')
:gsub('%^', '%%%^')
:gsub('%$', '%%%$')
:gsub('%(', '%%%(')
:gsub('%)', '%%%)')
:gsub('%.', '%%%.')
:gsub('%[', '%%%[')
:gsub('%]', '%%%]')
:gsub('%*', '%%%*')
:gsub('%+', '%%%+')
:gsub('%-', '%%%-')
:gsub('%?', '%%%?'))
end
-- Repeats the second parateter the first parameter number of times.
-- This is a stop-gap for the Extensions:LoopFunctions, #for
function p.loop(frame)
local iters = frame.args[1] -- How many times to loop
local s = frame.args[2] -- What to loop
local pattern = frame.args[3] or "$n$" -- What to replace with the iteration count
-- Escape the pattern to work with gsub
pattern = esc(pattern)
local ret = ""
if tonumber(iters) and s then
for x=1,iters do
ret = ret .. string.gsub(s, pattern, x)
end
end
return ret
end
-- Iterates through the arguments passed and uses a string to format them
-- This is a stop-gap for the Extensions:Loops, #forargs
function p.forArgs(frame)
local prefix = frame.args.prefix or 'var'
local keyStr = frame.args.key or '$key$'
local valStr = frame.args.value or '$val$'
local block = frame.args.block or ''
local stripPrefix = frame.args.stripPrefix or false
-- Escape patterns
prefix = '^' .. esc(prefix)
keyStr = esc(keyStr)
valStr = esc(valStr)
local pLen = prefix:len()
-- Get the parent frame, this is where all the arguments are
local parent = frame:getParent()
-- Iterate through all the variables in the parent
local ret = ''
for k,v in pairs(parent.args) do
-- Check if this is one we should handle
if string.find(k, prefix) then
-- Remove the prefix if needed
if stripPrefix then
k = k:sub(pLen)
end
-- Replace key
local str = string.gsub(block, keyStr, k)
-- Replace value
str = string.gsub(str, valStr, v)
-- Concatenate
ret = ret .. str
end
end
return ret
end
-- Iterates through the numbered arguments passed and uses a string to format them
-- This is a stop-gap for the Extensions:Loops, #fornumargs
function p.forNumArgs(frame)
local keyStr = frame.args.key or '$key$'
local valStr = frame.args.value or '$val$'
local block = frame.args.block or ''
local stripSpace = frame.args.strip
stripSpace = stripSpace == 'Yes' or stripSpace == '1' or stripSpace == 'True'
-- Escape patterns
keyStr = esc(keyStr)
valStr = esc(valStr)
-- Get the parent frame, this is where all the arguments are
local parent = frame:getParent()
-- Iterate through all the variables in the parent
local ret = ''
for i,v in ipairs(parent.args) do
-- Replace key
local str = string.gsub(block, keyStr, i)
local value = v
-- Unnamed parameters do not strip the spaces
if stripSpace then
value = mw.text.trim(value)
end
-- Replace value
str = string.gsub(str, valStr, value)
-- Concatenate
ret = ret .. str
end
return ret
end
return p
View All FTB Twitter Feed
Discussion
To discuss the topics on this wiki, you can visit our community forums!