Module:Palmarès tennis

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[créer] [purger]
-- Module destiné à accélérer le traitement des modèles
-- Palmarès WTA/début	Palmarès ATP/début
-- Palmarès WTA/ligne	Palmarès ATP/ligne
require('strict')

local z = { }

local TableBuilder = require( 'Module:TableBuilder' )
local tableauCategorie = mw.loadData ( 'Module:Palmarès_tennis/categorie' )


-- z.ligne génère une ligne de tableau mise en forme.
-- paramètres : genre : wta ou atp (en minuscule)
function z.ligne ( frame )
	local pargs = frame:getParent().args  -- paramètre du modèle
	
	local wikiTab = TableBuilder.new()     -- Table contenant les texte de chaque cellule du tableau
	local txt =''                          -- variable utilisé pour stocké un texte temporairement
	
	-- Clarification de certains paramètre
	local contexte = string.lower ( pargs.contexte or mw.text.trim ( pargs[1] or tableauCategorie.contexte or '' ) )
	contexte = string.match ( contexte, 'tournoi' )
		or string.match ( contexte, 'gc' )
		or string.match ( contexte, 'joueu' )
		or contexte
	local double
	local tableau = pargs.tableau or pargs[2]
	if type( tableau ) == 'string' then
		double = string.lower( string.sub ( mw.text.trim( tableau ), 1, 1 ) )  == 'd'
	else
		double = type( pargs.pn or pargs.v2 or pargs.f2 ) == 'string'
	end
	
	local titre = false
	local palmares = pargs['palmarès'] or pargs[3]
	if palmares then
		palmares = string.lower( string.sub ( mw.text.trim( palmares ), 1, 1 ) )
		titre = ( palmares ~= 'f' ) and ( palmares ~= 'd' )  -- titre = false si palmarès = défaite, finale, finaliste...
	else
		if pargs.f1 and not pargs.v1 then
			titre = true
		end
	end
	
	local spantext = pargs.spantext or ''
	local tournoi = pargs.tournoi or ''
	local edition = pargs ['édition'] or ''
	local refedition = pargs ['refédition'] or ''
	
	local dotation = pargs.dotation or ''
	if dotation:sub( 1, 1 ):lower() == 'n' then
		dotation = false
	end
	local devise = pargs.devise or '$'

	local categorie = pargs ['catégorie'] or ''
	local refcat = pargs.refcat or ''
	local genre = string.lower ( frame.args.genre or 'atp' )
	
	local couleur
	
	local annee = pargs['année'] or ''
	if annee == '' then
		annee = string.match ( pargs['date'] or '', '%d%d%d%d' )
	end

	-- catégorie tournoi :  le tableau 'cat' servira a formater la ligne à différent endroit
	local cat = tableauCategorie [genre] [ string.lower ( categorie ) ]
	if cat == nil then
		if categorie == '' then
			cat = tableauCategorie [genre] ['non connue']
		else
			cat = { couleur = 'transparent', texte = categorie, tri = 999, date = { debut = 0, fin = 10000 } }
		end
	end
	if cat.date.debut > ( tonumber ( annee ) or 0 ) or cat.date.fin < ( tonumber ( annee ) or 0 ) then
		cat = { couleur = 'transparent',
			texte = cat.texte .. frame:extensionTag { name = 'ref', content = "cette catégorie n'existe pas en " .. annee, args = { } },
			tri = 999
		}
	end
	
	-- couleur ligne
	if ( contexte ~= 'gc' ) and ( contexte ~= 'tournoi' ) then
		if type ( cat.couleur ) == 'table' then
			wikiTab.insert( '|- style="background-color:' .. cat.couleur [titre] .. ';"' )
		else
			wikiTab.insert( '|- style="background-color:' .. cat.couleur .. ';"' )
		end
	else
		if refcat ~= '' or refedition ~= '' then
			wikiTab.insert( '|- style="background-color:#FFFFCC;"' )
		else
			wikiTab.insert( '|-' )
		end
	end
	
	-- n° et date
	wikiTab.insert( 'style="text-align:right;"|' .. ( pargs['n°'] or '' ) )
	wikiTab.insert( pargs.date or '' )
	
	-- spantext
	if spantext ~= '' then
		local span = 7
		if contexte == 'joueu' and ( not double ) then
			span = span - 1
		end
		if not dotation then
			span = span - 1
		end
		wikiTab.insert( 'colspan=' .. span .. ' style="text-align:center;"|' .. spantext )
	else
	
		-- tournoi
		local ville = pargs.ville or pargs.lieu
		local drapeau = ''
		if pargs ['code pays'] then
				drapeau = frame:expandTemplate { title = pargs ['code pays'] .. '-d' } .. '&nbsp;'
		end
		if ( type ( ville ) == 'string' ) and ( ville ~= '' ) then
			if double then  -- si c'est un double, la ville éventuellement précédée du drapeau est mis sur la {{2e}} ligne
				ville = '<br />' .. drapeau .. ville
				drapeau = ''
			else
				ville = ', ' .. ville
			end
		 else
			ville = ''
		end
		if contexte == 'tournoi' then
			wikiTab.insert( edition .. refedition .. ville )
		else
			txt = ''
			if edition ~= '' then
				txt = '|' .. edition
			end
			wikiTab.insert(
				'<strong>' .. drapeau
				.. '[[' .. tournoi .. txt .. ']]</strong>' .. refedition
				.. ville
			)
		end
		
		-- catégorie
		wikiTab.insert( cat.texte .. refcat )
		
		-- dotation
		if dotation then
			if dotation == '' then
				wikiTab.insert( 'style="text-align:right;"|<small><abbr class="abbr" title="Dotation non connue" >NC</abbr></small>' )
			else
				if tonumber( dotation ) then
					dotation = mw.getContentLanguage():formatNum( tonumber( dotation ) )
				end
				wikiTab.insert( 'style="text-align:right;"|<small>' .. dotation .. '\194\160' .. devise .. '</small>' )
			end
		end
		
		-- surface
		local revetement, situation = z.determination_surface (pargs.surface)
		if situation then
			wikiTab.insert( revetement .. ' (' .. situation .. ')' )
		else
			wikiTab.insert( revetement )
		end
		
		-- Joueurs (vainqueur(s), finaliste(s), partenaire)
		local verifJoueur = function ( joueur )
			if type ( joueur ) ~= 'string' or joueur == '' then
				return '<abbr class="abbr" title="Non connu" >NC</abbr>'
			else
				return joueur
			end
		end
		if ( contexte == 'joueu' ) then
			if double then
				if titre then
					wikiTab.insert( [[''']] .. verifJoueur ( pargs.pn ) .. [[''']] )
					wikiTab.insert( verifJoueur ( pargs.f1 ) .. '<br />' .. verifJoueur ( pargs.f2 ) )
				else
					wikiTab.insert( [[''']] .. verifJoueur ( pargs.v1 ) .. '<br />' .. verifJoueur ( pargs.v2 ) .. [[''']] )
					wikiTab.insert( verifJoueur ( pargs.pn ) )
				end
			else
				if titre then
					wikiTab.insert( verifJoueur ( pargs.f1 ) )
				else
					wikiTab.insert( [[''']] .. verifJoueur ( pargs.v1 ) .. [[''']] )
				end
			end
		else
			if double then
				wikiTab.insert( [[''']] .. verifJoueur ( pargs.v1 ) .. '<br />' .. verifJoueur ( pargs.v2 ) .. [[''']] )
				wikiTab.insert( verifJoueur ( pargs.f1 ) .. '<br />' .. verifJoueur ( pargs.f2 ) )
			else
				wikiTab.insert( [[''']] .. verifJoueur ( pargs.v1 ) .. [[''']] )
				wikiTab.insert( verifJoueur ( pargs.f1 ) )
			end
		end
		
		-- score
		if ( type ( pargs.score ) == 'string' ) and ( pargs.score ~= '' ) then
			wikiTab.insert( pargs.score )
		else
			wikiTab.insert( '<abbr class="abbr" title="Score non connu" >NC</abbr>' )
		end
		
	end  -- fin de  if spantext ~= ''
	
	-- parcours
	local parcours = pargs.parcours
	if parcours == nil then   -- lien avec tournoi ou année si parcours n'existe pas. Pas d'affichage si parcours est vide.
		
		if annee and ( tournoi ~= '' ) then
			-- ajout (WTA 2022) pour les tournois, ou simplement 2022 pour les Masters, Classic...
			if tournoi:match ( '^[Tt]ournoi de tennis' ) then
				parcours = tournoi .. ' (' .. string.upper( genre ) .. ' ' .. annee .. ')'
			else
				parcours = tournoi .. ' ' .. annee
			end
			-- test existence page pour les années avant 2010, on suppose que la page existe pour les tournois récents
			if tonumber( annee ) < 2010 and not mw.title.new( parcours ).exists then
				parcours = ''
			end
		else
			parcours = ''
		end
	end
	if parcours ~= '' then
		if parcours:match ( '%[%[' ) or parcours:match ( '%[http') then
			wikiTab.insert( parcours )
		else
			if contexte == 'joueu' then
				txt = '|Parcours]]'
			else
				txt = '|Tableau]]'
			end
			if parcours:match( '[Ss]imple' ) or parcours:match( '[Dd]ouble' ) then
				wikiTab.insert( '[[' .. parcours .. txt )
			elseif double then
				wikiTab.insert( '[[' .. parcours .. '#Résultats en double' .. txt )
			else
				wikiTab.insert( '[[' .. parcours .. '#Résultats en simple' .. txt )
			end
		end
	else
		wikiTab.insert( '' )
	end
	
	return wikiTab.concat ( '\n|' )
