Module:Weblink

 Documentation

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 nilend--I validation de l'url (paramètre URL)url = mw.text.trim(url)-- II texte à afficher (paramètre "showntext")--- valeur spéciale : aucun texteif showntext == "-" thenreturn urlend--- laissé vide : texte basé sur l'URLif not showntext thenif mw.ustring.sub(url, 1, 1) == "[" thenreturn urlendlocal 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 urlendlocal space = mw.ustring.find(url, ' ') -- si le nom du site est passé par mégarde dans l'URL, le séparerif space thenlocal full = urlurl = mw.ustring.sub(full, 1, space - 1)showntext = mw.ustring.sub(full, space + 1)elseshowntext = string.gsub( url, 'https?://', '')---- remove trailing slashif string.sub(showntext, #showntext, #showntext) == '/' thenshowntext = string.sub(showntext, 1, #showntext - 1)endendendreturn '[' .. url .. ' ' .. showntext .. ']'endfunction p.makelinks(stringlist, separator)if not type(stringlist) == table thenreturn error('stringlist should be table')endif not separator thenseparator = '<br />'endfor i, j in pairs(stringlist) dostringlist[i] = p.makelink(j)endreturn table.concat(stringlist, separator)endreturn p