Module:Redirect: Difference between revisions
m
1 revision imported
Enwikipedia>SiBr4 (Moved from sandbox: making the isRedirect function available from other modules, like the main function is) |
m (1 revision imported) Tags: Mobile edit Mobile web edit Advanced mobile edit |
||
(6 intermediate revisions by 6 users not shown) | |||
Line 17:
-- redirect.
function p.getTargetFromText(text)
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
-- Gets the target of a redirect. If the page specified is not a redirect,
-- returns nil.
function p.getTarget(page, fulltext)
-- Get the title object. Both page names and title objects are allowed
-- as input.
Line 40 ⟶ 44:
), 2)
end
if not titleObj
return nil
end
▲ if target then
▲ local targetTitle = getTitle(target)
▲ if targetTitle then
▲ return targetTitle.prefixedText
else
return
end
else
return nil
titleObj.prefixedText▼
end
end
Line 67 ⟶ 63:
-- target page name, or the passed page name when not a redirect. The passed
-- page name can be given as plain text or as a page link.
--
-- Returns page name as plain text, or when the bracket parameter is given, as a
-- page link. Returns an error message when page does not exist or the redirect
-- target cannot be determined for some reason.
--]]
function p.luaMain(rname, bracket, fulltext)
if type(rname) ~= "string" or not rname:find("%S") then
return nil
Line 78 ⟶ 74:
bracket = bracket and "[[%s]]" or "%s"
rname = rname:match("%[%[(.+)%]%]") or rname
local target = p.getTarget(rname, fulltext)
local ret = target or rname
ret = getTitle(ret)
if ret then
if fulltext then
ret = ret.
else
end
return bracket:format(ret)
else
Line 92:
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {frameOnly = true})
return p.luaMain(args[1], args.bracket, args.fulltext) or ''
end
|