Jump to content

Module:LinkParser

From ICANNWiki

Documentation for this module may be created at Module:LinkParser/doc

-- Module:LinkParser
-- Parses links inserted by editors in templates, strips them of the protocol part, leading "www.", and any trailing "/", while otherwise printing exactly what was entered to minimize spoofing.

local p = {}

----------------------------------
-- Remove protocol, leading www., and trailing /
----------------------------------
function p.strip(link)
	-- Remove the protocol part: http://, https://, ftp://, etc.
	link = link:gsub("^%a+://", "")
	-- Remove leading "www."
	link = link:gsub("^www%.", "")
	-- Remove trailing "/"
	link = link:gsub("/$", "")
	return link
end

----------------------------------
-- Return a MediaWiki link with:
--   [ originalURL  strippedDisplayText ]
----------------------------------
function p.render(frame)
	local args = frame:getParent().args
	local rawLink = args["link"] or ""
	if rawLink == "" then
		return ""
	end

	-- Strip protocol, leading 'www.', and trailing '/'
	local displayText = p.strip(rawLink)

	-- Construct an external link of the form:
	--   [ rawLink displayText ]
	return string.format("[%s %s]", rawLink, displayText)
end

return p