X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=js%2Fformater.js;h=a4fbc2366d4e3217c466c79d6ca5a89c485cdcbd;hb=HEAD;hp=349abe759871cdf8e8d96620dea61de88c743678;hpb=eb44db02bab9c8b082201aa153dea8229432e894;p=euphorik.git diff --git a/js/formater.js b/js/formater.js deleted file mode 100644 index 349abe7..0000000 --- a/js/formater.js +++ /dev/null @@ -1,176 +0,0 @@ -// coding: utf-8 -// Copyright 2008 Grégory Burri -// -// This file is part of Euphorik. -// -// Euphorik is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Euphorik is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Euphorik. If not, see . - - -/** - * Objet permettant de formater du texte par exemple pour la substitution des liens dans les - * message par "[url]". - * TODO : améliorer l'efficacité des méthods notamment lié au smiles. - */ -euphorik.Formater = function() { - this.smiles = euphorik.conf.smiles; - this.protocoles = "http|https|ed2k"; - - this.regexUrl = new RegExp("(?:(?:" + this.protocoles + ")://|www\\.)[^ ]*", "gi"); - this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i"); - this.regexDomaine = new RegExp("^(?:(?:" + this.protocoles + ")://)(.*?)(?:$|/).*$", "i"); - this.regexTestProtocoleExiste = new RegExp("^(?:" + this.protocoles + ")://.*$", "i"); - this.regexNomProtocole = new RegExp("^(.*?)://"); -}; - -/** - * Formate un nick saise par l'utilisateur. - * @param nick le nick brut - * @return le nick filtré - */ -euphorik.Formater.prototype.filtrerInputPseudo = function(nick) { - return nick.replace(/\{|\}/g, "").trim(); -}; - -euphorik.Formater.prototype.getSmilesHTML = function() { - var XHTML = ""; - objectEach(this.smiles, function(nom) { - XHTML += "\"""; - }); - return XHTML; -}; - -/** - * Formatage complet d'un texte. - * @m le message - * @nick facultatif, permet de contruire le label des images sous la forme : " : " - */ -euphorik.Formater.prototype.traitementComplet = function(m, nick) { - return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), nick))); -}; - -/** - * Transforme les liens en entités clickables. - * Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message : - * "{5F}" ou 5F est la racine de la conversation. - * Ce lien sera transformer en {5F} pouvant être clické pour créer la conv 5F. - */ -euphorik.Formater.prototype.traiterLiensConv = function(m) { - return m.replace( - /\{\w+\}/g, - function(lien) { - return "" + lien + ""; - } - ); -}; - -/** - * FIXME : Cette méthode est attrocement lourde ! A optimiser. - * moyenne sur échantillon : 234ms - */ -euphorik.Formater.prototype.traiterSmiles = function(m) { - objectEach(this.smiles, function(nom, smiles) { - for (var i = 0; i < smiles.length; i++) { - m = m.replace(smiles[i], "\"""); - } - }); - return m; -}; - -euphorik.Formater.prototype.remplacerBalisesHTML = function(m) { - return m.replace(//g, ">").replace(/"/g, """); -}; - -euphorik.Formater.prototype.traiterURL = function(m, nick) { - var thisFormater = this; - var traitementUrl = function(url) { - // si ya pas de protocole on rajoute "http://" - if (!thisFormater.regexTestProtocoleExiste.test(url)) { - url = "http://" + url; - } - var extension = thisFormater.getShort(url); - return "[" + extension[0] + "]"; - }; - return m.replace(this.regexUrl, traitementUrl); -}; - -/** - * Formatage en utilisant un sous-ensemble des règles de Textile : http://en.wikipedia.org/wiki/Textile_(markup_language). - * par exemple _italic_ devient italic. - */ -euphorik.Formater.prototype.traiterWikiSyntaxe = function(m) { - return m.replace( - /(?:^| )_(.*?)_(?:$| )/g, - function(texte, capture) { - return '' + capture + ''; - } - ).replace( - /(?:^| )\*(.*?)\*(?:$| )/g, - function(texte, capture) { - return '' + capture + ''; - } - ); -}; - -/** - * Renvoie une version courte de l'url. - * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient en.wikipedia.org - */ -euphorik.Formater.prototype.getShort = function(url) { - var estUneImage = false; - var versionShort = null; - var rechercheImg = this.regexImg.exec(url); - - if (rechercheImg) { - versionShort = rechercheImg[1].toLowerCase(); - if (versionShort === "jpeg") { - versionShort = "jpg"; // jpeg -> jpg - } - estUneImage = true; - } else { - var rechercheDomaine = this.regexDomaine.exec(url); - if (rechercheDomaine && rechercheDomaine.length >= 2) { - versionShort = rechercheDomaine[1]; - } else { - var nomProtocole = this.regexNomProtocole.exec(url); - if (nomProtocole && nomProtocole.length >= 2) { - versionShort = nomProtocole[1]; - } - } - } - - return [versionShort ? versionShort : "url", estUneImage]; - }; - -euphorik.Formater.prototype.supprimerSmiles = function(m) { - objectEach(this.smiles, function(nom, smiles) { - for (var i = 0; i < smiles.length; i++) { - m = m.replace(smiles[i], ""); - } - }); - return m; -}; - -/** - * Traite les nick et messages à être affiché dans le titre d'une image visualisé avec lightbox. - * Supprime les smiles pour pas qu'ils puissent être remplacés par la fonction 'traiterSmiles'. - * TODO : trouver un moyen pour que les smiles puissent être conservés - */ -euphorik.Formater.prototype.traiterPourFenetreLightBox = function(M, urlCourante) { - var thisFormater = this; - var traitementUrl = function(url) { - return "[" + thisFormater.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]"; - }; - - return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl); -};