end


-- z.determination_surface renvoie dans deux paramètres la surface proprement dite, et si c'est en extérieur / intérieur.
-- Les abréviations 'Terre' et ext. / int. sont explicitées en abréviation discrète.
-- cette fonction est séparée pour pouvoir être plus facilement réutilisée.
-- l'entrée est un texte flou décrivant la surface, de tb à Terre battue (extérieur), de gi à Herbe Intérieure...
function z.determination_surface ( surface )
	if type ( surface ) ~= 'string' then
		return '<abbr class="abbr" title="Surface non connue" >NC</abbr>', nil
	end
	surface = string.lower ( surface )
	local tabRevetement = { t = '<abbr class="abbr" title="Terre battue" >Terre</abbr>', d = 'Dur', g = 'Gazon', h = 'Gazon', m = 'Moquette', s = 'Moquette' }
	local revetement = tabRevetement [ string.sub ( surface, 1, 1 ) ]
	local situation = '<abbr class="abbr" title="Extérieur" >ext.</abbr>'
	
	if revetement == nil then
		revetement = '<abbr class="abbr" title="Surface non connue" >NC</abbr>'
		situation = nil
	else
		if ( string.sub ( surface, 2, 1 ) == 'i' ) or string.match ( surface, ' i' ) or string.match ( surface, '%(i' ) then
			situation = '<abbr class="abbr" title="Intérieur" >int.</abbr>'
		end
	end
	
	return revetement, situation
