Module:Navbox with collapsible groups: Difference between revisions

From IxWiki
Jump to navigation Jump to search
>MusikAnimal
m Protected "Module:Navbox with collapsible groups": High-risk Lua module ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))
 
>Ahecht
pseudo-ternary operator doesn't work with "and nil"
 
Line 1: Line 1:
-- This module implements {{Navbox with collapsible groups}}
-- This module implements {{Navbox with collapsible groups}}
local q = {}
 
local Navbox = require('Module:Navbox')
require('strict')
local p = {}
local parentCfg = mw.loadData('Module:Navbox/configuration')
local thisCfg = mw.loadData('Module:Navbox with collapsible groups/configuration')
local cfg = {}
for k, v in pairs(thisCfg) do
if type(v) == 'table' then
cfg[k] = {}
if type(parentCfg[k]) == 'table' then
for kk, vv in pairs(parentCfg[k]) do cfg[k][kk] = vv end
end
for kk, vv in pairs(v) do cfg[k][kk] = vv end
end
end
local inArray = require("Module:TableTools").inArray
local getArgs -- lazily initialized


-- helper functions
-- helper functions
local andnum = function(s, n) return string.format(cfg.arg[s..'_and_num'], n) end
local isblank = function(v) return (v or '') == '' end
local function concatstrings(s)
local function concatstrings(s)
local r = table.concat(s, '')
local r = table.concat(s, '')
if r:match('^%s*$') then r = nil end
if r:match('^%s*$') then return nil end
return r
return r
end
end
 
