Module:Multiformat
Appearance
| This module is rated as alpha. It is ready for limited use and third-party feedback. It may be used on a small number of pages, but should be monitored closely. Suggestions for new features or adjustments to input and output are welcome. |
| Editing of this module by new or unregistered users is currently disabled. See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request, discuss changes on the talk page, request unprotection, log in, or create an account. |
| This module depends on the following other modules: |
| Description | Formats multiple numbered arguments and outputs them in order, etc. |
|---|---|
| Status | Alpha |
| Updated | June 5, 2026 (52 days ago) |
| Dependencies |
Formats multiple numbered arguments and outputs them in order, etc.
This module allows for formatting of multiple numbered arguments together in an output string.
Syntax:
- Many typical Lua character sequences (i.e.
\n) will insert the relevant character into the output. - %ARG% is the argument value
- %ARGNUM% is the argument number'
| Sequence | Result |
|---|---|
\{
|
{
|
\}
|
}
|
\[
|
[
|
\]
|
]
|
\p
|
|
|
\e
|
=
|
\_
|
|
Documentation
Package items
multiformat._all( pargs, cargs )(function)- Main entrypoint for formatting output string with all parameters
- Parameters:
pargsThe arguments from the template call (table)cargsThe arguments from the module call (table)cargs.startAllthe beginning formatting string sequence (string)cargs.endAllthe ending formatting string sequence (string)cargs.formatthe format string (string)cargs.nowikiWhether the arguments are encased in nowiki tags (boolean)cargs.emptyThe message to give if no arguments are specified (string)
- Returns: Output to preprocess (string)
multiformat.all( frame )(function)- Template call
- Parameter:
framecalling frame (Frame) - Returns: Output wikitext (string)
- Usage:
{{#invoke:Multiformat|all|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments and %ARGNAME% to replace with name of argument}} multiformat._named( pargs, cargs )(function)- Main entrypoint for formatting output string with named parameters only
- Parameters:
pargsThe arguments from the template call (table)cargsThe arguments from the module call (table)cargs.startAllthe beginning formatting string sequence (string)cargs.endAllthe ending formatting string sequence (string)cargs.formatthe format string (string)cargs.nowikiWhether the arguments are encased in nowiki tags (boolean)cargs.emptyThe message to give if no arguments are specified (string)
- Returns: Output to preprocess (string)
multiformat.named( frame )(function)- Template call
- Parameter:
framecalling frame (Frame) - Returns: Output wikitext (string)
- Usage:
{{#invoke:Multiformat|named|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments and %ARGNAME% to replace with name of argument}} multiformat._main( pargs, cargs )(function)- Main entrypoint for formatting output string
- Parameters:
pargsThe arguments from the template call (table)cargsThe arguments from the module call (table)cargs.startAllthe beginning formatting string sequence (string)cargs.endAllthe ending formatting string sequence (string)cargs.formatthe format string (string)cargs.firstArgthe first numbered argument to begin formatting (number)cargs.prefixthe argument prefix (string)cargs.suffixthe argument suffix (string)cargs.nowikiWhether the arguments are encased in nowiki tags (boolean)cargs.emptyThe message to give if no arguments are specified (string)
- Returns: Output to preprocess (string)
multiformat.main( frame )(function)- Template call
- Parameter:
framecalling frame (Frame) - Returns: Output wikitext (string)
- Usage:
{{#invoke:Multiformat|main|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments|firstArg=number of first arg}} multiformat._nArgs( pargs, cargs )(function)- Counts the number of arguments which match a particular format
- Parameters:
- Returns: Number of numbered parameters supplied (number)
multiformat.nArgs( frame )(function)- Template call
- Parameter:
framecalling frame (Frame) - Returns: Number of arguments passed (number)
- Usage:
{{#invoke:Multiformat|nArgs}}
Documentation above is auto generated
--- Formats multiple numbered arguments and outputs them in order, etc.
--
-- @module multiformat
-- @alias p
-- @require Module:Escape_input
-- @require Module:Yesno
-- @release alpha
local p = {}
local yn = require("Module:Yesno")
--- Main entrypoint for formatting output string with all parameters
--
-- @param {table} pargs The arguments from the template call
-- @param {table} cargs The arguments from the module call
-- @param {string} cargs.startAll the beginning formatting string sequence
-- @param {string} cargs.endAll the ending formatting string sequence
-- @param {string} cargs.format the format string
-- @param {boolean} cargs.nowiki Whether the arguments are encased in nowiki tags
-- @param {string} cargs.empty The message to give if no arguments are specified
-- @return {string} Output to preprocess
function p._all(pargs, cargs)
local nowiki = yn(cargs.nowiki) or false
local escape
if nowiki then
escape = function( s ) return mw.text.unstripNoWiki( s ) end
else
escape = require("Module:Escape input").escape
end
local startAll = escape(cargs.startAll or '')
local endAll = escape(cargs.endAll or '')
local formatString = escape(cargs.format)
local argArrayFilled = false
local outArr = {}
table.insert(outArr, startAll)
for k,v in pairs(pargs) do
table.insert(outArr, tostring(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(formatString, '%%ARGNAME%%', k), '%%ARG%%', v), '\\%%', '%')))
argArrayFilled = true
end
if not argArrayFilled then return escape(cargs.empty or '') end
table.insert(outArr, endAll)
return table.concat(outArr)
end
--- Template call
--
-- @param {Frame} frame calling frame
-- @return {string} Output wikitext
-- @usage {{#invoke:Multiformat|all|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments and %ARGNAME% to replace with name of argument}}
function p.all(frame)
local pargs = frame:getParent() ~= nil and frame:getParent().args or error("Error occurred while fetching wrapping template arguments for multiformat", 0)
local cargs = frame.args
return frame:preprocess(p.all(pargs, cargs))
end
--- Main entrypoint for formatting output string with named parameters only
--
-- @param {table} pargs The arguments from the template call
-- @param {table} cargs The arguments from the module call
-- @param {string} cargs.startAll the beginning formatting string sequence
-- @param {string} cargs.endAll the ending formatting string sequence
-- @param {string} cargs.format the format string
-- @param {boolean} cargs.nowiki Whether the arguments are encased in nowiki tags
-- @param {string} cargs.empty The message to give if no arguments are specified
-- @return {string} Output to preprocess
function p._named(pargs, cargs)
local nowiki = yn(cargs.nowiki) or false
local escape
if nowiki then
escape = function( s ) return mw.text.unstripNoWiki( s ) end
else
escape = require("Module:Escape input").escape
end
local startAll = escape(cargs.startAll or '')
local endAll = escape(cargs.endAll or '')
local formatString = escape(cargs.format)
local argArrayFilled = false
local outArr = {}
local maxNumArg = p._nArgs(pargs, { nowiki = nowiki })
table.insert(outArr, startAll)
for k,v in pairs(pargs) do
if not (tonumber(k) ~= nil and tonumber(k) >= 1 and tonumber(k) <= maxNumArg) then
table.insert(outArr, tostring(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(formatString, '%%ARGNAME%%', k), '%%ARG%%', v), '\\%%', '%')))
argArrayFilled = true
end
end
if not argArrayFilled then return escape(cargs.empty or '') end
table.insert(outArr, endAll)
return table.concat(outArr)
end
--- Template call
--
-- @param {Frame} frame calling frame
-- @return {string} Output wikitext
-- @usage {{#invoke:Multiformat|named|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments and %ARGNAME% to replace with name of argument}}
function p.named(frame)
local pargs = frame:getParent() ~= nil and frame:getParent().args or error("Error occurred while fetching wrapping template arguments for multiformat", 0)
local cargs = frame.args
return frame:preprocess(p._named(pargs, cargs))
end
--- Main entrypoint for formatting output string
--
-- @param {table} pargs The arguments from the template call
-- @param {table} cargs The arguments from the module call
-- @param {string} cargs.startAll the beginning formatting string sequence
-- @param {string} cargs.endAll the ending formatting string sequence
-- @param {string} cargs.format the format string
-- @param {number} cargs.firstArg the first numbered argument to begin formatting
-- @param {string} cargs.prefix the argument prefix
-- @param {string} cargs.suffix the argument suffix
-- @param {boolean} cargs.nowiki Whether the arguments are encased in nowiki tags
-- @param {string} cargs.empty The message to give if no arguments are specified
-- @return {string} Output to preprocess
function p._main(pargs, cargs)
local nowiki = yn(cargs.nowiki) or false
local escape
if nowiki then
escape = function( s ) return mw.text.unstripNoWiki( s ) end
else
escape = require("Module:Escape input").escape
end
local startAll = escape(cargs.startAll or '')
local endAll = escape(cargs.endAll or '')
local formatString = escape(cargs.format)
local currArg = tonumber(cargs.firstArg) or 1
local argPrefix = escape(cargs.prefix or '')
local argSuffix = escape(cargs.suffix or '')
local outArr = {}
if pargs[argPrefix .. currArg .. argSuffix] == nil then return escape(cargs.empty or '') end
table.insert(outArr, startAll)
while pargs[argPrefix .. currArg .. argSuffix] ~= nil do
table.insert(outArr, tostring(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(formatString, '%%ARGNUM%%', currArg), '%%ARG%%', pargs[argPrefix .. currArg .. argSuffix]), '\\%%', '%')))
currArg = currArg + 1
end
table.insert(outArr, endAll)
return table.concat(outArr)
end
--- Template call
--
-- @param {Frame} frame calling frame
-- @return {string} Output wikitext
-- @usage {{#invoke:Multiformat|main|startAll=start|endAll=end|format=String with %ARG% to replace with current arguments|firstArg=number of first arg}}
function p.main(frame)
local pargs = frame:getParent() ~= nil and frame:getParent().args or error("Error occurred while fetching wrapping template arguments for multiformat", 0)
local cargs = frame.args
return frame:preprocess(p._main(pargs, cargs))
end
--- Counts the number of arguments which match a particular format
--
-- @param {table} pargs The arguments from the template call
-- @param {table} cargs The arguments from the module call
-- @param {number} cargs.firstArg the first numbered argument to begin formatting
-- @param {string} cargs.prefix the argument prefix
-- @param {string} cargs.suffix the argument suffix
-- @param {boolean} cargs.nowiki Whether the arguments are encased in nowiki tags
-- @return {number} Number of numbered parameters supplied
function p._nArgs(pargs, cargs)
local currArg = tonumber(cargs.firstArg) or 1
local nowiki = yn(cargs.nowiki) or false
local escape
if nowiki then
escape = function( s ) return mw.text.unstripNoWiki( s ) end
else
escape = require("Module:Escape input").escape
end
local argPrefix = escape(cargs.prefix or '')
local argSuffix = escape(cargs.suffix or '')
local count = 0
while pargs[argPrefix .. currArg .. argSuffix] ~= nil do
count = count + 1
currArg = currArg + 1
end
return count
end
--- Template call
--
-- @param {Frame} frame calling frame
-- @return {number} Number of arguments passed
-- @usage {{#invoke:Multiformat|nArgs}}
function p.nArgs(frame)
local pargs = frame:getParent() ~= nil and frame:getParent().args or error("Error occurred while fetching wrapping template arguments for multiformat", 0)
local cargs = frame.args
return p._nArgs(pargs, cargs)
end
return p