« Module:Biblio/Lien web » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
m A modifié le niveau de protection de « Module:Biblio/Lien web » : Oups, la semi-protection devrait être suffisante ([Modifier=Autoriser uniquement les utilisateurs auto-confirmés] (infini) [Renommer=Autoriser uniquement les utilisateurs auto-con
0x010D (discussion | contributions)
catégorisation de "en ligne le" : uniquement si les paramètres sont non vide
 
(34 versions intermédiaires par 2 utilisateurs non affichées)
Ligne 5 : Ligne 5 :
local Outils = require( 'Module:Outils' )
local Outils = require( 'Module:Outils' )
local TableBuilder = require( 'Module:TableBuilder' )
local TableBuilder = require( 'Module:TableBuilder' )
local Langue = require( 'Module:Langue' )




function Lien.lienWeb( args )
local function formatLien( args, dead )
-- La table wiki va recevoir tout les petits bouts de texte, et sera concaténtée à la fin.
-- La table wiki va recevoir tout les petits bouts de texte, et sera concaténée à la fin.
-- Elle utilise la metat-table de TableBuilder, permettant d'utiliser les fonctions de la
-- Elle utilise la meta-table de TableBuilder, permettant d'utiliser les fonctions de la
-- librairy table comme des mèthodes. minsert permet d'insérer plusieurs élément en une
-- library table comme des méthodes. minsert permet d'insérer plusieurs éléments en une
-- seule fois en, ignorant les paramètre nil.
-- seule fois en, ignorant les valeurs nil.
local wiki = TableBuilder.new()
local wiki = TableBuilder.new()
local modele = '[[Modèle:Lien web|{{Lien web}}]]'
if dead then
modele = '[[Modèle:Lien brisé|{{Lien brisé}}]]'
end
-- validArg renvoi le premier paramètre non vide, ou nil s'ils sont tous vides.
-- validArg renvoie le premier paramètre non vide, ou nil s'ils sont tous vides.
-- Seul les noms des paramètres doivent être transmis à la fonction.
-- Seuls les noms des paramètres doivent être transmis à la fonction.
local validArg = function ( ... ) return Outils.validTextArg( args, ... ) end
local validArg = function ( ... ) return Commun.validTextArg( args, ... ) end
--Variables servant à la catégorisation
-- Variables servant à la catégorisation
local categUrl, categTitre, categLangue, categWork
args.categ = {}
-- Span initial, id
-- Span initial, id
wiki.minsert( Commun.spanInitial ( args, validArg ) )
local spanInitial, spanFinal = Commun.spanInitial ( args, validArg )
wiki.minsert( spanInitial,  Commun.libelle( args ) )
-- indication de langue
-- indication de langue
local indicLangue, codeLangue, categLangue = Commun.indicationDeLangue ( args, validArg )
local indicLangue, codeLangue = Commun.indicationDeLangue ( args, validArg )
if Outils.notEmpty( indicLangue ) then
if Outils.notEmpty( indicLangue ) then
wiki.minsert( indicLangue, ' ' )
wiki.minsert( indicLangue, ' ' )
end
end
-- auteur et coauteurs
-- Liste des auteurs et de leurs responsabilités (principales et secondaires)
local auteur = validArg( 'auteur', 'author', 'nom', 'nom1', 'last', 'auteurs', 'Auteur' )
local auteur = validArg( 'nom1', 'nom', 'auteur1', 'auteur', 'auteurs', 'auteur institutionnel', 'last1', 'last', 'author1', 'author' )
if auteur then
if auteur then
local lienAuteur = validArg( 'lien auteur' )
if validArg( 'auteurs' ) then
local prenom = validArg( 'prénom', 'prénom1', 'first' )
wiki.minsert( args.auteurs )
prenom = prenom and ( prenom .. ' ' )
if lienAuteur and string.sub( auteur, 1, 2 ) ~= '[[' then
wiki.minsert( '[[', lienAuteur, '|', prenom, auteur, ']], ' )
else
else
wiki.minsert( prenom, auteur, ', ' )
wiki.minsert( Commun.responsabilitePrincipale( args, validArg, true ) )
end
local coauteur = validArg( 'co-auteur', 'coauteurs', 'coauteur', 'coauthors' )
if coauteur then
wiki.minsert( ', ', coauteur )
end
end
end
wiki.minsert( Commun.responsabiliteSecondaire( args, validArg ), ', ' )
local coauteur = validArg( 'coauteurs', 'coauteur', 'coauthors' )
if coauteur then
wiki.minsert( coauteur, ', ' )
end
end
-- url
-- url
local url = validArg( 'url', 'lire en ligne', 'url texte' )
local url = validArg( 'url', 'lire en ligne', 'url texte', 'lien' )
if url then
if url then
wiki.minsert( '[', string.gsub ( string.gsub ( url, '%[', '%%5B' ), '%]', '%%5D' ), nil )
-- remplacement des crochets et espaces par leur équivalent HTML
-- le ", nil" permet de ne prendre en compte que le premier paramètre retourné par string.gsub
url = string.gsub( string.gsub( string.gsub( url, ' ', '%%20' ), '%[', '%%5B' ), '%]', '%%5D' )
else
-- ajout http:// si nécessaire :
categUrl = true
if not ( string.match( url, '^http' ) or string.match( url, '^ftp' ) or string.match( url, '^//' ) ) then
wiki.minsert( Outils.erreur( 'modèle [[Modèle:Lien web|{{Lien web}}]] : paramètre « <code>url</code> » manquant' ) )
url = 'http://' .. url
end
if dead then
wiki.minsert( '<span class="noarchive">' )
end
wiki.minsert( '[', url )
elseif not validArg( 'pmid', 'pmcid', 'doi', 'jstor', 'bibcod', 'math reviews', 'zbl', 'zbmath', 'arxiv' ) then
args.categ.url = true
end
end
-- titre
-- titre
local titre = validArg( 'titre', 'title' )
local titre = validArg( 'titre', 'title' )
local description = validArg( 'description' )
if titre then
if titre then
if auteur then
-- suppression des retours ligne éventuels, sinon MediaWiki ne reconnaît pas le lien externe
wiki.minsert( ' « <cite style="font-style: normal">', titre, '</cite> »]' )
-- et remplacement des crochets par leur équivalent HTML (uniquement le crochet de fermeture)
titre = titre:gsub( '\n', ' ' ):gsub ( '%]', '&#93;' )
local  c = titre:sub(1,1)
-- on teste d'abord si titre contient déjà des guillemets
if(string.byte(c) ~= 194) then
local sousTitre
if validArg( 'sous-titre' ) then
sousTitre = ' : ' .. args['sous-titre']
sousTitre:gsub( ' ?»$', '' )
else
titre:gsub( ' ?»$', '' )
end
local color = dead and ' color:red;'
wiki.minsert( ' « <cite style="font-style: normal;', color, '">', titre, sousTitre, '</cite> »', url and ']' )
else
wiki.minsert( ' ', titre, url and ']' )
end
-- traduction titre
local traductionTitre = validArg( 'traduction titre' )
if traductionTitre and traductionTitre ~= args.titre then
wiki.minsert( ' [« ', traductionTitre, ' »]' )
end
if description then
wiki.minsert( ', ', description )
end
elseif description then
if dead then
wiki.minsert( ' <span style="color:red;">', description, '</span>', url and ']' )
else
else
wiki.minsert( ' ', titre, ']' )
wiki.minsert( ' ', description, url and ']' )
end
end
else
else
categTitre = true
args.categ.titre = true
if categUrl then
if url then  
wiki.minsert( Outils.erreur( ' et paramètre « <code>titre</code> » manquant' ) )
wiki.minsert( ' ', url, ']' )
else
wiki.minsert( ' ', args.url, '] ', Outils.erreur( 'modèle [[Modèle:Lien web|{{Lien web}}]] : paramètre « <code>titre</code> » manquant' ) )
end
end
end
end
-- message d'erreur
if args.categ.url or args.categ.titre then
local absent = { }
if args.categ.url  then
table.insert( absent, '« <code>url</code> »' )
end
if args.categ.titre then
table.insert( absent, '« <code>titre</code> »' )
end
wiki.insert( 1, Outils.erreur( 'Modèle '
.. modele ..' : paramètre' .. ( #absent > 1 and 's ' or ' ')
.. mw.text.listToText( absent ) .. ' manquant.&ensp;'
) )
end
-- liens archives pour les liens brisés
if url and dead then
wiki.minsert( '<sup class="plainlinks">(',
'[http://web.archive.org/web/*/', url, ' Archive] • ',
'[http://archive.wikiwix.com/cache/?url=', url, ' Wikiwix] • ',
'[http://archive.is/', url, ' Archive.is] • ',
'[https://www.google.fr/search?q=cache:', url, ' Google] • ',
'[[Projet:Correction des liens externes#J\'ai trouvé un lien mort, que faire ?|Que faire ?]])',
'</sup></span>'
)
end
-- format
-- format
wiki.minsert( References.indicationDeFormat( args.format ) )
wiki.minsert( References.indicationDeFormat( validArg( 'format électronique', 'format' ) ) )
-- série, site, éditeur
-- série, site, éditeur
Ligne 81 : Ligne 147 :
if serie then
if serie then
wiki.minsert( [[, '']], serie, [['']] )
wiki.minsert( [[, '']], serie, [['']] )
categWork = validArg( 'work' ) -- il y a ambiguité sur l'utilisation de ce paramètre
args.categ.work = validArg( 'work' ) -- il y a ambiguïté sur l'utilisation de ce paramètre
end
end
local site = validArg( 'site', 'périodique', 'website' )
local site = validArg( 'site', 'website' )
if site then
if site then
wiki.minsert( [[, sur '']], site, [['']] )
wiki.minsert( [[, sur '']], site, [['']] )
end
end
local periodique = validArg( 'périodique' )
if periodique then
wiki.minsert( ', ', periodique )
end
local lieu = validArg( 'lieu', 'lieu édition', 'location' )
if lieu then
wiki.minsert( ', ', lieu )
end
local editeur = validArg( 'éditeur', 'publisher', 'editeur' )
local editeur = validArg( 'éditeur', 'publisher', 'editeur' )
if editeur then
if editeur then
wiki.minsert( ', ', editeur )
local lienEditeur = validArg( 'lien éditeur' )
if lienEditeur then
wiki.minsert( ', [[', lienEditeur, '|', editeur, ']]' )
else
wiki.minsert( ', ', editeur )
end
end
end
 
-- date
-- date
if validArg( 'année', 'date', 'year', 'en ligne le', 'en ligne' ) then
if validArg( 'année', 'date', 'year', 'en ligne le', 'en ligne' ) then
args.categ.enLigneLe = validArg( 'date' ) and validArg( 'en ligne le' ) and args.date ~= args['en ligne le']
args.date = validArg( 'date', 'en ligne le', 'en ligne' )
args.date = validArg( 'date', 'en ligne le', 'en ligne' )
wiki.minsert( ', ', Commun.inscriptionDate( args ) )
local lrm
if Langue.nonLatin( wiki.concat() ) then
lrm = '&lrm; '
end
wiki.minsert( ',', lrm, ' ', Commun.inscriptionDate( args ) )
end
end
if validArg( 'brisé le' ) then
local dateBrise = Commun.inscriptionDate{ date = args['brisé le'] }
-- la partie suivante se trouve entre parenthèse, et plus petit (sauf en note)
local texteBrise = 'brisé en '
if validArg( 'consulté le', 'page', 'accessdate', 'Consulté le', 'consulté', 'isbn', 'ISBN', 'issn', 'consultée le' ) then
if dateBrise and dateBrise:match( '>%d%d?%D' ) then
local precisions = TableBuilder.new(  )
texteBrise = 'brisé le '
-- 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' )
if consult then
if string.sub( consult, -1,-1) == '.' then
consult = string.sub( consult, 1, -2)
end
precisions.minsert( 'consulté le ' .. Outils.nobr( consult ) )
end
end
wiki.minsert( ', ', texteBrise, dateBrise )
wiki.minsert( ' <small style="line-height:1em;">', mw.text.nowiki( '(' ), precisions.concat( ', ' ), ')</small>' )
end
end
-- liens externe (isbn, doi...) et "consulté le" entre parenthèses, et en plus petit, sauf en note
wiki.minsert( References.affichageLiensExternes( args, validArg, false, true ) )
-- fin du span
-- fin du span
wiki.minsert( '</span>' )
wiki.minsert( spanFinal )
-- citation
-- citation et passage
local citation = validArg( 'citation', 'extrait', 'quote' )
local citation = validArg( 'citation', 'extrait', 'quote' )
if citation then
if citation then
wiki.minsert( '. <span class="citation">« ', citation, ' »</span>' )
wiki.minsert( ' : <span class="citation">« ', citation, ' »</span>' )
end
end
local page = validArg( 'page', 'pages', 'passage' )
local passage = validArg( 'page', 'pages', 'passage' )
if page then
if passage then
wiki.minsert( ', ', Commun.page, page )
if string.match( passage, '^[%dIVXLCM]' ) then
wiki.minsert( ', ', Commun.page, passage )
else
wiki.minsert( ', ', passage )
end
end
end
 
-- Ponctuation
local patternPonct = '[,.;:!?] ?»?$'
local ponctuation = mw.ustring.match( wiki.concat():gsub( '%b<>', '' ), patternPonct )
if not ponctuation then
local ponctuationFinale = validArg( 'ponctuation finale' )
if ponctuationFinale == ';' then
ponctuationFinale = ' ;'
end
wiki.minsert( ponctuationFinale )
end
if mw.title.getCurrentTitle().namespace == 0 then
if mw.title.getCurrentTitle().namespace == 0 then
wiki.minsert(
wiki.minsert(
categLangue and '[[Catégorie:Page du modèle Lien web comportant une erreur|langue]]',
args.categ.langue 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]]',
args.categ.url 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]]'
args.categ.titre and '[[Catégorie:Page du modèle Lien web comportant une erreur|titre]]',
args.categ.enLigneLe and '[[Catégorie:Page du modèle Lien web comportant une erreur|enLigneLe]]',
args.categ.isbnInvalid and '[[Catégorie:Page avec ISBN invalide]]',
args.categ.issnInvalid and '[[Catégorie:Page avec ISSN invalide]]',
dead and not validArg( 'nocat' ) and '[[Catégorie:Article contenant un lien mort]]'
)
)
elseif dead and not validArg( 'nocat' ) then
wiki.minsert( '[[Catégorie:Page contenant un lien mort]]' )
end
end
Ligne 142 : Ligne 238 :
end
end


