Difference between revisions of "Module:Sandbox/Erutuon/parameters"
< Module:Sandbox | Erutuon
Jump to navigation
Jump to search
blackwiki>Erutuon (check validity of parameter names) |
blackwiki>Erutuon (got it to work) |
||
| Line 6: | Line 6: | ||
else -- level is actually the format string. | else -- level is actually the format string. | ||
return error(string.format(level, ...), 2) | return error(string.format(level, ...), 2) | ||
| + | end | ||
| + | end | ||
| + | |||
| + | -- From [[wikt:Module:table]]. | ||
| + | local function defaultKeySort(key1, key2) | ||
| + | -- "number" < "string", so numbers will be sorted before strings. | ||
| + | local type1, type2 = type(key1), type(key2) | ||
| + | if type1 ~= type2 then | ||
| + | return type1 < type2 | ||
| + | else | ||
| + | return key1 < key2 | ||
end | end | ||
end | end | ||
| Line 15: | Line 26: | ||
end | end | ||
| − | table.sort(params_sorted) | + | table.sort(params_sorted, defaultKeySort) |
return params_sorted | return params_sorted | ||
| Line 34: | Line 45: | ||
local params = {} | local params = {} | ||
| − | for param_name in content:gmatch '{{{([% | + | for param_name in content:gmatch '{{{([%w _]+)%f[|]' do -- Not a completely accurate pattern. |
| + | -- This will incorrectly convert floats along with integers. | ||
| + | if tonumber(param_name) then | ||
| + | param_name = tonumber(param_name) | ||
| + | end | ||
| + | |||
params[param_name] = true | params[param_name] = true | ||
end | end | ||
| − | return | + | return params |
end | end | ||
function p.check_params(frame) | function p.check_params(frame) | ||
| − | local template_title = mw.title.getCurrentTitle() | + | local template_title = mw.title.getCurrentTitle() |
| − | local params_set = p.get_parameters() | + | |
| + | local params_set = p.get_parameters(template_title) | ||
| + | |||
| + | mw.log(table.concat(p.make_param_list(params_set), ", ")) | ||
| + | |||
local unrecognized_params = {} | local unrecognized_params = {} | ||
| − | for param_name in pairs(frame.args) do | + | for param_name in pairs(frame:getParent().args) do |
| − | if not | + | -- mw.log(param_name) |
| + | if not params_set[param_name] then | ||
unrecognized_params[param_name] = true | unrecognized_params[param_name] = true | ||
end | end | ||
| Line 52: | Line 73: | ||
if next(unrecognized_params) then | if next(unrecognized_params) then | ||
| − | local unrecognized_params_list = p.make_param_list( | + | local unrecognized_params_list = p.make_param_list(unrecognized_params) |
| − | |||
errorf("The parameters %s %s not recognized", | errorf("The parameters %s %s not recognized", | ||
table.concat(unrecognized_params_list, ", "), | table.concat(unrecognized_params_list, ", "), | ||
Revision as of 21:33, 13 August 2018
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
-- From [[wikt:Module:table]].
local function defaultKeySort(key1, key2)
-- "number" < "string", so numbers will be sorted before strings.
local type1, type2 = type(key1), type(key2)
if type1 ~= type2 then
return type1 < type2
else
return key1 < key2
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, defaultKeySort)
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 '{{{([%w _]+)%f[|]' do -- Not a completely accurate pattern.
-- This will incorrectly convert floats along with integers.
if tonumber(param_name) then
param_name = tonumber(param_name)
end
params[param_name] = true
end
return params
end
function p.check_params(frame)
local template_title = mw.title.getCurrentTitle()
local params_set = p.get_parameters(template_title)
mw.log(table.concat(p.make_param_list(params_set), ", "))
local unrecognized_params = {}
for param_name in pairs(frame:getParent().args) do
-- mw.log(param_name)
if not params_set[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)
errorf("The parameters %s %s not recognized",
table.concat(unrecognized_params_list, ", "),
unrecognized_params_list[2] and "are" or "is")
end
end
return p