X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;ds=sidebyside;f=js%2Fformateur.js;fp=js%2Fformateur.js;h=34b108bbcaa699b211cd0d9c2f217875cc447f87;hb=7fb422d6d4a7a59c8f74d938371a4a10474e8ea4;hp=0000000000000000000000000000000000000000;hpb=1ab57f78d9f702dc09b1af80f36546c79edfccca;p=euphorik.git
diff --git a/js/formateur.js b/js/formateur.js
new file mode 100644
index 0000000..34b108b
--- /dev/null
+++ b/js/formateur.js
@@ -0,0 +1,167 @@
+// 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 .
+
+/*jslint laxbreak:true */
+
+
+/**
+ * Classe 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.Formateur = 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 + ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i");
+ this.regexTestProtocoleExiste = new RegExp("^(?:" + this.protocoles + ")://.*$", "i");
+ this.regexNomProtocole = new RegExp("^(.*?)://");
+};
+
+/**
+ * Formate un pseudo saise par l'utilisateur.
+ * @param pseudo le pseudo brut
+ * @return le pseudo filtré
+ */
+euphorik.Formateur.prototype.filtrerInputPseudo = function(pseudo) {
+ return pseudo.replace(/\{|\}/g, "").trim();
+};
+
+euphorik.Formateur.prototype.getSmilesHTML = function() {
+ var XHTML = "";
+ this.smiles.each(function(nom) {
+ XHTML += "";
+ });
+ return XHTML;
+};
+
+/**
+ * Formatage complet d'un texte.
+ * @m le message
+ * @pseudo facultatif, permet de contruire le label des images sous la forme : " : "
+ */
+euphorik.Formateur.prototype.traitementComplet = function(m, pseudo) {
+ return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), pseudo)));
+};
+
+/**
+ * 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.Formateur.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.Formateur.prototype.traiterSmiles = function(m) {
+ this.smiles.each(function(nom, smiles) {
+ for (var i = 0; i < smiles.length; i++) {
+ m = m.replace(smiles[i], "");
+ }
+ });
+ return m;
+};
+
+euphorik.Formateur.prototype.remplacerBalisesHTML = function(m) {
+ return m.replace(//g, ">").replace(/"/g, """);
+};
+
+euphorik.Formateur.prototype.traiterURL = function(m, pseudo) {
+ var thisFormateur = this;
+ var traitementUrl = function(url) {
+ // si ya pas de protocole on rajoute "http://"
+ if (!thisFormateur.regexTestProtocoleExiste.test(url)) {
+ url = "http://" + url;
+ }
+ var extension = thisFormateur.getShort(url);
+ return "[" + extension[0] + "]";
+ };
+ return m.replace(this.regexUrl, traitementUrl);
+};
+
+/**
+ * Formatage en utilisant un sous-ensemble des règles de mediwiki.
+ * par exemple ''italic'' devient italic
+ */
+euphorik.Formateur.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 wikipedia.org
+ */
+euphorik.Formateur.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];
+ };
+
+/**
+ * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
+ */
+euphorik.Formateur.prototype.traiterPourFenetreLightBox = function(M, urlCourante) {
+ var thisFormateur = this;
+ var traitementUrl = function(url) {
+ return "[" + thisFormateur.getShort(url)[0] + (urlCourante == url ? "*" : "") + "]";
+ };
+
+ return this.remplacerBalisesHTML(M).replace(this.regexUrl, traitementUrl);
+};