Module:Separated entries: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
m (Vinifera7 moved page Module:Concat to Module:Separated entries without leaving a redirect)
No edit summary
Line 1: Line 1:
-- 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 = {}
local p = {}
local getArgs


function p.main(frame)
function p._main(args)
if not getArgs then
local separator = args.separator
getArgs = require('Module:Arguments').getArgs
-- 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
end
local args = getArgs(frame, {
-- Discard named parameters.
wrappers = 'Template:Concat',
local values = compressSparseArray(args)
trim = false
return mw.text.listToText(values, separator, conjunction)
})
return p._main(args, args.sep)
end
end


function p._main(args, sep)
local function makeInvokeFunction(separator, conjunction, first)
args = args or {}
return function (frame)
sep = sep or ', '
local args = require('Module:Arguments').getArgs(frame)
local iargs = {}
args.separator = separator or args.separator
for k, v in pairs(args) do
args.conjunction = conjunction or args.conjunction
if type(k) == 'number' and k >= 1 and math.floor(k) == k and string.find(v, '%S') then
args.first = first or args.first
table.insert(iargs, k)
return p._main(args)
end
end
end
table.sort(iargs)
for i, v in ipairs(iargs) do
iargs[i] = mw.text.trim(args[v])
end
return table.concat(iargs, sep)
end
end
p.main = makeInvokeFunction()
p.br = makeInvokeFunction('<br />')
p.comma = makeInvokeFunction(mw.message.new('comma-separator'):plain())


return p
return p

Revision as of 21:47, 23 June 2021

Module documentation[view] [edit] [history] [purge]


Lua logo

This module depends on the following other modules:

This module was adapted from Module:Separated entries on Wikipedia.
Adaptation is noted for reference and attribution only. This module may differ from the original in function or in usage. The documentation on Wikipedia may be helpful in understanding this module.

-- 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 "&#32;" 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 makeInvokeFunction(separator, conjunction, first)
	return function (frame)
		local args = require('Module:Arguments').getArgs(frame)
		args.separator = separator or args.separator
		args.conjunction = conjunction or args.conjunction
		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'):plain())

return p