Aller au contenu

Poképédia subit actuellement quelques soucis techniques !
L'équipe est au courant des problèmes et fait le nécessaire pour les arranger le plus rapidement possible. Merci de votre compréhension !

Module:Test

De Poképédia

Seulement pour tester. Si un modèle utilise une fonction de ce module, il cassera un jour !


local p = {}
local ressources = {
	["infosPokemon"] = require("Module:Ressources/infosPokemon")
}

function p.yop() -- met un mot en lowercase
	return mw.ustring.lower( "ÉLLOÈ" )
end

function p.poy() -- première lettre en majuscule
	return mw.getContentLanguage():ucfirst( "ébloubi" )
end

local numeros = mw.loadData("Module:Data/NumérosPokémon")

function p.infosPokemon( poke, mode )
	local forme, sexe, nom, lien, jeu, jeu_miniature
	if ( string.match(poke, "forme%([^)]*%)") ) then
		forme = " " .. string.sub(string.match(poke, "forme%([^)]*%)"), 7, -2)
		poke = string.gsub( poke, "forme%([^)]*%)", "" )
	else forme = "" end
	if ( string.match(poke, "sexe%([^)]*%)") ) then
		sexe = string.sub(string.match(poke, "sexe%([^)]*%)"), 6, -2)
		if sexe == "f" then sexe = " ♀" else sexe = " ♂" end
		poke = string.gsub( poke, "sexe%([^)]*%)", "" )
	else sexe = "" end
	if ( string.match(poke, "jeu%([^)]*%)") ) then
		jeu = mw.getCurrentFrame():expandTemplate{ title = 'Sup', args = { string.sub(string.match(poke, "jeu%([^)]*%)"), 5, -2) } }
		poke = string.gsub( poke, "jeu%([^)]*%)", "" )
	else jeu = "" end
	if ( string.match(poke, "jeu%-miniature%([^)]*%)") ) then
		jeu_miniature = " " .. string.sub(string.match(poke, "jeu%-miniature%([^)]*%)"), 15, -2)
		poke = string.gsub( poke, "jeu%-miniature%([^)]*%)", "" )
	else jeu_miniature = "" end
	
	if forme == " Alola" then
		poke = mw.text.trim( poke )
		nom = "Forme d'Alola"
		lien = poke .. " d'Alola"
	elseif forme == " Galar" then
		poke = mw.text.trim( poke )
		nom = "Forme de Galar"
		lien = poke .. " de Galar"
	elseif forme == " Gigamax" then
		poke = mw.text.trim( poke )
		nom = "Gigamax"
		lien = poke .. " Gigamax"
	else
		if ( string.match(poke, "nom%([^)]*%)") ) then
			nom = string.sub(string.match(poke, "nom%([^)]*%)"), 5, -2)
			poke = string.gsub( poke, "nom%([^)]*%)", "" )
		elseif forme ~= "" then nom = "Forme" .. forme
		elseif sexe == " ♀" then nom = "Femelle"
		elseif sexe == " ♂" then nom = "Mâle"
		else nom = "" end
		if ( string.match(poke, "lien%([^)]*%)") ) then
			lien = string.sub(string.match(poke, "lien%([^)]*%)"), 6, -2)
			poke = string.gsub( poke, "lien%([^)]*%)", "" )
		else lien = mw.text.trim( poke ) end
	end
	
	poke = mw.text.trim( poke )
	local num = numeros[poke]
	if num == nil then num = "???" end
	
	if mode == "miniature" then 
		if nom == "" then return "[[Fichier:Miniature " .. num .. forme .. sexe .. jeu_miniature .. ".png|link=" .. lien .. "|alt=" .. lien .. "|" .. lien .. "]]" .. jeu
		else return "[[Fichier:Miniature " .. num .. forme .. sexe .. jeu_miniature .. ".png|link=" .. lien .. "|alt=" .. poke .. " (" .. nom .. ")|" .. poke .. " (" .. nom .. ")]]" .. jeu
		end
	else
		if nom == "" then return "<span style='white-space: nowrap;' class='miniat-poke'>" .. num .. " [[Fichier:Miniature  " .. num .. forme .. sexe .. jeu_miniature .. ".png|link=" .. lien .. "|alt=]] [[" .. lien .. "|" .. poke .. "]]" .. jeu .. "</span>"
		else return "<span style='white-space: nowrap;' class='miniat-poke'>" .. num .. " [[Fichier:Miniature " .. num .. forme .. sexe .. jeu_miniature .. ".png|link=" .. lien .. "|alt=]] [[" .. lien .. "|" .. poke .. "]]" .. jeu .. "</span><br /><span style='font-style: italic; font-size: 90%;'>" .. nom .. "</span>"
		end
	end
