https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AURL%2Fsandbox-samecaseModule:URL/sandbox-samecase - Revision history2025-05-25T03:51:25ZRevision history for this page on the wikiMediaWiki 1.45.0-wmf.2https://en.wikipedia.org/w/index.php?title=Module:URL/sandbox-samecase&diff=884323815&oldid=prevRexxS: create alternate sandbox to keep the version that retains case of url per discussion at Template talk:URL #Displaying uppercase letters within URL?2019-02-20T22:35:44Z<p>create alternate sandbox to keep the version that retains case of url per discussion at <a href="/wiki/Template_talk:URL#Displaying_uppercase_letters_within_URL?" title="Template talk:URL">Template talk:URL #Displaying uppercase letters within URL?</a></p>
<p><b>New page</b></p><div>--<br />
-- This module implements {{URL}} but does not force the url into lowercase<br />
--<br />
-- See unit tests at [[Module:URL/tests]]<br />
<br />
local p = {}<br />
<br />
local 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 />
local function extractUrl(args)<br />
for name, val in pairs(args) do<br />
local url = name .. "=" .. val;<br />
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')<br />
local uri = safeUri(url);<br />
if uri and uri.host then<br />
return url<br />
end<br />
end<br />
end<br />
<br />
function p._url(url, text, msg)<br />
url = mw.text.trim(url or '')<br />
text = mw.text.trim(text or '')<br />
local nomsg = msg:sub(1,1):lower() == "n" or msg == 'false' -- boolean: true if msg is "false" or starts with n or N<br />
<br />
if url == '' then<br />
if text == '' then<br />
if nomsg then<br />
return nil<br />
else<br />
return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }<br />
end<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 = (uri.host or '') .. port .. (uri.relativePath or '')<br />
<br />
-- Add <wbr> before _/.-# sequences<br />
text = mw.ustring.gsub(text,"(/+)","<wbr/>%1") -- This entry MUST be the first. "<wbr/>" has a "/" in it, you know.<br />
text = mw.ustring.gsub(text,"(%.+)","<wbr/>%1")<br />
-- text = mw.ustring.gsub(text,"(%-+)","<wbr/>%1") -- DISABLED for now<br />
text = mw.ustring.gsub(text,"(%#+)","<wbr/>%1")<br />
text = mw.ustring.gsub(text,"(_+)","<wbr/>%1")<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 />
--[[<br />
The main entry point for caling from Template:URL.<br />
--]]<br />
function p.url(frame)<br />
local templateArgs = frame.args<br />
local parentArgs = frame:getParent().args<br />
local url = templateArgs[1] or parentArgs[1] or ''<br />
local text = templateArgs[2] or parentArgs[2]<br />
local msg = templateArgs.msg or parentArgs.msg or ''<br />
if not text then<br />
url = url or extractUrl(templateArgs) or extractUrl(parentArgs)<br />
end<br />
text = text or ''<br />
return p._url(url, text, msg)<br />
end<br />
<br />
--[[<br />
The entry point for calling from the forked Template:URL2.<br />
This function returns no message by default.<br />
It strips out wiki-link markup, html tags, and everything after a space.<br />
--]]<br />
function p.url2(frame)<br />
local templateArgs = frame.args<br />
local parentArgs = frame:getParent().args<br />
local url = templateArgs[1] or parentArgs[1] or ''<br />
local text = templateArgs[2] or parentArgs[2] or ''<br />
-- default to no message<br />
local msg = templateArgs.msg or parentArgs.msg or 'no'<br />
if text == '' then<br />
url = url or extractUrl(templateArgs) or extractUrl(parentArgs)<br />
end<br />
-- strip out html tags and [ ] from url<br />
url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "")<br />
-- truncate anything after a space<br />
url = url:gsub("%%20", " "):gsub(" .*", "")<br />
return p._url(url, text, msg)<br />
end<br />
<br />
return p</div>RexxS