Difference between revisions of "Module:URL"
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) | ||
| − | |||
return (s:gsub("^%s*(.-)%s*$", "%1")) | return (s:gsub("^%s*(.-)%s*$", "%1")) | ||
end | end | ||
| − | function | + | 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 | end | ||
| − | + | ||
| − | + | local uri = mw.uri.new(url) | |
| − | + | if uri.protocol == nil then | |
| − | |||
| − | local | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
url = 'http://' .. url | url = 'http://' .. url | ||
| + | uri = mw.uri.new(url) | ||
end | end | ||
if text == '' then | if text == '' then | ||
| − | + | if uri.path == '/' then uri.path = '' end | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | text = host | + | text = (uri.host or ''):lower() .. uri.relativePath |
| − | |||
| − | |||
| − | |||
end | end | ||
| Line 63: | Line 37: | ||
function p.url(frame) | function p.url(frame) | ||
| − | local url = | + | local url = frame.args[1] |
| − | local text = | + | local text = frame.args[2] |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
return p._url(url, text) | return p._url(url, text) | ||
end | end | ||
return p | return p | ||
Revision as of 09: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