Module:Biblio/Lien web
Apparence
La documentation pour ce module peut être créée à Module:Biblio/Lien web/doc
local Lien = { }
local Commun = require( 'Module:Biblio/Commun' )
local References = require( 'Module:Biblio/Références' )
local Outils = require( 'Module:Outils' )
local TableBuilder = require( 'Module:TableBuilder' )
function Lien.lienWeb( args )
-- La table wiki va recevoir tout les petits bouts de texte, et sera concaténée à la fin.
-- Elle utilise la meta-table de TableBuilder, permettant d'utiliser les fonctions de la
-- library table comme des méthodes. minsert permet d'insérer plusieurs éléments en une
-- seule fois en, ignorant les paramètre nil.
local wiki = TableBuilder.new()
-- validArg renvoie le premier paramètre non vide, ou nil s'ils sont tous vides.
-- Seuls les noms des paramètres doivent être transmis à la fonction.
local validArg = function ( ... ) return Outils.validTextArg( args, ... ) end
-- Variables servant à la catégorisation
local categUrl, categTitre, categLangue, categWork
-- Span initial, id
wiki.minsert( Commun.spanInitial ( args, validArg ) )
local libelle = validArg( 'libellé' )
if libelle then
wiki.minsert( '<small>[', libelle, ']</small> ' )
end
-- indication de langue
local indicLangue, codeLangue, categLangue = Commun.indicationDeLangue ( args, validArg )
if Outils.notEmpty( indicLangue ) then
wiki.minsert( indicLangue, ' ' )
end
-- auteur et coauteurs
local auteur = validArg( 'auteur', 'author', 'nom', 'nom1', 'last', 'auteurs', 'Auteur' )
if auteur then
local lienAuteur = validArg( 'lien auteur' )
local prenom = validArg( 'prénom', 'prénom1', 'first' )
prenom = prenom and ( prenom .. ' ' )
if lienAuteur and string.sub( auteur, 1, 2 ) ~= '[[' then
wiki.minsert( '[[', lienAuteur, '|', prenom, auteur, ']], ' )
else
wiki.minsert( prenom, auteur, ', ' )
end
end
local coauteur = validArg( 'coauteurs', 'coauteur', 'coauthors' )
if coauteur then
wiki.minsert( coauteur, ', ' )
end
-- url
local url = validArg( 'url', 'lire en ligne', 'url texte' )
if url then
-- remplacement des crochets par leur équivalent HTML
url = string.gsub ( string.gsub ( url, '%[', '%%5B' ), '%]', '%%5D' )
-- ajout http:// si nécessaire :
if not ( string.match( url, '^http' ) or string.match( url, '^//' ) ) then
url = 'http://' .. url
end
wiki.minsert( '[', url )
else
categUrl = true
wiki.minsert( Outils.erreur( 'modèle [[Modèle:Lien web|{{Lien web}}]] : paramètre « <code>url</code> » manquant' ) )
end
-- titre
local titre = validArg( 'titre', 'title' )
if titre then
-- suppression des retours ligne éventuels, sinon MediaWiki ne reconnaît pas le lien externe
titre = mw.ustring.gsub( titre, '\n', ' ' )
titre = titre:match'^%s*(.*%S)' or '';
local c = titre:sub(1,1)
-- on teste d'abord si titre contient déjà des guillemets
if(string.byte(c) ~= 194) then
wiki.minsert( ' « <cite style="font-style: normal">', titre, '</cite> »]' )
else
wiki.minsert( ' ', titre, ']' )
end
else
categTitre = true
if categUrl then
wiki.minsert( Outils.erreur( ' et paramètre « <code>titre</code> » manquant' ) )
else
wiki.minsert( ' ', args.url, '] ', Outils.erreur( 'modèle [[Modèle:Lien web|{{Lien web}}]] : paramètre « <code>titre</code> » manquant' ) )
end
end
-- format
wiki.minsert( References.indicationDeFormat( args.format ) )
-- série, site, éditeur
local serie = validArg( 'série', 'work' )
if serie then
wiki.minsert( [[, '']], serie, [['']] )
categWork = validArg( 'work' ) -- il y a ambiguïté sur l'utilisation de ce paramètre
end
local site = validArg( 'site', 'website' )
if site then
wiki.minsert( [[, sur '']], site, [['']] )
end
local periodique = validArg( 'périodique' )
if periodique then
wiki.minsert( ', ', periodique )
end
local editeur = validArg( 'éditeur', 'publisher', 'editeur' )
if editeur then
wiki.minsert( ', ', editeur )
end
-- date
if validArg( 'année', 'date', 'year', 'en ligne le', 'en ligne' ) then
args.date = validArg( 'date', 'en ligne le', 'en ligne' )
wiki.minsert( ',‎ ', Commun.inscriptionDate( args ) )
end
-- la partie suivante se trouve entre parenthèses, et en plus petit, sauf en note
if validArg( 'consulté le', 'page', 'accessdate', 'Consulté le', 'consulté', 'isbn', 'ISBN', 'issn', 'consultée le' ) then
local precisions = TableBuilder.new( )
-- isbn et issn
precisions.minsert( References.isbn( args, validArg ), References.issn( args, validArg ) )
-- consulté le
local consult = validArg( 'consulté le', 'accessdate', 'Consulté le', 'consulté', 'consultée le' )
local consuteLe = ''
if consult then
if string.sub( consult, -1,-1) == '.' then
consult = string.sub( consult, 1, -2)
end
if (string.match(consult, '^.* .* .*')) then
consulteLe = 'consulté le '
else
consulteLe = 'consulté en '
end
precisions.minsert( consulteLe .. Outils.nobr( consult ) )
end
wiki.minsert( ' <small style="line-height:1em;">', mw.text.nowiki( '(' ), precisions.concat( ', ' ), ')</small>' )
end
-- fin du span
wiki.minsert( '</span>' )
-- citation
local citation = validArg( 'citation', 'extrait', 'quote' )
if citation then
wiki.minsert( ' : <span class="citation">« ', citation, ' »</span>' )
end
local page = validArg( 'page', 'pages', 'passage' )
if page then
wiki.minsert( ', ', Commun.page, page )
end
if mw.title.getCurrentTitle().namespace == 0 then
wiki.minsert(
categLangue and '[[Catégorie:Page du modèle Lien web comportant une erreur|langue]]',
categUrl and '[[Catégorie:Page du modèle Lien web comportant une erreur|Url]]',
categTitre and '[[Catégorie:Page du modèle Lien web comportant une erreur|titre]]'
)
end
return wiki.concat()
end
return Lien