Module:ifmatch
Appearance
- This module lacks a documentation subpage. Please create it.
- Useful links: subpage list • links • transclusions • testcases • sandbox
local export = {}
-- index_limit: parameters larger than this can't be specified as numbered
-- parameters. This is so that invocations of {{ifmatch}} like the following work:
-- {{ifmatch|s|pattern|if_true|if_false}}
function export._getParameters(frame_args, arg_list, index_limit)
local new_args = {};
local index = 1;
local value;
for _, arg in ipairs(arg_list) do
value = frame_args[arg]
if value == nil and index <= index_limit then
value = frame_args[index];
index = index + 1;
end
new_args[arg] = value;
end
return new_args;
end
function export.ifmatch_helper(frame)
local new_args = export._getParameters(
frame:getParent().args,
{'s', 'pattern', 'start', 'plain', 'unicode'},
2
)
local s = assert(new_args.s)
local pattern = assert(new_args.pattern)
local start = new_args.start and math.floor(assert(tonumber(newargs.start), "start should be a number")) or 1
local yesno = require('Module:yesno')
local plain = yesno(new_args.plain)
local unicode = new_args.unicode
if unicode then
unicode = yesno(unicode)
else
unicode = true
end
-- If plain is true, mw.ustring.find behaves like string.find and there is
-- no point in potentially slowing down matching by using mw.ustring.find.
if plain then
unicode = false
end
local find = unicode and mw.ustring.find or string.find
return find(s, pattern, start, plain) and '1' or ''
end
return export