Merge branch 'master' of gburri.org:euphorik
[euphorik.git] / js / formatter.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 /*jslint laxbreak:true */
20
21 /**
22 * An object for text formatting like Wiki syntax or smiles substitution.
23 * TODO : improve the performance of the smiles substitution
24 */
25 euphorik.Formatter = function() {
26 this.smiles = euphorik.conf.smiles;
27 this.protocols = "http|https|ed2k";
28
29 this.regexUrl = new RegExp("(?:(?:" + this.protocols + ")://|www\\.)[^ ]*", "gi");
30 this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i");
31 this.regexDomain = new RegExp("^(?:(?:" + this.protocols + ")://)(.*?)(?:$|/).*$", "i");
32 this.regexTestIfProtocolExists = new RegExp("^(?:" + this.protocols + ")://.*$", "i");
33 this.regexProtocolName = new RegExp("^(.*?)://");
34 };
35
36 /**
37 * Formats a nick given by the user.
38 * Trim and remove "{..}".
39 * @param nick the given nick
40 * @return the cleaned nick
41 */
42 euphorik.Formatter.prototype.formatNick = function(nick) {
43 return nick.replace(/\{|\}/g, "").trim();
44 };
45
46 euphorik.Formatter.prototype.getSmilesHTML = function() {
47 var XHTML = "";
48 objectEach(this.smiles, function(name) {
49 XHTML += "<img class=\"" + name + "\" src=\"img/smileys/" + name + ".gif\" alt =\"" + name + "\" />";
50 });
51 return XHTML;
52 };
53
54 /**
55 * Complete formatting process applied to a text.
56 * - Remove HTML markups
57 * - Substitutes wiki syntax with HTML
58 * - Replaces URL with 'a' tag
59 * - Replaces smiles with HTML
60 * - Replaces the link to a conversation with HTML
61 * @m the raw message
62 * @nick optional, attaches the nick and the message to each images like "<pseudo> : <message>"
63 */
64 euphorik.Formatter.prototype.completeProcessing = function(m, nick) {
65 return this.processConversationLinks(this.processSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), nick)));
66 };
67
68 /**
69 * Processes all conversation links.
70 * The user can click on a conversation link to open it.
71 * A link is a number in between brackets like that : "{5F}" where '5F' is the id of the root message.
72 * This link will be turn in '<span class="conversationLink">{5F}</span>' which can be clicked to open the '5F' conversation.
73 */
74 euphorik.Formatter.prototype.processConversationLinks = function(m) {
75 return m.replace(
76 /\{\w+\}/g,
77 function(lien) {
78 return "<span class=\"conversationLink\">" + lien + "</span>";
79 }
80 );
81 };
82
83 /**
84 * Substitute the smiles (':)', ':P', etc.) with HTML.
85 * FIXME : This function is very heavy, to optimize !
86 * Average : 234ms
87 */
88 euphorik.Formatter.prototype.processSmiles = function(m) {
89 objectEach(this.smiles, function(name, smiles) {
90 for (var i = 0; i < smiles.length; i++) {
91 m = m.replace(smiles[i], "<img src=\"img/smileys/" + name + ".gif\" alt =\"" + name + "\" />");
92 }
93 });
94 return m;
95 };
96
97 euphorik.Formatter.prototype.remplacerBalisesHTML = function(m) {
98 return m.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
99 };
100
101 euphorik.Formatter.prototype.traiterURL = function(m, nick) {
102 var thisFormatter = this;
103 var traitementUrl = function(url) {
104 // si ya pas de protocole on rajoute "http://"
105 if (!thisFormatter.regexTestIfProtocolExists.test(url)) {
106 url = "http://" + url;
107 }
108 var extension = thisFormatter.getShort(url);
109 return "<a " + (extension[1] ? "title=\"" + (nick ? thisFormatter.traiterPourFenetreLightBox(nick, url) + ": " : "") + thisFormateur.traiterPourFenetreLightBox(m, url) + "\"" + " rel=\"lightbox\"" : "") + " href=\"" + url + "\" >[" + extension[0] + "]</a>";
110 };
111 return m.replace(this.regexUrl, traitementUrl);
112 };
113
114 /**
115 * Formatage en utilisant un sous-ensemble des règles de Textile : http://en.wikipedia.org/wiki/Textile_(markup_language).
116 * par exemple _italic_ devient <i>italic</i>.
117 */
118 euphorik.Formatter.prototype.traiterWikiSyntaxe = function(m) {
119 return m.replace(
120 /(?:^| )_(.*?)_(?:$| )/g,
121 function(texte, c1, c2, c3) {
122 return '<em>' + c1 + c2 + c3 + '</em>';
123 }
124 ).replace(
125 /(?:^| )\*(.*?)\*(?:$| )/g,
126 function(texte, c1, c2, c3) {
127 return '<strong>' + c1 + c2 + c3 + '</strong>';
128 }
129 );
130 };
131
132 /**
133 * Renvoie une version courte de l'url.
134 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient en.wikipedia.org
135 */
136 euphorik.Formatter.prototype.getShort = function(url) {
137 var estUneImage = false;
138 var versionShort = null;
139 var rechercheImg = this.regexImg.exec(url);
140
141 if (rechercheImg) {
142 versionShort = rechercheImg[1].toLowerCase();
143 if (versionShort === "jpeg") {
144 versionShort = "jpg"; // jpeg -> jpg
145 }
146 estUneImage = true;
147 } else {
148 var rechercheDomaine = this.regexDomain.exec(url);
149 if (rechercheDomaine && rechercheDomaine.length >= 2) {
150 versionShort = rechercheDomaine[1];
151 } else {
152 var protocolName = this.regexProtocolName.exec(url);
153 if (protocolName && protocolName.length >= 2) {
154 versionShort = protocolName[1];
155 }
156 }
157 }
158
159 return [versionShort ? versionShort : "url", estUneImage];
160 };
161
162 euphorik.Formatter.prototype.supprimerSmiles = function(m) {
163 objectEach(this.smiles, function(name, smiles) {
164 for (var i = 0; i < smiles.length; i++) {
165 m = m.replace(smiles[i], "");
166 }
167 });
168 return m;
169 };
170
171 /**
172 * Traite les nick et messages à être affiché dans le titre d'une image visualisé avec lightbox.
173 * Supprime les smiles pour pas qu'ils puissent être remplacés par la fonction 'processSmiles'.
174 * TODO : trouver un moyen pour que les smiles puissent être conservés
175 */
176 euphorik.Formatter.prototype.traiterPourFenetreLightBox = function(M, urlCourante) {
177 var thisFormatter = this;
178 var traitementUrl = function(url) {
179 return "[" + thisFormatter.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]";
180 };
181
182 return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl);
183 };