Module:For loop: Difference between revisions
From Wayne's Dusty Box of Words
ll>Johnuniq (require Module:Template invocation only if required (see Template talk:Post-nominals#Use of Module:Template_invocation); clean whitespace, variables) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 12:15, 8 February 2022
{{#ifeq:For loop|doc|{{#if:|Template:Pp}}|{{#switch:
{{#if: | | {{#ifeq:Module|Module | module | other }} }}
| module =
{{#switch: release
| pre-alpha | prealpha | pa =
| alpha | a =
| beta | b =
| release | r | general | g =
| protected | protect | p = [[File:{{#switch:Lua error in Module:Effective_protection_level at line 14: attempt to index local 'title' (a nil value).|autoconfirmed=Semi|extendedconfirmed=Extended|accountcreator|templateeditor=Template|#default=Full}}-protection-shackle.svg|40x40px|link=|alt=Protected]]
| semiprotected | semiprotect | semi =
}} | {{#switch: release
| pre-alpha | prealpha | pa = This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | alpha | a = This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | beta | b = This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | release | r | general | g = This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | protected | protect | p = This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | semiprotected | semiprotect | semi = This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is semi-protected from editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | #default = Script error: No such module "Error".}} |
| other | #default = Script error: No such module "Error". }}}} {{#ifeq:For loop|doc|{{#if:|Template:Pp}}|{{#switch:
{{#if: | | {{#ifeq:Module|Module | module | other }} }}
| module =
{{#switch: protected
| pre-alpha | prealpha | pa =
| alpha | a =
| beta | b =
| release | r | general | g =
| protected | protect | p = [[File:{{#switch:Lua error in Module:Effective_protection_level at line 14: attempt to index local 'title' (a nil value).|autoconfirmed=Semi|extendedconfirmed=Extended|accountcreator|templateeditor=Template|#default=Full}}-protection-shackle.svg|40x40px|link=|alt=Protected]]
| semiprotected | semiprotect | semi =
}} | {{#switch: protected
| pre-alpha | prealpha | pa = This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | alpha | a = This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | beta | b = This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | release | r | general | g = This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | protected | protect | p = This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | semiprotected | semiprotect | semi = This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is semi-protected from editing.{{#switch: For loop|doc|sandbox= | {{#ifeq: | true | | }} }} | #default = Script error: No such module "Error".}} |
| other | #default = Script error: No such module "Error". }}}}
This Lua module is used on approximately 576,000 pages, or roughly 62882% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
This module implements Template:Lt. Please see the template page for documentation.
-- This module implements {{for loop}}. local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local 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 calltemplates = yesno(args.substall or "", true) or not mw.isSubsting() 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 = '' 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