https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3AURL%2Fsandbox-samecase Module:URL/sandbox-samecase - Revision history 2025-05-25T03:51:25Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.2 https://en.wikipedia.org/w/index.php?title=Module:URL/sandbox-samecase&diff=884323815&oldid=prev RexxS: 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 .. &quot;=&quot; .. val;<br /> url = mw.ustring.gsub(url, &#039;^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])&#039;, &#039;http%1://%3&#039;)<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 &#039;&#039;)<br /> text = mw.text.trim(text or &#039;&#039;)<br /> local nomsg = msg:sub(1,1):lower() == &quot;n&quot; or msg == &#039;false&#039; -- boolean: true if msg is &quot;false&quot; or starts with n or N<br /> <br /> if url == &#039;&#039; then<br /> if text == &#039;&#039; then<br /> if nomsg then<br /> return nil<br /> else<br /> return mw.getCurrentFrame():expandTemplate{ title = &#039;tlx&#039;, args = { &#039;URL&#039;, &quot;&#039;&#039;example.com&#039;&#039;&quot;, &quot;&#039;&#039;optional display text&#039;&#039;&quot; } }<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, &#039;%s&#039;, function(s) return mw.uri.encode(s, &#039;PATH&#039;) 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, &#039;#$&#039;, &#039;&#039;)<br /> url = mw.ustring.gsub(url, &#039;%?$&#039;, &#039;&#039;)<br /> <br /> -- If it&#039;s an HTTP[S] URL without the double slash, fix it.<br /> url = mw.ustring.gsub(url, &#039;^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])&#039;, &#039;http%1://%3&#039;)<br /> <br /> -- Handle URLs from Wikidata of the format http&amp;#58;//<br /> url = mw.ustring.gsub(url, &#039;^[Hh][Tt][Tt][Pp]([Ss]?)&amp;#58;//&#039;, &#039;http%1://&#039;)<br /> <br /> local uri = safeUri(url)<br /> <br /> -- Handle URL&#039;s without a protocol and URL&#039;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) ~= &#039;//&#039; then<br /> url = &#039;http://&#039; .. url<br /> uri = safeUri(url)<br /> end<br /> <br /> if text == &#039;&#039; then<br /> if uri then<br /> if uri.path == &#039;/&#039; then uri.path = &#039;&#039; end<br /> <br /> local port = &#039;&#039;<br /> if uri.port then port = &#039;:&#039; .. uri.port end<br /> <br /> text = (uri.host or &#039;&#039;) .. port .. (uri.relativePath or &#039;&#039;)<br /> <br /> -- Add &lt;wbr&gt; before _/.-# sequences<br /> text = mw.ustring.gsub(text,&quot;(/+)&quot;,&quot;&lt;wbr/&gt;%1&quot;) -- This entry MUST be the first. &quot;&lt;wbr/&gt;&quot; has a &quot;/&quot; in it, you know.<br /> text = mw.ustring.gsub(text,&quot;(%.+)&quot;,&quot;&lt;wbr/&gt;%1&quot;)<br /> -- text = mw.ustring.gsub(text,&quot;(%-+)&quot;,&quot;&lt;wbr/&gt;%1&quot;) -- DISABLED for now<br /> text = mw.ustring.gsub(text,&quot;(%#+)&quot;,&quot;&lt;wbr/&gt;%1&quot;)<br /> text = mw.ustring.gsub(text,&quot;(_+)&quot;,&quot;&lt;wbr/&gt;%1&quot;)<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(&#039;&lt;span class=&quot;url&quot;&gt;[%s %s]&lt;/span&gt;&#039;, 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 &#039;&#039;<br /> local text = templateArgs[2] or parentArgs[2]<br /> local msg = templateArgs.msg or parentArgs.msg or &#039;&#039;<br /> if not text then<br /> url = url or extractUrl(templateArgs) or extractUrl(parentArgs)<br /> end<br /> text = text or &#039;&#039;<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 &#039;&#039;<br /> local text = templateArgs[2] or parentArgs[2] or &#039;&#039;<br /> -- default to no message<br /> local msg = templateArgs.msg or parentArgs.msg or &#039;no&#039;<br /> if text == &#039;&#039; then<br /> url = url or extractUrl(templateArgs) or extractUrl(parentArgs)<br /> end<br /> -- strip out html tags and [ ] from url<br /> url = (url or &#039;&#039;):gsub(&quot;&lt;[^&gt;]*&gt;&quot;, &quot;&quot;):gsub(&quot;[%[%]]&quot;, &quot;&quot;)<br /> -- truncate anything after a space<br /> url = url:gsub(&quot;%%20&quot;, &quot; &quot;):gsub(&quot; .*&quot;, &quot;&quot;)<br /> return p._url(url, text, msg)<br /> end<br /> <br /> return p</div> RexxS