Module:If in page
Appearance
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
| This Lua module is used on approximately 19,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module implements {{If in page}}, please see its documentation for more information.
Usage
{{#invoke:If in page|main}}
See also
local p = {}
local getArgs = require('Module:Arguments').getArgs
--args: 1 - ustring pattern, 2 - value if present, 3 - value if absent,
-- page - page to test if not this page
function p._main(args)
if not args["page"] then
args.page = mw.title.getCurrentTitle().fullText
end
local page = mw.title.new(args.page)
if not page then
--bad title
return args["3"] or ""
end
local content = page:getContent()
if not content then
--page does not exist
return args["3"] or ""
end
if mw.ustring.match(content, args["1"] or "") then
if args["sub"] then
--return value should have capture groups substed in
local pattern = args["1"] or ""
if mw.ustring.sub(pattern, 1, 1) ~= "^" then
--pattern does not force it to be at start of page
pattern = "^.-" .. pattern
end
if mw.ustring.sub(pattern, -1) ~= "$" then
--pattern does not force it to be at end of page
pattern = pattern .. ".*$"
end
--pattern will now match entire content, so running gsub will
--return the string that has been passed in parameter 2 with things
--like %1 substituted, NOTE: %0 does not work in this
local out = mw.ustring.gsub(content, pattern, args["2"] or "")
return out
else
return args["2"] or ""
end
else
return args["3"] or ""
end
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
return p