Anonymous user
Module:Template test case: Difference between revisions
fix error message for missing template parameters and remove dependency on Module:TableTools
m (fix variable name) |
(fix error message for missing template parameters and remove dependency on Module:TableTools) |
||
Line 3:
-- Load required modules
local yesno = require('Module:Yesno')
-- Set constants
Line 156 ⟶ 155:
obj.cfg = cfg
--
local generalOptions, templateOptions = {}, {}
do
-- Separate numbered and named options
local highestNum = 0▼
local optionNum = {} -- a unique key for option numbers inside templateOptions
for k in pairs(options) do▼
local rawTemplateOptions = {}
▲ for k, v in pairs(options) do
local prefix, num
if type(k) == 'string' then
if prefix then
num = tonumber(num)
rawTemplateOptions[num] = rawTemplateOptions[num] or {}
rawTemplateOptions[num][prefix] = v
rawTemplateOptions[num][optionNum] = num -- record for use in error messages
▲ end
else
generalOptions[k] = v
end
end
-- Remove gaps in the numbered options
for i = 3, highestNum do▼
if not options['template' .. i] then▼
for num in pairs(rawTemplateOptions) do
nums[#nums + 1] = num
end
table.sort(nums)
templateOptions[i] = rawTemplateOptions[num]
end
-- Check that there are no missing template options.
for i = 3, #templateOptions do -- Defaults are added later for 1 and 2
local num = t[optionNum]
error(obj:message(
'missing-template-option-error',
), 2)
end
Line 178 ⟶ 197:
end
--
▲ obj.options = templateOptions.other or {}
obj.options = generalOptions
▲ obj.options.showcode = yesno(obj.options.showcode)
▲ obj.options.collapsible = yesno(obj.options.collapsible)
-- Add default template options
Line 190 ⟶ 206:
templateOptions[2] = templateOptions[2] or {}
if templateOptions[1].template and not templateOptions[2].template then
templateOptions[2].template = templateOptions[1].template ..
end
if not templateOptions[1].template then
|