« Module:Palette » : différence entre les versions

Une page de Wikipédia, l'encyclopédie libre.
Contenu supprimé Contenu ajouté
FDo64 (discuter | contributions)
Mise en place des modifications de Zebulon84 : boite déroulante + palette directement en argument + trim
renommage de la variable, pour mieux montrer qu'il s'agit de mw.text.trim(), notamment pour empêcher tout risque de confusion avec Outils.trim()
 
(29 versions intermédiaires par 5 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
local Palette = { }
local mwTrim = mw.text.trim


local p = {}
local function paletteAvecParametre( i, args )
end


function Palette.listePalette( frame )
function p.listePalette( frame )
local args = frame.args
local args = frame.args

local parentArgs = frame:getParent().args
local parentArgs = {}
for k, v in pairs( frame:getParent().args ) do
local wikiTable = { '<div class="navbox_group" style="clear:both;">\n' }
-- 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
setmetatable( wikiTable, { __index = table } ) -- permet d'utiliser les fonctions de table comme des méthodes
local palettesVerticales = ''
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 = {}

local function _erreur( texte, param, ... )
local function _erreur( texte, param, ... )
if param then texte = texte:format( param, ... )
if param then texte = texte:format( param, ... )
end
end
local sep = #wikiTable > 1 and '<hr>' or ''
local sep = #wikiTable > 1 and '<hr>' or ''
return sep .. '<p><strong class="error" style="padding-left:.5em;">' .. texte .. '</strong></p>\n'
return sep .. '<p><strong class="error" style="padding-left:.5em;">' .. texte .. '</strong></p>\n'
Ligne 28 : Ligne 35 :
return _erreur( 'Erreur dans l’utilisation du [[Modèle:Palette]] : paramètre obligatoire absent.' )
return _erreur( 'Erreur dans l’utilisation du [[Modèle:Palette]] : paramètre obligatoire absent.' )
end
end

local function _paletteInconnue( i )
local function _paletteInconnue( i )
categories.paletteInconnue = categoriePaletteInconnue
categories.paletteInconnue = categoriePaletteInconnue
local nomPalette = mw.text.trim( parentArgs[i] )
local nomPalette = 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 )
return _erreur( 'Erreur : il n’existe pas de modèle {{[[Modèle:Palette %s|Palette %s]]}} [[Aide:Palette de navigation|(aide)]]', nomPalette, nomPalette )
end
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 function _paletteAvecParametres( i )
local argsPalette = { }
local argsPalette = {}
for n, v in pairs( parentArgs ) do
for n, v in pairs( parentArgs ) do
if not tonumber(n) then
if not tonumber(n) then
if n:match( ' ' .. i .. '$' ) then
if n:match( ' ' .. i .. '$' ) then
argsPalette[ n:sub(1, n:len() - 1 - tostring(i):len() ) ] = v
argsPalette[ n:sub(1, n:len() - 1 - tostring(i):len() ) ] = v
elseif not argsPalette[n] then
elseif n == 'nocat' .. i then
argsPalette[n] = v
argsPalette.nocat = v
elseif not argsPalette[n] and n ~= 'stylecorps' then
argsPalette[n] = v
end
end
end
end
end
end
return frame:expandTemplate{ title = 'Palette ' .. mw.text.trim(parentArgs[i]), args = argsPalette }
return frame:expandTemplate{ title = 'Palette ' .. parentArgs[i], args = argsPalette }
end
end

local function _tropDePalettes()
local function _tropDePalettes()
categories.tropDePalette = categorieTropDePalette
categories.tropDePalette = categorieTropDePalette
return _erreur( 'Erreur dans le [[Modèle:Palette]] : trop de palettes (maximum : %s)', maxPalette )
return _erreur( 'Erreur dans le [[Modèle:Palette]] : trop de palettes (maximum : %s)', maxPalette )
end
end


local boite = parentArgs['titre boîte déroulante'] or parentArgs['titre boite déroulante']
local boite = parentArgs['titre boîte déroulante'] or parentArgs['titre boite déroulante']
if boite then
if boite then
wikiTable[1] = '<div class="NavFrame navbox_group" style="clear:both;" >\n'
wikiTable[1] = '<div class="NavFrame navbox-container" style="clear:both;" >\n'
.. '<div class="NavHead" style="text-align:center; height:1.6em; background-color:'
.. '<div class="NavHead" style="text-align:center; height:1.6em; background-color:'
.. ( parentArgs.couleurFondT or '#CCF' )
.. ( parentArgs.couleurFondT or '#CCF' )
Ligne 64 : Ligne 79 :
end
end
local i = 1
local i = 1
local palettesValides = {}

