Spring til indhold

Modul:Navboks med sammenklappelige afsnit

Fra Wikipedia, den frie encyklopædi

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

-- 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 = {
		['navn']=true,['navbar']=true,['status']=true,['ramme']=true,
		['body_class']=true,['gruppe_class']=true,['liste_class']=true,
		['style']=true,['body_style']=true,['grund_style']=true,
		['titel']=true,['titel_class']=true,['titel_style']=true,
		['over']=true,['over_class']=true,['over_style']=true,
		['under']=true,['under_class']=true,['under_style']=true,
		['billede']=true,['billede_class']=true,['billede_style']=true,
		['billede_venstre']=true,['billede_venstre_style']=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('^liste[0-9][0-9]*$') 
					or k:match('^content[0-9][0-9]*$') ) then
				local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
				if (targs['liste' .. n] == nil and pargs['gruppe' .. n] == nil
					and pargs['sect' .. n] == nil and pargs['section' .. n] == nil) then
					targs['liste' .. n] = concatstrings(
						{pargs['liste' .. n] or '', pargs['content' .. n] or ''})
				end
			elseif (k:match('^gruppe[0-9][0-9]*$') 
					or k:match('^sect[0-9][0-9]*$') 
					or k:match('^section[0-9][0-9]*$') ) then
				local n = mw.ustring.gsub(k, '^[a-z]*([0-9]*)$', '%1')
				if targs['liste' .. n] == nil then
					local titel_style = concatstyles(
						{pargs['gruppe_style'] or '',pargs['secttitle_style'] or '', 
							pargs['gruppe' .. n .. '_style'] or '', 
							pargs['section' .. n ..'titel_style'] or ''})
					local liste_style = concatstyles(
						{pargs['liste_style'] or '', pargs['content_style'] or '', 
							pargs['liste' .. n .. '_style'] or '', 
							pargs['content' .. n .. '_style'] or ''})
					local titel = concatstrings(
						{pargs['gruppe' .. n] or '', 
							pargs['sect' .. n] or '',
							pargs['section' .. n] or ''})
					local liste = concatstrings(
						{pargs['liste' .. n] or '', 
							pargs['content' .. n] or ''})
					local status = (pargs['abbr' .. n] and pargs['abbr' .. n] == pargs['selected']) 
						and 'uncollapsed' or pargs['status' .. n] or 'collapsed'
					
					targs['liste' .. n] = Navbox._navbox(
						{'child', navbar = 'plain', status = status,
						grund_style = pargs['grund_style'],
						titel = titel, titel_style = titel_style,
						liste1 = liste, liste_style = liste_style,
						liste_class = pargs['liste' .. n .. '_class'],
						billede = pargs['billede' .. n],
						billede_venstre = pargs['billede_venstre' .. n],
						liste_padding = pargs['liste_padding']})
				end
			end
		end
	end
	-- ordering of style and bodystyle
	targs['style'] = concatstyles({targs['style'] or '', targs['body_style'] or ''})
	targs['body_style'] = nil
	
	-- child or subgroup
	if targs['ramme'] == nil then targs['ramme'] = pargs[1] end

	return Navbox._navbox(targs)
end

function q.navbox(frame)
	local pargs = require('Module:Arguments').getArgs(frame, {wrappers = {'Skabelon:Navboks med sammenklappelige afsnit'}})

	-- Read the arguments in the order they'll be output in, to make references number in the right order.
	local _
	_ = pargs.titel
	_ = pargs.over
	for i = 1, 20 do
		_ = pargs["gruppe" .. tostring(i)]
		_ = pargs["liste" .. tostring(i)]
	end
	_ = pargs.under

	return q._navbox(pargs)
end

return q