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