local function concatstyles(s)
local function concatstyles(s)
local r = table.concat(s, ';')
local r = ''
while r:match(';%s*;') do
for _, v in ipairs(s) do
r = mw.ustring.gsub(r, ';%s*;', ';')
v = mw.text.trim(v, "%s;")
if not isblank(v) then r = r .. v .. ';' end
end
end
if r:match('^%s*;%s*$') then r = nil end
if isblank(r) then return nil end
return r
return r
end
end
local function getSubgroup(args, listnum, listText)
local subArgs = {
[cfg.arg.border] = cfg.keyword.border_subgroup,
[cfg.arg.navbar] = cfg.keyword.navbar_plain
}
local hasSubArgs = false
for k, v in pairs(args) do
k = tostring(k)
for _, w in ipairs(cfg.keyword.subgroups) do
w = w .. listnum .. "_"
if (#k > #w) and (k:sub(1, #w) == w) then
subArgs[k:sub(#w + 1)] = v
hasSubArgs = true
end
end
end
return hasSubArgs and p._navbox(subArgs) or listText
end
-- Main functions
p._navbox = require('Module:Navbox')._navbox


function q._navbox(pargs)
function p._withCollapsibleGroups(pargs)
-- table for args passed to navbox
-- table for args passed to navbox
local targs = {}
local targs = {}
Line 25: Line 67:
-- process args
-- process args
local passthrough = {
local passthrough = {
['name']=true,['navbar']=true,['state']=true,['border']=true,
[cfg.arg.above]=true,[cfg.arg.aboveclass]=true,[cfg.arg.abovestyle]=true,
['bodyclass']=true,['groupclass']=true,['listclass']=true,
[cfg.arg.basestyle]=true,
['style']=true,['bodystyle']=true,['basestyle']=true,
[cfg.arg.below]=true,[cfg.arg.belowclass]=true,[cfg.arg.belowstyle]=true,
['title']=true,['titleclass']=true,['titlestyle']=true,
[cfg.arg.bodyclass]=true,[cfg.arg.bodystyle]=true,
['above']=true,['aboveclass']=true,['abovestyle']=true,
[cfg.arg.border]=true,
['below']=true,['belowclass']=true,['belowstyle']=true,
[cfg.arg.groupclass]=true,
['image']=true,['imageclass']=true,['imagestyle']=true,
[cfg.arg.image]=true,[cfg.arg.imageclass]=true,[cfg.arg.imagestyle]=true,
['imageleft']=true,['imageleftstyle']=true
[cfg.arg.imageleft]=true,[cfg.arg.imageleftstyle]=true,
[cfg.arg.listclass]=true,
[cfg.arg.name]=true,
[cfg.arg.navbar]=true,
[cfg.arg.state]=true,
[cfg.arg.style]=true,
[cfg.arg.title]=true,[cfg.arg.titleclass]=true,[cfg.arg.titlestyle]=true
}
}
for k,v in pairs(pargs) do
for k,v in pairs(pargs) do
Line 38: Line 86:
if passthrough[k] then
if passthrough[k] then
targs[k] = v
targs[k] = v
elseif (k:match('^list[0-9][0-9]*$')  
elseif (k:match(cfg.pattern.num)) then
or k:match('^content[0-9][0-9]*$') ) then
local n = k:match(cfg.pattern.num)
local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
local list_and_num = andnum('list', n)
if (targs['list' .. n] == nil and pargs['group' .. n] == nil
if ((k:match(cfg.pattern.listnum) or k:match(cfg.pattern.contentnum))
and pargs['sect' .. n] == nil and pargs['section' .. n] == nil) then
and targs[list_and_num] == nil
targs['list' .. n] = concatstrings(
and pargs[andnum('group', n)] == nil
{pargs['list' .. n] or '', pargs['content' .. n] or ''})
and pargs[andnum('sect', n)] == nil
end
and pargs[andnum('section', n)] == nil) then
elseif (k:match('^group[0-9][0-9]*$')  
targs[list_and_num] = concatstrings({
or k:match('^sect[0-9][0-9]*$')  
pargs[list_and_num] or '',
or k:match('^section[0-9][0-9]*$') ) then
pargs[andnum('content', n)] or ''
local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
})
if targs['list' .. n] == nil then
if (targs[list_and_num] and inArray(cfg.keyword.subgroups, targs[list_and_num])) then
local titlestyle = concatstyles(
targs[list_and_num] = getSubgroup(pargs, n, targs[list_and_num])
{pargs['groupstyle'] or '',pargs['secttitlestyle'] or '',  
end
pargs['group' .. n .. 'style'] or '',  
elseif ((k:match(cfg.pattern.groupnum) or k:match(cfg.pattern.sectnum) or k:match(cfg.pattern.sectionnum))
pargs['section' .. n ..'titlestyle'] or ''})
and targs[list_and_num] == nil) then
local liststyle = concatstyles(
local titlestyle = concatstyles({
{pargs['liststyle'] or '', pargs['contentstyle'] or '',  
pargs[cfg.arg.groupstyle] or '',
pargs['list' .. n .. 'style'] or '',  
pargs[cfg.arg.secttitlestyle] or '',  
pargs['content' .. n .. 'style'] or ''})
pargs[andnum('groupstyle', n)] or '',  
local title = concatstrings(
pargs[andnum('sectiontitlestyle', n)] or ''
{pargs['group' .. n] or '',  
})
pargs['sect' .. n] or '',
local liststyle = concatstyles({
pargs['section' .. n] or ''})
pargs[cfg.arg.liststyle] or '',
local list = concatstrings(
pargs[cfg.arg.contentstyle] or '',  
{pargs['list' .. n] or '',  
pargs[andnum('liststyle', n)] or '',  
pargs['content' .. n] or ''})
pargs[andnum('contentstyle', n)] or ''
local state = (pargs['abbr' .. n] and pargs['abbr' .. n] == pargs['selected'])  
})
and 'uncollapsed' or pargs['state' .. n] or 'collapsed'
local title = concatstrings({
pargs[andnum('group', n)] or '',
pargs[andnum('sect', n)] or '',
pargs[andnum('section', n)] or ''
})
local list = concatstrings({
pargs[list_and_num] or '',  
pargs[andnum('content', n)] or ''
})
if list and inArray(cfg.keyword.subgroups, list) then
list = getSubgroup(pargs, n, list)
end
local abbr_and_num = andnum('abbr', n)
local state = (pargs[abbr_and_num] and pargs[abbr_and_num] == pargs[cfg.arg.selected])  
and cfg.keyword.state_uncollapsed
or (pargs[andnum('state', n)] or cfg.keyword.state_collapsed)
targs['list' .. n] = Navbox._navbox(
targs[list_and_num] =p._navbox({
{'child', navbar = 'plain', state = state,
cfg.keyword.border_child,
basestyle = pargs['basestyle'],
[cfg.arg.navbar] = cfg.keyword.navbar_plain,
title = title, titlestyle = titlestyle,
[cfg.arg.state] = state,
list1 = list, liststyle = liststyle,
[cfg.arg.basestyle] = pargs[cfg.arg.basestyle],
listclass = pargs['list' .. n .. 'class'],
[cfg.arg.title] = title,
image = pargs['image' .. n],
[cfg.arg.titlestyle] = titlestyle,
imageleft = pargs['imageleft' .. n],
[andnum('list', 1)] = list,
listpadding = pargs['listpadding']})
[cfg.arg.liststyle] = liststyle,
[cfg.arg.listclass] = pargs[andnum('listclass', n)],
[cfg.arg.image] = pargs[andnum('image', n)],
[cfg.arg.imageleft] = pargs[andnum('imageleft', n)],
[cfg.arg.listpadding] = pargs[cfg.arg.listpadding]
})
end
end
end
end
Line 83: Line 151:
end
end
-- ordering of style and bodystyle
-- ordering of style and bodystyle
targs['style'] = concatstyles({targs['style'] or '', targs['bodystyle'] or ''})
targs[cfg.arg.style] = concatstyles({targs[cfg.arg.style] or '', targs[cfg.arg.bodystyle] or ''})
targs['bodystyle'] = nil
targs[cfg.arg.bodystyle] = nil
-- child or subgroup
-- child or subgroup
if targs['border'] == nil then targs['border'] = pargs[1] end
if targs[cfg.arg.border] == nil then targs[cfg.arg.border] = pargs[1] end


return Navbox._navbox(targs)
return p._navbox(targs)
end
end


function q.navbox(frame)
-- Template entry points
local pargs = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Navbox with collapsible groups'}})
function p.navbox (frame, boxtype)
local function readArgs(args, prefix)
-- Read the arguments in the order they'll be output in, to make references
-- number in the right order.
local _
_ = args[prefix .. cfg.arg.title]
_ = args[prefix .. cfg.arg.above]
-- Limit this to 20 as covering 'most' cases (that's a SWAG) and because
-- iterator approach won't work here
for i = 1, 20 do
_ = args[prefix .. andnum('group', i)]
if inArray(cfg.keyword.subgroups, args[prefix .. andnum('list', i)]) then
for _, v in ipairs(cfg.keyword.subgroups) do
readArgs(args, prefix .. v .. i .. "_")
end
end
end
_ = args[prefix .. cfg.arg.below]
end


-- Read the arguments in the order they'll be output in, to make references number in the right order.
if not getArgs then
local _
getArgs = require('Module:Arguments').getArgs
_ = pargs.title
_ = pargs.above
for i = 1, 20 do
_ = pargs["group" .. tostring(i)]
_ = pargs["list" .. tostring(i)]
end
end
_ = pargs.below
local args = getArgs(frame, {wrappers = {cfg.pattern[boxtype or 'navbox']}})
readArgs(args, "")
return p['_'..(boxtype or 'navbox')](args)
end


return q._navbox(pargs)
p['with collapsible groups'] = function (frame)
return p.navbox(frame, 'withCollapsibleGroups')
end
end


local q = {}
q._navbox = p._withCollapsibleGroups
q.navbox = p['with collapsible groups']
return q
return q

Latest revision as of 15:06, 17 September 2024

Documentation for this module may be created at Module:Navbox with collapsible groups/doc

-- This module implements {{Navbox with collapsible groups}}

require('strict')
local p = {}
local parentCfg = mw.loadData('Module:Navbox/configuration')
local thisCfg = mw.loadData('Module:Navbox with collapsible groups/configuration')
local cfg = {}
for k, v in pairs(thisCfg) do
	if type(v) == 'table' then
		cfg[k] = {}
		if type(parentCfg[k]) == 'table' then
			for kk, vv in pairs(parentCfg[k]) do cfg[k][kk] = vv end
		end
		for kk, vv in pairs(v) do cfg[k][kk] = vv end
	end
end
local inArray = require("Module:TableTools").inArray
local getArgs -- lazily initialized

-- helper functions
local andnum = function(s, n) return string.format(cfg.arg[s..'_and_num'], n) end
	
local isblank = function(v) return (v or '') == '' end

local function concatstrings(s)
	local r = table.concat(s, '')
	if r:match('^%s*$') then return nil end
	return r
end
	
local function concatstyles(s)
	local r = ''
	for _, v in ipairs(s) do
		v = mw.text.trim(v, "%s;")
		if not isblank(v) then r = r .. v .. ';' end
	end
	if isblank(r) then return nil end
	return r
end
	
local function getSubgroup(args, listnum, listText)
	local subArgs = {
		[cfg.arg.border] = cfg.keyword.border_subgroup,
		[cfg.arg.navbar] = cfg.keyword.navbar_plain
	}
	local hasSubArgs = false
	for k, v in pairs(args) do
		k = tostring(k)
		for _, w in ipairs(cfg.keyword.subgroups) do
			w = w .. listnum .. "_"
			if (#k > #w) and (k:sub(1, #w) == w) then
				subArgs[k:sub(#w + 1)] = v
				hasSubArgs = true
			end
		end
	end
	return hasSubArgs and p._navbox(subArgs) or listText
end

-- Main functions
p._navbox = require('Module:Navbox')._navbox

function p._withCollapsibleGroups(pargs)
	-- table for args passed to navbox
	local targs = {}

	-- process args
	local passthrough = {
		[cfg.arg.above]=true,[cfg.arg.aboveclass]=true,[cfg.arg.abovestyle]=true,
		[cfg.arg.basestyle]=true,
		[cfg.arg.below]=true,[cfg.arg.belowclass]=true,[cfg.arg.belowstyle]=true,
		[cfg.arg.bodyclass]=true,[cfg.arg.bodystyle]=true,
		[cfg.arg.border]=true,
		[cfg.arg.groupclass]=true,
		[cfg.arg.image]=true,[cfg.arg.imageclass]=true,[cfg.arg.imagestyle]=true,
		[cfg.arg.imageleft]=true,[cfg.arg.imageleftstyle]=true,
		[cfg.arg.listclass]=true,
		[cfg.arg.name]=true,
		[cfg.arg.navbar]=true,
		[cfg.arg.state]=true,
		[cfg.arg.style]=true,
		[cfg.arg.title]=true,[cfg.arg.titleclass]=true,[cfg.arg.titlestyle]=true
	}
	for k,v in pairs(pargs) do
		if k and type(k) == 'string' then
			if passthrough[k] then
				targs[k] = v
			elseif (k:match(cfg.pattern.num)) then
				local n = k:match(cfg.pattern.num)
				local list_and_num = andnum('list', n)
				if ((k:match(cfg.pattern.listnum) or k:match(cfg.pattern.contentnum))
						and targs[list_and_num] == nil
						and pargs[andnum('group', n)] == nil
						and pargs[andnum('sect', n)] == nil
						and pargs[andnum('section', n)] == nil) then
					targs[list_and_num] = concatstrings({
						pargs[list_and_num] or '',
						pargs[andnum('content', n)] or ''
					})
					if (targs[list_and_num] and inArray(cfg.keyword.subgroups, targs[list_and_num])) then
						targs[list_and_num] = getSubgroup(pargs, n, targs[list_and_num])
					end
				elseif ((k:match(cfg.pattern.groupnum) or k:match(cfg.pattern.sectnum) or k:match(cfg.pattern.sectionnum))
						and targs[list_and_num] == nil) then
					local titlestyle = concatstyles({
						pargs[cfg.arg.groupstyle] or '',
						pargs[cfg.arg.secttitlestyle] or '', 
						pargs[andnum('groupstyle', n)] or '', 
						pargs[andnum('sectiontitlestyle', n)] or ''
					})
					local liststyle = concatstyles({
						pargs[cfg.arg.liststyle] or '',
						pargs[cfg.arg.contentstyle] or '', 
						pargs[andnum('liststyle', n)] or '', 
						pargs[andnum('contentstyle', n)] or ''
					})
					local title = concatstrings({
						pargs[andnum('group', n)] or '',
						pargs[andnum('sect', n)] or '',
						pargs[andnum('section', n)] or ''
					})
					local list = concatstrings({
						pargs[list_and_num] or '', 
						pargs[andnum('content', n)] or ''
					})
					if list and inArray(cfg.keyword.subgroups, list) then
						list = getSubgroup(pargs, n, list)
					end
					local abbr_and_num = andnum('abbr', n)
					local state = (pargs[abbr_and_num] and pargs[abbr_and_num] == pargs[cfg.arg.selected]) 
						and cfg.keyword.state_uncollapsed
						or (pargs[andnum('state', n)] or cfg.keyword.state_collapsed)
					
					targs[list_and_num] =p._navbox({
						cfg.keyword.border_child,
						[cfg.arg.navbar] = cfg.keyword.navbar_plain,
						[cfg.arg.state] = state,
						[cfg.arg.basestyle] = pargs[cfg.arg.basestyle],
						[cfg.arg.title] = title,
						[cfg.arg.titlestyle] = titlestyle,
						[andnum('list', 1)] = list,
						[cfg.arg.liststyle] = liststyle,
						[cfg.arg.listclass] = pargs[andnum('listclass', n)],
						[cfg.arg.image] = pargs[andnum('image', n)],
						[cfg.arg.imageleft] = pargs[andnum('imageleft', n)],
						[cfg.arg.listpadding] = pargs[cfg.arg.listpadding]
					})
				end
			end
		end
	end
	-- ordering of style and bodystyle
	targs[cfg.arg.style] = concatstyles({targs[cfg.arg.style] or '', targs[cfg.arg.bodystyle] or ''})
	targs[cfg.arg.bodystyle] = nil
	
	-- child or subgroup
	if targs[cfg.arg.border] == nil then targs[cfg.arg.border] = pargs[1] end

	return p._navbox(targs)
end

-- Template entry points
function p.navbox (frame, boxtype)
	local function readArgs(args, prefix)
		-- Read the arguments in the order they'll be output in, to make references
		-- number in the right order.
		local _
		_ = args[prefix .. cfg.arg.title]
		_ = args[prefix .. cfg.arg.above]
		-- Limit this to 20 as covering 'most' cases (that's a SWAG) and because
		-- iterator approach won't work here
		for i = 1, 20 do
			_ = args[prefix .. andnum('group', i)]
			if inArray(cfg.keyword.subgroups, args[prefix .. andnum('list', i)]) then
				for _, v in ipairs(cfg.keyword.subgroups) do
					readArgs(args, prefix .. v .. i .. "_")
				end
			end
		end
		_ = args[prefix .. cfg.arg.below]
	end

	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame, {wrappers = {cfg.pattern[boxtype or 'navbox']}})
	readArgs(args, "")
	return p['_'..(boxtype or 'navbox')](args)
end

p['with collapsible groups'] = function (frame)
	return p.navbox(frame, 'withCollapsibleGroups')
end

local q = {}
q._navbox = p._withCollapsibleGroups
q.navbox = p['with collapsible groups']
return q