Editing Module:Navbox Automatic Groups
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
| Latest revision | Your text | ||
| Line 3: | Line 3: | ||
local PREFIX = '__NAVBOX_AUTO_GROUPS__' | local PREFIX = '__NAVBOX_AUTO_GROUPS__' | ||
local NOGROUP = '[nogroup]' | local NOGROUP = '[nogroup]' | ||
local CONTROL_ARGS = { | local CONTROL_ARGS = { groups = true, base = true, list = true, list_type = true, start = true } | ||
} | |||
-- Trim a value | -- Trim a value | ||
| Line 20: | Line 13: | ||
local function splitGroups(value) | local function splitGroups(value) | ||
local result = {} | local result = {} | ||
for _, group in ipairs(mw.text.split(value, ';', true)) do | for _, group in ipairs(mw.text.split(value, ';', true)) do | ||
group = trim(group) | group = trim(group) | ||
if group ~= '' then | if group ~= '' then | ||
result[#result + 1] = group | result[#result + 1] = group | ||
end | end | ||
end | end | ||
return result | return result | ||
end | end | ||
| Line 43: | Line 33: | ||
return group | return group | ||
end) | end) | ||
end | end | ||
| Line 62: | Line 38: | ||
local function getListOverrides(args) | local function getListOverrides(args) | ||
local overrides = {} | local overrides = {} | ||
for key, value in pairs(args) do | for key, value in pairs(args) do | ||
local groupName = tostring(key):match('^list_(.+)$') | local groupName = tostring(key):match('^list_(.+)$') | ||
if groupName then | if groupName then | ||
overrides[normalizeGroupName(groupName)] = tostring(value) | overrides[normalizeGroupName(groupName)] = tostring(value) | ||
end | end | ||
end | end | ||
return overrides | return overrides | ||
end | end | ||
| Line 81: | Line 54: | ||
for key, value in pairs(args) do | for key, value in pairs(args) do | ||
key = tostring(key) | key = tostring(key) | ||
if not CONTROL_ARGS[key] and not key:match('^list_.+$') then | if not CONTROL_ARGS[key] and not key:match('^list_.+$') then | ||
if type(value) == 'string' then | if type(value) == 'string' then | ||
| Line 91: | Line 63: | ||
end | end | ||
for i, exclusion in ipairs(exclusions) do | for i, exclusion in ipairs(exclusions) do | ||
dplArgs['not' .. i] = substituteGroup(notFormat, exclusion) | dplArgs['not' .. i] = substituteGroup(notFormat, exclusion) | ||
| Line 99: | Line 70: | ||
end | end | ||
-- Expand Navbox DPL | -- Expand Navbox DPL after placeholders and automatic exclusions are known | ||
local function expandDpl(frame, args, group, exclusions) | local function expandDpl(frame, args, group, exclusions) | ||
local dplArgs = getDplArgs(args, group, exclusions) | local dplArgs = getDplArgs(args, group, exclusions) | ||
return frame:expandTemplate{ title = 'Navbox DPL', args = dplArgs } | |||
return frame:expandTemplate{ | |||
end | end | ||
| Line 117: | Line 79: | ||
function p.main(frame) | function p.main(frame) | ||
local args = {} | local args = {} | ||
for key, value in pairs(frame.args) do | for key, value in pairs(frame.args) do | ||
args[key] = value | args[key] = value | ||
end | end | ||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then | ||
for key, value in pairs(parent.args) do | for key, value in pairs(parent.args) do | ||
| Line 152: | Line 110: | ||
local exclusions = {} | local exclusions = {} | ||
for _, group in ipairs(groups) do | for _, group in ipairs(groups) do | ||
if normalizeGroupName(group) ~= normalizeGroupName(NOGROUP) then | if normalizeGroupName(group) ~= normalizeGroupName(NOGROUP) then | ||
| Line 174: | Line 131: | ||
if list == nil then | if list == nil then | ||
if listType == 'dpl' then | if listType == 'dpl' then | ||
list = expandDpl( | list = expandDpl(frame, args, targetGroup, isNoGroup and exclusions or {}) | ||
else | else | ||
list = substituteGroup(defaultList, targetGroup) | list = substituteGroup(defaultList, targetGroup) | ||
| Line 189: | Line 141: | ||
end | end | ||
local row = { list = list } | |||
if not isNoGroup then | |||
row.group = group | |||
end | end | ||
rows[#rows + 1] = row | |||
end | end | ||
return PREFIX .. mw.text.jsonEncode({ | return PREFIX .. mw.text.jsonEncode({ start = start, rows = rows }) | ||
end | end | ||
return p | return p | ||