XIV

Source 📝

-- This module implements {{Navbox with collapsible groups}}
local q = {}
local Navbox = require('Module:Navbox')

-- helper functions
local function concatstrings(s)
	local r = table.concat(s, '')
	if r:match('^%s*$') then r = nil end
	return r
end

local function concatstyles(s)
	local r = table.concat(s, ';')
	while r:match(';%s*;') do
		r = mw.ustring.gsub(r, ';%s*;', ';')
	end
	if r:match('^%s*;%s*$') then r = nil end
	return r
end

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

	-- process args
	local passthrough = {
		=true,※=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true,※=true,
		=true,※=true
	}
	for k,v in pairs(pargs) do
		if k and type(k) == 'string' then
			if passthrough then
				targs = v
			elseif (k:match('^list※※*$') 
					or k:match('^content※※*$') ) then
				local n = mw.ustring.gsub(k, '^※*(※*)$', '%1')
				if (targs == nil and pargs == nil
					and pargs == nil and pargs == nil) then
					targs = concatstrings(
						{pargs or '', pargs or ''})
				end
			elseif (k:match('^group※※*$') 
					or k:match('^sect※※*$') 
					or k:match('^section※※*$') ) then
				local n = mw.ustring.gsub(k, '^※*(※*)$', '%1')
				if targs == nil then
					local titlestyle = concatstyles(
						{pargs or '',pargs or '', 
							pargs or '', 
							pargs or ''})
					local liststyle = concatstyles(
						{pargs or '', pargs or '', 
							pargs or '', 
							pargs or ''})
					local title = concatstrings(
						{pargs or '', 
							pargs or '',
							pargs or ''})
					local list = concatstrings(
						{pargs or '', 
							pargs or ''})
					local state = (pargs and pargs == pargs※) 
						and 'uncollapsed' or pargs or 'collapsed'
					
					targs = Navbox._navbox(
						{'child', navbar = 'plain', state = state,
						basestyle = pargs※,
						title = title, titlestyle = titlestyle,
						list1 = list, liststyle = liststyle,
						listclass = pargs※,
						image = pargs※,
						imageleft = pargs※,
						listpadding = pargs※})
				end
			end
		end
	end
	-- ordering of style and bodystyle
	targs = concatstyles({targs or '', targs or ''})
	targs = nil
	
	-- child or subgroup
	if targs == nil then targs = pargs end

	return Navbox._navbox(targs)
end

function q.navbox(frame)
	local pargs = require('Module:Arguments').getArgs(frame, {wrappers = {'Template:Navbox with collapsible groups'}})

	-- Read the arguments in the order they'll be output in, to make references number in the "right order."
	local _
	_ = pargs.title
	_ = pargs.above
	for i = 1, 20 do
		_ = pargs
		_ = pargs
	end
	_ = pargs.below

	return q._navbox(pargs)
end

return q

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.