Module:Navbox Automatic Groups: Difference between revisions

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..."
 
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 }


-- Trim a value
-- Trim a value
Line 32: Line 33:
return group
return group
end)
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
end


-- Find list_<group> overrides
-- Find list_<group> overrides
local function getListOverrides(frame)
local function getListOverrides(args)
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
Line 56: Line 45:
end
end
return overrides
return overrides
end
-- Build DPL arguments from all non-control parameters
local function getDplArgs(args, group, exclusions)
local dplArgs = {}
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
if #exclusions > 0 then
for i, exclusion in ipairs(exclusions) do
dplArgs['not' .. i] = exclusion
end
end
return dplArgs
end
-- Expand Navbox DPL after placeholders and automatic exclusions are known
local function expandDpl(frame, args, group, exclusions)
local dplArgs = getDplArgs(args, group, exclusions)
return frame:expandTemplate{ title = 'Navbox DPL', args = dplArgs }
end
end


-- Main
-- Main
function p.main(frame)
function p.main(frame)
local groupsArg = trim(frame.args.groups)
local args = {}
local base = trim(frame.args.base)
for key, value in pairs(frame.args) do
local defaultList = tostring(frame.args.list or '')
args[key] = value
local start = tonumber(trim(frame.args.start)) or 1
end
 
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
if groupsArg == '' then
Line 74: Line 105:


local groups = splitGroups(groupsArg)
local groups = splitGroups(groupsArg)
 
local overrides = getListOverrides(args)
if #groups == 0 then
error('Navbox Automatic Groups: no groups were specified.')
end
 
local overrides = getListOverrides(frame)
local exclusions = {}
local exclusions = {}


Line 88: Line 114:
end
end


local notString = makeNotString(exclusions)
local rows = {}
local rows = {}


Line 100: Line 125:


local overrideKey = normalizeGroupName(isNoGroup and 'nogroup' or group)
local overrideKey = normalizeGroupName(isNoGroup and 'nogroup' or group)
local list = overrides[overrideKey] or defaultList
local list = overrides[overrideKey]


list = substituteGroup(list, targetGroup)
if list == nil then
list = list:gsub('%%%%not%%%%', function()
if listType == 'dpl' then
return isNoGroup and notString or ''
list = expandDpl(frame, args, targetGroup, isNoGroup and exclusions or {})
end)
else
 
list = substituteGroup(defaultList, targetGroup)
local row = {
list = list:gsub('%%%%not%%%%', '')
list = list
end
}
else
list = substituteGroup(list, targetGroup)
list = list:gsub('%%%%not%%%%', '')
end


local row = { list = list }
if not isNoGroup then
if not isNoGroup then
row.group = group
row.group = group
end
end
rows[#rows + 1] = row
rows[#rows + 1] = row
end
end


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


return p
return p