Module:URL

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(' [%s %s] ', 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