Aller au contenu

Module:Nom dynastique

Cette page fait l’objet d’une mesure de semi-protection étendue.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 8 octobre 2017 à 10:07 et modifiée en dernier par Rehtse (discuter | contributions) (Annulation de la modification de Rehtse (d) erreur). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Ce module est utilisé par les modèles {{Souverain2}}, {{Souverain3}} et {{Souverain-}}.

Utilisation

Fonctions exportables :

  • fonction(frame) – description (courte description de fonction(frame) et autres informations pertinentes).
  • fonction2() – description2 (courte description de fonction2() et autres informations pertinentes).

Autres fonctions :

  • fonction() – description2 (courte description de fonction() et autres informations pertinentes).

Modules externes et autres éléments dont ce module a besoin pour fonctionner :

  • mw.title – description (courte description expliquant la dépendance de ce module externe).

Exemples

Pour des exemples, voir la page de test permettant de tester diverses modifications apportées.



local dyn = {}

local Outils = require( 'Module:Outils' )
local trim = Outils.trim

-- liste des chiffres romains pour conversion
local listeRomains = {	["I"] = "1",
	["Ier"] = "Premier",
	["Ire"] = "Première",
	["II"] = "2",
	["III"] = "3",
	["IV"] = "4",
	["V"] = "5",
	["VI"] = "6",
	["VII"] = "7",
	["VIII"] = "8",
	["IX"] = "9",
	["X"] = "10",
	["XI"] = "11", 
	["XII"] = "12",
	["XIII"] = "13",
	["XIV"] = "14",
	["XV"] = "15",
	["XVI"] = "16",
	["XVII"] = "17",
	["XVIII"] = "18",
	["XIX"] = "19",
	["XX"] = "20",
	["XXI"] = "21", 
	["XXII"] = "22",
	["XXIII"] = "23",
	["XXIV"] = "24"}

--  Ce booléen permet d'indiquer si on doit prendre le complément du lien pour l'afficher (exemple : "Louis Ier d'Italie"); c'est le cas
-- de l'appel par le modèle Souverain3
local afficher_le_complement = false

-- Gestion des erreurs
local erreur = ""

-- Types de chaînes
local typenom = "[a-zA-ZÇÉÈŒéèàùâêîôûëïöüçæœ%'%-]+"
local typeromains = "[IVXer]+"
local typecomplement = "[a-zA-ZÇÉÈŒéèàùâêîôûëïöüçæœ%'%- ]+"

-- Variables
local lien = ""
local complement = ""
local nom = ""
local quantieme = ""
local quantiemeaffiche = ""
local extension

-- Extraction d'une chaîne, on recherche si c'est de la forme  : "nom" "chiffres romains" "complément"
-- En retour, le premier paramètre indique s'il y a présence d'un nombre en chiffres romains
function dyn.chaine (chaine)
	local nom = ""
	local quantieme = ""
	local complement = ""
	local type_romain = false
	-- Si le paramètre passé n'existe pas, retour
	if not chaine
	then
		erreur = 'Lien absent'
		return
	else
		if string.len(chaine) == 0
		then
			erreur = 'Lien vide'
			return
		end
	end
	local nom = string.match( chaine, typenom, 1)
	-- Si le nom contient des caractères non prévus, retour
	if not nom
	then
		erreur = 'Lien incorrect'
		return
	end
	local format_nom_romain_complement = false
	local format_nom_romain = false
	local format_nom_complement = false

	local presence_romain = false
	if chaine:match (typenom..' '..typeromains..' ') then format_nom_romain_complement = true end
	if chaine:match (typenom..' '..typeromains) then format_nom_romain = true end

	if format_nom_romain_complement or format_nom_romain then presence_romain = true end
		
	if presence_romain
	then
		quantieme = string.match(chaine, typeromains, string.len(nom)+2)
		if listeRomains[quantieme]
		then
 			if format_nom_romain_complement
			then
				complement = string.sub(chaine, string.len(nom..quantieme)+2)
				complement = trim(complement)
			end
		else
			presence_romain = false
		end
	end
	
	return presence_romain, nom, quantieme, complement
end

