Module:Classification biologique

 Documentation[créer] [purger]
-- module (test) to handle classifications (biology)

local p = {}

--
function p.put(frame)
	local classification = frame.args["classification"]
	local ns = frame.args["ns"]
	local auts = frame.args["auteurs"]
	
	if (classification == nil) then
		return "[[Catégorie:Classification scientifique absente]]"
	end
	if (ns == nil) then
		return "[[Catégorie:Classification scientifique appelée sans nom scientifique]]"
	end
	if (auts == nil) then
		auts = ""
	end
	
	-- on récupère le contenu du module de classification
	local ctr = "Module:Classification biologique/" .. classification
	local ccurrent = require (ctr)
	if (ccurrent == nil) then
		return "[[Catégorie:Classification scientifique inconnue]]"
	end

	-- nom scientifique double ?
	tmpx = mw.text.split(ns, " ", true);
	if (tmpx[2] ~= nil) then
		ns = tmpx[1]
		sp = tmpx[2]
	else
		sp = nil
	end
	
	
	-- on cherche le taxon
	local taxon = ccurrent.taxons[ns]
	if (taxon == nil) then
		return "[[Catégorie:Classification scientifique avec nom scientifique inconnu]]"
	end
	
	-- on récupère les éléments d'info
	local t_nom = taxon["nom"]
	local t_article = taxon["article"]
	local t_rang = taxon["rang"]
	
	-- table pour empiler les éléments en cours
	local resu = {}
	
	local tmp = ""
	if (sp == nil) then
		-- on insert le premier
		tmp = "{{Taxobox taxon | " .. ccurrent.domain .. " | " .. t_rang ..
		      " | " .. t_nom .. " | " .. auts .. "}}<br/>"
	else
		-- c'est une espèce, qu'on gère
		tmp = "{{Taxobox taxon | " .. ccurrent.domain .. " | espèce | " ..
		      ns .. " " .. sp .. " | " .. auts .. "}}"
	end
	

	-- on le met dans la table
	table.insert(resu, tmp)
	
	-- si c'est une espèce on insert le genre
	if (sp ~= nil) then
		-- on cherche le taxon
		local z = ccurrent.taxons[ns]
		if (z == nil) then
			table.insert(resu, 1, "Erreur")
			return "Erreur…"
		end
		
		-- on prépare cette ligne (façon simple)
		local txt = ""
		txt = txt .. "{{Taxobox | " .. z["rang"] .. " | " ..
			      z["nom"] .. "}}"
 		-- ajout
 		table.insert(resu, 1, txt)
	end
	
	-- boucle pour chercher les niveaux du dessus
	local cur = t_nom
	while true do
		-- on cherche celui du dessus
		local sup = ccurrent.inclusions[t_nom]
		if (sup == nil) then
			break  -- terminé (ou erreur)
		end
		if (sup == "EOC") then
			break
		end
		
		-- on cherche le taxon
		local z = ccurrent.taxons[sup]
		if (z == nil) then
			return "[[Catégorie:Classification scientifique avec erreur interne]]"
		end
		
		-- on prépare cette ligne (façon simple)
		local txt = ""
		txt = txt .. "{{Taxobox | " .. z["rang"] .. " | " ..
			      z["nom"] .. "}}"
 		-- ajout
 		table.insert(resu, 1, txt)
 		-- suivant
 		t_nom = z["nom"]
	end
	
	-- on construit le résultat final
	local retour = ""
	for i, v in ipairs(resu) do
		retour = retour .. v
	end
	
	return frame:preprocess(retour)
end

