Module:Nom dinastico

De Vouiquipèdia, l’enciclopèdia abada.

La documentation pour ce module peut être créée à Module:Nom dinastico/doc

local p = {}

local Outils = require( 'Module:Outils' )
local trim = Outils.trim
local Romens = require('Module:Chifros romens')
local conversion = Romens.conversion

--  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 fare_vere_lo_complement = false

local tiporomens = "[IVXLCDM]+[ér]*"
local tiponom = '[%C]+'

local lim = ""
local complement = ""
local nom = ""

-- 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 p.chena(chena)
	local function extraccion_pieces(chena, separator)
		local posicion = 0
		presence_romen = true
		posicion = string.find(chena, ' '..tiporomens..separator)
		quantiemo = string.match(chena, ' '..tiporomens..separator)
		nom = string.sub(chena, 1, posicion-1)
 		if ((posicion+string.len(quantiemo)) < string.len(chena)) then
 			if string.sub(separator, 1, 1) == ' ' then
				complement = trim(string.sub(chena, posicion+string.len(quantiemo)))
			else
				complement = trim(string.sub(chena, posicion+string.len(quantiemo)-string.len(separator)))
				quantiemo=string.gsub(quantiemo, separator, '')
			end
 		end
 		nom=trim(nom)
 		quantiemo=trim(quantiemo)
 		
		return presence_romen, nom, quantiemo, complement
	end
	
	local nom = ""
	local quantiemo = ""
	local complement = ""
	local tipo_romen = false
	local presence_romen = false
	-- Si le paramètre passé n'existe pas, retour
	if not chena then
		return false, '<span class="error">Gins de paramètro</span>'
	else
		if string.len(chena) == 0
		then
			return false, '<span class="error">Gins de paramètro</span>'
		end
	end
	chena = ' '..chena..' '
	
	if string.match(chena, ' '..tiporomens..' ') then
		presence_romen, nom, quantiemo, complement = extraccion_pieces(chena, ' ')
	else
		if string.match(chena, ' '..tiporomens..', ') then
			presence_romen, nom, quantiemo, complement = extraccion_pieces(chena, ', ')
		end
	end

	return true, '', presence_romen, nom, quantiemo, complement
end

function p.surnom(chena)
	presence_etaliques, presence_guilyemets, presence_det, presence_virgula = false, false, false, false
	if mw.ustring.match(chena, "''") then
		presence_etaliques = true
		chena = mw.ustring.gsub(chena, "''", "")
	end
	if mw.ustring.match(chena, '«') then
		presence_guilyemets = true
		chena = mw.ustring.gsub(chena, '« ', '')
		chena = mw.ustring.gsub(chena, '«', '')
	end
	if mw.ustring.match(chena, '»') then
		presence_guilyemets = true
		chena = mw.ustring.gsub(chena, ' »', '')
		chena = mw.ustring.gsub(chena, '»', '')
	end
	if mw.ustring.match(chena, ' déta? ') or (mw.ustring.sub(chena, 1, 4) == 'dét ') or (mw.ustring.sub(chena, 1, 5) == 'déta ') then
		presence_det = true
		chena = mw.ustring.gsub(chena, 'dét ', '')
		chena = mw.ustring.gsub(chena, 'déta ', '')
	end
	if presence_etaliques or presence_guilyemets or presence_det then
		if mw.ustring.match(chena, ', ') then
			presence_virgula = true
			chena = mw.ustring.gsub(chena, ', ', ' ')
		end
	end
	
	return chena, presence_etaliques, presence_guilyemets, presence_det, presence_virgula
end

function p.neteyajo(chena)
	-- remplacement des espaces insécables
	if chena then
		chena = chena
			-- nbsp
			:gsub( '\194\160', ' ' )
			:gsub( '&nbsp;', ' ' )
			:gsub( '&#160;', ' ' )
			-- narrow nbsp
			:gsub( '\226\128\175', ' ' )
			:gsub( '&#8239;', ' ' )
			-- thin space
			:gsub( '\226\128\137', ' ' )
			:gsub( '&thinsp;', ' ' )
			:gsub( '&#8201;', ' ' )
			-- simple space
			:gsub( '&#32;', ' ' )
	end
	return chena
end

