---- This module implements {{URL}}---- See unit tests at [[Module:URL/tests]]localp={}functiontrim(s)return(mw.ustring.gsub(s,"^%s*(.-)%s*$","%1"))endfunctionsafeUri(s)localsuccess,uri=pcall(function()returnmw.uri.new(s)end)ifsuccessthenreturnuriendendfunctionp._url(url,text)url=trim(urlor'')text=trim(textor'')ifurl==''theniftext==''thenreturnmw.getCurrentFrame():expandTemplate{title='tlx',args={'URL',"1=''example.com''","2=''リンクの表示名(省略可)''"}}elsereturntextendend-- If the URL contains any unencoded spaces, encode them, because MediaWiki will otherwise interpret a space as the end of the URL.url=mw.ustring.gsub(url,'%s',function(s)returnmw.uri.encode(s,'PATH')end)-- If there is an empty query string or fragment id, remove it as it will cause mw.uri.new to throw an errorurl=mw.ustring.gsub(url,'#$','')url=mw.ustring.gsub(url,'%?$','')-- If it's an HTTP[S] URL without the double slash, fix it.url=mw.ustring.gsub(url,'^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])','http%1://%3')-- Handle URLs from Wikidata of the format http://url=mw.ustring.gsub(url,'^[Hh][Tt][Tt][Pp]([Ss]?)://','http%1://')localuri=safeUri(url)-- Handle URL's without a protocol and URL's that are protocol-relative, -- e.g. www.example.com/foo or www.example.com:8080/foo, and //www.example.com/fooifuriand(noturi.protocolor(uri.protocolandnoturi.host))andurl:sub(1,2)~='//'thenurl='http://'..urluri=safeUri(url)endiftext==''thenifurithenifuri.path=='/'thenuri.path=''endlocalport=''ifuri.portthenport=':'..uri.portendtext=mw.ustring.lower(uri.hostor'')..port..(uri.relativePathor'')else-- URL is badly-formed, so just display whatever was passed intext=urlendend-- Add <wbr> before _/.-# sequencestext=mw.ustring.gsub(text,"(/+)","<wbr/>%1")-- This entry MUST be the first. "<wbr/>" has a "/" in it, you know.text=mw.ustring.gsub(text,"(%.+)","<wbr/>%1")-- text = mw.ustring.gsub(text,"(%-+)","<wbr/>%1") -- DISABLED for nowtext=mw.ustring.gsub(text,"(%#+)","<wbr/>%1")text=mw.ustring.gsub(text,"(_+)","<wbr/>%1")returnmw.ustring.format('<span class="url">[%s %s]</span>',url,text)endfunctionp.url(frame)localtemplateArgs=frame.argslocalurl=templateArgs[1]or''localtext=templateArgs[2]or''returnp._url(url,text)endreturnp