function Lien.lienWeb( args )
return formatLien( args, false )
end
function Lien.lienBrise( args )
args.titre = Commun.validTextArg( args, 'titre', 'title', 'url', 'lire en ligne', 'url texte', 'lien' )
return formatLien( args, true )
end


return Lien
return Lien

Dernière version du 18 mai 2017 à 23:24

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' ) local Langue = require( 'Module:Langue' )


local function formatLien( args, dead ) -- 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 valeurs nil. local wiki = TableBuilder.new() local modele = '[[Modèle:Lien web|Modèle {{Lien web}} : paramètres « url » et « titre » manquant. ]]' if dead then modele = '[[Modèle:Lien brisé|Modèle:Lien brisé]]' end

-- 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 Commun.validTextArg( args, ... ) end

-- Variables servant à la catégorisation args.categ = {}

-- Span initial, id local spanInitial, spanFinal = Commun.spanInitial ( args, validArg ) wiki.minsert( spanInitial, Commun.libelle( args ) )

-- indication de langue local indicLangue, codeLangue = Commun.indicationDeLangue ( args, validArg ) if Outils.notEmpty( indicLangue ) then wiki.minsert( indicLangue, ' ' ) end

-- Liste des auteurs et de leurs responsabilités (principales et secondaires) local auteur = validArg( 'nom1', 'nom', 'auteur1', 'auteur', 'auteurs', 'auteur institutionnel', 'last1', 'last', 'author1', 'author' ) if auteur then if validArg( 'auteurs' ) then wiki.minsert( args.auteurs ) else wiki.minsert( Commun.responsabilitePrincipale( args, validArg, true ) ) end local coauteur = validArg( 'co-auteur', 'coauteurs', 'coauteur', 'coauthors' ) if coauteur then wiki.minsert( ', ', coauteur ) end wiki.minsert( Commun.responsabiliteSecondaire( args, validArg ), ', ' ) end

