Module:URL
Appearance
![]() | This Lua module is used in MediaWiki:Titleblacklist-custom-URL. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
This module implements {{URL}} and {{URL2}}. Please see the template page for documentation.
Lua interface
The p._url(url, text, msg)
function may be used by other Lua modules. It returns a formatted Wikitext for the given URL, made suitable for line wrapping using . It takes the following parameters:
- url
- REQUIRED. The URL to format.
- text
- OPTIONAL. Display text to put in the Wikitext link. Defaults to a pretty version of the URL.
- msg
- OPTIONAL. String. If content is false, n or N, do not emit a help message (using
{{tlx}}
) when URL is not given.
Example
The following module emits a prettified link to log the user out. It will wrap correctly to most widths.
local url = require('Module:URL')._url
local p = {}
p.main = function(frame)
return url("https://en.wikipedia.org/wiki/Special:UserLogout")
end
return p
See also
- {{#invoke:WikidataIB|url2}} – a simpler version which only allows one value
local p = {}
function trim(s)
if s == nil then return '' end
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function startsWith(s, prefix)
return (s:find(prefix, 1, true)) == 1
end
function split(s, sep) -- string split function for a single-character separator
local ret = {}
local n = 1
for w in s:gmatch("([^" .. sep .. "]*)") do
ret[n] = ret[n] or w -- only set once (so the blank after a string is ignored)
if w == '' then n = n + 1 end -- step forwards on a blank but not a string
end
return ret
end
function rest(tbl, start) -- get the elements of the table beginning at the specified starting index
local ret = {}
if start > #tbl then
return ret
end
for i = start, #tbl do
ret[i - start + 1] = tbl[i]
end
return ret
end
function p._url(url, text)
local lcUrl = url:lower()
if not startsWith(lcUrl, 'http://') and not startsWith(lcUrl, 'https://') and not startsWith(lcUrl, 'ftp://') then
url = 'http://' .. url
end
if text == '' then
local comps = split(url, '/')
local host = comps[3]:lower()
local path = table.concat(rest(comps, 4), '/')
text = host
if path ~= '' then
text = text .. '/' .. path
end
end
return string.format('<span class="url">[%s %s]</span>', url, text)
end
function p.url(frame)
local url = trim(frame.args[1])
local text = trim(frame.args[2])
if url == '' then
return frame:expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }
end
return p._url(url, text)
end
return p