Editing Module:Check for unknown parameters
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish 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 | local comments = {} | ||
local commentstr = '' | |||
-- create the list of known args | |||
-- create the list of known args, 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 | ||
else | |||
if (k == 'unknown' and type(v) == 'string') then | |||
unknown = 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 | ||
if( (not ignoreblank) or (ignoreblank and isnotempty(v)) ) then | |||
k = mw.ustring.gsub(k, '[^%w\-_ ]', '?') | |||
local r = mw.ustring.gsub(unknown, '_VALUE_', k) | |||
table.insert(res, r) | |||
table.insert(comments, '"' .. k .. '"') | |||
end | end | ||
if | elseif(checkpos and type(k) == 'number' and knownargs[tostring(k)] == nil) then | ||
table.insert( | 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 |