Aller au contenu

Module:Palèta

De Vouiquipèdia, l’enciclopèdia abada.

La documentation pour ce module peut être créée à Module:Palèta/doc

local mwTrim = mw.text.trim

local p = {}

function p.listaPaleta( frame )
	local args = frame.args

	local parentArgs = {}
	for k, v in pairs( frame:getParent().args ) do
		-- effectue un trim sur les paramètres non nommés
		if type( k ) == 'number' then
			v = mwTrim( v )
		end
		parentArgs[ k ] = v
	end

	local wikiTable = { '<div class="navbox-container" style="clear:both;">\n' }
	setmetatable( wikiTable, { __index = table } )   -- permet d'utiliser les fonctions de table comme des méthodes
	local paletesDAplomb = ''

	local mesPaleta = tonumber( args.mesPaleta ) or 4
	local categoriaPaletaEncognua = '[[Catègorie:Modèlo Palèta - Palèta encognua]]'
	local categoriaTropDePaleta = '[[Catègorie:Modèlo Palèta - Page que contint trop de palètes]]'
	local categoriaPaletaEnDroblo = '[[Catègorie:Modèlo Palèta - Palèta afichiêe en droblo]]'

	local categories = {}

	local function _fota( texto, param, ... )
		if param then texto = texto:format( param, ... )
		end
		local sep = #wikiTable > 1 and '<hr>' or ''
		return sep .. '<p><strong class="error" style="padding-left:.5em;">' .. texto .. '</strong></p>\n'
	end
	local function _ginsDePaleta()
		return _fota( 'Fôta dens l’usâjo du [[Modèlo:Palèta]] : paramètro oblegatouèro absent.' )
	end

	local function _paletaEncognua( i )
		categories.paletaEncognua = categoriaPaletaEncognua
		local nomPaleta = parentArgs[i]
		return _fota( 'Fôta : ègziste gins de modèlo {{[[Modèlo:Palèta %s|Palèta %s]]}} [[Éde:Palèta de navegacion|(éde)]]', nomPaleta, nomPaleta )
	end

	local function _paletaEnDroblo ( i )
		categories.paletaEnDroblo = categoriaPaletaEnDroblo
		return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
	end

	local function _paletaAvouecParametros( i )
		local argsPaleta = {}
		for n, v in pairs( parentArgs ) do
			if not tonumber(n) then
				if n:match( ' ' .. i .. '$' ) then
					argsPaleta[ n:sub(1, n:len() - 1 - tostring(i):len() ) ] = v
				elseif n == 'nocat' .. i then
					argsPaleta.nocat = v
				elseif not argsPaleta[n] and n ~= 'stilocôrp' then
					argsPaleta[n] = v
				end
			end
		end
		return frame:expandTemplate{ title = 'Palèta ' .. parentArgs[i], args = argsPaleta }
	end

	local function _tropDePaletes()
		categories.tropDePaleta = categoriaTropDePaleta
		return _fota( 'Fôta dens lo [[Modèlo:Palèta]] : trop de palètes (u més : %s)', mesPaleta )
	end


	local boueta = parentArgs['titro bouèta dèroulanta'] or parentArgs['titro bouèta dèroulanta']
	if boueta then
		wikiTable[1] = '<div class="NavFrame navbox-container" style="clear:both;" >\n'
			.. '<div class="NavHead" style="text-align:center; height:1.6em; background-color:'
			.. ( parentArgs.colorFondT or '#CCF' )
			.. '; color:' .. ( parentArgs.colorTextoT or 'black' ) .. ';">'
			.. boueta
			.. '</div>\n<div class="NavContent" style="margin-top:2px;">\n'
	end
	local i = 1
	local paletesValides = {}

	while parentArgs[i] and i <= mesPaleta do
		if parentArgs[i] ~= '' then
			for _, j in ipairs( paletesValides ) do
				if args[i] == args[j] and not args[i]:match('^palèta ') then
					wikiTable:insert ( _paletaEnDroblo(i))
				end
			end
			table.insert( paletesValides, i )
			if parentArgs[i]:match( '^<' ) then
				wikiTable:insert( parentArgs[i] )
			elseif parentArgs[i]:match( '^{|' ) then
				wikiTable:insert( '\n' )
				wikiTable:insert( parentArgs[i] )
			else
				local codePaleta = args[i]
				if codePaleta:match( '^%[%[:Modèlo:' ) then                  -- La palette n'existe pas
					wikiTable:insert( _paletaEncognua( i ) )
					table.remove( paletesValides )
				elseif codePaleta:match( '^palèta avouéc paramètros' ) then  -- C'est une palette nécessitant des paramètres nommés
					wikiTable:insert( _paletaAvouecParametros( i ) )
				elseif codePaleta:match( '^palèta d’aplomb avouéc paramètros' ) then  -- C'est une palette verticale nécessitant des paramètres nommés
					paletesDAplomb = paletesDAplomb .. ( _paletaAvouecParametros( i ) )
				elseif codePaleta:match( '^palèta d’aplomb' ) then        -- C'est une palette verticale
					paletesDAplomb = paletesDAplomb .. codePaleta:sub( 18 )
				else
					wikiTable:insert( (codePaleta:gsub( '^<div class="navbox%-container"', '<div' ) ) )
				end
			end
		end
		i = i + 1
	end

	if i == 1 then
		wikiTable:insert( _ginsDePaleta() )
	elseif i > mesPaleta and parentArgs[i] and parentArgs[i] ~= '' then
		wikiTable:insert( _tropDePaletes() )
	end
	if #wikiTable == 1 then
		wikiTable[1] = paletesDAplomb
	else
		if boueta then
			wikiTable:insert( '</div>' )
		end
		wikiTable:insert( '</div>' .. paletesDAplomb )
	end
	if mw.title.getCurrentTitle().namespace == 0 then
		for i, v in pairs( categories ) do
			wikiTable:insert( v )
		end
	end

	return wikiTable:concat()
end

return p