https://sarkarverse.org/index.php?title=Module:URL&feed=atom&action=historyModule:URL - Revision history2024-03-28T18:52:19ZRevision history for this page on the wikiMediaWiki 1.39.0https://sarkarverse.org/index.php?title=Module:URL&diff=2768&oldid=prevT12: 1 revision2013-10-14T05:58:37Z<p>1 revision</p>
<p><b>New page</b></p><div>--<br />
-- This module implements {{URL}}<br />
--<br />
-- See unit tests at [[Module:URL/tests]]<br />
<br />
local p = {}<br />
<br />
function trim(s)<br />
return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))<br />
end<br />
<br />
function safeUri(s)<br />
local success, uri = pcall(function()<br />
return mw.uri.new(s)<br />
end)<br />
if success then<br />
return uri<br />
end<br />
end<br />
<br />
function p._url(url, text)<br />
url = trim(url or '')<br />
text = trim(text or '')<br />
<br />
if url == '' then<br />
if text == '' then<br />
return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }<br />
else<br />
return text<br />
end<br />
end<br />
<br />
-- If the URL contains any unencoded spaces, encode them, because MediaWiki will otherwise interpret a space as the end of the URL.<br />
url = mw.ustring.gsub(url, '%s', function(s) return mw.uri.encode(s, 'PATH') end)<br />
<br />
-- If there is an empty query string or fragment id, remove it as it will cause mw.uri.new to throw an error<br />
url = mw.ustring.gsub(url, '#$', '')<br />
url = mw.ustring.gsub(url, '%?$', '')<br />
<br />
-- If it's an HTTP[S] URL without the double slash, fix it.<br />
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')<br />
<br />
-- Handle URLs from Wikidata of the format http&#58;//<br />
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?)&#58;//', 'http%1://')<br />
<br />
local uri = safeUri(url)<br />
<br />
-- Handle URL's without a protocol and URL's that are protocol-relative, <br />
-- e.g. www.example.com/foo or www.example.com:8080/foo, and //www.example.com/foo<br />
if uri and (not uri.protocol or (uri.protocol and not uri.host)) and url:sub(1, 2) ~= '//' then<br />
url = 'http://' .. url<br />
uri = safeUri(url)<br />
end<br />
<br />
if text == '' then<br />
if uri then<br />
if uri.path == '/' then uri.path = '' end<br />
<br />
local port = ''<br />
if uri.port then port = ':' .. uri.port end<br />
<br />
text = mw.ustring.lower(uri.host or '') .. port .. (uri.relativePath or '')<br />
else -- URL is badly-formed, so just display whatever was passed in<br />
text = url<br />
end<br />
end<br />
<br />
return mw.ustring.format('<span class="url">[%s %s]</span>', url, text)<br />
end<br />
<br />
function p.url(frame)<br />
local templateArgs = frame.args<br />
local url = templateArgs[1] or ''<br />
local text = templateArgs[2] or ''<br />
return p._url(url, text)<br />
end<br />
<br />
return p</div>T12