Module:Redirect/sandbox: Difference between revisions

Content deleted Content added
mNo edit summary
rejig this so that the frame object doesn't have to be available all the time; also make the top-level variables local, as apparently it is ever-so-slightly quicker
Line 9:
local p = {}
 
local function warOnGsub(text, repl)
if repl then
text = mw.ustring.gsub(text, "%%", "%%%%")
Line 18:
end
 
local function getArgsgetRedirect(framername)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs( origArgs ) do
v = mw.text.trim( v )
if v ~= '' then
args[k] = v
end
end
return args
end
 
function getRedirect(rname)
-- Get the title object, passing the function through pcall
-- in case we are over the expensive function count limit.
Line 53 ⟶ 27:
return rname
end
 
local redirect = mw.ustring.match(rpage:getContent() or "", "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" )
if redirect then
Line 63 ⟶ 36:
end
 
function p.main_main(frameargs)
 
local args = getArgs(frame)
local rname, bracket = args[1], args.bracket
bracket = bracket and "[[%s]]" or "%s"
if type(rname) ~= "string" or not mw.ustring.match(rname, "%S") then return end
rname = mw.ustring.match(rname, "%[%[(.+)%]%]") or rname
 
local redirect = getRedirect(rname)
 
if redirect then
return mw.ustring.format(bracket, redirect)
Line 80 ⟶ 49:
end
 
local function getPage(sourceName)
 
if type(sourceName) ~= "string" or not mw.ustring.match(sourceName, "%S") then return end
-- the following delink doesn't make as much sense for this usage but is done for consistency.
Line 93 ⟶ 61:
end
return text
end
 
local function nowikize(frame, text, nowiki, default)
local frame = mw.getCurrentFrame()
if (default or nowiki) and (nowiki ~= 'no') then
text = frame:preprocess('<pre><nowiki>'..text..'</nowiki></pre>')
Line 103 ⟶ 71:
end
 
function p.block_block(frameargs)
-- this feature takes an initial page as an argument. It obtains all the links from that page
-- starting from the first instance of some link named as start=, and replaces every link after
-- that which is a redirect with the non-redirected canonical name, up until pcall throws an error
-- due to expense. Finally, it returns the substituted page.
 
local args = getArgs(frame)
local sourceName, start, text, pipe, nowiki = args[1], args[2], args.text, args.pipe, args.nowiki
if type(sourceName) == "string" then sourceName = mw.text.trim(sourceName) or sourceName end
Line 162 ⟶ 128:
end
end
return nowikize(frame, text, nowiki, true)
end
 
local function makeWrapper(func)
return function (frame)
-- If called via #invoke, use the args passed into the invoking
-- template, or the args passed to #invoke if any exist. Otherwise
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs( origArgs ) do
v = mw.text.trim( v )
if v ~= '' then
args[k] = v
end
end
return func(args)
end
end
 
local funcNames = {'main', 'block'}
 
for _, funcName in ipairs(funcNames) do
p[funcName] = makeWrapper(p['_' .. funcName])
end