Module:Redirect: Difference between revisions

consistent parsing of passed page name, output when passed bracket, and interpretation of redirect
(rm extra space)
(consistent parsing of passed page name, output when passed bracket, and interpretation of redirect)
Line 9:
 
function p.main(frame)
local args, pargs = frame.args, (frame:getParent() or {}).args or {}
local rname, bracket = args[1] or pargs[1], args.bracket or ""pargs.bracket
parent=frame.getParent(frame)
end
if parent then pargs = parent.args else pargs={} end
if not rname or not mw.ustring.match(rname, "%S") then return "" end
local rname=args[1] or pargs[1] or ""
local bracket =args. bracket and "[[%s]]" or pargs.bracket"%s"
rname = local strip=mw.ustring.match(rname,"%[%[(.+)%]%]") or rname
local bb = "";local be= "" -- store brackets if restoring to an output if bracket option is set
if rname then
local rpage, err if= mw.ustringtitle.gsubnew(rname,"%s","") == "" then return "" end
end
if bracket then
-- avoid expensive operation when nothing to do
local strip=mw.ustring.match(rname,"%[%[(.+)%]%]")
if not if bracketrpage then
if strip then rname=strip;bb="[[";be="]]" end
else err = "File not found (mw.title.new failed)"
end
elseif rpage.id == else0 return ""then
else err = "File not found (id=0):"
end
local elseif not rpage=mw.title.new(rname)isRedirect then
return else returnmw.ustring.format(bracket, rname) -- not a redirect so leaveuse thepassed originalpage textname (for some general-purpose template use)
local err=""
endelse
if rpage then -- avoid possibly expensive FileExists if we don't have anything
local redirect = mw.ustring.match( rpage:getContent() or "", "^#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*%[%[(.-)%]%]" )
if rpage.id ~= 0 then
-- if rpage.fileExists then
if rpage.isRedirectredirect then
return mw.ustring.format(bracket, redirect)
return (bb.. tostring(mw.ustring.match(tostring(rpage.getContent(rpage)),"[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt].-%[%[(.-)%]%]")..be) or [=[<span style="text-color:red;">[[Module:redirect]] error: failed to understand - [[]=] .. rname .. [=[]]</span]=])
end
else return rname -- not a redirect so leave the original text (for some general-purpose template use)
end
-- err else= err="File does notfailed exist:to understand"
-- end
else err="File not found (id=0):"
return [=['<span style="text-color:red;">[[Module:redirect]] error: ]=]' .. err .. [=[' - [[]=]' .. rname .. [=[']]</span>]=]'
end
else err="File not found (mw.title.new failed)"
end
return [=[<span style="text-color:red;">[[Module:redirect]] error: ]=] .. err .. [=[ - [[]=] .. rname .. [=[]]</span>]=]
end
 
Anonymous user