Jump to content

Module:Reply to/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
allow non-sequential parameters to maintain their order
improve speed
Line 17: Line 17:
end
end
for k = 1, maxArg do
for k = 1, maxArg do
if mw.ustring.match(args[k] or '','%S') then
if args[k] and mw.ustring.match(args[k],'%S') then
local title = mw.title.new(args[k])
local title = mw.title.new(args[k])
if not title then return makeError('Input contains forbidden characters.') end
if not title then return makeError('Input contains forbidden characters.') end

Revision as of 14:06, 31 March 2022

local p = {}

local function makeError(msg)
	msg ='Error in [[Template:Reply to]]: ' .. msg
	return mw.text.tag('strong', {['class']='error'}, msg)
end

function p.replyto(frame)
	local args = frame:getParent().args
	local outArray, outStr = {}, ""
	args.label1 = args.label1 or args.label
	local maxArg = 0
	for k, _ in pairs(args) do
		if type(k) == 'number' and k > maxArg then
			maxArg=k
		end
	end
	for k = 1, maxArg do
		if args[k] and mw.ustring.match(args[k],'%S') then
			local title = mw.title.new(args[k])
			if not title then return makeError('Input contains forbidden characters.') end
			title = title.rootText
			local label = args['label'..tostring(k)]
			if label == '' then 
				label = '​'
			end
			table.insert(outArray, '[[User:' .. title .. '|' .. (label or title) .. ']]')
		end
	end

	if #outArray > (tonumber(frame.args.max) or 50) then
		return makeError('More than ' .. tostring(frame.args.max or 50) .. ' names specified.')
	else
		if #outArray < 1 then
			if frame.args.example then args[1] = frame.args.example else return makeError('Username not given.') end
		end
		if args.c == '' then
			outStr = table.concat(outArray, ", ")
		else
			outStr = mw.text.listToText(outArray, ', ', (#outArray == 2 and ' ' or ', ') .. (args.c or 'and') .. ' ')
		end
		return mw.text.tag('span', {['class']='template-ping'}, (args.prefix or '@') .. outStr .. (args.p or ':'))
	end
end

return p