🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/Module:Reply_to/sandbox
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

function p.replyto(frame)
	local origArgs = frame:getParent().args
	local args = {}
	local maxArg = 1
	local usernames = 0
	for k, v in pairs(origArgs) do
		if type(k) == 'number' then
			if v:match('%S') then
				if k > maxArg then maxArg = k end
				usernames = usernames + 1
				local title = mw.title.new(v)
				if not title then return makeError('Input contains forbidden characters.') end
				args[k] = title.rootText
			end
		elseif v == '' and k:sub(1, 5) == 'label' then
			args[k] = '​'
		else
			args[k] = v
		end
	end

	if usernames > (tonumber(frame.args.max) or 50) then
		return makeError(string.format(
			'More than %s names specified.',
			tostring(frame.args.max or 50)
		))
	end
	if usernames < 1 then
		if frame.args.example then args[1] = frame.args.example else return makeError('Username not given.') end
	end
	args['label1'] = args['label1'] or args['label']
	local isfirst = true
	local outStr = args['prefix'] or '@'
	for i = 1, maxArg do
		if args[i] then
			if isfirst then
				isfirst = false
			else
				if ( (usernames > 2) or ((usernames == 2) and (args['c'] == '')) ) then outStr = outStr..', ' end
				if i == maxArg then outStr = outStr..' '..(args['c'] or 'and') .. ' ' end
			end
			local item
			local label = args['label'..tostring(i)] or args[i]
			if args[i]:sub(1, 3) == "~20" then  -- assume temporary account
				item = string.format(
					'[[User:%s|&#x200B;]][[User talk:%s|%s]]',  -- hidden ping and link to user talk
					args[i],
					args[i],
					label
				)
			else
				item = string.format(
					'[[User:%s|%s]]',
					args[i],
					label
				)
			end
			outStr = outStr .. item
		end
	end
	outStr = outStr..(args['p'] or ':')
	return mw.text.tag('span', {['class']='template-ping'}, outStr)
end

return p