-- Mise en forme, traitement des donneés
function dyn.traitement (a1, a2, a3, a4)
	local lien = ""
	local nom = ""
	local quantieme = ""
	local complement = ""
	local nom_affiche = ""
	local quantieme_affiche = ""
	local complement_affiche = ""
	-- Si le premier paramètre n'existe pas, retour
	if not a1
	then
		erreur = 'lien absent'
		return
	end
	
	-- Test de présence d'un deuxième paramètre
	local parametre2 = false
	if a2
	then
		if string.len(a2) > 0
		then
			parametre2 = true
		end
	end
	if parametre2
	then
		-- Ca où il y a au moins deux paramètres
		if a2 == string.match(a2, typeromains)
		then
			-- Si le deuxième paramètre est un nombre en chiffres romains, on est dans le cas de :
			--{{Souverain|Louis|XIV|(roi de France)|, le Roi Soleil}} et similaires
			nom = a1
			nom_affiche = a1
		    quantieme = a2
		    quantieme_affiche = dyn.typo(quantieme)
		    lien = nom..' '..quantieme
		    local parametre3 = false
		    if a3
		    then
		    	if string.len(a3) > 0
		    	then
			    	complement = a3
			    	lien = lien..' '..complement
			    end
		    end
		    if afficher_le_complement == true
		    then
		    	complement_affiche = a3
		    else
		    	if a4
			    then
			    	if string.len(a4) > 0
			    	then
				    	complement_affiche = a4
				    end
				end
			end
		else
			-- Si le deuxième paramètre n'est pas un nombre en chiffres romains, on est dans le cas de :
			--{{Souverain|Louis XIV (roi de France)|, le Roi Soleil}} et similaires
			lien = a1
			-- On vérifie d'abord si complément contient un texte à mettre en forme, auquel cas il s'impose sur le libellé du lien
			c1, c2, c3, c4 = dyn.chaine (a2)
			if c1 == true
			then
				quantieme_affiche = dyn.typo(c3)
				nom_affiche = c2
				if c4
				then
					if string.len(c4) > 0
					then
						complement_affiche = c4
					end
				end
			else
				c1, c2, c3, c4 = dyn.chaine (a1)
				nom_affiche = c2
				quantieme_affiche = dyn.typo(c3)
				complement_affiche = a2
			end
		end
	else
		-- Si le deuxième paramètre n'existe pas, on est dans le cas de : {{Souverain|Louis XIV de France}} et similaires
		c1, c2, c3, c4 = dyn.chaine (a1)
		lien = a1
		if c1
		then
			nom_affiche = c2
			quantieme_affiche = dyn.typo(c3)
		end
		if afficher_le_complement == true
		then
			if trim(c4)
			then
				complement_affiche = trim(c4)
			else
				afficher_le_complement = false
			end
		end
	end
	return lien, nom_affiche, quantieme_affiche, complement_affiche
end

function dyn.typo(quantieme)
	-- Mise en forme typographique : éventuelle infobulle et éventuel exposant à partir du chiffre romain envoyé
	-- On obtient le "quantième affiché"
	
	-- On teste d'abord si on a une chaine de la forme "Ier" ou "Ire"
	local test1 = "non"
	if string.len(quantieme) > 2
	then
		test1 = string.reverse(quantieme)
		test1 = string.sub(test1, 1, 2)
		test1 = string.reverse(test1)
	end
	
	-- émulation des modèles {{Ire}}, {{III}}
	local infobulle = ""
	local abbr = ""
	local exposant = "non"
	local numero = ""
	local fin = ""
	local quantieme_affiche = ""
	if (test1 == "re") or (test1 == "er")
	then
		if test1 == "re"
		then
			exposant = "<sup>re</sup>"
			infobulle = "première"
		else
			exposant = "<sup>er</sup>"
			infobulle = "premier"
		end
		quantieme = "I"..test1
		numero = "I"
		quantiemeaffiche = '<abbr class="abbr" title="'..infobulle..'" ><span class="romain" style="text-transform:uppercase">I</span>'..exposant..'</abbr>'
		fin = '</span>'..exposant..'</abbr>'
	else
		quantieme = string.upper(quantieme)
		infobulle = listeRomains[quantieme]
		numero = quantieme
		fin = '</span></abbr>'
	end
	if infobulle == nil
	then
		infobulle = "incoonu"
	end
	quantieme_affiche = '<abbr class="abbr" title="'..infobulle..'" ><span class="romain" style="text-transform:uppercase">'..numero..fin
	return quantieme_affiche
end

-- Fonction de mise en forme du lien à afficher
-- de la forme [[lien | "nom affiché"&nbsp;"quantième affiché" "complément affiché" ]], les éléments de la partie droite sont optionnels
function dyn.ecriture_avec_lien (lien, nom_affiche, quantieme_affiche, complement_affiche)
	local texte1 = ""
	local complement_affiche2 = complement_affiche
	-- si le complement affiché (", le Roi Soleil" dans le cas de Louis XIV) commence par une virgule, on ne met pas d'espace avant,
	-- mais on vérifie qu'il y a bien une espace entre la virgule et le mot suivant
	if complement_affiche
	then
		if string.len(complement_affiche) > 0
		then
			if string.sub(complement_affiche, 1, 1) == ","
			then
				if string.sub(complement_affiche, 2, 1) ~= " "
				then
					complement_affiche2 = ", "..string.sub(complement_affiche, 2)
				end
			end
		end
	end
	texte1 = '[['..lien
	if string.len(nom_affiche) > 0
	then
		if string.len(quantieme_affiche) > 0
		then
			-- S'il y a un nombre, ajout d'un "nowrap" pour l'espace insécable
			texte1 = texte1..'|<span class="nowrap">'..nom_affiche..' '..quantieme_affiche..'</span>'
			if complement_affiche2
			then
				if string.len(complement_affiche2) > 0
				then
					texte1 = texte1..' '..complement_affiche2
				end
			end
		else
			texte1 = texte1..'|'..nom_affiche
			if string.len(complement_affiche2) > 0
			then
				texte1 = texte1..' '..complement_affiche2
			end
		end
	else
		if complement_affiche2
		then
			if string.len(complement_affiche2) > 0
			then
				texte1 = texte1..'|'..complement_affiche2
			end
		end
	end
 	texte1 = texte1.."]]"
	return texte1
