לדלג לתוכן

יחידה:Navbox with collapsible groups

מתוך ויקיפדיה, האנציקלופדיה החופשית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Navbox with collapsible groups/תיעוד

-- This module implements {{ניווט קבוצות עם הסתרה}}
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 = {
		['name']=true,['navbar']=true,['state']=true,['border']=true,
		['bodyclass']=true,['groupclass']=true,['listclass']=true,
		['style']=true,['bodystyle']=true,['basestyle']=true,
		['title']=true,['titleclass']=true,['titlestyle']=true,
		['above']=true,['aboveclass']=true,['abovestyle']=true,
		['below']=true,['belowclass']=true,['belowstyle']=true,
		['image']=true,['imageclass']=true,['imagestyle']=true,
		['imageleft']=true,['imageleftstyle']=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('^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 pargs['group' .. n] == nil
					and pargs['sect' .. n] == nil and pargs['section' .. n] == nil) then
					targs['list' .. n] = concatstrings(
						{pargs['list' .. n] or '', pargs['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(
						{pargs['groupstyle'] or '',pargs['secttitlestyle'] or '', 
							pargs['group' .. n .. 'style'] or '', 
							pargs['section' .. n ..'titlestyle'] or ''})
					local liststyle = concatstyles(
						{pargs['liststyle'] or '', pargs['contentstyle'] or '', 
							pargs['list' .. n .. 'style'] or '', 
							pargs['content' .. n .. 'style'] or ''})
					local title = concatstrings(
						{pargs['group' .. n] or '', 
							pargs['sect' .. n] or '',
							pargs['section' .. n] or ''})
					local list = concatstrings(
						{pargs['list' .. n] or '', 
							pargs['content' .. n] or ''})
					local state = (pargs['abbr' .. n] and pargs['abbr' .. n] == pargs['selected']) 
						and 'uncollapsed' or pargs['state' .. n] or 'collapsed'
					
					targs['list' .. n] = Navbox._navbox(
						{'child', navbar = 'plain', state = state,
						basestyle = pargs['basestyle'],
						title = title, titlestyle = titlestyle,
						list1 = list, liststyle = liststyle,
						listclass = pargs['list' .. n .. 'class'],
						image = pargs['image' .. n],
						imageleft = pargs['imageleft' .. n],
						listpadding = pargs['listpadding']})
				end
			end
		end
	end
	-- ordering of style and bodystyle
	targs['style'] = concatstyles({targs['style'] or '', targs['bodystyle'] or ''})
	targs['bodystyle'] = nil
	
	-- child or subgroup
	if targs['border'] == nil then targs['border'] = pargs[1] end

	return Navbox._navbox(targs)
end

function q.navbox(frame)
	local pargs = require('Module:Arguments').getArgs(frame, {wrappers = {'תבנית:ניווט קבוצות עם הסתרה'}})

	-- Translate ordinal Hebrew letters
	local translatedPargs = {};
	for k,v in pairs(pargs) do
		if mw.ustring.match(k, '[א-ת]') ~=nil then
			local enKey = mw.ustring.gsub( k, 'שם', 'name' )
			enKey = mw.ustring.gsub( enKey, 'הסתרה', 'state' )
			enKey = mw.ustring.gsub( enKey, 'קישורים', 'navbar' )
			enKey = mw.ustring.gsub( enKey, 'גבול', 'border' )
			enKey = mw.ustring.gsub( enKey, 'סגנון תוכן', 'bodystyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון בסיסי', 'basestyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון כותרת', 'titlestyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון קבוצה', 'groupstyle' )
			enKey = mw.ustring.gsub( enKey, 'רוחב קבוצה', 'groupwidth' )
			enKey = mw.ustring.gsub( enKey, 'סגנון רשימה', 'liststyle' )
			enKey = mw.ustring.gsub( enKey, 'ריפוד רשימה', 'listpadding' )
			enKey = mw.ustring.gsub( enKey, 'סגנון אי זוגי', 'oddstyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון זוגי', 'evenstyle' )
			enKey = mw.ustring.gsub( enKey, 'זוגי ואי זוגי', 'evenodd' )
			enKey = mw.ustring.gsub( enKey, 'סגנון תמונה משמאל', 'imageleftstyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון תמונה', 'imagestyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון למעלה', 'abovestyle' )
			enKey = mw.ustring.gsub( enKey, 'סגנון למטה', 'belowstyle' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה תבנית', 'navboxclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה תוכן', 'bodyclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה כותרת', 'titleclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה קבוצה', 'groupclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה רשימה', 'listclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה למעלה', 'aboveclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה למטה', 'belowclass' )
			enKey = mw.ustring.gsub( enKey, 'מחלקה קבוצה בכותרת', 'titlegroupstyle' )
			enKey = mw.ustring.gsub( enKey, 'קבוצה בכותרת', 'titlegroup' )
			enKey = mw.ustring.gsub( enKey, 'סגנון פנימי', 'innerstyle' )
			enKey = mw.ustring.gsub( enKey, 'לא לעטוף', 'nowrapitems' )
			enKey = mw.ustring.gsub( enKey, 'יתום', 'orphan' )
			enKey = mw.ustring.gsub( enKey, 'כותרת', 'title' )
			enKey = mw.ustring.gsub( enKey, 'קבוצה', 'group' )
			enKey = mw.ustring.gsub( enKey, 'רשימה', 'list' )
			enKey = mw.ustring.gsub( enKey, 'סגנון', 'style' )
			enKey = mw.ustring.gsub( enKey, 'תמונה משמאל', 'imageleft' )
			enKey = mw.ustring.gsub( enKey, 'תמונה', 'image' )
			enKey = mw.ustring.gsub( enKey, 'למעלה', 'above' )
			enKey = mw.ustring.gsub( enKey, 'למטה', 'below' )
			enKey = mw.ustring.gsub( enKey, 'קיצור', 'abbr' )
			enKey = mw.ustring.gsub( enKey, 'נבחר', 'selected' )

			translatedPargs[enKey] = v
		else
			translatedPargs[k] = v
		end
	end
	
	local paramOptions = {
		["state"] = {
			["אוטומטי"] = "autocollapse",
			["מוסתר"] = "collapsed",
			["מורחב"] = "expanded",
			["פשוט"] = "plain",
			["ללא"] = "off"
		},
		["navbar"] = {
			["פשוט"] = "plain",
			["ללא"] = "off"
		},
		["border"] = {
			["צאצא"] = "child",
			["תת קבוצה"] = "subgroup",
			["ללא"] = "none"
		},
		["evenodd"] = {
			 ["הפוך"] = "swap",
			 ["זוגי"] = "even",
			 ["אי זוגי"] = "odd",
			 ["ללא"] = "off"
		},
		["nowrapitems"] = {
			["כן"] = "yes"
		},
		["orphan"] = {
			["כן"] = "yes"
		}
	}
	
	for k,v in pairs(paramOptions) do
		for hebrewValue,englishValue in pairs(v) do
			if translatedPargs[k] == hebrewValue then
				translatedPargs[k] = englishValue
			end
		end
	end

	-- Read the arguments in the order they'll be output in, to make references number in the right order.
	local _
	_ = translatedPargs.title
	_ = translatedPargs.above
	for i = 1, 20 do
		_ = translatedPargs["group" .. tostring(i)]
		_ = translatedPargs["list" .. tostring(i)]
	end
	_ = translatedPargs.below

	return q._navbox(translatedPargs)
end

return q