Module:URL

From WikiProjectMed
Jump to navigation Jump to search

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

  1 --
  2 -- This module implements {{URL}}
  3 --
  4 -- See unit tests at [[Module:URL/testcases]]
  5 
  6 local p = {}
  7  
  8 local function safeUri(s)
  9 	local success, uri = pcall(function()
 10 		return mw.uri.new(s)
 11 	end)
 12 	if success then
 13 		return uri
 14 	end
 15 end
 16 
 17 local function extractUrl(args)
 18 	for name, val in pairs(args) do
 19 		if name ~= 2 and name ~= "msg" then
 20 			local url = name .. "=" .. val;
 21 			url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
 22 			local uri = safeUri(url);
 23 			if uri and uri.host then
 24 				return url
 25 			end
 26 		end
 27 	end
 28 end
 29 
 30 function p._url(url, text, msg)
 31 	url = mw.text.trim(url or '')
 32 	text = mw.text.trim(text or '')
 33 	local nomsg = (msg or ''):sub(1,1):lower() == "n" or msg == 'false' -- boolean: true if msg is "false" or starts with n or N
 34 
 35 	if url == '' then
 36 		if text == '' then
 37 			if nomsg then
 38 				return nil
 39 			else
 40 				return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }
 41 			end
 42 		else
 43 			return text
 44 		end
 45 	end
 46 
 47 	-- If the URL contains any unencoded spaces, encode them, because MediaWiki will otherwise interpret a space as the end of the URL.
 48 	url = mw.ustring.gsub(url, '%s', function(s) return mw.uri.encode(s, 'PATH') end)
 49 
 50 	-- If there is an empty query string or fragment id, remove it as it will cause mw.uri.new to throw an error
 51 	url = mw.ustring.gsub(url, '#$', '')
 52 	url = mw.ustring.gsub(url, '%?$', '')
 53 
 54 	-- If it's an HTTP[S] URL without the double slash, fix it.
 55 	url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
 56 
 57 	-- Handle URLs from Wikidata of the format http://
 58 	url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?)://', 'http%1://')
 59 
 60 	local uri = safeUri(url)
 61 
 62 	-- Handle URL's without a protocol and URL's that are protocol-relative, 
 63 	-- e.g. www.example.com/foo or www.example.com:8080/foo, and //www.example.com/foo
 64 	if uri and (not uri.protocol or (uri.protocol and not uri.host)) and url:sub(1, 2) ~= '//' then
 65 		url = 'http://' .. url
 66 		uri = safeUri(url)
 67 	end
 68 
 69 	if text == '' then
 70 		if uri then
 71 			if uri.path == '/' then uri.path = '' end
 72 
 73 			local port = ''
 74 			if uri.port then port = ':' .. uri.port end
 75 
 76 			text = mw.ustring.lower(uri.host or '') .. port .. (uri.relativePath or '')
 77 
 78 			-- Add <wbr> before _/.-# sequences
 79 			text = mw.ustring.gsub(text,"(/+)","<wbr/>%1")      -- This entry MUST be the first. "<wbr/>" has a "/" in it, you know.
 80 			text = mw.ustring.gsub(text,"(%.+)","<wbr/>%1")
 81 			-- text = mw.ustring.gsub(text,"(%-+)","<wbr/>%1") 	-- DISABLED for now
 82 			text = mw.ustring.gsub(text,"(%#+)","<wbr/>%1")
 83 			text = mw.ustring.gsub(text,"(_+)","<wbr/>%1")
 84 		else -- URL is badly-formed, so just display whatever was passed in
 85 			text = url
 86 		end
 87 	end
 88 
 89 	return mw.ustring.format('<span class="url">[%s %s]</span>', url, text)
 90 end
 91 
 92 --[[
 93 The main entry point for calling from Template:URL.
 94 --]]
 95 function p.url(frame)
 96 	local templateArgs = frame.args
 97 	local parentArgs = frame:getParent().args
 98 	local url = templateArgs[1] or parentArgs[1]
 99 	local text = templateArgs[2] or parentArgs[2] or ''
100 	local msg = templateArgs.msg or parentArgs.msg or ''
101 	url = url or extractUrl(templateArgs) or extractUrl(parentArgs) or ''
102 	return p._url(url, text, msg)
103 end
104 
105 --[[
106 The entry point for calling from the forked Template:URL2.
107 This function returns no message by default.
108 It strips out wiki-link markup, html tags, and everything after a space.
109 --]]
110 function p.url2(frame)
111 	local templateArgs = frame.args
112 	local parentArgs = frame:getParent().args
113 	local url = templateArgs[1] or parentArgs[1]
114 	local text = templateArgs[2] or parentArgs[2] or ''
115 	-- default to no message
116 	local msg = templateArgs.msg or parentArgs.msg or 'no'
117 	url = url or extractUrl(templateArgs) or extractUrl(parentArgs) or ''
118 	-- if the url came from a Wikidata call, it might have a pen icon appended
119 	-- we want to keep that and add it back at the end.
120 	local u1, penicon = mw.ustring.match( url, "(.*)(&nbsp;<span class='penicon.*)" )
121 	if penicon then url = u1 end
122 	-- strip out html tags and [ ] from url
123 	url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "")
124 	-- truncate anything after a space
125 	url = url:gsub("%%20", " "):gsub(" .*", "")
126 	return (p._url(url, text, msg) or "") .. (penicon or "")
127 end
128 
129 return p