while parentArgs[i] and i <= maxPalette do
if parentArgs[i]:match( '%S' ) then
while parentArgs[i] and i <= maxPalette do
if parentArgs[i]:match( '^<table class="navbox' ) then
if parentArgs[i] ~= '' then
for _, j in ipairs( palettesValides ) do
if args[i] == args[j] and not args[i]:match('^palette ') then
wikiTable:insert ( _paletteEnDouble(i))
end
end
table.insert( palettesValides, i )
if parentArgs[i]:match( '^<' ) then
wikiTable:insert( parentArgs[i] )
elseif parentArgs[i]:match( '^{|' ) then
wikiTable:insert( '\n' )
wikiTable:insert( parentArgs[i] )
wikiTable:insert( parentArgs[i] )
else
else
local codePalette = args[i]
local codePalette = args[i]
if codePalette:match( '^%[%[:Modèle:' ) then -- La palette n'existe pas
local testCodePalette = codePalette:sub(1, 35):lower()
if testCodePalette:match( '^%[%[:modèle:' ) then -- La palette n'existe pas
wikiTable:insert( _paletteInconnue( i ) )
wikiTable:insert( _paletteInconnue( i ) )
table.remove( palettesValides )
elseif testCodePalette:match( '^palette avec paramètres' ) then -- C'est une palette nécessitant des paramètres nommés
elseif codePalette:match( '^palette avec paramètres' ) then -- C'est une palette nécessitant des paramètres nommés
wikiTable:insert( _paletteAvecParametres( i ) )
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
elseif codePalette:match( '^palette verticale avec paramètres' ) then -- C'est une palette verticale nécessitant des paramètres nommés
palettesVerticales = palettesVerticales .. ( _paletteAvecParametres( i ) )
palettesVerticales = palettesVerticales .. ( _paletteAvecParametres( i ) )
elseif testCodePalette:match( '^palette verticale' ) then -- C'est une palette verticale
elseif codePalette:match( '^palette verticale' ) then -- C'est une palette verticale
palettesVerticales = palettesVerticales .. codePalette:sub( 18 )
palettesVerticales = palettesVerticales .. codePalette:sub( 18 )
else
else
wikiTable:insert( (codePalette:gsub( '^<div class="navbox_group"', '<div' ) ) )
wikiTable:insert( (codePalette:gsub( '^<div class="navbox%-container"', '<div' ) ) )
end
end
end
end
Ligne 87 : Ligne 112 :
i = i + 1
i = i + 1
end
end

if i == 1 then
if i == 1 then
wikiTable:insert( _pasDePalette() )
wikiTable:insert( _pasDePalette() )
Ligne 106 : Ligne 131 :
end
end
end
end

return wikiTable:concat()
return wikiTable:concat()
end
end


return p

return Palette

Dernière version du 7 novembre 2023 à 07:52

 Documentation[voir] [modifier] [historique] [purger]

Utilisation

Fonctions exportables :

Modules externes et autres éléments dont ce module a besoin pour fonctionner : aucun

Exemples

Pour des exemples, voir la page de test permettant de tester diverses modifications apportées.

local mwTrim = mw.text.trim

local p = {}

function p.listePalette( 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 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 = 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 ' .. 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
	local palettesValides = {}

	while parentArgs[i] and i <= maxPalette do
		if parentArgs[i] ~= '' then
			for _, j in ipairs( palettesValides ) do
				if args[i] == args[j] and not args[i]:match('^palette ') then
					wikiTable:insert ( _paletteEnDouble(i))
				end
			end
			table.insert( palettesValides, i )
			if parentArgs[i]:match( '^<' ) then
				wikiTable:insert( parentArgs[i] )
			elseif parentArgs[i]:match( '^{|' ) then
				wikiTable:insert( '\n' )
				wikiTable:insert( parentArgs[i] )
			else
				local codePalette = args[i]
				if codePalette:match( '^%[%[:Modèle:' ) then                  -- La palette n'existe pas
					wikiTable:insert( _paletteInconnue( i ) )
					table.remove( palettesValides )
				elseif codePalette:match( '^palette avec paramètres' ) then  -- C'est une palette nécessitant des paramètres nommés
					wikiTable:insert( _paletteAvecParametres( i ) )
				elseif codePalette:match( '^palette verticale avec paramètres' ) then  -- C'est une palette verticale nécessitant des paramètres nommés
					palettesVerticales = palettesVerticales .. ( _paletteAvecParametres( i ) )
				elseif codePalette: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 p