2 // Copyright 2008 Grégory Burri
4 // This file is part of Euphorik.
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.
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.
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/>.
19 /*jslint laxbreak:true */
22 * An object for text formatting like Wiki syntax or smiles substitution.
23 * TODO : improve the performance of the smiles substitution
25 euphorik
.Formatter = function() {
26 this.smiles
= euphorik
.conf
.smiles
;
27 this.protocols
= "http|https|ed2k";
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("^(.*?)://");
37 * Formats a nick given by the user.
38 * Trim and remove "{..}".
39 * @param nick the given nick
40 * @return the cleaned nick
42 euphorik
.Formatter
.prototype.formatNick = function(nick
) {
43 return nick
.replace(/\{|\}/g, "").trim();
46 euphorik
.Formatter
.prototype.getSmilesHTML = function() {
48 objectEach(this.smiles
, function(name
) {
49 XHTML
+= "<img class=\"" + name
+ "\" src=\"img/smileys/" + name
+ ".gif\" alt =\"" + name
+ "\" />";
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
62 * @nick optional, attaches the nick and the message to each images like "<pseudo> : <message>"
64 euphorik
.Formatter
.prototype.completeProcessing = function(m
, nick
) {
65 return this.processConversationLinks(this.processSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m
)), nick
)));
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.
74 euphorik
.Formatter
.prototype.processConversationLinks = function(m
) {
78 return "<span class=\"conversationLink\">" + lien
+ "</span>";
84 * Substitute the smiles (':)', ':P', etc.) with HTML.
85 * FIXME : This function is very heavy, to optimize !
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
+ "\" />");
97 euphorik
.Formatter
.prototype.remplacerBalisesHTML = function(m
) {
98 return m
.replace(/</g
, "<").replace(/>/g
, ">").replace(/"/g, ""
;");
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
;
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>";
111 return m
.replace(this.regexUrl
, traitementUrl
);
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>.
118 euphorik
.Formatter
.prototype.traiterWikiSyntaxe = function(m
) {
120 /(?:^| )_(.*?)_(?:$| )/g
,
121 function(texte
, c1
, c2
, c3
) {
122 return '<em>' + c1
+ c2
+ c3
+ '</em>';
125 /(?:^| )\*(.*?)\*(?:$| )/g
,
126 function(texte
, c1
, c2
, c3
) {
127 return '<strong>' + c1
+ c2
+ c3
+ '</strong>';
133 * Renvoie une version courte de l'url.
134 * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient en.wikipedia.org
136 euphorik
.Formatter
.prototype.getShort = function(url
) {
137 var estUneImage
= false;
138 var versionShort
= null;
139 var rechercheImg
= this.regexImg
.exec(url
);
142 versionShort
= rechercheImg
[1].toLowerCase();
143 if (versionShort
=== "jpeg") {
144 versionShort
= "jpg"; // jpeg -> jpg
148 var rechercheDomaine
= this.regexDomain
.exec(url
);
149 if (rechercheDomaine
&& rechercheDomaine
.length
>= 2) {
150 versionShort
= rechercheDomaine
[1];
152 var protocolName
= this.regexProtocolName
.exec(url
);
153 if (protocolName
&& protocolName
.length
>= 2) {
154 versionShort
= protocolName
[1];
159 return [versionShort
? versionShort : "url", estUneImage
];
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
], "");
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
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
? "*" : "") + "]";
182 return this.remplacerBalisesHTML(this.supprimerSmiles(M
)).replace(this.regexUrl
, traitementUrl
);