end

function dyn.ecriture_sans_lien (lien, nom_affiche, quantieme_affiche, complement_affiche)
	local texte1 = ""
	local complement_affiche2 = complement_affiche
	-- si le complement affiché (", le Roi Soleil" dans le cas de Louis XIV) commence par une virgule, on ne met pas d'espace avant,
	-- mais on vérifie qu'il y a bien une espace entre la virgule et le mot suivant
	if complement_affiche
	then
		if string.len(complement_affiche) > 0
		then
			if string.sub(complement_affiche, 1, 1) == ","
			then
				if string.sub(complement_affiche, 2, 1) ~= " "
				then
					complement_affiche2 = ", "..string.sub(complement_affiche, 2)
				end
			end
		end
	end
	if string.len(nom_affiche) > 0
	then
		if string.len(quantieme_affiche) > 0
		then
			-- S'il y a un nombre, ajout d'un "nowrap" pour l'espace insécable
			texte1 = texte1..'<span class="nowrap">'..nom_affiche..' '..quantieme_affiche..'</span>'
			if complement_affiche2
			then
				if string.len(complement_affiche2) > 0
				then
					texte1 = texte1..' '..complement_affiche2
				end
			end
		else
			texte1 = texte1..nom_affiche
			if string.len(complement_affiche2) > 0
			then
				texte1 = texte1..' '..complement_affiche2
			end
		end
	else
		if complement_affiche2
		then
			if string.len(complement_affiche2) > 0
			then
				texte1 = texte1..complement_affiche2
			end
		end
		if texte1 == ""
		then
			texte1 = lien
		end
	end
	return texte1
end

-- Affichage dans le cas général (émule le modèle Souverain2)
function dyn.Souverain2 (frame)
	local args = Outils.extractArgs( frame )
	local arg1, arg2, arg3, arg4 = args[1] or "", args[2] or "", args[3] or "", args[4] or ""
	
	local t1, t2, t3, t4 = dyn.traitement(arg1, arg2, arg3, arg4)
	if string.len(erreur) > 0
	then
		return erreur
	end
	
    local texte = dyn.ecriture_avec_lien (t1, t2, t3, t4)
   	if string.len(erreur) > 0
	then
		return erreur
	else
    	return texte
    end
end

-- Affichage dans le cas où le lien est de la forme [[Charles V le Sage|{{nobr|Charles V}} le Sage]]
-- correspond au modèle Souverain3, ce qui permet d'écrire :
-- {{Souverain3|Charles V le Sage}}
-- plutôt que: 
-- {{Souverain2|Charles V le Sage|le Sage}}
-- (émule le modèle Souverain3)
function dyn.Souverain3 (frame)
	local args = Outils.extractArgs( frame )
	local a1, a2, a3 = args[1] or "", args[2] or "", args[3] or ""
	
    afficher_le_complement = true
	local t1, t2, t3, t4 = dyn.traitement(a1, a2, a3)
   	if string.len(erreur) > 0
	then
		return erreur
	end
	
    local texte = dyn.ecriture_avec_lien (t1, t2, t3, t4)
   	if string.len(erreur) > 0
	then
		return erreur
	else
    	return texte
    end
end

-- Affichage dans le cas où on veut une mise en forme correspndant à {{nobr|Charles {{V}}}} ou {{nobr|Louis {{Ier}}}}
-- Aucune mise en forme s'il n'y a pas un nombre en chiffres romains
-- (correspond au modèle Souverain-)
function dyn.Souverain_sans_lien (frame)
	local args = Outils.extractArgs( frame )
	local a1, a2, a3, a4 = args[1] or "", args[2] or "", args[3] or "", args[4] or ""
	
    afficher_le_complement = true
	local t1, t2, t3, t4 = dyn.traitement(a1, a2, a3, a4)
   	if string.len(erreur) > 0
	then
		return erreur
	end
	
    local texte = dyn.ecriture_sans_lien (t1, t2, t3, t4)
   	if string.len(erreur) > 0
	then
		return erreur
	else
    	return texte
    end
end

return dyn