end

function p.infosPokemon2(frame)
	poke = frame.args[1]
	mode = frame.args[2]
	return p.infosPokemon(poke, mode)
end

function p.testregexp(frame)
	a = frame.args[1]
	
	b = a:gsub(".*{{Abréviation|(.*)}}.*", "%1")
	return b
end

local numeros = mw.loadData("Module:Data/NumérosPokémon")

function separate_thousands(amount)
	local formatted = amount
	while true do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1 %2')
		if (k==0) then
			break
		end
	end
	return formatted
end

function p.donnees(frame)
	local donnees = frame.args[1]
	local lignes = {}
	if donnees ~= nil then lignes = mw.text.split(donnees, "\n") end
	
	images_rangement = {
		["RB"] = "[[Fichier:Vide.png|32px]]",
		["J"] = "[[Fichier:Vide.png|32px]]",
		["OA"] = "[[Fichier:Sprite XXX OA.png|32px]]",
		["C"] = "[[Fichier:Sprite XXX OA.png|32px]]",
		["RS"] = "[[Fichier:Sprite Sac (XXX) ♂ RS.png|32px]]",
		["RFVF"] = "[[Fichier:Sprite Sac (XXX) ♂ RFVF.png|32px]]",
		["E"] = "[[Fichier:Sprite Sac (XXX) ♂ E.png|32px]]",
		["DP"] = "[[Fichier:Miniature Poche XXX (sélectionnée) DP.png|32px]]",
		["Pt"] = "[[Fichier:Miniature Poche XXX (sélectionnée) Pt.png|32px]]",
		["HGSS"] = "[[Fichier:Miniature Poche XXX (sélectionnée) HGSS.png]]",
		["NB"] = "[[Fichier:Sprite Sac (XXX, transition) ♂ NB.png|32px]]",
		["N2B2"] = "[[Fichier:Sprite Sac (XXX, transition) ♂ N2B2.png|32px]]",
		["XY"] = "[[Fichier:Miniature Poche XXX (sélectionnée) XY.png]]",
		["ROSA"] = "[[Fichier:Miniature Poche XXX (sélectionnée) ROSA.png]]",
		["SL"] = '<div style="width:32px; text-align:center; display:inline-block">[[Fichier:Miniature Poche XXX SL.png]]</div>',
		["USUL"] = '<div style="width:32px; text-align:center; display:inline-block">[[Fichier:Miniature Poche XXX USUL.png]]</div>',
		["LGPE"] = "[[Fichier:Miniature XXX LGPE.png]]",
		["EB"] = "[[Fichier:Miniature Poche XXX (sélectionnée) EB.png|class=imagenoire|32px]]",
		["DEPS"] = "[[Fichier:Miniature Poche XXX (sélectionnée) DEPS.png|32px]]",
		["LPA"] = "[[Fichier:Miniature Sacoche LPA.png|32px]]",
		["EV"] = "[[Fichier:Miniature Poche XXX (sélectionnée) EV.png|32px]]",
		["LPZA"] = "[[Fichier:Miniature Poche XXX LPZA.png|class=imagenoire|32px]]",
	}
	
	function afficher_monnaie(monnaie, jeu)
		local d = {
			["P"] = frame:expandTemplate({ title = 'P'}),
			["Poké"] = frame:expandTemplate({ title = 'Monnaie PDM', args = { ["jeu"] = jeu }}),
			["Méga-Fragment"] = frame:expandTemplate({ title = "Objet", args = { "Méga-Fragment", ["jeu"] = jeu, ["nom"] = "non" }}),
			["Méga-Fragments"] = frame:expandTemplate({ title = "Objet", args = { "Méga-Fragment", ["jeu"] = jeu, ["nom"] = "non" }})
		}
		if d[monnaie]
		then return d[monnaie]
		else return monnaie
		end
	end
	
	local corps_result = {}
	local has_notes = false
	local donnees_dict = {}

	local i = 1
	while lignes[i] ~= nil do
		local ligne = lignes[i]
		local ligne_split = mw.text.split(ligne, " / ")
		
		for j = 3, 5 do
			if ligne_split[j] == nil or ligne_split[j] == "-"
			then ligne_split[j] = "—"
			end
		end
		
		donnees_dict[i] = {
			["jeu"] = ligne_split[1],
			["rangement"] = ligne_split[2],
			["achat"] = ligne_split[3],
			["vente"] = ligne_split[4],
			["note"] = ligne_split[5]
		}
		
		i = i + 1
	end
	
	i = 1
	while lignes[i] ~= nil do
		local jeu = donnees_dict[i].jeu
		local rangement = donnees_dict[i].rangement
		local achat = donnees_dict[i].achat
		local vente = donnees_dict[i].vente
		local note = donnees_dict[i].note
		
		if jeu ~= ""
		then
			precedent_exists = donnees_dict[i-1] ~= nil and donnees_dict[i-1].jeu ~= ""
			
			table.insert(corps_result, "<tr><td>" .. frame:expandTemplate({ title = 'Abréviation', args = { jeu } }) .. "</td>")
			
			if not rangement
			then rangement = "—"
			else
				if jeu == "OA" or jeu == "C"
				then rangement = "Poche " .. rangement
				end
				
				if images_rangement[jeu]
				then rangement = images_rangement[jeu]:gsub("XXX", rangement) .. " " .. rangement
				end
			end
			table.insert(corps_result, '<td style="white-space:nowrap; text-align:left">' .. rangement .. '</td>')
			
			achat_texte = "—"
			local rowspan = 1
			if not(precedent_exists and achat == donnees_dict[i-1].achat)
			then
				while donnees_dict[i + rowspan] and achat == donnees_dict[i + rowspan].achat do
					rowspan = rowspan + 1
				end
				
				if achat
				then
					achats = mw.text.split(achat, ", ")
					achat_texte = ""
					local j = 1
					while achats[j] do
						achat_j = achats[j]
						local precision_achat = ""
						local parenthese_pos = achat_j:find(" %(")
						if parenthese_pos
						then precision_achat = achat_j:sub(parenthese_pos):gsub(" %(", " <small>(") .. "</small>"
							achat_j = achat_j:sub(0, parenthese_pos - 1)
						end
						
						espace_pos = achat_j:find(" ")
						local unite_achat = ""
						if espace_pos
						then unite_achat = achat_j:sub(espace_pos + 1) -- + 1 pour enlever l'espace
							unite_achat = " " .. afficher_monnaie(unite_achat, jeu)
							achat_j = achat_j:sub(0, espace_pos)
						end
						
						if achat_j == "-"
						then achat_j = "—"
						else achat_j = separate_thousands(achat_j)
						end
						
						if achat_texte ~= "" then achat_texte = achat_texte .. "<br>" end
						achat_texte = achat_texte .. achat_j .. unite_achat .. precision_achat
						
						j = j + 1
					end
				end
				table.insert(corps_result, '<td rowspan="' .. rowspan .. '" style="white-space:nowrap">' .. achat_texte .. "</td>")
			end
			
			vente_texte = "—"
			rowspan = 1
			if not(precedent_exists and vente == donnees_dict[i-1].vente)
			then 
				while donnees_dict[i + rowspan] and vente == donnees_dict[i + rowspan].vente do
					rowspan = rowspan + 1
				end
				
				if vente
				then
					ventes = mw.text.split(vente, ", ")
					vente_texte = ""
					local j = 1
					while ventes[j] do
						vente_j = ventes[j]
						local precision_vente = ""
						local parenthese_pos = vente_j:find(" %(")
						if parenthese_pos
						then precision_vente = vente_j:sub(parenthese_pos):gsub(" %(", " <small>(") .. "</small>"
							vente_j = vente_j:sub(0, parenthese_pos - 1)
						end
						
						mw.log(vente_j)
						
						espace_pos = vente_j:find(" ")
						local unite_vente = ""
						if espace_pos
						then unite_vente = vente_j:sub(espace_pos + 1) -- + 1 pour enlever l'espace
							
						mw.log(unite_vente)
							unite_vente = " " .. afficher_monnaie(unite_vente, jeu)
							vente_j = vente_j:sub(0, espace_pos)
						end
						
						mw.log(vente_j)
						
						if vente_j == "-"
						then vente_j = "—"
						else vente_j = separate_thousands(vente_j)
						end
						
						if vente_texte ~= "" then vente_texte = vente_texte .. "<br>" end
						vente_texte = vente_texte .. vente_j .. unite_vente .. precision_vente
						
						j = j + 1
					end
				end
				table.insert(corps_result, '<td rowspan="' .. rowspan .. '" style="white-space:nowrap">' .. vente_texte .. "</td>")
			end
			
			rowspan = 1
			if not(precedent_exists and note == donnees_dict[i-1].note)
			then
				while donnees_dict[i + rowspan] and note == donnees_dict[i + rowspan].note do
					rowspan = rowspan + 1
				end
				
				if note
				then has_notes = true
				else note = "—"
				end
				
				table.insert(corps_result, '<td rowspan="' .. rowspan .. '">' .. note .. "</td>")
			end
			table.insert(corps_result, "</tr>")
		end
		
		i = i + 1
	end

	local result = {}
	local corps = table.concat(corps_result, "")
	
	table.insert(result, '<table class="tableaustandard centre">')
	table.insert(result, '<tr><th rowspan="2">Jeu</th><th rowspan="2">Rangement</th><th colspan="2">Prix</th>')
	if has_notes
	then table.insert(result, '<th rowspan="2">Notes</th>')
	else corps = corps:gsub('<td rowspan="%d">—</td></tr>', '</tr') -- on enlève la colonne Notes
	end
	
	table.insert(result, '</tr><tr><th>Achat</th><th>Vente</th></tr>')

	table.insert(result, corps)
	table.insert(result, "</table>")
	
	return table.concat(result, "")