-- url local url = validArg( 'url', 'lire en ligne', 'url texte', 'lien' ) if url then -- remplacement des crochets et espaces par leur équivalent HTML url = string.gsub( string.gsub( string.gsub( url, ' ', '%%20' ), '%[', '%%5B' ), '%]', '%%5D' ) -- ajout http:// si nécessaire : if not ( string.match( url, '^http' ) or string.match( url, '^ftp' ) or string.match( url, '^//' ) ) then url = 'http://' .. url end if dead then wiki.minsert( '' ) end wiki.minsert( '[', url ) elseif not validArg( 'pmid', 'pmcid', 'doi', 'jstor', 'bibcod', 'math reviews', 'zbl', 'zbmath', 'arxiv' ) then args.categ.url = true end

-- titre local titre = validArg( 'titre', 'title' ) local description = validArg( 'description' ) if titre then -- suppression des retours ligne éventuels, sinon MediaWiki ne reconnaît pas le lien externe -- et remplacement des crochets par leur équivalent HTML (uniquement le crochet de fermeture) titre = titre:gsub( '\n', ' ' ):gsub ( '%]', ']' ) local c = titre:sub(1,1) -- on teste d'abord si titre contient déjà des guillemets if(string.byte(c) ~= 194) then local sousTitre if validArg( 'sous-titre' ) then sousTitre = ' : ' .. args['sous-titre'] sousTitre:gsub( ' ?»$', ) else titre:gsub( ' ?»$', ) end local color = dead and ' color:red;' wiki.minsert( ' « ', titre, sousTitre, ' »', url and ']' ) else wiki.minsert( ' ', titre, url and ']' ) end

