« Module:Weblink » : différence entre les versions

Une page de Wikipédia, l'encyclopédie libre.
Contenu supprimé Contenu ajouté
Zolo (discuter | contributions)
Aucun résumé des modifications
Metamorforme42 (discuter | contributions)
correctif pour affichage correct avec les modèles de langue
 
(23 versions intermédiaires par 4 utilisateurs non affichées)
Ligne 2 : Ligne 2 :


function p.makelink(url, showntext)
function p.makelink(url, showntext)
if not url then
if (not url) or (url == "-") then
return nil
return nil
end
end

--I validation de l'url (paramètre URL)
url = mw.text.trim(url)


-- II texte à afficher (paramètre "showntext")

--- valeur spéciale : aucun texte
if showntext == "-" then
return url
end

--- laissé vide : texte basé sur l'URL
if not showntext then
if not showntext then
if mw.ustring.sub(url, 1, 1) == "[" then
showntext = string.gsub( url, '(http://|https://)', '')
return url
end
local sub12 = mw.ustring.sub(url, 1, 12)
if sub12 == '<span class=' or sub12 == '<cite class=' or sub12 == '<abbr class=' then
-- résultat produit par [[modèle:URL]], [[modèle:Lien web]], [[modèle:Site officiel]], ou [[modèle:en]]
return url
end
local space = mw.ustring.find(url, ' ') -- si le nom du site est passé par mégarde dans l'URL, le séparer
if space then
local full = url
url = mw.ustring.sub(full, 1, space - 1)
showntext = mw.ustring.sub(full, space + 1)
else
showntext = string.gsub( url, 'https?://', '')
---- remove trailing slash
if string.sub(showntext, #showntext, #showntext) == '/' then
showntext = string.sub(showntext, 1, #showntext - 1)
end
end
end
end

return '[' .. url .. ' ' .. showntext .. ']'
return '[' .. url .. ' ' .. showntext .. ']'
end
end


Ligne 19 : Ligne 52 :
end
end
for i, j in pairs(stringlist) do
for i, j in pairs(stringlist) do
stringlist[i] = p.makelink(k)
stringlist[i] = p.makelink(j)
return table.concat(stringlist, separator )
end
end
return table.concat(stringlist, separator)
end
end



Dernière version du 22 novembre 2022 à 16:57

 Documentation[voir] [modifier] [historique] [purger]

Transforme des textes en lien externe

Utilisation

Fonctions :

  • makelink(lien, texte) – crée un lien vers lien et affiche le texte texte, ou à défaut, affiche l'URL du lien moins le "http://" initial. Ne retourne rien si lien est nul ou égal à "-".
  • makelinks(liste, séparateur) – crée des liens vers toutes les URL indiquées dans la table liste et les sépare par le paramètre séparateur.

Exemples

Pour des exemples, voir la page de test permettant de tester diverses modifications apportées.

local p = {}

function p.makelink(url, showntext)
	if (not url) or (url == "-") then 
		return nil
	end

	--I validation de l'url (paramètre URL)
	url = mw.text.trim(url)


	-- II texte à afficher (paramètre "showntext")

	--- valeur spéciale : aucun texte
	if showntext == "-" then
		return url
	end

	--- laissé vide : texte basé sur l'URL
	if not showntext then
		if mw.ustring.sub(url, 1, 1) == "[" then
			return url
		end
		local sub12 = mw.ustring.sub(url, 1, 12)
		if sub12 == '<span class=' or sub12 == '<cite class=' or sub12 == '<abbr class=' then
			-- résultat produit par [[modèle:URL]], [[modèle:Lien web]], [[modèle:Site officiel]], ou [[modèle:en]]
			return url
		end
		local space = mw.ustring.find(url, ' ') -- si le nom du site est passé par mégarde dans l'URL, le séparer
		if space then
			local full = url
			url = mw.ustring.sub(full, 1, space - 1)
			showntext = mw.ustring.sub(full, space + 1)
		else
			showntext = string.gsub( url, 'https?://', '')
			---- remove trailing slash
			if string.sub(showntext, #showntext, #showntext) == '/' then
				showntext = string.sub(showntext, 1, #showntext - 1)
			end
		end
	end

	return '[' .. url .. ' ' .. showntext .. ']'
end

function p.makelinks(stringlist, separator)
	if not type(stringlist) == table then
		return error('stringlist should be table')
	end
	if not separator then
		separator = '<br />'
	end
	for i, j in pairs(stringlist) do
		stringlist[i] = p.makelink(j)
	end
	return table.concat(stringlist, separator)
end

return p