Module:URL: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius
Wrap the URL parsing in a pcall to catch errors from malformed URLs
meta>Mr. Stradivarius
add support for protocol-relative URLs, per protected edit request
Line 43: Line 43:
     local uri = safeUri(url)
     local uri = safeUri(url)
      
      
     -- Handle URL's without a protocol, e.g. www.example.com/foo or www.example.com:8080/foo
     -- Handle URL's without a protocol and URL's that are protocol-relative,  
     if uri and (not uri.protocol or (uri.protocol and not uri.host)) then
    -- e.g. www.example.com/foo or www.example.com:8080/foo, and //www.example.com/foo
     if uri and (not uri.protocol or (uri.protocol and not uri.host)) and url:sub(1, 2) ~= '//' then
         url = 'http://' .. url
         url = 'http://' .. url
         uri = safeUri(url)
         uri = safeUri(url)

Revision as of 10:58, 31 May 2013

Documentation for this module may be created at Module:URL/doc

--
-- This module implements {{URL}}
--
-- See unit tests at [[Module:URL/tests]]

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

function safeUri(s)
    local success, uri = pcall(function()
        return mw.uri.new(s)
    end)
    if success then
        return uri
    end
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
    
    -- 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) return mw.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 error
    url = 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')
    
    local uri = 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/foo
    if uri and (not uri.protocol or (uri.protocol and not uri.host)) and url:sub(1, 2) ~= '//' then
        url = 'http://' .. url
        uri = safeUri(url)
    end
    
    if text == '' then
        if uri then
            if uri.path == '/' then uri.path = '' end
            
            local port = ''
            if uri.port then port = ':' .. uri.port end
            
            text = mw.ustring.lower(uri.host or '') .. port .. (uri.relativePath or '')
        else -- URL is badly-formed, so just display whatever was passed in
            text = url
        end
    end

    return mw.ustring.format('<span class="url">[%s %s]</span>', url, text)
end

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

return p