MOD avancement dans le processus de mise en production (pas fini)
[euphorik.git] / js / formateur.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
18
19
20 /**
21 * Classe permettant de formater du texte par exemple pour la substitution des liens dans les
22 * message par "[url]".
23 * TODO : améliorer l'efficacité des méthods notamment lié au smiles.
24 */
25 euphorik.Formateur = function() {
26 this.smiles = euphorik.conf.smiles;
27 this.protocoles = "http|https|ed2k";
28
29 this.regexUrl = new RegExp("(?:(?:" + this.protocoles + ")://|www\\.)[^ ]*", "gi");
30 this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i");
31 this.regexDomaine = new RegExp("^(?:(?:" + this.protocoles + ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i");
32 this.regexTestProtocoleExiste = new RegExp("^(?:" + this.protocoles + ")://.*$", "i");
33 this.regexNomProtocole = new RegExp("^(.*?)://");
34 };
35
36 /**
37 * Formate un pseudo saise par l'utilisateur.
38 * @param pseudo le pseudo brut
39 * @return le pseudo filtré
40 */
41 euphorik.Formateur.prototype.filtrerInputPseudo = function(pseudo) {
42 return pseudo.replace(/\{|\}/g, "").trim();
43 };
44
45 euphorik.Formateur.prototype.getSmilesHTML = function() {
46 var XHTML = "";
47 objectEach(this.smiles, function(nom) {
48 XHTML += "<img class=\"" + nom + "\" src=\"img/smileys/" + nom + ".gif\" alt =\"" + nom + "\" />";
49 });
50 return XHTML;
51 };
52
53 /**
54 * Formatage complet d'un texte.
55 * @m le message
56 * @pseudo facultatif, permet de contruire le label des images sous la forme : "<Pseudo> : <Message>"
57 */
58 euphorik.Formateur.prototype.traitementComplet = function(m, pseudo) {
59 return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), pseudo)));
60 };
61
62 /**
63 * Transforme les liens en entités clickables.
64 * Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message :
65 * "{5F}" ou 5F est la racine de la conversation.
66 * Ce lien sera transformer en <span class="lienConv">{5F}</span> pouvant être clické pour créer la conv 5F.
67 */
68 euphorik.Formateur.prototype.traiterLiensConv = function(m) {
69 return m.replace(
70 /\{\w+\}/g,
71 function(lien) {
72 return "<span class=\"lienConv\">" + lien + "</span>";
73 }
74 );
75 };
76
77 /**
78 * FIXME : Cette méthode est attrocement lourde ! A optimiser.
79 * moyenne sur échantillon : 234ms
80 */
81 euphorik.Formateur.prototype.traiterSmiles = function(m) {
82 objectEach(this.smiles, function(nom, smiles) {
83 for (var i = 0; i < smiles.length; i++) {
84 m = m.replace(smiles[i], "<img src=\"img/smileys/" + nom + ".gif\" alt =\"" + nom + "\" />");
85 }
86 });
87 return m;
88 };
89
90 euphorik.Formateur.prototype.remplacerBalisesHTML = function(m) {
91 return m.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
92 };
93
94 euphorik.Formateur.prototype.traiterURL = function(m, pseudo) {
95 var thisFormateur = this;
96 var traitementUrl = function(url) {
97 // si ya pas de protocole on rajoute "http://"
98 if (!thisFormateur.regexTestProtocoleExiste.test(url)) {
99 url = "http://" + url;
100 }
101 var extension = thisFormateur.getShort(url);
102 return "<a " + (extension[1] ? "title=\"" + (pseudo ? thisFormateur.traiterPourFenetreLightBox(pseudo, url) + ": " : "") + thisFormateur.traiterPourFenetreLightBox(m, url) + "\"" + " rel=\"lightbox\"" : "") + " href=\"" + url + "\" >[" + extension[0] + "]</a>";
103 };
104 return m.replace(this.regexUrl, traitementUrl);
105 };
106
107 /**
108 * Formatage en utilisant un sous-ensemble des règles de Textile : http://en.wikipedia.org/wiki/Textile_(markup_language).
109 * par exemple _italic_ devient <i>italic</i>.
110 */
111 euphorik.Formateur.prototype.traiterWikiSyntaxe = function(m) {
112 return m.replace(
113 /_(.*?)_/g,
114 function(texte, capture) {
115 return '<em class="leger">' + capture + '</em>';
116 }
117 ).replace(
118 /\*(.*?)\*/g,
119 function(texte, capture) {
120 return '<em class="fort">' + capture + '</em>';
121 }
122 );
123 };
124
125 /**
126 * Renvoie une version courte de l'url.
127 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
128 */
129 euphorik.Formateur.prototype.getShort = function(url) {
130 var estUneImage = false;
131 var versionShort = null;
132 var rechercheImg = this.regexImg.exec(url);
133
134 if (rechercheImg) {
135 versionShort = rechercheImg[1].toLowerCase();
136 if (versionShort === "jpeg") {
137 versionShort = "jpg"; // jpeg -> jpg
138 }
139 estUneImage = true;
140 } else {
141 var rechercheDomaine = this.regexDomaine.exec(url);
142 if (rechercheDomaine && rechercheDomaine.length >= 2) {
143 versionShort = rechercheDomaine[1];
144 } else {
145 var nomProtocole = this.regexNomProtocole.exec(url);
146 if (nomProtocole && nomProtocole.length >= 2) {
147 versionShort = nomProtocole[1];
148 }
149 }
150 }
151
152 return [versionShort ? versionShort : "url", estUneImage];
153 };
154
155 euphorik.Formateur.prototype.supprimerSmiles = function(m) {
156 objectEach(this.smiles, function(nom, smiles) {
157 for (var i = 0; i < smiles.length; i++) {
158 m = m.replace(smiles[i], "");
159 }
160 });
161 return m;
162 };
163
164 /**
165 * Traite les pseudo et messages à être affiché dans le titre d'une image visualisé avec lightbox.
166 * Supprime les smiles pour pas qu'ils puissent être remplacés par la fonction 'traiterSmiles'.
167 * TODO : trouver un moyen pour que les smiles puissent être conservés
168 */
169 euphorik.Formateur.prototype.traiterPourFenetreLightBox = function(M, urlCourante) {
170 var thisFormateur = this;
171 var traitementUrl = function(url) {
172 return "[" + thisFormateur.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]";
173 };
174
175 return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl);
176 };