Module:Palette : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
(retouche de la modification précédente)
m (A changé le niveau de protection pour « Module:Palette » ([Modifier=Autoriser uniquement les utilisateurs autopatrolled] (infini) [Renommer=Autoriser uniquement les administrateurs] (infini)))
 
(20 révisions intermédiaires par 4 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
 
local Palette = { }
 
local Palette = { }
 
local function paletteAvecParametre( i, args )
 
 
end
 
  
 
function Palette.listePalette( frame )
 
function Palette.listePalette( frame )
Ligne 9 : Ligne 5 :
 
local parentArgs = frame:getParent().args  
 
local parentArgs = frame:getParent().args  
 
 
local wikiTable = { '<div class="navbox_group" style="clear:both;">\n' }
+
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
 
setmetatable( wikiTable, { __index = table } )  -- permet d'utiliser les fonctions de table comme des méthodes
 +
local palettesVerticales = ''
 
 
 
local maxPalette = tonumber( args.maxPalette ) or 4
 
local maxPalette = tonumber( args.maxPalette ) or 4
 
local categoriePaletteInconnue = '[[Catégorie:Modèle Palette - Palette inconnue]]'
 
local categoriePaletteInconnue = '[[Catégorie:Modèle Palette - Palette inconnue]]'
 
local categorieTropDePalette = '[[Catégorie:Modèle Palette - Page contenant trop de palettes]]'
 
local categorieTropDePalette = '[[Catégorie:Modèle Palette - Page contenant trop de palettes]]'
 +
local categoriePaletteEnDouble = '[[Catégorie:Modèle Palette - Palette affichée en double]]'
 
 
 
local categories = { }
 
local categories = { }
Ligne 21 : Ligne 19 :
 
if param then texte = texte:format( param, ... )
 
if param then texte = texte:format( param, ... )
 
end  
 
end  
return '<p><strong class="error">' .. texte .. '</strong></p>\n'
+
local sep = #wikiTable > 1 and '<hr>' or ''
 +
return sep .. '<p><strong class="error" style="padding-left:.5em;">' .. texte .. '</strong></p>\n'
 
end
 
end
 
local function _pasDePalette()
 
local function _pasDePalette()
Ligne 29 : Ligne 28 :
 
local function _paletteInconnue( i )
 
local function _paletteInconnue( i )
 
categories.paletteInconnue = categoriePaletteInconnue
 
categories.paletteInconnue = categoriePaletteInconnue
return _erreur( 'Erreur : il n’existe pas de modèle « Palette %s »', parentArgs[i] )
+
local nomPalette = mw.text.trim( parentArgs[i] )
 +
return _erreur( 'Erreur : il n’existe pas de modèle {{[[Modèle:Palette %s|Palette %s]]}} [[Aide:Palette de navigation|(aide)]]', nomPalette, nomPalette )
 +
end
 +
 
 +
local function _paletteEnDouble ( i )
 +
categories.paletteEnDouble = categoriePaletteEnDouble
 +
return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
 
end
 
end
 
 
Ligne 36 : Ligne 41 :
 
for n, v in pairs( parentArgs ) do
 
for n, v in pairs( parentArgs ) do
 
if not tonumber(n) then
 
if not tonumber(n) then
argsPalette[n] = v
+
if n:match( ' ' .. i .. '$' ) then
if n:match( i .. '$' ) then
+
argsPalette[ n:sub(1, n:len() - 1 - tostring(i):len() ) ] = v
argsPalette[ n:sub(0 - #tostring(i) ) ] = v
+
elseif n == 'nocat' .. i then
elseif not argsPalette[n] then
+
argsPalette.nocat = v
 +
elseif not argsPalette[n] and n ~= 'stylecorps' then
 
argsPalette[n] = v
 
argsPalette[n] = v
 
end
 
end
 
end
 
end
 
end
 
end
return frame:expandTemplate{ title = 'Palette ' .. parentArgs[i], args = argsPalette }
+
return frame:expandTemplate{ title = 'Palette ' .. mw.text.trim(parentArgs[i]), args = argsPalette }
 
end
 
end
 
 
Ligne 52 : Ligne 58 :
 
end  
 
end  
 
 
 +
 +
local boite = parentArgs['titre boîte déroulante'] or parentArgs['titre boite déroulante']
 +
if boite 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.couleurFondT or '#CCF' )
 +
.. '; color:' .. ( parentArgs.couleurTexteT or 'black' ) .. ';">'
 +
.. boite
 +
.. '</div>\n<div class="NavContent" style="margin-top:2px;">\n'
 +
end
 
local i = 1
 
local i = 1
 
 
while parentArgs[i] and parentArgs[i] ~= '' and i <= maxPalette  do
+
while parentArgs[i] and i <= maxPalette  do
local codePalette = args[i]   
+
if parentArgs[i]:match( '%S' ) then
local testCodePalette = codePalette:sub(1, 25):lower()
+
local j
+
for j = 1, i - 1 do
if testCodePalette:match( '^%[%[:modèle:' ) then                  -- La palette n'existe pas
+
    if args[i] == args[j] and not args[i]:match('^palette ') then
wikiTable:insert( _paletteInconnue( i ) )
+
wikiTable:insert ( _paletteEnDouble(i))
elseif testCodePalette:match( '^palette verticale' ) then       -- C'est une palette verticale
+
end
wikiTable:insert( 1, codePalette:sub( 18 ) )
+
end
elseif testCodePalette:match( '^palette avec paramètres' ) then  -- C'est une palette nécessitant des paramètres nommés
+
if parentArgs[i]:match( '<table class="navbox' ) or parentArgs[i]:match( '{| ?class="navbox' ) then
wikiTable:insert( _paletteAvecParametres( i ) )
+
wikiTable:insert( parentArgs[i] )
else
+
else
wikiTable:insert( (codePalette:gsub( '<div class="navbox_group"', '<div' ) ) )
+
local codePalette = args[i]   
-- une ancre ^ pourra être ajoutée pour gagner en performance lorsque le modèle palette ne commencera plus par {{clr}}
+
local testCodePalette = codePalette:lower()
 +
if testCodePalette:match( '^%[%[:modèle:' ) then                  -- La palette n'existe pas
 +
wikiTable:insert( _paletteInconnue( i ) )
 +
elseif testCodePalette:match( '^palette avec paramètres' ) then -- C'est une palette nécessitant des paramètres nommés
 +
wikiTable:insert( _paletteAvecParametres( i ) )
 +
elseif testCodePalette:match( '^palette verticale avec paramètres' ) then  -- C'est une palette verticale nécessitant des paramètres nommés
 +
palettesVerticales = palettesVerticales .. ( _paletteAvecParametres( i ) )
 +
elseif testCodePalette:match( '^palette verticale' ) then        -- C'est une palette verticale
 +
palettesVerticales = palettesVerticales .. codePalette:sub( 18 )
 +
else
 +
wikiTable:insert( (codePalette:gsub( '^<div class="navbox-container"', '<div' ) ) )
 +
end
 +
end
 
end
 
end
 
 
i = i + 1
 
i = i + 1
 
end
 
end
Ligne 77 : Ligne 104 :
 
wikiTable:insert( _tropDePalettes() )
 
wikiTable:insert( _tropDePalettes() )
 
end
 
end
+
if #wikiTable == 1 then
wikiTable:insert( '</div>' )
+
wikiTable[1] = palettesVerticales
+
else
for i, v in pairs( categories ) do
+
if boite then
wikiTable:insert( v )
+
wikiTable:insert( '</div>' )
 +
end
 +
wikiTable:insert( '</div>' .. palettesVerticales )
 +
end
 +
if mw.title.getCurrentTitle().namespace == 0 then
 +
for i, v in pairs( categories ) do
 +
wikiTable:insert( v )
 +
end
 
end
 
end
 
 

Version actuelle datée du 17 mai 2017 à 13:47

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

local Palette = { }

function Palette.listePalette( 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 palettesVerticales = ''
	
	local maxPalette = tonumber( args.maxPalette ) or 4
	local categoriePaletteInconnue = '[[Catégorie:Modèle Palette - Palette inconnue]]'
	local categorieTropDePalette = '[[Catégorie:Modèle Palette - Page contenant trop de palettes]]'
	local categoriePaletteEnDouble = '[[Catégorie:Modèle Palette - Palette affichée en double]]'
	
	local categories = { }
	
	local function _erreur( 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 _pasDePalette()
		return _erreur( 'Erreur dans l’utilisation du [[Modèle:Palette]] : paramètre obligatoire absent.' )
	end
	
	local function _paletteInconnue( i )
		categories.paletteInconnue = categoriePaletteInconnue
		local nomPalette = mw.text.trim( parentArgs[i] )
		return _erreur( 'Erreur : il n’existe pas de modèle {{[[Modèle:Palette %s|Palette %s]]}} [[Aide:Palette de navigation|(aide)]]', nomPalette, nomPalette )
	end

	local function _paletteEnDouble ( i )
		categories.paletteEnDouble = categoriePaletteEnDouble
		return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
	end
	
	local function _paletteAvecParametres( i )
		local argsPalette = { }
		for n, v in pairs( parentArgs ) do
			if not tonumber(n) then
				if n:match( ' ' .. i .. '$' ) then
					argsPalette[ n:sub(1, n:len() - 1 - tostring(i):len() ) ] = v
				elseif n == 'nocat' .. i then
					argsPalette.nocat = v
				elseif not argsPalette[n] and n ~= 'stylecorps' then
					argsPalette[n] = v					
				end
			end
		end
		return frame:expandTemplate{ title = 'Palette ' .. mw.text.trim(parentArgs[i]), args = argsPalette }
	end
	
	local function _tropDePalettes()
		categories.tropDePalette = categorieTropDePalette
		return _erreur( 'Erreur dans le [[Modèle:Palette]] : trop de palettes (maximum : %s)', maxPalette )
	end 
	
	
	local boite = parentArgs['titre boîte déroulante'] or parentArgs['titre boite déroulante']
	if boite 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.couleurFondT or '#CCF' )
			.. '; color:' .. ( parentArgs.couleurTexteT or 'black' ) .. ';">'
			.. boite
			.. '</div>\n<div class="NavContent" style="margin-top:2px;">\n'
	end
	local i = 1
	
	while parentArgs[i] and i <= maxPalette  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('^palette ') then
					wikiTable:insert ( _paletteEnDouble(i))
				end
			end
			if parentArgs[i]:match( '<table class="navbox' ) or parentArgs[i]:match( '{| ?class="navbox' ) then
				wikiTable:insert( parentArgs[i] )
			else
				local codePalette = args[i]  
				local testCodePalette = codePalette:lower()
				if testCodePalette:match( '^%[%[:modèle:' ) then                  -- La palette n'existe pas
					wikiTable:insert( _paletteInconnue( i ) )
				elseif testCodePalette:match( '^palette avec paramètres' ) then  -- C'est une palette nécessitant des paramètres nommés
					wikiTable:insert( _paletteAvecParametres( i ) )
				elseif testCodePalette:match( '^palette verticale avec paramètres' ) then  -- C'est une palette verticale nécessitant des paramètres nommés
					palettesVerticales = palettesVerticales .. ( _paletteAvecParametres( i ) )
				elseif testCodePalette:match( '^palette verticale' ) then        -- C'est une palette verticale
					palettesVerticales = palettesVerticales .. codePalette:sub( 18 )
				else
					wikiTable:insert( (codePalette:gsub( '^<div class="navbox-container"', '<div' ) ) )
				end
			end
		end
		i = i + 1
	end
		
	if i == 1 then
		wikiTable:insert( _pasDePalette() )
	elseif i > maxPalette and parentArgs[i] and parentArgs[i] ~= '' then
		wikiTable:insert( _tropDePalettes() )
	end
	if #wikiTable == 1 then
		wikiTable[1] = palettesVerticales
	else
		if boite then
			wikiTable:insert( '</div>' )
		end
		wikiTable:insert( '</div>' .. palettesVerticales )
	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 Palette