-- traduction titre local traductionTitre = validArg( 'traduction titre' ) if traductionTitre and traductionTitre ~= args.titre then wiki.minsert( ' [« ', traductionTitre, ' »]' ) end if description then wiki.minsert( ', ', description ) end elseif description then if dead then wiki.minsert( ' ', description, '', url and ']' ) else wiki.minsert( ' ', description, url and ']' ) end else args.categ.titre = true if url then wiki.minsert( ' ', url, ']' ) end end

-- message d'erreur if args.categ.url or args.categ.titre then local absent = { } if args.categ.url then table.insert( absent, '« url »' ) end if args.categ.titre then table.insert( absent, '« titre »' ) end wiki.insert( 1, Outils.erreur( 'Modèle ' .. modele ..' : paramètre' .. ( #absent > 1 and 's ' or ' ') .. mw.text.listToText( absent ) .. ' manquant. ' ) ) end

-- liens archives pour les liens brisés if url and dead then wiki.minsert( '(', 'url, ' Archive • ', 'url, ' Wikiwix • ', 'url, ' Archive.is • ', 'url, ' Google • ', 'Que faire ?)', '' ) end


-- format wiki.minsert( References.indicationDeFormat( validArg( 'format électronique', 'format' ) ) )

-- série, site, éditeur local serie = validArg( 'série', 'work' ) if serie then wiki.minsert( , '', serie, '' ) args.categ.work = 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 lieu = validArg( 'lieu', 'lieu édition', 'location' ) if lieu then wiki.minsert( ', ', lieu ) end local editeur = validArg( 'éditeur', 'publisher', 'editeur' ) if editeur then local lienEditeur = validArg( 'lien éditeur' ) if lienEditeur then wiki.minsert( ', ', editeur, '' ) else wiki.minsert( ', ', editeur ) end end

