Différences entre les pages « Module:Nombre2texte » et « Module:Biblio »

De Les Mots de l'agronomie
(Différence entre les pages)
imported>Jacques Ducloy
m (1 révision importée)
 
imported>Jacques Ducloy
m (1 révision importée)
 
Ligne 1 : Ligne 1 :
local p = { }
+
-- Le module biblio centralise les différentes fonctions utiles pour les bibliographie et références.
  
--[[
+
local Biblio = {  }
ordinal renvoie une chaine correspondant à l'abréviation de l'adjectif ordinal du nombre.
+
 
Paramètres :
+
require( 'Module:No globals' )
1 = nombre (string ou number)
+
local Outils = require( 'Module:Outils' )
2 = true pour avoir première au lieu de premier su nombre = 1
+
 
--]]
+
 
function p.ordinal( nombre, feminin )
+
Biblio.ouvrage = function ( frame )
local num = tonumber( nombre )
+
local args = Outils.extractArgs( frame )
if num == nil then
+
local Ouvrage = require( 'Module:Biblio/Ouvrage' )
return mw.text.trim( tostring( nombre ) or '' )
+
return Ouvrage.ouvrage( args )
else
 
local nom = p.nombre2texte_reel( nombre, nil, 'ordinal', 'réformée', feminin and 'féminin' )
 
local exposant
 
if num == 1 then
 
exposant = (feminin and 're' or 'er')
 
else
 
exposant = 'e'
 
end
 
return '<abbr class="abbr" title="' .. nom .. '">' .. num .. '<sup>' .. exposant .. '</sup></abbr>'
 
end
 
 
end
 
end
  
