Module:Separated entries: Difference between revisions

m
16 revisions imported from templatewiki:Module:Separated_entries
(Generic implementation of Module:Br separated entries)
 
m (16 revisions imported from templatewiki:Module:Separated_entries)
 
(15 intermediate revisions by 7 users not shown)
Line 1:
-- This module takes positional parameters as input and concatenates them with
local getArgs = require('Module:Arguments').getArgs
-- 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_main(frameargs)
local argsseparator = getArgs(frame, {args.separator
-- Decode (convert to Unicode) HTML escape sequences, such as " " for space.
trim = true,
and mw.text.decode(args.separator) or ''
removeBlanks = true
local conjunction = origArgsargs.conjunction and mw.text.decode(args.conjunction) or separator
})
-- Discard values before the starting parameter.
return p._main(args)
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(argsvalues, separator, conjunction)
end
function p._main(origArgs)
local separator = origArgs.separator or ''
local conjunction = origArgs.conjunction or separator
 
local function makeInvokeFunction(separator, conjunction, first)
args = {}
return function (frame)
for k, v in pairs(origArgs) do
local getArgsargs = require('Module:Arguments').getArgs(frame)
-- Discard named parameters.
local args.separator = origArgs.separator or ''args.separator
if type(k) == 'number' then
args.conjunction = conjunction or args.conjunction
table.insert(args, v)
args.first = first or args.first
end
return p._main(args)
end
return mw.text.listToText(args, separator, conjunction)
end
 
p.main = makeInvokeFunction()
p.br = makeInvokeFunction('<br />')
p.comma = makeInvokeFunction(mw.message.new('comma-separator'):plain())
 
return p