Editing Module:Redirect
From MINR.ORG WIKI
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 12: | Line 12: | ||
return nil | return nil | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
-- Gets the target of a redirect. If the page specified is not a redirect, | -- Gets the target of a redirect. If the page specified is not a redirect, | ||
-- returns nil. | -- returns nil. | ||
− | function p.getTarget(page | + | function p.getTarget(page) |
-- Get the title object. Both page names and title objects are allowed | -- Get the title object. Both page names and title objects are allowed | ||
-- as input. | -- as input. | ||
Line 48: | Line 36: | ||
-- Find the target by using string matching on the page content. | -- Find the target by using string matching on the page content. | ||
− | local target = | + | local target = string.match( |
+ | titleObj:getContent() or "", | ||
+ | "^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]]-)%]%]" | ||
+ | ) | ||
if target then | if target then | ||
local targetTitle = getTitle(target) | local targetTitle = getTitle(target) | ||
if targetTitle then | if targetTitle then | ||
− | + | return targetTitle.prefixedText | |
− | |||
− | |||
− | |||
− | |||
else | else | ||
return nil | return nil | ||
Line 65: | Line 52: | ||
error(string.format( | error(string.format( | ||
'could not parse redirect on page "%s"', | 'could not parse redirect on page "%s"', | ||
− | + | titleObj.prefixedText | |
)) | )) | ||
end | end | ||
Line 79: | Line 66: | ||
-- target cannot be determined for some reason. | -- target cannot be determined for some reason. | ||
--]] | --]] | ||
− | function p.luaMain(rname, bracket | + | function p.luaMain(rname, bracket) |
if type(rname) ~= "string" or not rname:find("%S") then | if type(rname) ~= "string" or not rname:find("%S") then | ||
return nil | return nil | ||
Line 85: | Line 72: | ||
bracket = bracket and "[[%s]]" or "%s" | bracket = bracket and "[[%s]]" or "%s" | ||
rname = rname:match("%[%[(.+)%]%]") or rname | rname = rname:match("%[%[(.+)%]%]") or rname | ||
− | local target = p.getTarget(rname | + | local target = p.getTarget(rname) |
local ret = target or rname | local ret = target or rname | ||
ret = getTitle(ret) | ret = getTitle(ret) | ||
if ret then | if ret then | ||
− | + | ret = ret.prefixedText | |
− | |||
− | |||
− | |||
− | |||
return bracket:format(ret) | return bracket:format(ret) | ||
else | else | ||
Line 103: | Line 86: | ||
function p.main(frame) | function p.main(frame) | ||
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true}) | local args = require('Module:Arguments').getArgs(frame, {frameOnly = true}) | ||
− | return p.luaMain(args[1], args.bracket | + | return p.luaMain(args[1], args.bracket) or '' |
end | end | ||
− | -- Returns | + | -- Returns "yes" if the specified page is a redirect, and the blank string |
− | function p. | + | -- otherwise. |
− | local titleObj = getTitle( | + | function p.isRedirect(frame) |
+ | local args = require('Module:Arguments').getArgs(frame, {frameOnly = true}) | ||
+ | local titleObj = getTitle(args[1]) | ||
if not titleObj then | if not titleObj then | ||
− | return | + | return '' |
end | end | ||
if titleObj.isRedirect then | if titleObj.isRedirect then | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return 'yes' | return 'yes' | ||
else | else |