Jump to content

Editing Module:Navbox Automatic Groups

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
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 = {
groups = true,
base = true,
list = true,
list_type = true,
not_format = true,
start = true
}


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


-- Link a group name if the page exists
-- Build the automatic not parameters
local function linkGroup(group)
local function makeNotString(exclusions)
local page = mw.text.decode(group or '')
if #exclusions == 0 then
local frame = mw.getCurrentFrame()
return ''
 
end
if page ~= ''
local result = {}
and not page:match('[#<>%[%]{}]+')
for i, group in ipairs(exclusions) do
and frame:callParserFunction('PROTECTIONEXPIRY:edit', page) ~= '' then
result[#result + 1] = '|not' .. i .. '=' .. group
return '[[' .. group .. '|' .. group .. ']]'
end
end
 
return table.concat(result, '\n')
return group
end
end


-- Find list_<group> overrides
-- Find list_<group> overrides
local function getListOverrides(args)
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
-- 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
end


-- Main
-- Main
function p.main(frame)
function p.main(frame)
local args = {}
local groupsArg = trim(frame.args.groups)
 
local base = trim(frame.args.base)
-- Get arguments passed directly to the module
local defaultList = tostring(frame.args.list or '')
for key, value in pairs(frame.args) do
local start = tonumber(trim(frame.args.start)) or 1
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
if groupsArg == '' then
Line 149: Line 74:


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 = {}


-- 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 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
 
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
list = substituteGroup(list, targetGroup)
if hasContent(list) then
list = list:gsub('%%%%not%%%%', function()
local row = {
return isNoGroup and notString or ''
list = list
end)
}


-- Link normal group names when the corresponding page exists
local row = {
if not isNoGroup then
list = list
row.group = linkGroup(group)
}
end


rows[#rows + 1] = row
if not isNoGroup then
row.group = group
end
end
rows[#rows + 1] = row
end
end


Please note that all contributions to The Petit Planet Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see Petit Planet Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template
Below are commonly used wikitext from MediaWiki:Edittools. Click on any to insert them in the edit box.
File Pages==Summary====Licensing=={{Fairuse}}{{File|}}
Dynamic Page List¦²«»²{¦}²«»«/»«!----»
Array in DPL¹^2{}^2^2{¹}^2
Parser Functions{{#if:||}}
{{#ifeq:|||}}
{{#expr:}}
{{#switch:||#default=}}
{{#replace:||}}
{{#titleparts:}}
{{#tag:|}}
Markup{{}}|[][[]][[Category:]]#REDIRECT [[]] [[Category:Redirect Pages]]<code></code><includeonly></includeonly><noinclude></noinclude><nowiki></nowiki><!----><br>§
Notices{{Stub|}}{{Stub Dialogue}}{{Upcoming}}{{Under Construction}}{{Placeholder|}}
Magic Words{{!}}{{formatnum:}}{{lc:}}{{uc:}}{{PAGENAME}}{{FULLPAGENAME}}{{ROOTPAGENAME}}{{BASEPAGENAME}}{{SUBPAGENAME}}{{DISPLAYTITLE:|noreplace}}__TOC____NOTOC____EXPECTUNUSEDCATEGORY____HIDDENCAT__
HTML Entities&mdash;&nbsp;&comma;&shy;