Jump to content

Module:URL

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Toohool (talk | contribs) at 07:49, 22 February 2013 (simplify code by using the mw.uri library). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--
-- This module implements {{URL}}
--

local p = {}
 
function trim(s)
    return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function p._url(url, text)
    url = trim(url or '')
    text = trim(text or '')
    
    if url == '' then
        if text == '' then
            return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }
        else
            return text
        end
    end
    
    local uri = mw.uri.new(url)
    if uri.protocol == nil then
        url = 'http://' .. url
        uri = mw.uri.new(url)
    end
    
    if text == '' then
        if uri.path == '/' then uri.path = '' end
        
        text = (uri.host or ''):lower() .. uri.relativePath
    end
    
    return string.format('<span class="url">[%s %s]</span>', url, text)
end

function p.url(frame)
    local url = frame.args[1]
    local text = frame.args[2]
    return p._url(url, text)
end

return p