Jump to content

Module:Reply to/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 18:24, 30 March 2022 (simplify). 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 k, v in pairs(args) do
		if type(k) == 'number' and mw.ustring.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(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