Jump to content

Module:For: Difference between revisions

From Philosophical Research
copy from mediawiki.org page "Module:For", time 2024-06-15T04:30Z
 
m template title
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local export = {}
-- This module implements the {{For loop}} template.
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}


function export.render(frame)
function p.templatemain(frame)
local for_args = frame:getParent().args
  return p.main(frame:newChild{title = "Template:For"})
end


local tname = for_args.call or "1x"
function p.main(frame)
local sep = for_args[1]
  local args = getArgs(frame, {
local pv = for_args.pv or "1"
    trim = false,
local prefix = for_args.prefix or ""
    removeBlanks = false
local suffix = for_args.suffix or for_args.postfix or ""
  })
local pc = {}
  return p._main(args)
end


local output = {}
function p._main(args)
  local template = args['call'] or 'void'
  local calltemplates = yesno(args.substall or "", true) or not mw.isSubsting()
  local variableParam = args.pv
  -- Fix for positional parameters.
  variableParam = tonumber(variableParam) or variableParam or 1
  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 i = 1
  local result = ''
while for_args["pc" .. i .. "n"] do
  local addSeparator = false;
local pnam = for_args["pc" .. i .. "n"]
  for _, v in ipairs(variableVals) do
local pval = for_args["pc" .. i .. "v"]
    v = mw.text.trim(v)
pc[pnam] = pval
    if #v > 0 or not yesno(args.skipBlanks) then
i = i + 1
      if addSeparator then
end
        result = result .. sep
      end
      addSeparator = true;
      local targs = constantArgs
      targs[variableParam] = variableValPrefix .. v .. variableValPostfix
      if calltemplates then
        local output = p.callTemplate(template, targs)
        if #mw.text.trim(output) == 0 then
          addSeparator = false
        end
        result = result .. output
      else
        local makeTemplate = require('Module:Template invocation').invocation
        result = result .. makeTemplate(template, targs)
      end
    end
  end
  return result
end


i = 2
function p.getConstants(args)
while for_args[i] do
  local constantArgNums = p.getArgNums(args, 'pc', 'n')
local args = mw.clone(pc)
  local constantArgs = {}
args[pv] = prefix .. for_args[i] .. suffix
  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


if output[1] then
function p.getVariableVals(args)
output[#output + 1] = sep
  local variableVals = {}
end
  if args.start or args.stop or args.by then
output[#output + 1] = frame:expandTemplate {
    if args[2] then
title = tname,
      error("Both start/stop/by and numbered parameters specified")
args = args
    end
}
    local start = tonumber(args.start or 1)
i = i + 1
    local stop = tonumber(args.stop or 1)
end
    local by = tonumber(args.by or 1)
    for i = start, stop, by do
      variableVals [#variableVals + 1] = i
    end
  else
    for i, v in ipairs(args) do
      if i ~= 1 then
        variableVals[i - 1] = v
      end
    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, _ 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


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


return export
return p

Latest revision as of 09:46, 21 January 2026

Template parameters[edit]

  • 1 - separator to put between template calls
  • pv - parameter to iterate over
  • start - minimum value to start the loop at
  • stop - final value to end the loop at
  • by - number to increment by
  • call - Template to call on each run of the loop
  • pcXn,pcXv - parameters to supply to loop

-- This module implements the {{For loop}} template.
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}

function p.templatemain(frame)
  return p.main(frame:newChild{title = "Template:For"})
end

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 calltemplates = yesno(args.substall or "", true) or not mw.isSubsting()
  local variableParam = args.pv
  -- Fix for positional parameters.
  variableParam = tonumber(variableParam) or variableParam or 1
  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 = ''
  local addSeparator = false;
  for _, v in ipairs(variableVals) do
    v = mw.text.trim(v)
    if #v > 0 or not yesno(args.skipBlanks) then
      if addSeparator then
        result = result .. sep
      end
      addSeparator = true;
      local targs = constantArgs
      targs[variableParam] = variableValPrefix .. v .. variableValPostfix
      if calltemplates then
        local output = p.callTemplate(template, targs)
        if #mw.text.trim(output) == 0 then
          addSeparator = false
        end
        result = result .. output
      else
        local makeTemplate = require('Module:Template invocation').invocation
        result = result .. makeTemplate(template, targs)
      end
    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 = {}
  if args.start or args.stop or args.by then
    if args[2] then
      error("Both start/stop/by and numbered parameters specified")
    end
    local start = tonumber(args.start or 1)
    local stop = tonumber(args.stop or 1)
    local by = tonumber(args.by or 1)
    for i = start, stop, by do
      variableVals [#variableVals + 1] = i
    end
  else
    for i, v in ipairs(args) do
      if i ~= 1 then
        variableVals[i - 1] = v
      end
    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, _ 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