Module:Navbox Automatic Groups: Difference between revisions
Appearance
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| Line 3: | Line 3: | ||
local PREFIX = '__NAVBOX_AUTO_GROUPS__' | local PREFIX = '__NAVBOX_AUTO_GROUPS__' | ||
local NOGROUP = '[nogroup]' | local NOGROUP = '[nogroup]' | ||
local CONTROL_ARGS = { groups = true, base = true, list = true, list_type = true, start = true } | local CONTROL_ARGS = { | ||
groups = true, | |||
base = true, | |||
list = true, | |||
list_type = true, | |||
not_format = true, | |||
start = true | |||
} | |||
-- Trim a value | -- Trim a value | ||
| Line 13: | Line 20: | ||
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 38: | Line 48: | ||
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 54: | Line 67: | ||
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 63: | Line 77: | ||
end | end | ||
-- Add automatically generated DPL exclusions | |||
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 70: | Line 85: | ||
end | end | ||
-- Expand Navbox DPL | -- Expand Navbox DPL | ||
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{ | ||
title = 'Navbox DPL', | |||
args = dplArgs | |||
} | |||
end | |||
-- Check whether expanded content actually contains anything | |||
local function hasContent(value) | |||
return mw.text.trim(tostring(value or '')) ~= '' | |||
end | end | ||
| Line 79: | Line 102: | ||
function p.main(frame) | function p.main(frame) | ||
local args = {} | local args = {} | ||
-- Get arguments passed directly to the module | |||
for key, value in pairs(frame.args) do | for key, value in pairs(frame.args) do | ||
args[key] = value | args[key] = value | ||
end | end | ||
-- Get arguments passed through Template:Navbox Automatic Groups | |||
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 110: | Line 137: | ||
local exclusions = {} | local exclusions = {} | ||
-- Build the list of named groups used for [nogroup] 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 131: | Line 159: | ||
if list == nil then | if list == nil then | ||
if listType == 'dpl' then | if listType == 'dpl' then | ||
list = expandDpl(frame, args, targetGroup, isNoGroup and exclusions or {}) | list = expandDpl( | ||
frame, | |||
args, | |||
targetGroup, | |||
isNoGroup and exclusions or {} | |||
) | |||
else | else | ||
list = substituteGroup(defaultList, targetGroup) | list = substituteGroup(defaultList, targetGroup) | ||
| Line 141: | Line 174: | ||
end | end | ||
local row = { list = list } | -- Do not create a row when the generated list is empty | ||
if hasContent(list) then | |||
local row = { | |||
list = list | |||
} | |||
if not isNoGroup then | |||
row.group = group | |||
end | |||
rows[#rows + 1] = row | |||
end | end | ||
end | end | ||
return PREFIX .. mw.text.jsonEncode({ start = start, rows = rows }) | return PREFIX .. mw.text.jsonEncode({ | ||
start = start, | |||
rows = rows | |||
}) | |||
end | end | ||
return p | return p | ||
Revision as of 14:27, 3 September 2026
| Main | Documentation | Usage |
This module implements {{Navbox Automatic Groups}}.
local p = {}
local PREFIX = '__NAVBOX_AUTO_GROUPS__'
local NOGROUP = '[nogroup]'
local CONTROL_ARGS = {
groups = true,
base = true,
list = true,
list_type = true,
not_format = true,
start = true
}
-- Trim a value
local function trim(value)
return mw.text.trim(tostring(value or ''))
end
-- Split the groups parameter
local function splitGroups(value)
local result = {}
for _, group in ipairs(mw.text.split(value, ';', true)) do
group = trim(group)
if group ~= '' then
result[#result + 1] = group
end
end
return result
end
-- Normalize a group name for override matching
local function normalizeGroupName(value)
value = trim(value):gsub('%s+', ' ')
return mw.ustring.lower(value)
end
-- Replace the group placeholder
local function substituteGroup(value, group)
return tostring(value or ''):gsub('%%%%group%%%%', function()
return group
end)
end
-- Find list_<group> overrides
local function getListOverrides(args)
local overrides = {}
for key, value in pairs(args) do
local groupName = tostring(key):match('^list_(.+)$')
if groupName then
overrides[normalizeGroupName(groupName)] = tostring(value)
end
end
return overrides
end
-- Build DPL arguments
local function getDplArgs(args, group, exclusions)
local dplArgs = {}
local notFormat = tostring(args.not_format or '%%group%%')
for key, value in pairs(args) do
key = tostring(key)
if not CONTROL_ARGS[key] and not key:match('^list_.+$') then
if type(value) == 'string' then
dplArgs[key] = substituteGroup(value, group)
else
dplArgs[key] = value
end
end
end
-- Add automatically generated DPL exclusions
for i, exclusion in ipairs(exclusions) do
dplArgs['not' .. i] = substituteGroup(notFormat, exclusion)
end
return dplArgs
end
-- Expand Navbox DPL
local function expandDpl(frame, args, group, exclusions)
local dplArgs = getDplArgs(args, group, exclusions)
return frame:expandTemplate{
title = 'Navbox DPL',
args = dplArgs
}
end
-- Check whether expanded content actually contains anything
local function hasContent(value)
return mw.text.trim(tostring(value or '')) ~= ''
end
-- Main
function p.main(frame)
local args = {}
-- Get arguments passed directly to the module
for key, value in pairs(frame.args) do
args[key] = value
end
-- Get arguments passed through Template:Navbox Automatic Groups
local parent = frame:getParent()
if parent then
for key, value in pairs(parent.args) do
if args[key] == nil then
args[key] = value
end
end
end
local groupsArg = trim(args.groups)
local base = trim(args.base)
local defaultList = tostring(args.list or '')
local listType = mw.ustring.lower(trim(args.list_type))
local start = tonumber(trim(args.start)) or 1
if groupsArg == '' then
error('Navbox Automatic Groups: missing groups parameter.')
end
if start < 1 or start % 1 ~= 0 then
error('Navbox Automatic Groups: start must be a positive integer.')
end
local groups = splitGroups(groupsArg)
local overrides = getListOverrides(args)
local exclusions = {}
-- Build the list of named groups used for [nogroup] exclusions
for _, group in ipairs(groups) do
if normalizeGroupName(group) ~= normalizeGroupName(NOGROUP) then
exclusions[#exclusions + 1] = group
end
end
local rows = {}
for _, group in ipairs(groups) do
local isNoGroup = normalizeGroupName(group) == normalizeGroupName(NOGROUP)
local targetGroup = isNoGroup and base or group
if isNoGroup and base == '' then
error('Navbox Automatic Groups: [nogroup] requires a base parameter.')
end
local overrideKey = normalizeGroupName(isNoGroup and 'nogroup' or group)
local list = overrides[overrideKey]
if list == nil then
if listType == 'dpl' then
list = expandDpl(
frame,
args,
targetGroup,
isNoGroup and exclusions or {}
)
else
list = substituteGroup(defaultList, targetGroup)
list = list:gsub('%%%%not%%%%', '')
end
else
list = substituteGroup(list, targetGroup)
list = list:gsub('%%%%not%%%%', '')
end
-- Do not create a row when the generated list is empty
if hasContent(list) then
local row = {
list = list
}
if not isNoGroup then
row.group = group
end
rows[#rows + 1] = row
end
end
return PREFIX .. mw.text.jsonEncode({
start = start,
rows = rows
})
end
return p