پودمان:نامک/پیوند تارنما

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
local Lien = { }

local Commun = require( 'پودمان:نامک/انبازین' )
local References = require( 'پودمان:نامک/بازخنان' )

-- Si un concat est effectué pendant les traitements,
-- permet de réutiliser ensuite son résultat, au lieu de refaire un concat complet à la fin des traitements.
local function intermediateConcat( array )
	local state = table.concat( array )

	array[ 1 ] = state
	for i = 2, #array do
		array[ i ] = nil
	end

	return state
end

local function formatLien( args, dead )
	-- La table wiki va recevoir tous les petits morceaux de texte, et sera concaténée à la fin.
	local wiki = {}

	-- permet d'écrire "insert( value )" au lieu de "wiki[#wiki+1] = value"
	local function insert( value )
		wiki[#wiki+1] = value
	end

	local modele = '[[الگو:پیوند تارنما|{{پیوند ترانما}}]]'
	if dead then
		modele = '[[الگو:پیوند شکسته|{{پیوند شکسته}}]]'
	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 function validArg( ... )
		return Commun.validTextArg( args, ... )
	end

	-- Variables servant à la catégorisation
	args.categ = {}
	if validArg( 'پیوند مرده', 'deadurl', 'پیوند شکسته' ) then
		dead = true
	end

	-- Span initial, id
	local spanInitial, spanFinal = Commun.spanInitial( args, validArg )
	insert( spanInitial )

	-- libellé
	insert( Commun.libelle( args ) or nil )

	-- indication de langue
	local indicLangue, codeLangue = Commun.indicationDeLangue( args, validArg )
	if indicLangue and indicLangue ~= '' then
		insert( indicLangue .. ' ' )
	end

	-- Liste des auteurs et de leurs responsabilités (principales et secondaires)
	local auteur = validArg( 'نام خانوادگی1', 'نام خانوادگی', 'نویسنده1', 'نویسنده', 'نویسندگان', 'نویسنده حقوقی' )
	if auteur then
		if validArg( 'نویسندگان' ) then
			insert( args.auteurs )
		else
			insert( Commun.responsabilitePrincipale( args, validArg ) or nil )
		end
		local coauteur = validArg( 'همنویسنده', 'همنویسندگان' )
		if coauteur then
			insert( ', ' .. coauteur )
		end
		insert( Commun.responsabiliteSecondaire( args, validArg ) or nil )
		insert( ', ' )
	end

	local url = validArg( 'نشانی', 'خواندن برخط', 'متن نشانی', 'پیوند' )
	local archiveUrl = validArg( 'نشانی بایگانی' )
	local titre = validArg( 'عنوان' )
	local description = validArg( 'توضیحات' )
	-- 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
			insert( '<span class="noarchive">' )
		end
		if codeLangue and titre then
			insert( '<span lang="' .. codeLangue .. '">' )
		end
		insert( '[' .. 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( '%]', '&#93;' )

		-- on teste d'abord si titre contient déjà des guillemets
		if titre:match( '^«.+»$' ) then
			insert( titre )
		else
			local sousTitre = validArg( 'زیرعنوان' )
			-- par précaution, ne pas modifier la valeur de la variable locale "titre"
			if sousTitre then
				sousTitre = ' : ' .. sousTitre
			else
				sousTitre = ''
			end
			local color = ''
			if dead and not archiveUrl then
				color = ' color:red;'
			end
			insert( '« <cite style="font-style:normal;' .. color .. '">' .. titre .. sousTitre .. '</cite> »' )
		end
		if url then
			insert( ']' )
		end
		if codeLangue and url then
			insert( '</span>' )
		end

		-- traduction titre
		local traductionTitre = validArg( 'ترجمه عنوان' )
		if traductionTitre and traductionTitre ~= args.titre then
			insert( ' [« ' .. traductionTitre .. ' »]' )
		end
		if description then
			insert( ', ' .. description )
		end
	elseif description then
		if dead and not archiveUrl then
			insert( '<span style="color:red;">' .. description .. '</span>' )
		else
			insert( description )
		end
		if url then
			insert( ']' )
		end
	else
		args.categ.titre = true
		if url then
			insert( 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>نشانی</code> »' )
		end
		if args.categ.titre then
			table.insert( absent, '« <code>عنوان</code> »' )
		end
		table.insert( wiki, 1, '<span class="error">الگو '
			.. modele .. ' : پارامتر' .. ( #absent > 1 and 's ' or ' ' )
			.. mw.text.listToText( absent ) .. ' یافت نشد' .. ( #absent > 1 and 's' or '' )
			.. '.&ensp;</span>'
		)
	end

	-- liens archives pour les liens brisés
	if url and dead and not archiveUrl then
		insert( '<sup class="plainlinks">('
			.. '[https://web.archive.org/web/*/' .. url .. ' Archive.org] • '
			.. '[https://archive.wikiwix.com/cache/?url=' .. url .. ' Wikiwix] • '
			.. '[https://archive.is/' .. url .. ' Archive.is] • '
			.. '[https://webcache.googleusercontent.com/search?hl=fr&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 = 'بایگانی'
		local dateArchive = validArg( 'تاریخ بایگانی' )
		if dateArchive then
			dateArchive = Commun.inscriptionDate{ date = dateArchive }
			if dateArchive and dateArchive ~= '' then
				textArchive = textArchive .. ' از ' .. dateArchive
			end
		end
		insert( ' <small class="plainlinks cachelinks">&#91;[' .. archiveUrl .. ' ' .. textArchive .. ']&#93;</small>' )
	end
	if url and ( dead or archiveUrl ) then
		insert( '</span>' )
	end

	-- affichage de l'icône du niveau d'accès à l'information
	local paywall = validArg( 'دسترسی نشانی' )
	if paywall then
		local suffixeIcone = References.indicationIconeAcces( paywall )
		if suffixeIcone and suffixeIcone ~= '' then
			insert( suffixeIcone )
		else
			table.insert( wiki, 1, '<span class="error">الگو '
				.. modele .. ' : پارامتر « <code>دسترسی نشانی</code> » با مقداردهی <code>'
				.. paywall .. '</code> ناشناخته.&ensp;</span>'
			)
		end
	end

	-- format
	local indicationDeFormat = References.indicationDeFormat( validArg( 'فرمت الکترونیک', 'فرمت' ) )
	if indicationDeFormat and indicationDeFormat ~= '' then
		insert( indicationDeFormat )
	end

	-- Nature du document
	local nature = validArg( 'نوع پرونده' )
	if nature then
		insert( ' (' .. nature .. ')' )
	end

	-- série, site, éditeur
	local serie = validArg( 'série', 'کار' )
	if serie then
		insert( ', <span class="italique">' .. serie .. '</span>' )
		args.categ.work = validArg( 'کار' )	-- il y a ambiguïté sur l'utilisation de ce paramètre
	end
	local site = validArg( 'سایت', 'وبگاه' )
	if site then
		insert( ', در <span class="italique">' .. site .. '</span>' )
	end
	local periodique = validArg( 'گاهنامه' )
	if periodique then
		insert( ', <i>' .. periodique .. '</i>' )
	end
	local lieu = validArg( 'مکان', 'مکان نشر' )
	if lieu then
		insert( ', ' .. lieu )
	end
	local editeur = validArg( 'ناشر' )
	if editeur then
		local lienEditeur = validArg( 'پیوند ناشر' )
		if lienEditeur then
			insert( ', [[' .. lienEditeur .. '|' .. editeur .. ']]' )
		else
			insert( ', ' .. editeur )
		end
	end

	-- date
	if validArg( 'سال', 'تاریخ', 'برخط در', 'برخط' ) then
		args.categ.enLigneLe = validArg( 'تاریخ' ) and validArg( 'برخط در' ) and args.date ~= args['برخط در']
		args.date = validArg( 'تاریخ', 'برخط در', 'برخط' )
		local dateFormatee = Commun.inscriptionDate( args )
		if dateFormatee and dateFormatee ~= '' then
			local Langue = require( 'پودمان:زبان' )
			local lrm = ''
			if Langue.nonLatin( intermediateConcat( wiki ) ) then
				lrm = '&lrm;'
			end
			insert( ',' .. lrm .. ' ' .. dateFormatee )
		end
	end

	-- liens externes (isbn, doi...) et "consulté le" entre parenthèses, et en plus petit, sauf en note
	local liensExternes = References.affichageLiensExternes( args, validArg, false, true )
	if liensExternes and liensExternes ~= '' then
		insert( liensExternes )
	end

	-- fin du span
	insert( spanFinal )

	-- citation et passage
	local citation = validArg( 'گفتاورد', 'گزیده', 'نقل قول' )
	if citation then
		if codeLangue then
			local Langue = require( 'پودمان:زبان' )
			citation = Langue.lang{ codeLangue, citation }
		end
		insert( '\194\160: <span class="citation">« ' .. citation .. ' »</span>' )
	end
	local passage = validArg( 'صفحه', 'صفحات', 'تکه' )
	if passage then
		insert( ', ' .. Commun.formatePassage( passage ) )
	end

	-- Plume "Ouvrage utilisé pour la rédaction de l'article"
	if validArg( 'خامه' ) then
		local patternPonct = '[,.;:!?] ?»?$'
		local ponctuation = intermediateConcat( wiki ):gsub( '%b<>', '' ):match( patternPonct )
		if not ponctuation then
			insert( '.' )
		end
		insert( Commun.plume )
	end

	if mw.title.getCurrentTitle().namespace == 0 then
		insert(
			( args.categ.langue and '[[رده:Page du modèle Lien web comportant une erreur|langue]]' or '' )
			.. ( args.categ.url and '[[رده:Page du modèle Lien web comportant une erreur|Url]]' or '' )
			.. ( args.categ.titre and '[[رده:Page du modèle Lien web comportant une erreur|titre]]' or '' )
			.. ( args.categ.enLigneLe and '[[رده:Page du modèle Lien web comportant une erreur|enLigneLe]]' or '' )
			.. ( args.categ.isbnInvalid and '[[رده:Page avec ISBN invalide]]' or '' )
			.. ( args.categ.issnInvalid and '[[رده:Page avec ISSN invalide]]' or '' )
		)
	end

	if dead and not validArg( 'nocat' ) then
		if mw.title.getCurrentTitle().namespace == 0 then
			insert( '[[رده:Article contenant un lien mort]]' )
		else
			insert( '[[رده:Page contenant un lien mort]]' )
		end
	end

	return table.concat( wiki )
end

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

function Lien.lienBrise( args )
	args.titre = Commun.validTextArg( args, 'عنوان', 'نشانی', 'خواندن برخط', 'متن نشانی', 'پیوند' )
	return formatLien( args, true )
end

return Lien