Module:Classements en fin de saison

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

local genreTab = {
	F = 'la WTA', D = 'la WTA', W = 'la WTA',
	M = "l'ATP", H = "l'ATP", A = "l'ATP",
}
local increase = '[[Fichier:Increase2.svg|9px|en augmentation|link=]] '
local decrease = '[[Fichier:Decrease2.svg|9px|en diminution|link=]] '
local steady = '[[Fichier:Steady.svg|9px|stagnation|link=]] '
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 genreTab.F
		end
	end
	return genre or genreTab.M
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 yearRow(genre)
	local rowObject = {
		html = mw.html.create('tr')
	}
	rowObject.html
		:addClass('rank-year-row')
		:tag('th')
			:attr('scope', 'row')
			:wikitext("Année")
	function rowObject:add(year)
		mw.log( t)
		self.html:tag('td')
			:wikitext("[[Saison " .. year .. " de " .. genre .. "|" .. year .."]]")
	end
	function rowObject:addEmpty(title)
		self.html:tag('td')
			:attr('title', title)
	end
	return rowObject
end

local function rankRow(list, competition)
	if not list then
		return {
			html = '',
			add = function () end,
			addEmpty = function () end,
		}
	end
	local best = norank
	local previous = norank
	local tr = mw.html.create('tr')
	tr:tag('th')
		:attr('scope', 'row')
		:wikitext("Rang en " .. competition)
	for i, rank in ipairs(list) do
		if rank < best then
			best = rank
		end
	end
	
	local rowObject = {
		html = tr,
	}
	function rowObject:addEmpty(title)
		self.html:tag('td')
			:attr('title', title)
		previous = norank
	end
	function rowObject:add(i)
		rank = list[i]
		local td = self.html:tag('td')
		if rank < norank and previous ~= norank then
			if rank < previous then
				td:node(increase)
			elseif rank > previous then
				td:node(decrease)
			else
				td:node(steady)
			end
		end
		if rank == norank then
			td:wikitext('–')
		elseif rank == best then
			td:tag('b')
				:wikitext(rank)
		else
			td:wikitext(rank)
		end
		previous = rank
	end
	return rowObject
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, nom)
	local s = '<small>Source : {{Stats ITF}}</small>'
	if nom then
		s = '<small>Source : {{Stats ITF||'.. nom .. '}}</small>'
	end
	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
				table.insert(years, 1, yearRanks[1])
				table.insert(single, 1, yearRanks[2] or norank)
				table.insert(double, 1, yearRanks[3] or norank)
			end
		end
	end
	local yearObj = yearRow(genre)
	local singleObj = rankRow(single, 'simple' )
	local doubleObj = rankRow(double, 'double')
	for i = 1, #years do
		if years[i-1] and years[i-1] < years[i] - 1 then
			local title = 'Pas de classements en ' .. (years[i-1] + 1)
			if years[i-1] < years[i] - 2 then
				if years[i-1] == years[i] - 3 then
					title = title .. ' et ' .. (years[i] - 1)
				else
					title = 'Pas de classements entre ' .. (years[i-1] + 1) .. ' et ' .. (years[i] - 1)
				end
			end
			yearObj:addEmpty(title)
			singleObj:addEmpty(title)
			doubleObj:addEmpty(title)
		end
		yearObj:add(years[i])
		singleObj:add(i)
		doubleObj:add(i)
	end

	local html = init()
		:node(yearObj.html)
	if single then
		html:node(singleObj.html)
	end
	if double then
		html:node(doubleObj.html)
	end
	return tostring(html) .. source(frame, args.source, args.nom)
end

return p