Module:Multiformat

--- Formats multiple numbered arguments and outputs them in order, etc.
--
-- @module multiformat
-- @alias p
-- @require Module:Escape_input
-- @release alpha

local p = {}
local yn = require("Module:Yesno")

--- 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 module is encased in nowiki tags
-- @param {string} cargs.empty The message to give if no arguments are specified
-- @return 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 Output wikitext
-- @usage {{#invoke:Multiformat|main|startAll=start|endAll=end|formatString=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.prefix the argument suffix
-- @return 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 Output wikitext
-- @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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.