Jump to content

Module:Separated entries

Permanently protected module
From Meta, a Wikimedia project coordination wiki
Module documentation
-- This module takes positional parameters as input and concatenates them with
-- an optional separator. The final separator (the "conjunction") can be
-- specified independently, enabling natural-language lists like
-- "foo, bar, baz and qux". The starting parameter can also be specified.

local compressSparseArray = require('Module:TableTools').compressSparseArray
local p = {}

function p._main(args)
	local separator = args.separator
		-- Decode (convert to Unicode) HTML escape sequences, such as " " for space.
		and mw.text.decode(args.separator) or ''
	local conjunction = args.conjunction and mw.text.decode(args.conjunction) or separator
	-- Discard values before the starting parameter.
	local start = tonumber(args.start)
	if start then
		for i = 1, start - 1 do args[i] = nil end
	end
	-- Discard named parameters.
	local values = compressSparseArray(args)
	return mw.text.listToText(values, separator, conjunction)
end

local function localize(message, lang)
	if type(message) == 'table' and type(message.inLanguage) == 'function' then
		if type(lang) == 'string' and lang ~= '' then
			message = message:inLanguage(lang)
		end
		return message:plain()
	else
		return message
	end
end

local function makeInvokeFunction(separator, conjunction, first)
	return function (frame)
		local args = require('Module:Arguments').getArgs(frame)
		args.separator = localize(separator or args.separator, args.lang)
		args.conjunction = localize(conjunction or args.conjunction, args.lang)
		args.first = first or args.first
		return p._main(args)
	end
end

p.main = makeInvokeFunction()
p.br = makeInvokeFunction('<br />')
p.comma = makeInvokeFunction(mw.message.new('comma-separator'))

return p