/mediawiki-1.45.3/resources/assets/site/logo.svg Module:ListToText - wikiNonStop Jump to content

Module:ListToText

From wikiNonStop
Revision as of 09:54, 12 April 2026 by Majestix (talk | contribs) (1 revision imported: Initial Setup)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation

Module Quality

Usage

This module is invoked by Template:ListToText. To invoke this module from another module, use the call function of this module:

local listToText = require( "Module:ListToText" ).call
-- The following line assigns "1, 2, 3 and 4" to txt:
local txt = listToText { "1", "2", "3", "4" }

Parameters to Template:ListToText are also applicable to this call function. See the template documentation to learn more.

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


local function replace( str, pattern, repl )
	while true do
		local s, e = mw.ustring.find( str, pattern, 1, true )
		if s == nil then break end
		str = mw.ustring.sub( str, 1, s - 1 ) .. repl .. mw.ustring.sub( str, e + 1, -1 )
	end
	return str
end


local function int( msg )
	return mw.getCurrentFrame():expandTemplate{ title = "int", args = { msg } }
end


function p.call( args )
	if type( args.fmt ) == "string" then
		for i, v in ipairs( args ) do
			local formatted = replace( args.fmt, "$1", args[i] )
			args[i] = mw.getCurrentFrame():preprocess( formatted )
		end
	end
	
	args.sep = args.sep or int( "comma-separator" )
	if yesno( args.noconj ) then
		args.conj = args.sep
	else
		args.conj = args.conj or ( int( "and" ) .. int( "word-separator" ) )
	end
	
	return mw.text.listToText( args, args.sep, args.conj )
end


function p.main( frame )
	local args = {}
	for k, v in pairs( frame:getParent().args ) do
		args[ k ] = v
	end
	for k, v in pairs( frame.args ) do
		args[ k ] = v
	end
	return p.call( args )
end


return p