Content deleted Content added
Jackmcbarn (talk | contribs) don't need htmldecode now |
split this into two functions, and convert spaces to tabs |
||
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 = {}
function p.main(frame)
mArguments = require('Module:Arguments')
local args = mArguments.getArgs(frame)
return p._main(rname, bracket) or ''
function p._main(rname, bracket)
return nil
else▼
▲ end
▲ end
▲ local rname, bracket = args[1], args.bracket
▲ if type(rname) ~= "string" or not mw.ustring.match(rname, "%S") then return end
▲ bracket = bracket and "[[%s]]" or "%s"
▲ rname = mw.ustring.match(rname, "%[%[(.+)%]%]") or rname
▲ -- Get the title object, passing the function through pcall
▲ -- in case we are over the expensive function count limit.
▲ local noError, rpage = pcall(mw.title.new, rname)
▲ if not noError or not rpage then
▲ -- mw.title.new failed, so use the passed page name.
▲ return mw.ustring.format(bracket, rname)
▲ elseif not rpage.isRedirect then
▲ -- the page is not a redirect, so use the normalized name of the page we
▲ -- were given.
▲ return mw.ustring.format(bracket, rpage.prefixedText)
▲ end
-- Match the redirect target text from the page content.
local redirect = rpage:getContent() or "", "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" )
redirect = mw.text.decode(redirect, true)▼
-- The page is a redirect, but matching failed. This indicates a bug in
end▼
-- the redirect matching pattern, so throw an error.
error('could not parse redirect on page [[:' .. rname .. ']]')
end
|