Module:Sandbox/RexxS/ScanParms

-- Test module to demo scanning of unknown parameters
-- May be two parameters, representing url and text or just the url
-- Parameters may be unnamed or specifically set as 1 and 2
-- Problem arises when an unnamed parameter includes =

local p = {}

p.scan = function(frame)

	if not frame:getParent().args then
		return nil	-- or return "No arguments" if debugging
	end
	
	local keys = {}
	local vals = {}
	local url
	local text
	local ctr = 0
	
	-- scan the parameters passed to see if they contain a period
	for k, v in pairs(frame:getParent().args) do
		k = tostring(k)
		if k:find(".", 1, true)	then
			-- the parameter name has a . in it, so probably like www.xyz.com?a=n
			-- reconstruct parameter and set name to k1 or k2
			ctr = ctr + 1
			vals[#vals+1] = k .. "=" .. v
			keys[#keys+1] = "k" .. ctr
		else
			vals[#vals+1] = v
			keys[#keys+1] = k
		end
	end

	-- now sort out which is url and which is text
	if #keys == 0 then
		return nil -- or "No parameters passed" if debugging
	elseif #keys == 1 then
		-- just a url supplied
		url = vals[1]
		text = vals[1]
	elseif ctr > 1 then
		-- more than one parameter name contained a period, so one is url, other is text
		url = vals[2]
		text = vals[1]
	elseif ctr == 1 then
		-- just one parameter name contained a period, so that's the url
		if keys[1] == "k1" then
			-- it was the first one
			url = vals[1]
			text = vals[2]
		else
			-- it was the second one
			url = vals[2]
			text = vals[1]
		end
	else
		-- either no parameter name had colons or there's more than 2 parameters
		-- so bail out with a reasonable default
		url = vals[1]
		text = vals[2]
	end
	
	return "url=" .. url .. " -- text=" .. text
end

return p