Content deleted Content added
Jackmcbarn (talk | contribs) get rid of 2 more variables, and reorder functions |
add two more functions for calling from Lua, getTarget and getTargetString (was an edit conflict from earlier, overwriting for now) |
||
Line 6:
-- Thus these are roughly the same:
-- [[{{#invoke:redirect|main|redirect-page-name}}]] and {{#invoke:redirect|main|redirect-page-name|bracket=yes}}
local mArguments -- lazily initialise [[Module:Arguments]]
local p = {}
local function getTitle(...)
return titleObj
else
return nil
end▼
end
function p.main(frame)▼
local args = mArguments.getArgs(frame)
local rname, bracket = args[1], args.bracket
end
function p._main(rname, bracket)
Line 13 ⟶ 31:
return nil
end
bracket = bracket and "[[%s]]" or "%s"
rname = rname:match("%[%[(.+)%]%]") or rname
local target = p.getTargetString(rname)
if target then
else
▲ local success, rpage = pcall(mw.title.new, rname)
▲ if not success or not rpage then
return bracket:format(rname)
elseif not rpage.isRedirect then▼
▲ return bracket:format(rpage.prefixedText)
end
end
function p.getTarget(page)
-- Match the redirect target text from the page content.▼
return getTitle(p.getTargetString)
local redirect = string.match(▼
end
rpage:getContent() or "",▼
function p.getTargetString(page)
-- Get the title object. Both page names and title objects are allowed
-- as input.
local titleObj
if type(page) == 'string' then
titleObj = getTitle(page)
elseif type(page) == 'table' and type(page.getContent) == 'function' then
titleObj = page
else
local msg = 'invalid input to "getTarget":'
.. 'the first parameter must be a string or a title object'
error(msg, 2)
end
return nil
end
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]"
)
if
-- Decode html entities and percent encodings.
return
else
-- The page is a redirect, but matching failed. This indicates a bug in
-- the redirect matching pattern, so throw an error.
.. titleObj.prefixedText
.. ']]'
error(msg)
end
▲end
▲function p.main(frame)
▲ local args = require('Module:Arguments').getArgs(frame)
▲ return p._main(args[1], args.bracket) or ''
end
|