Jump to content

Module:Navbox Automatic Groups

From The Petit Planet Wiki
Revision as of 12:19, 3 September 2026 by ReisuDesign (talk | contribs) (Created page with "local p = {} local PREFIX = '__NAVBOX_AUTO_GROUPS__' local NOGROUP = '[nogroup]' -- 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 loc...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation

This module implements {{Navbox Automatic Groups}}.

local p = {}

local PREFIX = '__NAVBOX_AUTO_GROUPS__'
local NOGROUP = '[nogroup]'

-- 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

-- Build the automatic not parameters
local function makeNotString(exclusions)
	if #exclusions == 0 then
		return ''
	end
	local result = {}
	for i, group in ipairs(exclusions) do
		result[#result + 1] = '|not' .. i .. '=' .. group
	end
	return table.concat(result, '\n')
end

-- Find list_<group> overrides
local function getListOverrides(frame)
	local overrides = {}
	for key, value in pairs(frame.args) do
		local groupName = tostring(key):match('^list_(.+)$')
		if groupName then
			overrides[normalizeGroupName(groupName)] = tostring(value)
		end
	end
	return overrides
end

-- Main
function p.main(frame)
	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

	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)

	if #groups == 0 then
		error('Navbox Automatic Groups: no groups were specified.')
	end

	local overrides = getListOverrides(frame)
	local exclusions = {}

	for _, group in ipairs(groups) do
		if normalizeGroupName(group) ~= normalizeGroupName(NOGROUP) then
			exclusions[#exclusions + 1] = group
		end
	end

	local notString = makeNotString(exclusions)
	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] 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

		rows[#rows + 1] = row
	end

	return PREFIX .. mw.text.jsonEncode({
		start = start,
		rows = rows
	})
end

return p