Jump to content

Module:Reply to/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 02:05, 1 April 2022 (experimental edit to discuss at Template talk:Reply to#Rewrite; revert when done). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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

	for i, v in ipairs(args) do
		if string.match(v,'%S') then
			local title = mw.title.new(v)
			if not title then return makeError('Input contains forbidden characters.') end
			title = title.rootText
			local label = args['label'..tostring(i)]
			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.')
	end
	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

return p