end

function p.sortedSummary(frame)
	local title = mw.title.getCurrentTitle()
    local content = title:getContent()
    local aimedTitleLevel = frame.args[1]
    if aimedTitleLevel == nil then aimedTitleLevel = 2 else aimedTitleLevel = tonumber(aimedTitleLevel) end

    local sections = {}
    local foundTitles = {}
    
    for line in content:gmatch("[^\n]+") do
    	local hashes, title = line:match("^(=+)%s(.*)%s%1$")
    	
    	if hashes then
    		title = frame:preprocess(title)
    		title = title:gsub("''", "")
    		title = title:match("^%[%[[^|]+|([^]]+)%]%]$")
			    or title:match("^%[%[([^]]+)%]%]$")
			    or title
    		local titleLevel = #hashes
    		local id
    		if foundTitles[title] == nil then
    			foundTitles[title] = 1
    			id = mw.uri.anchorEncode(title)
			else
				foundTitles[title] = foundTitles[title] + 1
				id = mw.uri.anchorEncode(title) .. "_" .. foundTitles[title]
			end
			
			if (titleLevel == aimedTitleLevel) then
				table.insert(sections, {
					title = title,
					id = '#' .. id,
					titleLevel = titleLevel				
				})
			end
    	end
    end
    
    table.sort(sections, function(a, b)
	    return a.title < b.title
	end)
   
    local result = {}
    for _, section in ipairs(sections) do
    	table.insert(result, '* [[' .. section.id .. '|' .. section.title .. ']]')
    end

	return table.concat(result, '\n')
end

return p