Difference between revisions of "Module:URL"

From annadreambrush.com/wiki
Jump to navigation Jump to search
imported>Toohool
(should return the display text unchanged if no url param is specified)
imported>Toohool
(simplify code by using the mw.uri library)
Line 6: Line 6:
 
   
 
   
 
function trim(s)
 
function trim(s)
    if s == nil then return '' end
 
 
     return (s:gsub("^%s*(.-)%s*$", "%1"))
 
     return (s:gsub("^%s*(.-)%s*$", "%1"))
 
end
 
end
  
function startsWith(s, prefix)
+
function p._url(url, text)
     return (s:find(prefix, 1, true)) == 1
+
     url = trim(url or '')
end
+
    text = trim(text or '')
 
+
      
function split(s, sep) -- string split function for a single-character separator
+
    if url == '' then
     local ret = {}
+
        if text == '' then
    local n = 1
+
            return mw.getCurrentFrame():expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }
    for w in s:gmatch("([^" .. sep .. "]*)") do
+
         else
         ret[n] = ret[n] or w  -- only set once (so the blank after a string is ignored)
+
            return text
         if w == '' then n = n + 1 end -- step forwards on a blank but not a string
+
         end
 
     end
 
     end
     return ret
+
      
end
+
     local uri = mw.uri.new(url)
 
+
     if uri.protocol == nil then
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
 
         url = 'http://' .. url
 +
        uri = mw.uri.new(url)
 
     end
 
     end
 
      
 
      
 
     if text == '' then
 
     if text == '' then
         local comps = split(url, '/')
+
         if uri.path == '/' then uri.path = '' end
        local host = comps[3]
 
        local path = table.concat(rest(comps, 4), '/')
 
 
 
        local idx = host:find('?') or host:find('#') -- if there's a query string or anchor directly after the host name, without the default path ("/") in between, its case should be preserved
 
        if idx == nil then
 
            host = host:lower()
 
        else
 
            host = host:sub(1, idx - 1):lower() .. host:sub(idx)
 
        end
 
 
          
 
          
         text = host
+
         text = (uri.host or ''):lower() .. uri.relativePath
        if path ~= '' then
 
            text = text .. '/' .. path
 
        end
 
 
     end
 
     end
 
      
 
      
Line 63: Line 37:
  
 
function p.url(frame)
 
function p.url(frame)
     local url = trim(frame.args[1])
+
     local url = frame.args[1]
     local text = trim(frame.args[2])
+
     local text = frame.args[2]
 
 
    if url == '' then
 
        if text == '' then
 
            return frame:expandTemplate{ title = 'tlx', args = { 'URL', "''example.com''", "''optional display text''" } }
 
        else
 
            return text
 
        end
 
    end
 
   
 
 
     return p._url(url, text)
 
     return p._url(url, text)
 
end
 
end
  
 
return p
 
return p

Revision as of 08:49, 22 February 2013

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

--
-- 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