Content deleted Content added
add two more functions for calling from Lua, getTarget and getTargetString (was an edit conflict from earlier, overwriting for now) |
merge my version with Jackmcbarn's changes |
||
Line 6:
-- Thus these are roughly the same:
-- [[{{#invoke:redirect|main|redirect-page-name}}]] and {{#invoke:redirect|main|redirect-page-name|bracket=yes}}
local p = {}
Line 17 ⟶ 15:
else
return nil
end▼
end▼
function p.main(frame)▼
mArguments = require('Module:Arguments')▼
return p._main(rname, bracket) or ''▼
end▼
function p._main(rname, bracket)▼
if type(rname) ~= "string" or not rname:find("%S") then▼
return nil▼
end▼
bracket = bracket and "[[%s]]" or "%s"▼
rname = rname:match("%[%[(.+)%]%]") or rname▼
local target = p.getTargetString(rname)▼
if target then▼
return bracket:format(target)▼
else▼
return bracket:format(rname)▼
end
end
function p.getTarget(page)
-- Get the title object. Both page names and title objects are allowed
-- as input.
local titleObj
if type(page) == 'string' or type(page) == 'number' then
titleObj = getTitle(page)
elseif type(page) == 'table' and type(page.getContent) == 'function' then
titleObj = page
else
error(string.format(
"bad argument #1 to 'getTarget'"
.. " (string, number, or title object expected, got %s)",
type(page)
), 2)
end
if not titleObj or not titleObj.isRedirect then
Line 75 ⟶ 50:
-- The page is a redirect, but matching failed. This indicates a bug in
-- the redirect matching pattern, so throw an error.
error(string.format(
end
▲end
▲function p._main(rname, bracket)
▲ if type(rname) ~= "string" or not rname:find("%S") then
▲ return nil
▲ end
▲ bracket = bracket and "[[%s]]" or "%s"
▲ rname = rname:match("%[%[(.+)%]%]") or rname
▲ if target then
▲ return bracket:format(target)
▲ else
▲ return bracket:format(rname)
▲ end
▲end
▲function p.main(frame)
end
|