Module:PingRFCParticipants

local getArgs = require('Module:Arguments').getArgs
p = {}

--testing. I have no idea why I left this in here, but I did. Fight me.
function p.test(frame)
	local pgCont = mw.title.new("User:MjolnirPants/Voices"):getContent()
	return pgCont
end

--The container which is actually called by the template
function p.PingRFCUsers(frame)
	local args = getArgs(frame)
	return p._PingRFCUsers(args)
end

--The actual function
function p._PingRFCUsers(args)
	--dim the local variables that do not need to be
	--dimmed later (to be initialized from an existing var)
	local pageSource = ""
	local uName = ""
	--cat is the variable which will be returned
	local cat = ""
	
	--check the size of the arguments array
	--if there's more than one, the second one
	--should be the name of the page on which
	--to find the discussion
	if args[2] then
		--get the title from the second argument
		pageSource = mw.title.new(args[2]):getContent()
		cat = "Pinging participants of [[" .. args[2] .. "#" .. args[1] .."|previous discussion]]: {{ping"
	else		
		--If no second parameter was passed, then
		--the discussion is on the current page
		pageSource = mw.title.getCurrentTitle():getContent()
		cat = "Pinging participants of [[#" .. args[1] .."|previous discussion]]: {{ping"
	end
	
	--find the specified discussion in the page and trim the
	--contents to just that discussion
	local zz = 4
	local i, j = string.find(pageSource, "==" .. args[1] .. "==")
	if i == nil then
		i, j = string.find(pageSource, "== " .. args[1] .. " ==")
		zz = 6
	end
	pageSource = string.sub(pageSource, i + string.len(args[1]) + zz)
	--If this is the last discussion on the page, then
	--there's no need to trim the end of the string off
	if string.find(pageSource, "==") then
		i, j = string.find(pageSource, "==")
		pageSource = string.sub(pageSource, 1, i - 1)
	end
	
	--find the first username within that discussion
	--and prime the loop
	i, j = string.find(pageSource, "%[%[User:")
	--prime these to be higher than i to avoid empty
	--strings being returned in edge cases
	local k, l, m, n = i + 1
	
	--Loop through the string, finding usernames and
	--trimming the string down until it can't find
	--any more usernames in it.
	while i ~= nil do
		i, j = string.find(pageSource, "%[%[User:")
		--don't run the code if it didn't find a username this loop
		if j then
			--trim the front of the string off and reset
			--where j points to (the character before the first
			--character in the username)
			pageSource = string.sub(pageSource, j)
			j = 1
			--find the next pipe
			k, l = string.find(pageSource, "|")
			--find the next pair of right square brackets
			m, n = string.find(pageSource, "%]%]")
			--decide which one ends the username, grab the
			--username and trim the string to the end of
			--that name
			if (m > k) then
				uName = string.sub(pageSource, j + 1, k - 1)
				pageSource = string.sub(pageSource, k)
			else
				uName = string.sub(pageSource, j + 1, m - 1)
				pageSource = string.sub(pageSource, m)
			end
			--store the name in the return variable, along
			--with a pipe, but only if that name doesn't
			--already appear in the string with a pipe (so that
			--usernames which are contained in existing names
			--will still get added)
			if not string.find(cat, uName .. "|") then
				cat = cat .. "|" .. uName
			end
		end
	end
	
	--finalize the return variable
	cat = cat .. "}} ~~~~"
	
	--all done
	return cat
end

return p