Edge Rewrite
// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a21d79f1bb9df8bb

Jump to content

Module:Multiformat

Permanently protected module
From Wikipedia, the free encyclopedia

--- 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