Jump to content

Module:Reply to/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
remove unneeded check
experimental edit to discuss at Template talk:Reply to#Rewrite; revert when done
Line 10: Line 10:
local outArray, outStr = {}, ""
local outArray, outStr = {}, ""
args.label1 = args.label1 or args.label
args.label1 = args.label1 or args.label

local argIndex = {}
for k, _ in pairs(args) do
for i, v in ipairs(args) do
if type(k) == 'number' then
if string.match(v,'%S') then
local title = mw.title.new(v)
table.insert(argIndex, k)
end
end
table.sort(argIndex)
for _, k in ipairs(argIndex) do
if string.match(args[k],'%S') then
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
title = title.rootText
title = title.rootText
local label = args['label'..tostring(k)]
local label = args['label'..tostring(i)]
if label == '' then
if label == '' then
label = '​'
label = '​'
end
end
Line 32: Line 26:
if #outArray > (tonumber(frame.args.max) or 50) then
if #outArray > (tonumber(frame.args.max) or 50) then
return makeError('More than ' .. tostring(frame.args.max or 50) .. ' names specified.')
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
else
outStr = mw.text.listToText(outArray, ', ', (#outArray == 2 and ' ' or ', ') .. (args.c or 'and') .. ' ')
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 mw.text.tag('span', {['class']='template-ping'}, (args.prefix or '@') .. outStr .. (args.p or ':'))
end
end



Revision as of 02:05, 1 April 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

	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 = '&#x200B;'
			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