Jump to content

Module:Reply to/sandbox

From Wikipedia, the free encyclopedia
local p = {}

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

local function makeLink(u, l)
	return '[[User:' .. u .. '|' .. (l or u) .. ']]'
end

function p.replyto(frame)
	local outArray, argIndex = {}, {}
	local args = frame:getParent().args
	args.label1 = args.label1 or args.label

	for k, v in pairs(args) do
		if type(k) == 'number' and string.match(v,'%S') then
			table.insert(argIndex, k)
		end
	end
	table.sort(argIndex)
	for _, k in ipairs(argIndex) do
		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, makeLink(title, label) )
	end

	if #outArray > (tonumber(frame.args.max) or 50) then
		return makeError('More than ' .. tostring(frame.args.max or 50) .. ' names specified.')
	end
	if #outArray < 1 then
		if frame.args.example then
			outArray[1] = makeLink(frame.args.example)
		else
			return makeError('Username not given.')
		end
	end

	local conjuction = (#outArray == 2 and ' ' or ', ') .. (args.c or 'and') .. ' '
	local outStr = (args.c == '') and table.concat(outArray, ', ') or mw.text.listToText(outArray, ', ', conjuction)
	
	return mw.text.tag('span', {['class']='template-ping'}, (args.prefix or '@') .. outStr .. (args.p or ':'))
end

return p