-- date if validArg( 'année', 'date', 'year', 'en ligne le', 'en ligne' ) then args.categ.enLigneLe = validArg( 'date' ) and validArg( 'en ligne le' ) and args.date ~= args['en ligne le'] args.date = validArg( 'date', 'en ligne le', 'en ligne' ) local lrm if Langue.nonLatin( wiki.concat() ) then lrm = '‎ ' end wiki.minsert( ',', lrm, ' ', Commun.inscriptionDate( args ) ) end if validArg( 'brisé le' ) then local dateBrise = Commun.inscriptionDate{ date = args['brisé le'] } local texteBrise = 'brisé en ' if dateBrise and dateBrise:match( '>%d%d?%D' ) then texteBrise = 'brisé le ' end wiki.minsert( ', ', texteBrise, dateBrise ) end

-- liens externe (isbn, doi...) et "consulté le" entre parenthèses, et en plus petit, sauf en note wiki.minsert( References.affichageLiensExternes( args, validArg, false, true ) )

-- fin du span wiki.minsert( spanFinal )

-- citation et passage local citation = validArg( 'citation', 'extrait', 'quote' ) if citation then wiki.minsert( ' : « ', citation, ' »' ) end local passage = validArg( 'page', 'pages', 'passage' ) if passage then if string.match( passage, '^[%dIVXLCM]' ) then wiki.minsert( ', ', Commun.page, passage ) else wiki.minsert( ', ', passage ) end end

-- Ponctuation local patternPonct = '[,.;:!?] ?»?$' local ponctuation = mw.ustring.match( wiki.concat():gsub( '%b<>', ), patternPonct ) if not ponctuation then local ponctuationFinale = validArg( 'ponctuation finale' ) if ponctuationFinale == ';' then ponctuationFinale = ' ;' end wiki.minsert( ponctuationFinale ) end

if mw.title.getCurrentTitle().namespace == 0 then wiki.minsert( args.categ.langue and , args.categ.url and , args.categ.titre and , args.categ.enLigneLe and , args.categ.isbnInvalid and , args.categ.issnInvalid and , dead and not validArg( 'nocat' ) and ) elseif dead and not validArg( 'nocat' ) then wiki.minsert( ) end

return wiki.concat() end

function Lien.lienWeb( args ) return formatLien( args, false ) end

function Lien.lienBrise( args ) args.titre = Commun.validTextArg( args, 'titre', 'title', 'url', 'lire en ligne', 'url texte', 'lien' ) return formatLien( args, true ) end

return Lien