Jump to content

Module:Reply to/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
experimental edit to discuss at Template talk:Reply to#Rewrite; revert when done
restore ability to skip nil, fix example handling
Line 4: Line 4:
msg ='Error in [[Template:Reply to]]: ' .. msg
msg ='Error in [[Template:Reply to]]: ' .. msg
return mw.text.tag('strong', {['class']='error'}, msg)
return mw.text.tag('strong', {['class']='error'}, msg)
end

local function makeLink(u, l)
return '[[User:' .. u .. '|' .. (l or u) .. ']]'
end
end


function p.replyto(frame)
function p.replyto(frame)
local args = frame:getParent().args
local args = frame:getParent().args
local outArray, outStr = {}, ""
local outArray = {}
args.label1 = args.label1 or args.label
args.label1 = args.label1 or args.label


local argIndex = {}
for i, v in ipairs(args) do
for k, _ in pairs(args) do
if string.match(v,'%S') then
if type(k) == 'number' 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(i)]
local label = args['label'..tostring(k)]
if label == '' then
if label == '' then label = '​' end
table.insert( outArray, makeLink(title, label) )
label = '​'
end
table.insert(outArray, '[[User:' .. title .. '|' .. (label or title) .. ']]')
end
end
end
end
Line 28: Line 37:
end
end
if #outArray < 1 then
if #outArray < 1 then
if frame.args.example then args[1] = frame.args.example else return makeError('Username not given.') end
if frame.args.example then
outArray[1] = makeLink(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
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 ':'))
return mw.text.tag('span', {['class']='template-ping'}, (args.prefix or '@') .. outStr .. (args.p or ':'))
end
end

Revision as of 02:16, 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, _ in pairs(args) do
		if type(k) == 'number' then
			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
			title = title.rootText
			local label = args['label'..tostring(k)]
			if label == '' then label = '&#x200B;' end
			table.insert( outArray, makeLink(title, label) )
		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
			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