Aller au contenu

Module:Classements en fin de saison

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 26 mai 2022 à 02:56 et modifiée en dernier par Zebulon84 (discuter | contributions) (retouche de la modif. précédente). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[créer] [purger]
local p = {}

local genreTab = {
	W = 'WTA', F = 'WTA', D = 'WTA',
	A = 'APT', M = 'APT', H = 'APT'
}
local increase = '<span data-sort-value="">[[Fichier:Increase2.svg|9px|en augmentation|link=]]</span> '
local decrease = '<span data-sort-value="">[[Fichier:Decrease2.svg|9px|en diminution|link=]]</span> '
local steady = '<span data-sort-value="">[[Fichier:Steady.svg|9px|en stagnation|link=]]</span> '
local norank = 10000

local function getGenre(genre)
	genre = genre and genreTab[genre:sub(1, 1):upper()]
	if not genre then
		local entity = mw.wikibase.getEntityIdForCurrentPage() or 'Q105550'
		local p21 = mw.wikibase.getBestStatements(entity, 'P21')
		if p21[1] 
			and p21[1].mainsnak.snaktype == 'value' 
			and p21[1].mainsnak.datavalue.value.id == 'Q6581072'
		then
			return 'WTA'
		end
	end
	return genre or 'ATP'
end

local function init()
	local html = mw.html.create('table')
	html:addClass('wikitable')
		:addClass('yearend-rank-table')
		:tag('caption')
			:wikitext("Classements à l'issue de chaque saison")
			
	return html
end

local function yearLine(list, genre)
	local g = "l'ATP"
	if genre == 'WTA' then
		g = "la WTA"
	end

	local line = mw.html.create('tr')
		:addClass('rank-year-row')
	line:tag('th')
			:attr('scope', 'row')
			:wikitext("Année")

	for _, y in ipairs(list) do
		line:tag('td')
				:wikitext("[[Saison " .. y .. " de " .. g .. "|" .. y .."]]")
	end
	return line
end

local function rankLine(list, comp)
	local line = mw.html.create('tr')
	line:tag('th')
			:attr('scope', 'row')
			:wikitext("Rang en " .. comp)
			
	local best = norank
	for i, r in ipairs(list) do
		if r < best then
			best = r
		end
	end

	for i, r in ipairs(list) do
		local td = line:tag('td')
		if i > 1 and r < norank and list[i-1] ~= norank then
			if r < list[i-1] then
				td:node(increase)
			elseif r > list[i-1] then
				td:node(decrease)
			else
				td:node(steady)
			end
		end
		if r == best then
			td:tag('b')
				:wikitext(r)
		elseif r == norank then
			td:wikitext('—')
		else
			td:wikitext(r)
		end
	end
	return line
end

local function split(text)
	if type(text) ~= 'string' then
		return {}
	end
	text = mw.text.trim(text):gsub("'", '')
	local list = mw.text.split( text, '[%s,;:]+' )
	for i=1, #list do
		list[i] = tonumber(list[i]) or norank
	end
	return list
end

local function source(frame, source)
	local s = '<small>Source : {{Stats ITF}}</small>'
	if source then
		s = '<small>Source : {{Stats '.. source .. '}}</small>'
	end
	return frame:preprocess(s)
end

function p.rankTable(frame)
	local args = frame.getParent and frame:getParent().args or frame
	local genre = getGenre(args.genre)
	local years = split(args['années'] or args['année'])
	local single = args.simple and split(args.simple)
	local double = args.double and split(args.double)
	if #years == 0 and args[1] then
		years, single, double = {}, {}, {}
		for _, arg in ipairs(args) do
			local yearRanks = split(arg)
			if yearRanks[1] then
				years[#years + 1] = yearRanks[1]
				single[#single + 1] = yearRanks[2] or '–'
				double[#double + 1] = yearRanks[3] or '–'
			end
		end
	end

	local html = init()
		:node(yearLine(years, genre))
	if single then
		html:node(rankLine(single, 'simple'))
	end
	if double then
		html:node(rankLine(double, 'double'))
	end
	return tostring(html) .. source(frame, args.source)
end

return p