Module:Redirect: Difference between revisions
From Roses, Tulips, & Liberty
Content added Content deleted
m (1 revision imported) |
(use mw.title.new, code by Galobtter and Rummskartoffel) |
||
Line 12: | Line 12: | ||
return nil |
return nil |
||
end |
end |
||
end |
|||
-- Gets the name of a page that a redirect leads to, or nil if it isn't a |
|||
-- redirect. |
|||
function p.getTargetFromText(text) |
|||
local target = string.match( |
|||
text, |
|||
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)%]%]" |
|||
) or string.match( |
|||
text, |
|||
"^%s*#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*:?%s*%[%[([^%[%]|]-)|[^%[%]]-%]%]" |
|||
) |
|||
return target and mw.uri.decode(target, 'PATH') |
|||
end |
end |
||
Line 44: | Line 31: | ||
), 2) |
), 2) |
||
end |
end |
||
if not titleObj |
if not titleObj then |
||
return nil |
return nil |
||
end |
end |
||
⚫ | |||
⚫ | |||
-- Find the target by using string matching on the page content. |
|||
⚫ | |||
local target = p.getTargetFromText(titleObj:getContent() or "") |
|||
⚫ | |||
if target then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
return targetTitle.prefixedText |
|||
end |
|||
else |
else |
||
return |
return targetTitle.prefixedText |
||
end |
end |
||
else |
else |
||
return nil |
|||
-- The page is a redirect, but matching failed. This indicates a bug in |
|||
-- the redirect matching pattern, so throw an error. |
|||
error(string.format( |
|||
'could not parse redirect on page "%s"', |
|||
fulltext and titleObj.fullText or titleObj.prefixedText |
|||
)) |
|||
end |
end |
||
end |
end |