Module:URL: Difference between revisions
Jump to navigation
Jump to search
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/ | -- 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 | ||
local url = name .. "=" .. val; | |||
url = mw.ustring.gsub(url, '^[Hh][Tt][Tt][Pp]([Ss]?):(/?)([^/])', 'http%1://%3') | |||
local uri = safeUri(url); | |||
if uri and uri.host then | |||
return url | |||
end | |||
end | end | ||
end | end | ||
Line 89: | Line 91: | ||
--[[ | --[[ | ||
The main entry point for | 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] | 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 '' | ||
url = url or extractUrl(templateArgs) or extractUrl(parentArgs) 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] | 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' | ||
url = url or extractUrl(templateArgs) or extractUrl(parentArgs) or '' | |||
-- strip out html tags and [ ] from url | -- strip out html tags and [ ] from url | ||
url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "") | url = (url or ''):gsub("<[^>]*>", ""):gsub("[%[%]]", "") |