Module:URL: Difference between revisions

Making extraction of URLs containing "=" actually work per edit request
m (1 revision imported)
(Making extraction of URLs containing "=" actually work per edit request)
Line 2: Line 2:
-- This module implements {{URL}}
-- This module implements {{URL}}
--
--
-- See unit tests at [[Module:URL/tests]]
-- See unit tests at [[Module:URL/testcases]]


local p = {}
local p = {}
Line 17: Line 17:
local function extractUrl(args)
local function extractUrl(args)
for name, val in pairs(args) do
for name, val in pairs(args) do
local url = name .. "=" .. val;
if name ~= 2 and name ~= "msg" then
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
local url = name .. "=" .. val;
local uri = safeUri(url);
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3')
if uri and uri.host then
local uri = safeUri(url);
return url
if uri and uri.host then
return url
end
end
end
end
end
Line 89: Line 91:


--[[
--[[
The main entry point for caling from Template:URL.
The main entry point for calling from Template:URL.
--]]
--]]
function p.url(frame)
function p.url(frame)
local templateArgs = frame.args
local templateArgs = frame.args
local parentArgs = frame:getParent().args
local parentArgs = frame:getParent().args
local url = templateArgs[1] or parentArgs[1] or ''
local url = templateArgs[1] or parentArgs[1]
local text = templateArgs[2] or parentArgs[2]
local text = templateArgs[2] or parentArgs[2] or ''
local msg = templateArgs.msg or parentArgs.msg or ''
local msg = templateArgs.msg or parentArgs.msg or ''
if not text then
url = url or extractUrl(templateArgs) or extractUrl(parentArgs) or ''
url = url or extractUrl(templateArgs) or extractUrl(parentArgs)
end
text = text or ''
return p._url(url, text, msg)
return p._url(url, text, msg)
end
end
Line 112: Line 111:
local templateArgs = frame.args
local templateArgs = frame.args
local parentArgs = frame:getParent().args
local parentArgs = frame:getParent().args
local url = templateArgs[1] or parentArgs[1] or ''
local url = templateArgs[1] or parentArgs[1]
local text = templateArgs[2] or parentArgs[2] or ''
local text = templateArgs[2] or parentArgs[2] or ''
-- default to no message
-- default to no message
local msg = templateArgs.msg or parentArgs.msg or 'no'
local msg = templateArgs.msg or parentArgs.msg or 'no'
if text == '' then
url = url or extractUrl(templateArgs) or extractUrl(parentArgs) or ''
url = url or extractUrl(templateArgs) or extractUrl(parentArgs)
end
-- strip out html tags and [ ] from url
-- strip out html tags and [ ] from url
url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "")
url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "")
Anonymous user