Module:Redirect/sandbox: Difference between revisions

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(...)
local success, rpagetitleObj = pcall(mw.title.new, rname...)
if not success or not rpage then
return titleObj
else
return nil
end
end
 
function p.main(frame)
local argsmArguments = require('Module:Arguments').getArgs(frame)
local args = mArguments.getArgs(frame)
local rname, bracket = args[1], args.bracket
return p._main(args[1]rname, args.bracket) or ''
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
-- Get the title object, passing the function through pcall in case we are
return bracket:format(rpage.prefixedTexttarget)
-- over the expensive function count limit, etc.
else
local success, rpage = pcall(mw.title.new, rname)
if not success or not rpage then
-- mw.title.new failed, so use the passed page name.
return bracket:format(rname)
elseif not rpage.isRedirect then
-- the page is not a redirect, so use the normalized name of the page we
-- were given.
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
elseifif not rpagetitleObj or not titleObj.isRedirect then
return nil
end
-- MatchFind the redirect target textby using string matching fromon the page content.
local redirecttarget = string.match(
rpagetitleObj:getContent() or "",
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]"
)
if redirecttarget then
-- Decode html entities and percent encodings.
redirecttarget = mw.text.decode(redirecttarget, true)
redirecttarget = mw.uri.decode(redirecttarget, 'WIKI')
return bracket:format(redirect)target
else
-- The page is a redirect, but matching failed. This indicates a bug in
-- the redirect matching pattern, so throw an error.
error(local msg = 'could not parse redirect on page [[:' .. rname .. ']]')
.. 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