Jump to content

Module:LinkParser: Difference between revisions

Created page with "-- Parses links inserted by editors in templates, strips them of the protocol part, and otherwise prints exactly what was entered to minimize spoofing local p = {} ---------------------------------- -- Remove protocol and leading www. ---------------------------------- function p.strip(link) -- Remove the protocol part: http://, https://, ftp://, etc. -- (%a+ matches one or more letters, so it will strip e.g. http, https, ftp.) link = link:gsub("^%a+://", "") -- Re..."
 
No edit summary
Line 1: Line 1:
-- Parses links inserted by editors in templates, strips them of the protocol part, and otherwise prints exactly what was entered to minimize spoofing
-- 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 = {}
local p = {}


----------------------------------
----------------------------------
-- Remove protocol and leading www.
-- Remove protocol, leading www., and trailing /
----------------------------------
----------------------------------
function p.strip(link)
function p.strip(link)
-- Remove the protocol part: http://, https://, ftp://, etc.
-- Remove the protocol part: http://, https://, ftp://, etc.
-- (%a+ matches one or more letters, so it will strip e.g. http, https, ftp.)
link = link:gsub("^%a+://", "")
link = link:gsub("^%a+://", "")
-- Remove leading "www."
-- Remove leading "www."
link = link:gsub("^www%.", "")
link = link:gsub("^www%.", "")
-- Remove trailing "/"
link = link:gsub("/$", "")
return link
return link
end
end
Line 26: Line 29:
end
end


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



Revision as of 15:38, 9 February 2025

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

-- 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