Special page

Difference between pages "Module:Check for unknown parameters" and "Module:Check for unknown parameters/sandbox"

(Difference between pages)
Page 1
Page 2
m (1 revision imported)
 
(Module Creation)
 
Line 2: Line 2:
 
-- with a list of arguments, returning a specified result if an argument is
 
-- with a list of arguments, returning a specified result if an argument is
 
-- not on the list
 
-- not on the list
 +
require('strict');
 +
 
local p = {}
 
local p = {}
  
Line 35: Line 37:
 
end
 
end
  
function p._check(args, pargs)
+
function p._check(args, pargs, frame)
 
if type(args) ~= "table" or type(pargs) ~= "table" then
 
if type(args) ~= "table" or type(pargs) ~= "table" then
 
-- TODO: error handling
 
-- TODO: error handling
Line 44: Line 46:
 
local knownargs = {}
 
local knownargs = {}
 
local regexps = {}
 
local regexps = {}
 +
local template_name = isnotempty (args.autofetch) and frame:getParent():getTitle() or nil;
 +
if template_name then -- if a template name is supplied
 +
local template_content = mw.title.new (template_name):getContent() or nil;
 +
if template_content then -- if we could get unparsed template content
 +
template_content = template_content:gsub ('<!%-%-.-%-%->', ''); -- strip html comments; any unique parameters in comments are not supported
 +
for param in template_content:gmatch ('{{{([^|}]+)') do -- get the next parameter name from template
 +
param = mw.text.trim (param); -- trim wihtespace if any
 +
knownargs[param] = 1; -- save it
 +
end
 +
end
 +
-- mw.logObject (knownargs)
 +
end
 
for k, v in pairs(args) do
 
for k, v in pairs(args) do
 
if type(k) == 'number' then
 
if type(k) == 'number' then
Line 92: Line 106:
 
if isnotempty(preview_text) then
 
if isnotempty(preview_text) then
 
preview_text = require('Module:If preview')._warning({preview_text})
 
preview_text = require('Module:If preview')._warning({preview_text})
elseif preview == nil then
+
elseif preview_text == nil then
 
preview_text = unknown_text
 
preview_text = unknown_text
 
end
 
end
Line 114: Line 128:
 
local args = frame.args
 
local args = frame.args
 
local pargs = frame:getParent().args
 
local pargs = frame:getParent().args
return p._check(args, pargs)
+
return p._check(args, pargs, frame)
 
end
 
end
  
 
return p
 
return p