Jump to content

Module:Reply to/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
restore ability to skip nil, fix example handling
simplify
Line 16: Line 16:


local argIndex = {}
local argIndex = {}
for k, _ in pairs(args) do
for k, v in pairs(args) do
if type(k) == 'number' then
if type(k) == 'number' and string.match(v,'%S') then
table.insert(argIndex, k)
table.insert(argIndex, k)
end
end
Line 23: Line 23:
table.sort(argIndex)
table.sort(argIndex)
for _, k in ipairs(argIndex) do
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
local title = mw.title.new(args[k])
title = title.rootText
if not title then return makeError('Input contains forbidden characters.') end
local label = args['label'..tostring(k)]
title = title.rootText
local label = args['label'..tostring(k)]
if label == '' then label = '​' end
table.insert( outArray, makeLink(title, label) )
if label == '' then label = '​' end
table.insert( outArray, makeLink(title, label) )
end
end
end



Revision as of 02:21, 6 April 2022

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 args = frame:getParent().args
	local outArray = {}
	args.label1 = args.label1 or args.label

	local argIndex = {}
	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