--[[
 
  Fonction de traitement d'une "tranche" de nombres entre 0 et 999.
 
  Retourne la forme texturelle (5 → cinq, 46 → quarante six, 432 → quatre cent trente deux…)
 
  Les paramètres sont les chiffres, du plus grand au plus petit (centaine, dizaine, unité).
 
  La valeur nil signifie "0" (pour n'importe lequel des paramètres)
 
  La fonction retourne le texte ou 'nil' si la valeur est zéro (pour gérer les 0 millier…)
 
  Le paramètre 'langue' indique la variante de langue (fr, be, ch ou ch2).
 
  Data est la table des données (issue de loadData())
 
--]]
 
function p.traite_tranche(_c1, _c2, _c3, langue, Data)
 
    local c1, c2, c3
 
if (_c1 == nil) then c1 = 0 else c1 = tonumber(_c1) or 0 end
 
if (_c2 == nil) then c2 = 0 else c2 = tonumber(_c2) or 0 end
 
if (_c3 == nil) then c3 = 0 else c3 = tonumber(_c3) or 0 end
 
  
if (c1 == 0 and c2 == 0 and c3 == 0) then
+
Biblio.chapitre = function ( frame )
return nil -- sil signifie "zéro" (mais à traiter spécialement quand entouré)
+
local args = Outils.extractArgs( frame )
end
+
local Ouvrage = require( 'Module:Biblio/Ouvrage' )
local resu = ""
+
return Ouvrage.chapitre( args )  
 
-- on calcule la valeur restante (sans les centaines)
 
local val = 10*c2 + c3
 
-- présence d'une centaine ?
 
if (c1 ~= 0) then
 
if (c1 == 1) then
 
resu = "cent " -- séparateur
 
else
 
-- plusieurs centaines : on ajoute l'unité
 
resu = Data.infcent[c1] .. " cent"
 
-- si pas d'unité 100 prend un 's'
 
if (val == 0) then
 
resu = resu .. "s "
 
else
 
resu = resu .. " "
 
end
 
end
 
end
 
-- reste = 0 ?
 
if (val == 0) then
 
-- on retourne directement la centaine
 
return resu
 
end
 
-- c'est forcément un nombre pré-défini
 
local vvv
 
if (langue == "fr") then
 
vvv = Data.infcent[val]
 
elseif (langue == "be") then
 
vvv = Data.infcent_be[val] or Data.infcent[val]
 
elseif (langue == "ch") then
 
vvv = Data.infcent_ch[val] or Data.infcent_be[val] or Data.infcent[val]
 
else
 
vvv = Data.infcent_ch2[val] or Data.infcent_be[val] or Data.infcent[val]
 
end
 
return resu .. vvv .. " "
 
-- note : cette fonction retourne *toujours* un " " à la fin du terme
 
 
end
 
end
  
--[[
 
  Fonction principale
 
  Reçoit en paramètre (premier non nommé) le nombre à traiter.
 
  Retourne la forme textuelle de ce nombre.
 
--]]
 
function p.nombre2texte_reel(pnombre, plangue, ptype, porthographe, pgenre, pmajuscule, pordinal)
 
-- le nombre à convertir (vient toujours du modèle)
 
local valeur = pnombre
 
if (valeur == nil) then
 
return '<span class="error">Il faut un paramètre non nommé numérique.</span>'
 
elseif type(valeur) == "sting" then
 
 
-- s'il y a une virgule, on l'ignore
 
local bla = mw.ustring.find(valeur, "[.,]")
 
if (bla ~= nil) then
 
-- extraction de la partie avant la virgule
 
valeur = mw.ustring.match(mw.text.trim(valeur), "^[-]?[0-9]*")
 
end
 
elseif type(valeur) == "number" then
 
valeur = math.floor(valeur)
 
end
 
 
local nvaleur = tonumber(valeur)
 
if (type(nvaleur) ~= "number") then
 
return '<span class="error">Le paramètre doit être un nombre.</span>'
 
end
 
-- limites
 
if (nvaleur < -999999999999 or nvaleur > 999999999999) then
 
return '<span class="error">Nombre trop grand ou trop petit.</span>'
 
end
 
-- note : ici il faudrait s'assurer que le nombre est un entier !
 
  
-- on extrait le moins si présent
+
Biblio.article = function ( frame )
local signe = false
+
local args = Outils.extractArgs( frame )
if (nvaleur < 0) then
+
local Ouvrage = require( 'Module:Biblio/Article' )
nvaleur = -nvaleur
+
return Ouvrage.article( args )  
signe = true
+
end
end
 
  
-- option : choix de la langue
 
local langue = plangue
 
if (langue == nil) then
 
langue = "fr"
 
else
 
langue = mw.text.trim(langue)
 
end
 
-- validation des valeurs permises
 
if (langue ~= "fr" and langue ~= "be" and langue ~= "ch" and langue ~= "ch2") then
 
return '<span class="error">Paramètre langue non reconnu (fr, be, ch ou ch2).</span>'
 
end
 
  
-- type de résultat : seule valeur autorisée : 'ordinal'
+
Biblio.lienWeb = function( frame )
local style = ptype
+
local args = Outils.extractArgs( frame )
if (style ~= nil and style ~= "ordinal") then
+
local Ouvrage = require( 'Module:Biblio/Lien web' )
style = nil
+
return Ouvrage.lienWeb( args )  
end
+
end
  
-- type d'orthographe
+
Biblio.lienBrise = function( frame )
local ortho = porthographe
+
local args = Outils.extractArgs( frame )
if (ortho ~= nil and ortho ~= "réformée") then
+
local Ouvrage = require( 'Module:Biblio/Lien web' )
ortho = nil
+
return Ouvrage.lienBrise( args )
end
+
end
 
-- genre : uniquement pour l'ordinal "premier / première"
 
local genre = pgenre
 
if (genre ~= nil and genre ~= "féminin") then
 
genre = nil
 
end
 
 
-- majuscule : mettre une majuscule au premier mot
 
local maj = pmajuscule
 
if (maj ~= nil and maj ~= "oui") then
 
maj = nil
 
end
 
 
 
-- cas (très) simple : 0
 
if (nvaleur == 0) then
 
if (style == "ordinal") then
 
if (maj) then
 
return "Zéroième"
 
else
 
return "zéroième"
 
end
 
else
 
if (maj) then
 
return "Zéro"
 
else
 
return "zéro"
 
end
 
end
 
end
 
  
-- on charge les données
 
local Data = mw.loadData( 'Module:Nombre2texte/Data' )
 
  
-- on traite les autres cas simples : le nombre est pré-codé
+
Biblio.dictionnaire = function( frame )
local val
+
local args = Outils.extractArgs( frame )
if (langue == "fr") then
+
local article = Outils.validTextArg( args, 'notice', 'article', 'titre chapitre' )
val = Data.infcent[nvaleur]
+
local Ouvrage = require( 'Module:Biblio/Ouvrage' )
elseif (langue == "be") then
+
if article then
val = Data.infcent_be[nvaleur] or Data.infcent[nvaleur]
+
args['titre chapitre'] = article
elseif (langue == "ch") then
+
return Ouvrage.chapitre( args )
val = Data.infcent_ch[nvaleur] or Data.infcent_be[nvaleur] or Data.infcent[nvaleur]
 
 
else
 
else
val = Data.infcent_ch2[nvaleur] or Data.infcent_be[nvaleur] or Data.infcent[nvaleur]
+
args.titre = args['titre ouvrage']
 +
args['sous-titre'] = args['sous-titre ouvrage']
 +
args.auteur1 = args['auteur ouvrage']
 +
args.auteurs = args['auteurs ouvrage']
 +
return Ouvrage.ouvrage( args )
 
end
 
end
 +
end
  
local res = val or ""
+
-- catégorise une page en fonction du namespace
if (val == nil) then
+
local function categorise(args)
-- pas de résultat, on fait le "calcul"
+
local namespaceCategorisation = { [0] = true, [4] = true, [10] = true, [12] = true, [14] = true, [100] = true, [104] = true }
 
+
local category = args[1]
-- on l'éclate en une table des différents caractères
+
local flag = args[2]
local tvaleur = mw.text.split(valeur, "")
+
if type(category) == 'string'
local nb = #tvaleur -- nombre d'éléments
+
and flag
 +
and namespaceCategorisation[ mw.title.getCurrentTitle().namespace ]
 +
then
 +
return '[[Category:' .. category .. ']]'
 +
end
 +
return ''
 +
end
  
-- on boucle sur les triplets de chiffres et on stocke le résultat dans une table
+
Biblio.ISBN = function ( frame )
local tbl = {}
+
local args = Outils.extractArgs( frame )
while (true) do
+
if Outils.trim( args[1] ) then
-- on prend les 3 valeurs concernées
+
local newArgs = { isbn = args[1] }
local p1 = tvaleur[nb-2]
+
local i = 2
local p2 = tvaleur[nb-1]
+
while args[i] do
local p3 = tvaleur[nb]
+
newArgs[ 'isbn' .. i ] = args[i]
-- si les 3 sont 'nil' on a terminé
+
i = i + 1
if (p1 == nil and p2 == nil and p3 == nil) then
 
break
 
end
 
-- on calcule la valeur du bloc concerné (rangé dans la table)
 
local tmp = mw.text.trim(p.traite_tranche(p1, p2, p3, langue, Data) or "zéro")
 
table.insert(tbl, tmp)
 
-- décalage
 
nb = nb - 3
 
 
end
 
end
 +
return Biblio.identifiantsBiblio( newArgs )
 +
else
 +
return '<small>([[International Standard Book Number|ISBN]]&nbsp;inconnu)</small>' .. categorise{ 'ISBN nécessaire', true }
 +
end
 +
end
  
-- on construit le résultat final en combinant les éléments
+
Biblio.rechercheIsbn = function( frame )
-- et en ajoutant les milliers/millions/...
+
local args = Outils.extractArgs( frame )
local pos = 1
+
local References = require( 'Module:Biblio/Références' )
while (tbl[pos] ~= nil) do
+
local newArgs = { isbn = args[1], sansLabel = true, categ = {} }
local el = ""
+
local isbn = References.isbn( newArgs ) or ''
-- on l'ajoute, s'il existe
+
local categ = categorise{ 'Page avec ISBN invalide', newArgs.categ.isbnInvalid }
if (tbl[pos] ~= "zéro " and tbl[pos] ~= "zéro") then
+
return isbn .. categ
if (pos == 1) then
+
end
-- rang "1", on ajoute simplement la valeur
 
el = tbl[pos] .. " "
 
else
 
-- si la valeur est "un" on ajoute seulement le rang
 
if (tbl[pos] == "un " or tbl[pos] == "un") then
 
el = Data.sup[pos] .. " "
 
else
 
-- on ajoute X + rang
 
el = tbl[pos] .. " " .. Data.sup[pos]
 
-- le pluriel, sauf pour 1000, et le séparateur
 
if (pos ~= 2) then
 
el = el .. "s "
 
else
 
el = el .. " "
 
end
 
end
 
end
 
end
 
-- on insert
 
res = el .. res
 
 
 
-- on passe au suivant
 
pos = pos + 1
 
end
 
 
 
-- suppression espaces
 
res = mw.text.trim(res)
 
  
end -- fin (si on n'avait pas trouvé en pré-défini)
 
  
if (style ~= nil) then
+
Biblio.EAN = function ( frame )
-- ordinal : on cherche la fin du nombre pour ajouter le "ième" qui convient
+
local args = Outils.extractArgs( frame )
if (res == "zéro") then
+
if Outils.trim( args[1] ) then
res = "zéroième" -- eurk!
+
local newArgs = { ean = args[1], categ = {} }
elseif (res == "un") then
+
local i = 2
if (genre == nil) then
+
while args[i] do
res = "premier"
+
newArgs[ 'ean' .. i ] = args[i]
else
+
i = i + 1
res = "première"
 
end
 
else
 
-- on récupère le dernier mot
 
local fin = mw.ustring.match(res, "%a*$")
 
-- on récupère le reste (début)
 
local debut = mw.ustring.gsub(res, "%a*$", "")
 
 
-- on génère la fin en ordinal
 
local nfin = Data.iemes[fin]
 
if (nfin == nil) then
 
nfin = '<span class="error">erreur interne d\'ordinal.</span>'
 
end
 
res = debut .. nfin
 
 
end
 
end
 +
return Biblio.identifiantsBiblio( newArgs )
 +
else
 +
return '<small>([[EAN 13|EAN]]&nbsp;inconnu)</small>[[Catégorie:EAN nécessaire]]'
 
end
 
end
 +
end
 +
  
-- si orthographe réformée on remplace les espaces par des tirets
+
Biblio.ISSN = function ( frame )
if (ortho == "réformée") then
+
local args = Outils.extractArgs( frame )
res = mw.ustring.gsub(res, "[ ]", "-")
+
if Outils.trim( args[1] ) then
else
+
local newArgs = { issn = args[1], categ = {} }
-- sinon on remplace les espaces par des insécables
+
local i = 2
res = mw.ustring.gsub(res, "[ ]", "&#160;")
+
while args[i] do
end
+
newArgs[ 'issn' .. i ] = args[i]
if (style == nil) then
+
i = i + 1
-- traitement de signe éventuel (sauf ordinaux)
 
if (signe) then
 
res = "moins&#160;" .. res
 
 
end
 
end
 +
return Biblio.identifiantsBiblio( newArgs )
 +
else
 +
return '<small>([[International Standard Serial Number|ISSN]]&nbsp;inconnu)</small>' .. categorise{ 'ISSN nécessaire', true }
 
end
 
end
 +
end
  
-- si demandé on passe la première lettre en majuscule
+
Biblio.rechercheIssn = function( frame )
if (maj) then
+
local args = Outils.extractArgs( frame )
local langage = mw.getContentLanguage()
+
local References = require( 'Module:Biblio/Références' )
res = langage:ucfirst(res)
+
local newArgs = { issn = args[1], sansLabel = true, categ = {} }
end
+
local issn = References.issn( newArgs ) or ''
 +
local categ = categorise{ 'Page avec ISSN invalide', newArgs.categ.issnInvalid }
 +
return issn .. categ
 +
end
  
-- on retourne
+
Biblio.BNF = function ( frame )
return res
+
local References = require( 'Module:Biblio/Références' )
 +
return References.bnf( Outils.extractArgs( frame )[1] )
 
end
 
end
  
--[[
+
Biblio.enLigne = function ( frame )
  Fonction principale
+
local args = Outils.extractArgs( frame )
  Reçoit en paramètre (premier non nommé) le nombre à traiter.
+
local References = require( 'Module:Biblio/Références' )
  Retourne la forme textuelle de ce nombre.
+
return '<small>[' .. References.enLigne( args ) .. ']</small>'
--]]
+
end
function p.nombre2texte(frame)
 
local pframe = frame:getParent()
 
  
return p.nombre2texte_reel(
+
Biblio.identifiantsBiblio = function( frame )
pframe.args[1] or frame.args[1], -- pas obligé. Pour permettre des exemples, avec priorité au modèle
+
local args = Outils.extractArgs( frame )
frame.args["langue"] or pframe.args["langue"],
+
args.categ = {}
frame.args["type"] or pframe.args["type"],
+
local References = require( 'Module:Biblio/Références' )
frame.args["orthographe"] or pframe.args["orthographe"],
+
return References.affichageLiensExternes( args, nil, true, true )
frame.args["genre"] or pframe.args["genre"],
+
.. categorise{ 'Page avec ISBN invalide', args.categ.isbnInvalid }
frame.args["majuscule"] or pframe.args["majuscule"],
+
.. categorise{ 'Page avec ISSN invalide', args.categ.issnInvalid }
frame.args["ordinal"] or pframe.args["ordinal"]);
+
.. categorise{ 'Page avec EAN invalide', args.categ.eanInvalid }
 
end
 
end
  
return p
+
return Biblio

Version actuelle datée du 28 décembre 2020 à 11:52

Modèle:Protection

Ce module sert de base aux fonctions bibliographiques. Lorsqu'elles sont appelées directement par un modèle (via #invoke), les paramètres passés à ce modèle sont automatiquement transmis au module, sauf si le modèle spécifie une autre valeur pour ce paramètre.

Utilisation

Fonctions exportables :

  • ouvrage(frame) – modèle {{Ouvrage}} ;
  • chapitre(frame) – modèle {{Chapitre}} ;
  • article(frame) – modèle {{Article}} ;
  • lienWeb(frame) – modèle {{Lien web}} ;
  • dictionnaire(frame) – appelle le modèle chapitre si un paramètre titre chapitre, article ou notice est non vide, sinon appelle le modèle ouvrage ;
  • ISBN(frame) – affiche en petit et entre parenthèses ISBN suivi d'autant de codes que de paramètres non nommés, avec lien à la page ad hoc ;
  • ISSN(frame) – affiche en petit et entre parenthèses ISSN suivi d'autant de codes que de paramètres non nommés, avec lien à la page ad hoc ;
  • enLigne() – similaire au modèle {{Lire en ligne}} (le texte par défaut est « en ligne »).

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

Exemples

Utilisation de la fonction ouvrage dans un modèle pour afficher une référence :

  • {{#invoke:Biblio |ouvrage |auteur=Martine Michu |titre=Mes vacances à la mer}}
  • Martine Michu, Mes vacances à la mer

Une page utilisant ce modèle avec le paramètre « passage = 28 » affichera :

  • Martine Michu, Mes vacances à la mer, p. 28


Modèle:Projet Scribunto



-- Le module biblio centralise les différentes fonctions utiles pour les bibliographie et références.

local Biblio = {  }

require( 'Module:No globals' )
local Outils = require( 'Module:Outils' )


Biblio.ouvrage = function ( frame )
	local args = Outils.extractArgs( frame )
	local Ouvrage = require( 'Module:Biblio/Ouvrage' )
	return Ouvrage.ouvrage( args ) 
end


Biblio.chapitre = function ( frame )
	local args = Outils.extractArgs( frame )
	local Ouvrage = require( 'Module:Biblio/Ouvrage' )
	return Ouvrage.chapitre( args ) 
end


Biblio.article = function ( frame )
	local args = Outils.extractArgs( frame )
	local Ouvrage = require( 'Module:Biblio/Article' )
	return Ouvrage.article( args ) 
end


Biblio.lienWeb = function( frame )
	local args = Outils.extractArgs( frame )
	local Ouvrage = require( 'Module:Biblio/Lien web' )
	return Ouvrage.lienWeb( args ) 
end

Biblio.lienBrise = function( frame )
	local args = Outils.extractArgs( frame )
	local Ouvrage = require( 'Module:Biblio/Lien web' )
	return Ouvrage.lienBrise( args )
end


Biblio.dictionnaire = function( frame )
	local args = Outils.extractArgs( frame )
	local article = Outils.validTextArg( args, 'notice', 'article', 'titre chapitre' )
	local Ouvrage = require( 'Module:Biblio/Ouvrage' )
	if article then
		args['titre chapitre'] = article
		return Ouvrage.chapitre( args ) 
	else
		args.titre = args['titre ouvrage']
		args['sous-titre'] = args['sous-titre ouvrage']
		args.auteur1 = args['auteur ouvrage']
		args.auteurs = args['auteurs ouvrage']
		return Ouvrage.ouvrage( args )
	end
end

-- catégorise une page en fonction du namespace
local function categorise(args)
	local namespaceCategorisation = { [0] = true, [4] = true, [10] = true, [12] = true, [14] = true, [100] = true, [104] = true }
	local category = args[1]
	local flag = args[2]
	if type(category) == 'string'
		and flag 
		and namespaceCategorisation[ mw.title.getCurrentTitle().namespace ]
	then
		return '[[Category:' .. category .. ']]'
	end
	return ''
end

Biblio.ISBN = function ( frame )
	local args = Outils.extractArgs( frame )
	if Outils.trim( args[1] ) then
		local newArgs = { isbn = args[1] }
		local i = 2
		while args[i] do
			newArgs[ 'isbn' .. i ] = args[i]
			i = i + 1
		end
		return Biblio.identifiantsBiblio( newArgs )
	else 
		return '<small>([[International Standard Book Number|ISBN]]&nbsp;inconnu)</small>' .. categorise{ 'ISBN nécessaire', true }
	end
end

Biblio.rechercheIsbn = function( frame )
	local args = Outils.extractArgs( frame )
	local References = require( 'Module:Biblio/Références' )
	local newArgs = { isbn = args[1], sansLabel = true, categ = {} }
	local isbn = References.isbn( newArgs ) or ''
	local categ = categorise{ 'Page avec ISBN invalide', newArgs.categ.isbnInvalid }
	return isbn .. categ
end


Biblio.EAN = function ( frame )
	local args = Outils.extractArgs( frame )
	if Outils.trim( args[1] ) then
		local newArgs = { ean = args[1], categ = {} }
		local i = 2
		while args[i] do
			newArgs[ 'ean' .. i ] = args[i]
			i = i + 1
		end
		return Biblio.identifiantsBiblio( newArgs )
	else 
		return '<small>([[EAN 13|EAN]]&nbsp;inconnu)</small>[[Catégorie:EAN nécessaire]]'
	end
end


Biblio.ISSN = function ( frame )
	local args = Outils.extractArgs( frame )
	if Outils.trim( args[1] ) then
		local newArgs = { issn = args[1], categ = {} }
		local i = 2
		while args[i] do
			newArgs[ 'issn' .. i ] = args[i]
			i = i + 1
		end
		return Biblio.identifiantsBiblio( newArgs )
	else 
		return '<small>([[International Standard Serial Number|ISSN]]&nbsp;inconnu)</small>' .. categorise{ 'ISSN nécessaire', true }
	end
end

Biblio.rechercheIssn = function( frame )
	local args = Outils.extractArgs( frame )
	local References = require( 'Module:Biblio/Références' )
	local newArgs = { issn = args[1], sansLabel = true, categ = {} }
	local issn = References.issn( newArgs ) or ''
	local categ = categorise{ 'Page avec ISSN invalide', newArgs.categ.issnInvalid }
	return issn .. categ
end

Biblio.BNF = function ( frame )
	local References = require( 'Module:Biblio/Références' )
	return References.bnf( Outils.extractArgs( frame )[1] )
end

Biblio.enLigne = function ( frame )
	local args = Outils.extractArgs( frame )
	local References = require( 'Module:Biblio/Références' )
	return '<small>[' .. References.enLigne( args )  .. ']</small>'
end

Biblio.identifiantsBiblio = function( frame )
	local args = Outils.extractArgs( frame )
	args.categ = {}
	local References = require( 'Module:Biblio/Références' )
	return References.affichageLiensExternes( args, nil, true, true ) 
		.. categorise{ 'Page avec ISBN invalide', args.categ.isbnInvalid }
		.. categorise{ 'Page avec ISSN invalide', args.categ.issnInvalid }
		.. categorise{ 'Page avec EAN invalide', args.categ.eanInvalid }
end

return Biblio