--
function p.put2(frame)
	local classification = frame.args["classification"]
	local ns = frame.args["ns"]
	local auts = frame.args["auteurs"]
	local image = frame.args["image"]
	local legende = frame.args["légende"]
	
	
	if (classification == nil) then
		return "[[Catégorie:Classification scientifique absente]]"
	end
	if (ns == nil) then
		return "[[Catégorie:Classification scientifique appelée sans nom scientifique]]"
	end
	if (auts == nil) then
		auts = ""
	end

	-- on récupère le contenu du module de classification
	local ctr = "Module:Classification biologique/" .. classification
	local ccurrent = require (ctr)
	if (ccurrent == nil) then
		return "[[Catégorie:Classification scientifique inconnue]]"
	end

	-- nom scientifique double ?
	nsx = ns
	tmpx = mw.text.split(ns, " ", true);
	if (tmpx[2] ~= nil) then
		ns = tmpx[1]
		sp = tmpx[2]
	else
		sp = nil
	end
	
	
	-- on cherche le taxon
	local taxon = ccurrent.taxons[ns]
	if (taxon == nil) then
		return "[[Catégorie:Classification scientifique avec nom scientifique inconnu]]"
	end
	
	-- on récupère les éléments d'info
	local t_nom = taxon["nom"]
	local t_article = taxon["article"]
	local t_rang = taxon["rang"]
	
	-- table pour empiler les éléments en cours
	local resu = {}
	
	local tmp = ""
	if (sp == nil) then
		-- on insert le premier
		tmp = "{{Taxobox taxon | " .. ccurrent.domain .. " | " .. t_rang ..
		      " | " .. t_nom .. " | " .. auts .. "}}<br/>"
	else
		-- c'est une espèce, qu'on gère
		tmp = "{{Taxobox taxon | " .. ccurrent.domain .. " | espèce | " ..
		      ns .. " " .. sp .. " | " .. auts .. "}}"
	end
	

	-- on le met dans la table
	table.insert(resu, tmp)
	
	-- si c'est une espèce on insert le genre
	if (sp ~= nil) then
		-- on cherche le taxon
		local z = ccurrent.taxons[ns]
		if (z == nil) then
			table.insert(resu, 1, "Erreur")
			return "Erreur…"
		end
		
		-- on prépare cette ligne (façon simple)
		local txt = ""
		txt = txt .. "{{Taxobox | " .. z["rang"] .. " | " ..
			      z["nom"] .. "}}"
 		-- ajout
 		table.insert(resu, 1, txt)
	end
	
	-- boucle pour chercher les niveaux du dessus
	local cur = t_nom
	while true do
		-- on cherche celui du dessus
		local sup = ccurrent.inclusions[t_nom]
		if (sup == nil) then
			break  -- terminé (ou erreur)
		end
		if (sup == "EOC") then
			break
		end
		
		-- on cherche le taxon
		local z = ccurrent.taxons[sup]
		if (z == nil) then
			return "[[Catégorie:Classification scientifique avec erreur interne]]"
		end
		
		-- on prépare cette ligne (façon simple)
		local txt = ""
		txt = txt .. "{{Taxobox | " .. z["rang"] .. " | " ..
			      z["nom"] .. "}}"
 		-- ajout
 		table.insert(resu, 1, txt)
 		-- suivant
 		t_nom = z["nom"]
	end
	-- on insert taxobox début
	ddd = ""
	-- italiques ou pas ?
	if ((ccurrent.domain == "végétal") or (
		(t_rang == "espèce") or (t_rang == "genre"))
	   ) then
		ddd = "{{Taxobox début | " .. ccurrent.domain .. " | ''" .. nsx .. "'' | "
	else
		ddd = "{{Taxobox début | " .. ccurrent.domain .. " | " .. nsx .. " | "
	end
	if (image ~= nil) then
		ddd = ddd .. image .. " | "
	else
		ddd = ddd .. " | "
	end
	if (legende ~= nil) then
		ddd = ddd .. legende .. " | "
	else
		ddd = ddd .. " | "
	end
	ddd = ddd .. "classification=" .. classification .. " }}"
 	table.insert(resu, 1, ddd)

	-- on construit le résultat final
	local retour = ""
	for i, v in ipairs(resu) do
		retour = retour .. v
	end
	
	return frame:preprocess(retour)
end


--
return p