« Module:Biblio/Lien web » : différence entre les versions
Contenu supprimé Contenu ajouté
ainsi, dans les cas "sans url" on ne se retrouve pas avec une espace en trop (HTML fusionne les espaces multiples en une seule espace, mais autant faire propre) |
amélioration lisibilité code |
||
Ligne 83 : | Ligne 83 : | ||
-- on teste d'abord si titre contient déjà des guillemets |
-- on teste d'abord si titre contient déjà des guillemets |
||
if titre:match( '^«.+»$' ) then |
if titre:match( '^«.+»$' ) then |
||
wiki.minsert( titre |
wiki.minsert( titre ) |
||
else |
else |
||
local sousTitre = validArg( 'sous-titre' ) |
local sousTitre = validArg( 'sous-titre' ) |
||
Ligne 90 : | Ligne 90 : | ||
end |
end |
||
local color = dead and not archiveUrl and ' color:red;' |
local color = dead and not archiveUrl and ' color:red;' |
||
wiki.minsert( '« <cite style="font-style: normal;', color, '">', titre, sousTitre, '</cite> » |
wiki.minsert( '« <cite style="font-style: normal;', color, '">', titre, sousTitre, '</cite> »' ) |
||
end |
|||
if url then |
|||
wiki.minsert( ']' ) |
|||
end |
end |
||
if codeLangue and url then |
if codeLangue and url then |
||
Ligne 106 : | Ligne 109 : | ||
elseif description then |
elseif description then |
||
if dead and not archiveUrl then |
if dead and not archiveUrl then |
||
wiki.minsert( '<span style="color:red;">', description, '</span> |
wiki.minsert( '<span style="color:red;">', description, '</span>' ) |
||
else |
else |
||
wiki.minsert( description |
wiki.minsert( description ) |
||
end |
|||
if url then |
|||
wiki.minsert( ']' ) |
|||
end |
end |
||
else |
else |
Version du 11 juin 2020 à 04:20
[voir] [modifier] [historique] [purger]
Utilisation
Fonctions exportables :
lienWeb(args)
– affiche un lien vers un site web ; voir {{Lien web}} ; il faut fournir une table de paramètres simplelienBrise(args)
– affiche un lien brisé, auquel sont ajoutés des liens vers des sites d'archivage ; voir {{Lien brisé}} ; il faut fournir une table de paramètres simple
Modules externes et autres éléments dont ce module a besoin pour fonctionner :
Module:Biblio/Commun
Module:Biblio/Références
Module:Langue
– indirectement ( chargé par Module:Biblio/Commun ) si le paramètre langue est renseigné
La documentation de ce module est générée par le modèle {{Documentation module}}.
Elle est incluse depuis sa sous-page de documentation. Veuillez placer les catégories sur cette page-là.
Les éditeurs peuvent travailler dans le bac à sable (modifier).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local Lien = { }
local Commun = require( 'Module:Biblio/Commun' )
local References = require( 'Module:Biblio/Références' )
local TableBuilder = require( 'Module:TableBuilder' )
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|{{Lien web}}]]'
if dead then
modele = '[[Modèle:Lien brisé|{{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 = {}
if validArg( 'dead-url', 'deadurl', 'brisé le', 'lien brisé' ) then
dead = true
end
-- 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 indicLangue and 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 ) )
end
local coauteur = validArg( 'co-auteur', 'coauteurs', 'coauteur', 'coauthors' )
if coauteur then
wiki.minsert( ', ', coauteur )
end
wiki.minsert( Commun.responsabiliteSecondaire( args, validArg ), ', ' )
end
local url = validArg( 'url', 'lire en ligne', 'url texte', 'lien' )
local archiveUrl = validArg( 'archive-url', 'archiveurl' )
local titre = validArg( 'titre', 'title' )
local description = validArg( 'description' )
-- url
if url then
-- remplacement des crochets et espaces par leur équivalent HTML
url = url:gsub( '%[', '%%5B' ):gsub( '%]', '%%5D' ):gsub( ' ', '%%20' )
-- ajout http:// si nécessaire :
if not ( url:match( '^https?://' ) or url:match( '^ftp://' ) or url:match( '^//' ) ) then
url = 'http://' .. url
end
if dead or archiveUrl then
wiki.minsert( '<span class="noarchive">' )
end
if codeLangue and titre then
wiki.minsert( '<span lang="' .. codeLangue .. '">' )
end
wiki.minsert( '[', url, ' ' )
elseif not validArg( 'pmid', 'pmcid', 'doi', 'jstor', 'bibcod', 'math reviews', 'zbl', 'zbmath', 'arxiv' ) then
args.categ.url = true
end
-- titre
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 ( '%]', ']' )
-- on teste d'abord si titre contient déjà des guillemets
if titre:match( '^«.+»$' ) then
wiki.minsert( titre )
else
local sousTitre = validArg( 'sous-titre' )
if sousTitre then
sousTitre = ' : ' .. sousTitre
end
local color = dead and not archiveUrl and ' color:red;'
wiki.minsert( '« <cite style="font-style: normal;', color, '">', titre, sousTitre, '</cite> »' )
end
if url then
wiki.minsert( ']' )
end
if codeLangue and url then
wiki.minsert( '</span>' )
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 and not archiveUrl then
wiki.minsert( '<span style="color:red;">', description, '</span>' )
else
wiki.minsert( description )
end
if url then
wiki.minsert( ']' )
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, '« <code>url</code> »' )
end
if args.categ.titre then
table.insert( absent, '« <code>titre</code> »' )
end
wiki.insert( 1, '<span class="error">Modèle '
.. modele .. ' : paramètre' .. ( #absent > 1 and 's ' or ' ' )
.. mw.text.listToText( absent ) .. ' manquant' .. ( #absent > 1 and 's' or '' )
.. '. </span>'
)
end
-- liens archives pour les liens brisés
if url and dead and not archiveUrl 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>'
)
elseif archiveUrl then
if not ( archiveUrl:match( '^http' ) or archiveUrl:match( '^//' ) ) then
archiveUrl = 'http://' .. archiveUrl
end
local textArchive = ' archive'
local dateArchive = validArg( 'archive-date', 'archivedate' )
if dateArchive then
dateArchive = Commun.inscriptionDate{ date = dateArchive }
textArchive = ' archive du '
end
wiki.minsert( ' <small class="plainlinks cachelinks">[[', archiveUrl, textArchive, dateArchive, ']]</small>' )
end
if url and ( dead or archiveUrl ) then
wiki.minsert( '</span>' )
end
-- format
wiki.minsert( References.indicationDeFormat( validArg( 'format électronique', 'format' ) ) )
-- Nature du document
local nature = validArg( 'nature document' )
if nature then
wiki.minsert( ' (', nature, ')' )
end
-- série, site, éditeur
local serie = validArg( 'série', 'work' )
if serie then
wiki.minsert( ', <span class="italique">', serie, '</span>' )
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 <span class="italique">', site, '</span>' )
end
local periodique = validArg( 'périodique' )
if periodique then
wiki.minsert( ', <i>', periodique, '</i>' )
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( ', [[', lienEditeur, '|', editeur, ']]' )
else
wiki.minsert( ', ', editeur )
end
end
-- date
if validArg( 'année', 'date', 'year', 'en ligne le', 'en ligne' ) then
local Langue = require( 'Module:Langue' )
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
-- 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
local Langue = require( 'Module:Langue' )
if codeLangue then
citation = Langue.lang{ codeLangue, citation }
end
wiki.minsert( '\194\160: <span class="citation">« ', citation, ' »</span>' )
end
local passage = validArg( 'page', 'pages', 'passage' )
if passage then
if passage:match( '^[%dIVXLCM]' ) then
wiki.minsert( ', ', Commun.page, passage )
else
wiki.minsert( ', ', passage )
end
end
-- Plume "Ouvrage utilisé pour la rédaction de l'article"
if validArg( 'plume' ) then
local patternPonct = '[,.;:!?] ?»?$'
local ponctuation = wiki.concat():gsub( '%b<>', '' ):match( patternPonct )
if not ponctuation then
wiki.minsert( '.' )
end
wiki.minsert( Commun.plume )
end
if mw.title.getCurrentTitle().namespace == 0 then
wiki.minsert(
args.categ.langue and '[[Catégorie:Page du modèle Lien web comportant une erreur|langue]]',
args.categ.url and '[[Catégorie:Page du modèle Lien web comportant une erreur|Url]]',
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
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