-- Mise en forme, traitement des donneés
function p.tretament(a1, a2, a3, a4)
	local eprova = true
	local messajo = ''
	local lim = ''
	local nom = ''
	local quantiemo = ''
	local complement = ''
	local nom_fat_vere = ''
	local quantiemo_fat_vere = ''
	local complement_fat_vere = ''
	
	a1 = p.neteyajo(a1)
	a2 = p.neteyajo(a2)
	a3 = p.neteyajo(a3)
	a4 = p.neteyajo(a4)

	-- Si le premier paramètre n'existe pas, retour
	if (a1 == '') and (a2 == '') then
		return false, '<span class="error">Paramètros absents</span>'
	end
	
	-- Test de présence d'un deuxième paramètre
	local parametro2 = false
	if a2 then
		if string.len(a2) > 0 then
			parametro2 = true
		end
	end
	if parametro2 then
		-- Ca où il y a au moins deux paramètres
		if a2 == string.match(a2, tiporomens) 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_fat_vere = a1
		    quantiemo = a2
		    eprova, messajo, quantiemo_fat_vere = p.tipo(quantiemo)
		    if not eprova then return eprova, messajo end
		    lim = nom..' '..quantiemo
		    complement = ''
		    if a3 then
		    	complement = a3
		    	if string.len(complement) > 0 then
			    	lim = lim..' '..p.surnom(complement)
			    end
		    end
		    if not a4 then a4 ='' end
		    if a4 == '' then
		    	if a3 and fare_vere_lo_complement then
					complement_fat_vere = a3
				end
			else
		    	complement_fat_vere = a4
			end
		else
			-- Si le deuxième paramètre n'est pas un nombre en chiffres romains, soit il y a une erreur,
			-- soit on est dans le cas de :
			--{{Souverain|Louis XIV (roi de France)|, le Roi Soleil}} et similaires
			--ou {{Souverain|René II d'Anjou|René III de Poitiers}}
			--Il faut qu'il y ait un nombre romain dans au moins un des deux
			lim = 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
			eprova, messajo, presence_romens, nom, quantiemo, complement = p.chena(a2)
			if not eprova then return eprova, messajo end
			if presence_romens then
				eprova, messajo, quantiemo_fat_vere = p.tipo(quantiemo)
				if not eprova then return eprova, messajo end
				nom_fat_vere = nom
				if complement then
					complement_fat_vere = complement
				end
			else
				eprova, messajo, presence_romens, nom, quantiemo, complement = p.chena(a1)
				if not eprova then return eprova, messajo end
				nom_fat_vere = nom
				if presence_romens then
					eprova, messajo, quantiemo_fat_vere = p.tipo(quantiemo)
					if not eprova then return eprova, messajo end
					complement_fat_vere = a2
				else
					eprova = false
					messajo = '<span class="error">Un nombro en chifros romens est nècèssèro</span>'
				end
			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
		eprova, messajo, presence_romens, nom, quantiemo, complement = p.chena(a1)
		if not eprova then return eprova, messajo end
		local presence_etaliques = false
		lim = a1
		if presence_romens then
			nom_fat_vere = nom
			eprova, messajo, quantiemo_fat_vere = p.tipo(quantiemo)
			if not eprova then return eprova, messajo end
		end
		lim, presence_etaliques, presence_guilyemets = p.surnom(lim)
		if fare_vere_lo_complement == true then
			if trim(complement) then
				complement_fat_vere = trim(complement)
			else
				if (presence_etaliques or presence_guilyemets) and not presence_romens then
					complement_fat_vere = a1
				else
					fare_vere_lo_complement = false
				end
			end
		end
	end
  	return eprova, messajo, lim, nom_fat_vere, quantiemo_fat_vere, complement_fat_vere
end

function p.tipo(quantiemo)
	local eprova = true
	local messajo = ''
	-- 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 eprova1 = 'nan'
	if string.len(quantiemo) > 2 then
		eprova1 = string.reverse(quantiemo)
		eprova1 = string.sub(eprova1, 1, 2)
		eprova1 = string.reverse(eprova1)
	end
	
	-- émulation des modèles {{Ire}}, {{III}}
	local enfobula = ''
	local abbr = ''
	local exposent = 'nan'
	local numero = ''
	local fin = ''
	local quantiemo_fat_vere = ''
	if (eprova1 == 're') or (eprova1 == 'ér') then
		if eprova1 == 're' then
			exposent = '<sup>re</sup>'
			enfobula = 'premiére'
		else
			exposent = '<sup>ér</sup>'
			enfobula = "premiér"
		end
		quantiemo = 'I'..eprova1
		numero = 'I'
		fin = '</span>'..exposent..'</abbr>'
	else
		quantiemo = string.upper(quantiemo)
 		eprova, messajo, enfobula = conversion(quantiemo)
		if not eprova then return eprova, messajo, '' end
 		numero = quantiemo
		fin = '</span></abbr>'
	end
	if enfobula == ''	then
		enfobula = 'encognu'
	end
	quantiemo_fat_vere = '<abbr class="abbr" title="'..enfobula..'" ><span class="romen" style="text-transform:uppercase">'..numero..fin
	return eprova, messajo, quantiemo_fat_vere
end

function p.tipo_complement(chena)
	if chena then
		if string.len(chena) > 0 then
-- 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 not (string.sub(chena, 1, 1) == ',') then
				chena = ' '..chena
			end
-- S'il y a des guillemets, on compense le bug qui empêche la gestion correcte des insécables dans un lien wiki
			if mw.ustring.match(chena, '« '..tiponom..' »') then
 				chena = mw.ustring.gsub(chena, '« ', '«&#160;')
 				chena = mw.ustring.gsub(chena, ' »', '&#160;»')
			end
		end
	end
	return true, '', chena
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 p.ecritura_avouec_lim(lim, nom_fat_vere, quantiemo_fat_vere, complement_fat_vere)
	local eprova = true
	local texto1 = ''

	local eprova, messajo, complement_fat_vere2 = p.tipo_complement(complement_fat_vere)

	texto1 = '[['..lim
	if nom_fat_vere then
		if string.len(nom_fat_vere) > 0 then
			if string.len(quantiemo_fat_vere) > 0 then
				-- S'il y a un nombre, ajout d'un "nowrap" pour l'espace insécable
				texto1 = texto1..'|<span class="nowrap">'..nom_fat_vere..' '..quantiemo_fat_vere..'</span>'
				if complement_fat_vere2
				then
					if string.len(complement_fat_vere2) > 0 then
						texto1 = texto1..complement_fat_vere2
					end
				end
			else
				texto1 = texto1..'|'..nom_fat_vere
				if string.len(complement_fat_vere2) > 0 then
					texto1 = texto1..complement_fat_vere2
				end
			end
		end
	else
		if complement_fat_vere2 then
			if string.len(complement_fat_vere2) > 0 then
				texto1 = texto1..'|'..trim(complement_fat_vere2)
			end
		end
	end
 	texto1 = texto1.."]]"
 	return eprova, messajo, texto1
end

function p.ecritura_sen_lim(lim, nom_fat_vere, quantiemo_fat_vere, complement_fat_vere)
	local eprova = true
	local messajo = ''
	local texto1 = ''

	local eprova, messajo, complement_fat_vere2 = p.tipo_complement(complement_fat_vere)

	if quantiemo_fat_vere and nom_fat_vere then
		if (string.len(nom_fat_vere) > 0) then
			if string.len(quantiemo_fat_vere) > 0 then
				-- S'il y a un nombre, ajout d'un "nowrap" pour l'espace insécable
				texto1 = texto1..'<span class="nowrap">'..nom_fat_vere..' '..quantiemo_fat_vere..'</span>'
				if complement_fat_vere2 then
					texto1 = texto1..complement_fat_vere2
				end
			else
				if complement_fat_vere2 then
					texto1 = texto1..nom_fat_vere..complement_fat_vere2
				end
			end
		else
			texto1 = texto1..quantiemo_fat_vere
			if complement_fat_vere2 then
				texto1 = texto1..complement_fat_vere2
			end
		end
	else
		if complement_fat_vere2 then
			if string.len(complement_fat_vere2) > 0 then
				texto1 = texto1..trim(complement_fat_vere2)
			end
		end
	end
	if texto1 == '' then
		texto1 = lim
	end
	return eprova, messajo, texto1
end

-- Affichage dans le cas général (émule le modèle Souverain2)
function p.Soveren2(frame)
	local args = Outils.extractArgs( frame )
	local arg1, arg2, arg3, arg4 = args[1] or '', args[2] or '', args[3] or '', args[4] or ''
	
	local eprova, messajo, t1, t2, t3, t4 = p.tretament(arg1, arg2, arg3, arg4)
	if not eprova then
		return messajo
	end
				
    local eprova, messajo, texto = p.ecritura_avouec_lim(t1, t2, t3, t4)
   	if not eprova then
 		return messajo
 	else
     	return texto
     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 p.Soveren3(frame)
	local args = Outils.extractArgs( frame )
	local a1, a2, a3 = args[1] or "", args[2] or "", args[3] or ""
	
    fare_vere_lo_complement = true
	local eprova, messajo, t1, t2, t3, t4 = p.tretament(a1, a2, a3)
   	if not eprova then
		return messajo
	end
	
    local eprova, messajo, texto = p.ecritura_avouec_lim(t1, t2, t3, t4)
   	if not eprova then
		return messajo
	else
    	return texto
    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 p.Soveren_sen_lim(frame)
	local args = Outils.extractArgs( frame )
	local a1, a2, a3, a4 = args[1] or "", args[2] or "", args[3] or "", args[4] or ""
	
    fare_vere_lo_complement = false
	local eprova, messajo, t1, t2, t3, t4 = p.tretament(a1, a2, a3, a4)
   	if not eprova then
		return messajo
	end
	
    local eprova, messajo, texto = p.ecritura_sen_lim(t1, t2, t3, t4)
   	if not eprova then
		return messajo
	else
    	return texto
    end
end

return p