Module:Navbox: Difference between revisions
Removed auto-category |
ReisuDesign (talk | contribs) adds support for automatic groups |
||
| Line 136: | Line 136: | ||
args = h.parseArgs(args) | args = h.parseArgs(args) | ||
end | end | ||
args = h.expandAutomaticGroups(args) | |||
h.runHook('onLoadConfig', config, args) | h.runHook('onLoadConfig', config, args) | ||
| Line 161: | Line 163: | ||
return h.render(dataTree) | return h.render(dataTree) | ||
end | end | ||
end | |||
-- Expand automatic group markers | |||
function h.expandAutomaticGroups(args) | |||
local PREFIX = '__NAVBOX_AUTO_GROUPS__' | |||
local markers = {} | |||
for k, v in pairs(args) do | |||
if type(k) == 'number' and type(v) == 'string' and v:sub(1, #PREFIX) == PREFIX then | |||
markers[#markers + 1] = k | |||
end | |||
end | |||
table.sort(markers) | |||
for _, markerKey in ipairs(markers) do | |||
local payload = args[markerKey]:sub(#PREFIX + 1) | |||
local ok, data = pcall(mw.text.jsonDecode, payload) | |||
if not ok or type(data) ~= 'table' or type(data.rows) ~= 'table' then | |||
error('Invalid Navbox Automatic Groups data.') | |||
end | |||
local index = tonumber(data.start) or 1 | |||
if index < 1 or index % 1 ~= 0 then | |||
error('Invalid Navbox Automatic Groups start index.') | |||
end | |||
for _, row in ipairs(data.rows) do | |||
local groupKey = index .. ':group' | |||
local listKey = index .. ':list' | |||
if args[groupKey] ~= nil or args[listKey] ~= nil then | |||
error('Navbox Automatic Groups overlaps an existing row at index ' .. index .. '.') | |||
end | |||
if row.group ~= nil and row.group ~= '' then | |||
args[groupKey] = row.group | |||
end | |||
if row.list ~= nil then | |||
args[listKey] = row.list | |||
end | |||
index = index + 1 | |||
end | |||
args[markerKey] = nil | |||
end | |||
return args | |||
end | end | ||