Difference between revisions of "Module:URL"

From annadreambrush.com/wiki
Jump to navigation Jump to search
imported>Toohool
(get params from the parent frame instead of passing them to #invoke)
imported>Toohool
(use unicode string functions)
Line 6: Line 6:
 
   
 
   
 
function trim(s)
 
function trim(s)
     return (s:gsub("^%s*(.-)%s*$", "%1"))
+
     return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
 
end
 
end
  
Line 30: Line 30:
 
         if uri.path == '/' then uri.path = '' end
 
         if uri.path == '/' then uri.path = '' end
 
          
 
          
         text = (uri.host or ''):lower() .. uri.relativePath
+
         text = mw.ustring.lower(uri.host or '') .. uri.relativePath
 
     end
 
     end
  
     return string.format('<span class="url">[%s %s]</span>', url, text)
+
     return mw.ustring.format('<span class="url">[%s %s]</span>', url, text)
 
end
 
end
  

Revision as of 23:01, 25 February 2013

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

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

local p = {}
 
function trim(s)
    return (mw.ustring.gsub(s, "^%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 = mw.ustring.lower(uri.host or '') .. uri.relativePath
    end

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

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

return p