Module:Redirect/sandbox: Difference between revisions

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')
-- If called via #invoke, use the args passed into the invoking
local args = mArguments.getArgs(frame)
-- template, or the args passed to #invoke if any exist. Otherwise
local rname, bracket = args[1], args.bracket
-- assume args are being passed directly in from the debug console
return p._main(rname, bracket) or ''
-- or from another Lua module.
end
local origArgs
 
if frame == mw.getCurrentFrame() then
function p._main(rname, bracket)
origArgs = frame:getParent().args
if type(rname) ~= "string" or not mw.ustring.matchrname:find(rname, "%S") then return end
for k, v in pairs( frame.args ) do
return nil
origArgs = frame.args
end
break
 
end
bracket = bracket and "[[%s]]" or "%s"
else
rname = mw.ustring.rname:match(rname, "%[%[(.+)%]%]") or rname
origArgs = frame
 
end
-- Get the title object, passing the function through pcall in case we are
-- Trim whitespace and remove blank arguments.
-- in case we are over the expensive function count limit, etc.
local args = {}
local noErrorsuccess, rpage = pcall(mw.title.new, rname)
for k, v in pairs( origArgs ) do
if not noErrorsuccess or not rpage then
v = mw.text.trim( v )
-- mw.title.new failed, so use the passed page name.
if v ~= '' then
return mw.ustring.bracket:format(bracket, rname)
args[k] = v
elseif not rpage.isRedirect then
end
-- the page is not a redirect, so use the normalized name of the page we
end
-- were given.
local rname, bracket = args[1], args.bracket
return mw.ustring.bracket:format(bracket, rpage.prefixedText)
end
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 = mw.ustringstring.match(
rpage:getContent() or "",
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" )
)
if redirect then
-- Decode html entities and percent encodings.
redirect = mw.text.decode(redirect, true)
redirect = mw.uritext.decode(redirect, 'WIKI'true)
redirect = mw.texturi.decode(redirect, true'WIKI')
return mw.ustring.bracket:format(bracket, redirect)
else
else
return mw.ustring.format('<span class="error">[[Module:redirect]] error: could not parse redirect - [[%s]]</span>', rname)
-- 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
end