Module:Sandbox/Erutuon/parameters
< Module:Sandbox | Erutuon
Jump to navigation
Jump to search
Revision as of 21:10, 13 August 2018 by blackwiki>Erutuon (check validity of parameter names)
Script error: The function "show_parameters" does not exist.
local p = {}
local function errorf(level, ...)
if type(level) == number then
return error(string.format(...), level + 1)
else -- level is actually the format string.
return error(string.format(level, ...), 2)
end
end
function p.make_param_list(params_set)
local params_sorted = {}
for param_name in pairs(params_set) do
table.insert(params_sorted, param_name)
end
table.sort(params_sorted)
return params_sorted
end
function p.get_parameters(pagename)
local page
if type(pagename) == "string" then
page = mw.title.new(pagename)
elseif type(pagename) == "table" and pagename.nsText then
page = pagename
else
errorf("Bad argument #1 to get_parameters (expected pagename or page title, got %s)",
type(pagename))
end
local content = page:getContent()
local params = {}
for param_name in content:gmatch '{{{([%a _]+)%f[|]' do -- Not a completely accurate pattern.
params[param_name] = true
end
return p
end
function p.check_params(frame)
local template_title = mw.title.getCurrentTitle() -- Will this work when template is transcluded?
local params_set = p.get_parameters()
local unrecognized_params = {}
for param_name in pairs(frame.args) do
if not params[param_name] then
unrecognized_params[param_name] = true
end
end
if next(unrecognized_params) then
local unrecognized_params_list = p.make_param_list(unrecognized_params_list)
local fun = require "Module:fun"
errorf("The parameters %s %s not recognized",
table.concat(unrecognized_params_list, ", "),
unrecognized_params_list[2] and "are" or "is")
end
end
return p