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

Une page de Wikipédia, l'encyclopédie libre.
Contenu supprimé Contenu ajouté
regex plus stricte : ça doit se trouver au début de l'argument, sachant que comme il n'est pas trimmé on peut avoir des espaces ou des sauts de lignes
trim trailing spaces, nitpicks
Ligne 1 : Ligne 1 :
local Palette = { }
local p = { }


function Palette.listePalette( frame )
function p.listePalette( frame )
local args = frame.args
local args = frame.args
local parentArgs = frame:getParent().args
local parentArgs = frame:getParent().args

local wikiTable = { '<div class="navbox-container" 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 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 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 25 : Ligne 25 :
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
Ligne 36 : Ligne 36 :
return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
return "" -- inutile d'afficher une erreur visible pour ce cas puisque la mise en page n'est pas cassée
end
end

local function _paletteAvecParametres( i )
local function _paletteAvecParametres( i )
local argsPalette = { }
local argsPalette = { }
Ligne 46 : Ligne 46 :
argsPalette.nocat = v
argsPalette.nocat = v
elseif not argsPalette[n] and n ~= 'stylecorps' then
elseif not argsPalette[n] and n ~= 'stylecorps' then
argsPalette[n] = v
argsPalette[n] = v
end
end
end
end
Ligne 52 : Ligne 52 :
return frame:expandTemplate{ title = 'Palette ' .. mw.text.trim(parentArgs[i]), args = argsPalette }
return frame:expandTemplate{ title = 'Palette ' .. mw.text.trim(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
Ligne 70 : Ligne 70 :
local i = 1
local i = 1
local palettesValides = {}
local palettesValides = {}

while parentArgs[i] and i <= maxPalette do
while parentArgs[i] and i <= maxPalette do
if parentArgs[i]:match( '%S' ) then
if parentArgs[i]:match( '%S' ) then
for _, j in ipairs( palettesValides ) do
for _, j in ipairs( palettesValides ) do
if args[i] == args[j] and not args[i]:match('^palette ') then
if args[i] == args[j] and not args[i]:match('^palette ') then
wikiTable:insert ( _paletteEnDouble(i))
wikiTable:insert ( _paletteEnDouble(i))
end
end
Ligne 82 : Ligne 82 :
wikiTable:insert( parentArgs[i] )
wikiTable:insert( parentArgs[i] )
else
else
local codePalette = args[i]
local codePalette = args[i]
local testCodePalette = codePalette:lower()
local testCodePalette = codePalette:lower()
if testCodePalette:match( '^%[%[:modèle:' ) then -- La palette n'existe pas
if testCodePalette:match( '^%[%[:modèle:' ) then -- La palette n'existe pas
Ligne 100 : Ligne 100 :
i = i + 1
i = i + 1
end
end

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

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


return p

return Palette

Version du 5 octobre 2021 à 13:12

 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 p = { }

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

	while parentArgs[i] and i <= maxPalette do
		if parentArgs[i]:match( '%S' ) 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( '^%s*<table class="navbox' ) or parentArgs[i]:match( '^%s*{| ?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 ) )
					table.remove( palettesValides )
				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 p