Module:Navbox with collapsible groups: Difference between revisions

    From Nonbinary Wiki
    No edit summary
    No edit summary
    Line 22: Line 22:


    function p._navbox(args)
    function p._navbox(args)
    local tracking = ''
    -- table for args passed to navbox
    -- table for args passed to navbox
    local targs = {}
    local targs = {}
    Line 42: Line 40:
    if passthrough[k] then
    if passthrough[k] then
    targs[k] = v
    targs[k] = v
    tracking = tracking .. '<span style="color:green">' .. k .. '</span>; '
    elseif (k:match('^list[0-9][0-9]*$')  
    elseif (k:match('^list[0-9][0-9]*$')  
    or k:match('^content[0-9][0-9]*$') ) then
    or k:match('^content[0-9][0-9]*$') ) then
    Line 49: Line 46:
    and args['sect' .. n] == nil and args['section' .. n] == nil) then
    and args['sect' .. n] == nil and args['section' .. n] == nil) then
    targs['list' .. n] = concatstrings(args['list' .. n] or '', args['content' .. n] or '')
    targs['list' .. n] = concatstrings(args['list' .. n] or '', args['content' .. n] or '')
    tracking = tracking .. '<span style="color:blue">' .. k .. '</span>; '
    end
    end
    elseif (k:match('^group[0-9][0-9]*$')  
    elseif (k:match('^group[0-9][0-9]*$')  
    Line 83: Line 79:
    imageleft = args['imageleft' .. n],
    imageleft = args['imageleft' .. n],
    listpadding = args['listpadding']})
    listpadding = args['listpadding']})
    tracking = tracking .. '<span style="color:red">' .. k .. '</span>; '
    end
    end
    end
    end
    Line 91: Line 86:
    -- child or subgroup
    -- child or subgroup
    if targs['border'] == nil then targs['border'] = args[1] end
    if targs['border'] == nil then targs['border'] = args[1] end
    local tracking = '<hr /><center><b>targs</b></center><hr />\n'
    tracking = tracking .. '\n<hr />\n'
    for k,v in pairs(targs) do
    for k,v in pairs(targs) do
    tracking = tracking .. k .. ' = ' .. v .. '<br>\n'
    tracking = tracking .. k .. ' = ' .. v .. '<br>\n'

    Revision as of 16:11, 24 October 2018

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

    -- This module implements {{Navbox with collapsible groups}}
    local p = {}
    
    local Navbox = require('Module:Navbox')
    local getArgs -- lazily initialized
    
    -- helper functions
    local function concatstrings(t)
    	local res = table.concat(t, '')
    	if res:match('^%s*$') then res = nil end
    	return res
    end
    
    local function concatstyles(t)
    	local res = table.concat(t, ';')
    	while res:match(';%s*;') do
    		res = mw.ustring.gsub(res, ';%s*;', ';')
    	end
    	if res:match('^%s*;%s*$') then res = nil end
    	return res
    end
    
    function p._navbox(args)
    	-- table for args passed to navbox
    	local targs = {}
    
    	-- process args
    	local passthrough = {
    		['name']=1,['navbar']=1,['state']=1,['border']=1,
    		['bodyclass']=1,['groupclass']=1,['listclass']=1,
    		['style']=1,['bodystyle']=1,['basestyle']=1,
    		['title']=1,['titleclass']=1,['titlestyle']=1,
    		['above']=1,['aboveclass']=1,['abovestyle']=1,
    		['below']=1,['belowclass']=1,['belowstyle']=1,
    		['image']=1,['imageclass']=1,['imagestyle']=1,
    		['imageleft']=1,['imageleftstyle']=1
    	}
    	for k,v in pairs(args) do
    		if k and type(k) == 'string' then
    			if passthrough[k] then
    				targs[k] = v
    			elseif (k:match('^list[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['list' .. n] == nil and args['group' .. n] == nil
    					and args['sect' .. n] == nil and args['section' .. n] == nil) then
    					targs['list' .. n] = concatstrings(args['list' .. n] or '', args['content' .. n] or '')
    				end
    			elseif (k:match('^group[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['list' .. n] == nil then
    					local titlestyle = concatstyles(
    						{args['groupstyle'] or '',args['secttitlestyle'] or '', 
    							args['group' .. n .. 'style'] or '', 
    							args['section' .. n ..'titlestyle'] or ''})
    					local liststyle = concatstyles(
    						{args['liststyle'] or '', args['contentstyle'] or '', 
    							args['list' .. n .. 'style'] or '', 
    							args['content' .. n .. 'style'] or ''})
    					local title = concatstrings(
    						{args['group' .. n] or '', 
    							args['sect' .. n] or '',
    							args['section' .. n] or ''})
    					local list = concatstrings(
    						{args['list' .. n] or '', 
    							args['content' .. n] or ''})
    					local state = (args['abbr' .. n] and args['abbr' .. n] == args['selected']) 
    						and 'uncollapsed' or args['state' .. n] or 'collapsed'
    
    					targs['list' .. n] = Navbox._navbox(
    						{'child', navbar = 'plain', state = state,
    						basestyle = args['basestyle'],
    						title = title, titlestyle = titlestyle,
    						list1 = list, liststyle = liststyle,
    						listclass = args['list' .. n .. 'class'],
    						image = args['image' .. n],
    						imageleft = args['imageleft' .. n],
    						listpadding = args['listpadding']})
    				end
    			end
    		end
    	end
    
    	-- child or subgroup
    	if targs['border'] == nil then targs['border'] = args[1] end
    	local tracking = '<hr /><center><b>targs</b></center><hr />\n'
    	for k,v in pairs(targs) do
    		tracking = tracking .. k .. ' = ' .. v .. '<br>\n'
    	end
    	tracking = tracking .. '\n<hr />\n'
    
    	return Navbox._navbox(targs) .. tracking
    end
    
    function p.navbox(frame)
    	if not getArgs then
    		getArgs = require('Module:Arguments').getArgs
    	end
    	args = 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 _
    	_ = args.title
    	_ = args.above
    	for i = 1, 20 do
    		_ = args["group" .. tostring(i)]
    		_ = args["list" .. tostring(i)]
    	end
    	_ = args.below
    
    	return p._navbox(args)
    end
    
    return p