end



-- z.debut génére un tableau et la ligne de titre
-- paramètres : genre : wta ou atp
function z.debut ( frame )
	local pargs = frame:getParent().args  -- paramètre du modèle
	local contexte = string.lower ( pargs.contexte or mw.text.trim ( pargs[1] or '' ) )
	local tableau = string.lower ( pargs.tableau or mw.text.trim ( pargs[2] or '' ) )
	local palmares = string.lower ( pargs [ 'palmarès' ] or mw.text.trim ( pargs[3] or '' ) )
	local ligneTitre = '\n! scope="col" style="background-color:#CFCFCF;"|'
	local ligneTitreGc = '\n! scope="col" class="unsortable" style="background-color: #CFCFCF; width: 22%;"|'
	local ligneSansTri = '\n! scope="col" class="unsortable" style="background-color: #CFCFCF;"|'
	
	local wikiText = '{|'
	local txt
	
	-- préparation des variables
	contexte = string.match ( contexte, 'tournoi' ) or contexte
	contexte = string.match ( contexte, 'gc' ) or contexte
	contexte = string.match ( contexte, 'joueu' ) or contexte  -- valable pour joueur ou joueuse
	palmares = string.sub ( palmares, 1, 1 )
	local titre = ( palmares ~= 'f' ) and ( palmares ~= 'd' )  -- titre = false si palmarès = défaite, finale, finaliste...
	local double = string.sub ( tableau, 1, 1 )  == 'd'
	local wta = frame.args.genre == 'wta'
	
	-- style tableau
	if wta and contexte ~= 'tournoi' then
		wikiText = wikiText .. 'class="sortable wikitable" style="font-size:11px"'
	else
		wikiText = wikiText .. 'class="wikitable alternance sortable" style="font-size: .85em;"'
	end
	wikiText = wikiText .. '\n|-'
	
	-- grand Chelem
	if string.match ( contexte, 'gc' ) then
		wikiText = wikiText .. '\n! scope="col" style="background-color: #CFCFCF;"|width: 12%;"|Année'
		wikiText = wikiText .. ligneTitreGc .. "[[Open d'Australie]]"
		wikiText = wikiText .. ligneTitreGc .. "[[Internationaux de France de tennis|Roland-Garros]]"
		wikiText = wikiText .. ligneTitreGc .. "[[Tournoi de Wimbledon|Wimbledon]]"
		wikiText = wikiText .. ligneTitreGc .. "[[US Open de tennis|US Open]]"
	else
		-- N°
		wikiText = wikiText .. ligneTitre  -- .. '<abbr class="abbr" title="Numéro" >N<sup>o</sup></abbr>'
		
		-- Date
		local dateRefArgs
		if wta then
			dateRefArgs = { name = 'date2' }
		else
			dateRefArgs = { name = 'date', group = 'T' }
		end
		txt = frame:extensionTag {
			name = 'ref',
			content = 'Pour chaque tournoi, la date indiquée correspond à la première journée, en général un [[lundi]].',
			args = dateRefArgs
		}
		wikiText = wikiText .. ligneTitre .. 'Date' .. txt
		
		-- Nom du Tournoi
		wikiText = wikiText .. ligneTitre .. 'Nom et lieu du tournoi'
		
		-- Catégorie
		txt = ''
		if not wta then txt = '#Catégories des tournois masculins' end
		wikiText = wikiText .. ligneTitre .. '[[Catégorisation des tournois de tennis' .. txt ..'|Cat.]]'
		
		-- dotation
		local dotation = pargs.dotation or ''
		if dotation:sub( 1, 1 ):lower() ~= 'n' then
			if wta then
				wikiText = wikiText .. ligneTitre .. '($)'
			else
				wikiText = wikiText .. ligneTitre .. 'Dotation'
			end
		end
		
		-- Surface
		wikiText = wikiText .. ligneTitre .. '[[Surfaces de jeu au tennis|Surface]]'
		
		-- Joueurs (vainqueur(s), finaliste(s), partenaire)
		txt = ''
		if double then txt = 's' end
		if ( contexte == 'joueu' ) and titre then
			if double then
				wikiText = wikiText .. ligneTitre .. 'Partenaire'
			end
		else
			wikiText = wikiText .. ligneTitre .. 'Vainqueur' .. txt
		end
		if ( contexte == 'joueu' ) and ( not titre ) then
			if double then
				wikiText = wikiText .. ligneTitre ..  'Partenaire'
			end
		else
			wikiText = wikiText .. ligneTitre .. 'Finaliste' .. txt
		end
		
		-- Score et parcours
		wikiText = wikiText .. ligneSansTri .. 'Score'
		wikiText = wikiText .. ligneSansTri .. ''
	end
	
	return wikiText
end


return z