Module:Sandbox/Lemondoge/fixDoubleRedirects

local p = {}

-- Use a “blank” function, so that it’ll do the same thing with any parameters function p.main(frame) local page = mw.title.getCurrentTitle local redr = page:getContent:match("#REDIRECT[^!-~]+%[%[(.-)%]%]") local ns = redr:match("^(.-)+:") or '' local name = redr:match('[^#]+', redr:find(ns) or 1) local frag = redr:match("(.-)+", redr:find(name)) local redirectTarget = mw.title.makeTitle(ns, name, frag)

-- recurse through redirects to find the target local function locateFinalDestination(redirect) if not redirect.isRedirect then return redirect end return locateFinalDestination(redirect.redirectTarget) end -- strip away the invocation of this module, preprocess, and prepare for returning. Init value in match is just to improve efficiency slightly local function stripInvoke(wikitext) return frame:preprocess(wikitext:match("|%s*main%s*|(.*)}}", 10)) end if not redirectTarget then -- just return the page return stripInvoke(page:getContent) end

redirectName = locateFinalDestination(redirectTarget).fullText

return stripInvoke(			page:getContent:gsub( '(%#REDIRECT[^!-~]+)%[%[(.-)%]%]', "%1%[%[" .. redirectName .. "%]%]"			)		) end

return p