Editing Module:Navbox Automatic Groups
Appearance
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]' | ||
-- Trim a value | -- Trim a value | ||
| Line 20: | Line 12: | ||
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 45: | Line 34: | ||
end | end | ||
-- | -- Build the automatic not parameters | ||
local function | local function makeNotString(exclusions) | ||
if #exclusions == 0 then | |||
local | return '' | ||
end | |||
local result = {} | |||
for i, group in ipairs(exclusions) do | |||
result[#result + 1] = '|not' .. i .. '=' .. group | |||
end | end | ||
return table.concat(result, '\n') | |||
return | |||
end | end | ||
-- Find list_<group> overrides | -- Find list_<group> overrides | ||
local function getListOverrides( | local function getListOverrides(frame) | ||
local overrides = {} | local overrides = {} | ||
for key, value in pairs(frame.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 | ||
-- Main | -- Main | ||
function p.main(frame) | function p.main(frame) | ||
local | local groupsArg = trim(frame.args.groups) | ||
local base = trim(frame.args.base) | |||
local defaultList = tostring(frame.args.list or '') | |||
local start = tonumber(trim(frame.args.start)) or 1 | |||
local base = trim(args.base) | |||
local defaultList = tostring(args.list or '' | |||
local start = tonumber(trim(args.start)) or 1 | |||
if groupsArg == '' then | if groupsArg == '' then | ||
| Line 149: | Line 74: | ||
local groups = splitGroups(groupsArg) | local groups = splitGroups(groupsArg) | ||
local overrides = getListOverrides( | |||
if #groups == 0 then | |||
error('Navbox Automatic Groups: no groups were specified.') | |||
end | |||
local overrides = getListOverrides(frame) | |||
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 159: | Line 88: | ||
end | end | ||
local notString = makeNotString(exclusions) | |||
local rows = {} | local rows = {} | ||
| Line 170: | Line 100: | ||
local overrideKey = normalizeGroupName(isNoGroup and 'nogroup' or group) | local overrideKey = normalizeGroupName(isNoGroup and 'nogroup' or group) | ||
local list = overrides[overrideKey] | local list = overrides[overrideKey] or defaultList | ||
list = substituteGroup(list, targetGroup) | |||
list = list:gsub('%%%%not%%%%', function() | |||
return isNoGroup and notString or '' | |||
end) | |||
local row = { | |||
list = list | |||
} | |||
if not isNoGroup then | |||
row.group = group | |||
end | end | ||
rows[#rows + 1] = row | |||
end | end | ||