Mòdul:Paleta

Un article de Wikipèdia, l'enciclopèdia liura.

La documentacion d'utilizacion d'aquel modul se pòt crear a Mòdul:Paleta/ús

local Paleta = { }

function Paleta.listaPaleta( frame )
	local args = frame.args
	local parentArgs = frame:getParent().args 
	
	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 paletasVerticalas = ''
	
	local maxPaleta = tonumber( args.maxPaleta ) or 4
	local categoriaPaletaDesconeguda = '[[Categoria:Modèl Paleta - Paleta desconeguda]]'
	local categoriaTropDePaleta = '[[Categoria:Modèl Paleta - Pagina contenent tròp de paletas]]'
	local categoriaPaletaEnDoble = '[[Categoria:Modèl Paleta - Paleta afichada en doble]]'
	
	local categorias = { }
	
	local function _error( texte, param, ... )
		if param then texte = texte:format( param, ... )
		end 
		local sep = #wikiTable > 1 and '<hr>' or ''
		return sep .. '<p><strong class="error" style="padding-left:.5em;">' .. texte .. '</strong></p>\n'
	end
	local function _pasDePaleta()
		return _error( 'Error dins l’utilizacion del [[Modèl:Paleta]] : paramètre obligatòri absent.' )
	end
	
	local function _paletaDesconeguda( i )
		categorias.paletaDesconeguda = categoriaPaletaDesconeguda
		local nomPaleta = mw.text.trim( parentArgs[i] )
		return _error( 'Error : existís pas de modèl {{[[Modèl:Paleta %s|Paleta %s]]}} [[Ajuda:Paleta de navigacion|(ajuda)]]', nomPaleta, nomPaleta )
	end

	local function _paletaEnDoble ( i )
		categorias.paletaEnDoble = categoriaPaletaEnDoble
		return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
	end
	
	local function _paletaAmbeParametres( 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 ~= 'stylecorps' then
					argsPaleta[n] = v					
				end
			end
		end
		return frame:expandTemplate{ title = 'Paleta ' .. mw.text.trim(parentArgs[i]), args = argsPaleta }
	end
	
	local function _tropDePaletas()
		categorias.tropDePaleta = categoriaTropDePaleta
		return _error( 'Error dins lo [[Modèl:Paleta]] : tròp de paletas (maximum : %s)', maxPaleta )
	end 
	
	
	local bostia = parentArgs['títol bóstia desrotlanta'] or parentArgs['títol bóstia desenrotlanta']
	if bostia 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.colorFonsT or '#CCF' )
			.. '; color:' .. ( parentArgs.colorTexteT or 'black' ) .. ';">'
			.. bostia
			.. '</div>\n<div class="NavContent" style="margin-top:2px;">\n'
	end
	local i = 1
	
	while parentArgs[i] and i <= maxPaleta  do
		if parentArgs[i]:match( '%S' ) then
			local j 
			for j = 1, i - 1 do
			    if args[i] == args[j] and not args[i]:match('^paleta ') then
					wikiTable:insert ( _paletaEnDoble(i))
				end
			end
			if parentArgs[i]:match( '<table class="navbox' ) or parentArgs[i]:match( '{| ?class="navbox' ) then
				wikiTable:insert( parentArgs[i] )
			else
				local codePaleta = args[i]  
				local testCodePaleta = codePaleta:lower()
				if testCodePaleta:match( '^%[%[:modèl:' ) then                  -- La palette n'existe pas
					wikiTable:insert( _paletaDesconneguda( i ) )
				elseif testCodePaleta:match( '^paleta amb paramètres' ) then  -- C'est une palette nécessitant des paramètres nommés
					wikiTable:insert( _paletAmbeParametres( i ) )
				elseif testCodePaleta:match( '^paleta verticala amb paramètres' ) then  -- C'est une palette verticale nécessitant des paramètres nommés
					paletasVerticalas = paletasVerticalas .. ( _paletaAmbeParametres( i ) )
				elseif testCodePaleta:match( '^paleta verticala' ) then        -- C'est une palette verticale
					paletasVerticalas = paletasVerticalas .. 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( _pasDePaleta() )
	elseif i > maxPaleta and parentArgs[i] and parentArgs[i] ~= '' then
		wikiTable:insert( _tropDePaletas() )
	end
	if #wikiTable == 1 then
		wikiTable[1] = paletasVerticalas
	else
		if bostia then
			wikiTable:insert( '</div>' )
		end
		wikiTable:insert( '</div>' .. paletasVerticalas )
	end
	if mw.title.getCurrentTitle().namespace == 0 then
		for i, v in pairs( categorias ) do
			wikiTable:insert( v )
		end
	end
	
	return wikiTable:concat()
end


return Paleta