Editing Module:Check for unknown parameters
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 3: | Line 3: | ||
-- not on the list | -- not on the list | ||
local p = {} | local p = {} | ||
− | |||
− | |||
− | |||
− | |||
local function isnotempty(s) | local function isnotempty(s) | ||
− | return s and s:match('% | + | return s and s:match( '^%s*(.-)%s*$' ) ~= '' |
end | end | ||
− | + | function p.check (frame) | |
− | + | local args = frame.args | |
− | + | local pargs = frame:getParent().args | |
− | local | + | local ignoreblank = isnotempty(frame.args['ignoreblank']) |
− | + | local checkpos = isnotempty(frame.args['checkpositional']) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | local | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | local ignoreblank = isnotempty(args['ignoreblank']) | ||
− | local | ||
local knownargs = {} | local knownargs = {} | ||
− | local unknown = | + | local unknown = 'Found _VALUE_, ' |
− | |||
− | |||
local res = {} | local res = {} | ||
local regexps = {} | local regexps = {} | ||
− | + | local comments = {} | |
+ | local commentstr = '' | ||
+ | |||
-- create the list of known args, regular expressions, and the return string | -- create the list of known args, regular expressions, and the return string | ||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
if type(k) == 'number' then | if type(k) == 'number' then | ||
− | v = | + | v = mw.ustring.gsub(v, '^%s*(.-)%s*$', '%1') |
knownargs[v] = 1 | knownargs[v] = 1 | ||
− | elseif k | + | else |
− | + | if (k == 'unknown' and type(v) == 'string') then | |
+ | unknown = v | ||
+ | elseif (mw.ustring.match(k, '^regexp[%d]*$') and type(v) == 'string') then | ||
+ | table.insert(regexps, '^' .. v .. '$') | ||
+ | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
-- loop over the parent args, and make sure they are on the list | -- loop over the parent args, and make sure they are on the list | ||
− | for k, v in pairs(pargs) do | + | for k,v in pairs(pargs) do |
− | if type(k) == 'string' and knownargs[k] == nil then | + | if (type(k) == 'string' and knownargs[k] == nil) then |
− | local knownflag = | + | local knownflag = nil |
− | for | + | for r = 1,#regexps do |
− | if mw.ustring.match(k, | + | if( mw.ustring.match(k,regexps[r]) ) then |
− | knownflag = | + | knownflag = 1 |
− | |||
end | end | ||
end | end | ||
− | if not knownflag and ( not ignoreblank or isnotempty(v) ) | + | if( (not knownflag) and ( (not ignoreblank) or (ignoreblank and isnotempty(v)) ) ) then |
− | table.insert( | + | k = mw.ustring.gsub(k, '[^%w\-_ ]', '?') |
+ | local r = mw.ustring.gsub(unknown, '_VALUE_ ', k) | ||
+ | table.insert(res, r) | ||
+ | table.insert(comments, '"' .. k .. '"') | ||
+ | end | ||
+ | elseif(checkpos and type(k) == 'number' and knownargs[tostring(k)] == nil) then | ||
+ | if( (not ignoreblank) or (ignoreblank and isnotempty(v)) ) then | ||
+ | local r = mw.ustring.gsub(unknown, '_VALUE_ ', k) | ||
+ | table.insert(res, r) | ||
+ | table.insert(comments, '"' .. k .. '"') | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
end | end | ||
− | + | if(#comments > 0) then | |
− | if # | + | commentstr = '<!-- Module:Check for unknown parameters results: ' .. |
− | + | table.concat(comments, ', ') .. '-->' | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | table. | ||
− | |||
end | end | ||
− | + | ||
− | return table.concat(res) | + | return table.concat(res) .. commentstr |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
return p | return p |