'formater' -> 'formatter'
[euphorik.git] / js / formatter.js
diff --git a/js/formatter.js b/js/formatter.js
new file mode 100644 (file)
index 0000000..8c3bf05
--- /dev/null
@@ -0,0 +1,183 @@
+// 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
+/*jslint laxbreak:true */\r
+\r
+/**\r
+  * An object for text formatting like Wiki syntax or smiles substitution.\r
+  * TODO : improve the performance of the smiles substitution\r
+  */\r
+euphorik.Formatter = function() {\r
+   this.smiles = euphorik.conf.smiles;\r
+   this.protocols = "http|https|ed2k";\r
+   \r
+   this.regexUrl = new RegExp("(?:(?:" + this.protocols + ")://|www\\.)[^ ]*", "gi");\r
+   this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i");\r
+   this.regexDomain = new RegExp("^(?:(?:" + this.protocols + ")://)(.*?)(?:$|/).*$", "i");\r
+   this.regexTestIfProtocolExists = new RegExp("^(?:" + this.protocols + ")://.*$", "i");\r
+   this.regexProtocolName = new RegExp("^(.*?)://");\r
+};\r
+\r
+/**\r
+  * Formats a nick given by the user.\r
+  * Trim and remove "{..}".\r
+  * @param nick the given nick\r
+  * @return the cleaned nick\r
+  */\r
+euphorik.Formatter.prototype.formatNick = function(nick) {\r
+   return nick.replace(/\{|\}/g, "").trim();\r
+};\r
+\r
+euphorik.Formatter.prototype.getSmilesHTML = function() {\r
+   var XHTML = "";\r
+   objectEach(this.smiles, function(name) {\r
+      XHTML += "<img class=\"" + name + "\" src=\"img/smileys/" + name + ".gif\" alt =\"" + name + "\" />";\r
+   });\r
+   return XHTML;\r
+};\r
+\r
+/**\r
+  * Complete fomratting process applied to a text.\r
+  *  - Remove HTML markups\r
+  *  - Substitutes wiki syntax with HTML\r
+  *  - Replaces URL with 'a' tag\r
+  *  - Replaces smiles with HTML\r
+  *  - Replaces the link to a conversation with HTML\r
+  * @m the raw message\r
+  * @nick optional, attaches the nick and the message to each images like "<pseudo> : <message>"\r
+  */\r
+euphorik.Formatter.prototype.completeProcessing = function(m, nick) {\r
+   return this.processConversationLinks(this.processSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), nick)));\r
+};\r
+\r
+/**\r
+  * Processes all conversation links.\r
+  * The user can click on a conversation link to open it.\r
+  * A link is a number in between brackets like that : "{5F}" where '5F' is the id of the root message.\r
+  * This link will be turn in '<span class="conversationLink">{5F}</span>' which can be clicked to open the '5F' conversation.\r
+  */\r
+euphorik.Formatter.prototype.processConversationLinks = function(m) {\r
+   return m.replace(\r
+      /\{\w+\}/g,\r
+      function(lien) {\r
+         return "<span class=\"conversationLink\">" + lien + "</span>";\r
+      }\r
+   );\r
+};\r
+\r
+/**\r
+  * Substitute the smiles (':)', ':P', etc.) with HTML.\r
+  * FIXME : This function is very heavy, to optimize !\r
+  * Average : 234ms\r
+  */\r
+euphorik.Formatter.prototype.processSmiles = function(m) {\r
+   objectEach(this.smiles, function(name, smiles) {\r
+      for (var i = 0; i < smiles.length; i++) {\r
+         m = m.replace(smiles[i], "<img src=\"img/smileys/" + name + ".gif\" alt =\"" + name + "\"  />");\r
+      }\r
+   });\r
+   return m;\r
+};\r
+\r
+euphorik.Formatter.prototype.remplacerBalisesHTML = function(m) {\r
+   return m.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");\r
+};\r
+\r
+euphorik.Formatter.prototype.traiterURL = function(m, nick) {\r
+   var thisFormater = this;\r
+   var traitementUrl = function(url) {    \r
+      // si ya pas de protocole on rajoute "http://"\r
+      if (!thisFormater.regexTestIfProtocolExists.test(url)) {\r
+         url = "http://" + url;\r
+      }\r
+      var extension = thisFormater.getShort(url);\r
+      return "<a " + (extension[1] ? "title=\"" + (nick ? thisFormater.traiterPourFenetreLightBox(nick, 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.Formatter.prototype.traiterWikiSyntaxe = function(m) {\r
+   return m.replace(\r
+      /(?:^| )_(.*?)_(?:$| )/g,\r
+      function(texte, c1, c2, c3) {\r
+         return '<em>' + c1 + c2 + c3 + '</em>';\r
+      }\r
+   ).replace(\r
+      /(?:^| )\*(.*?)\*(?:$| )/g,\r
+      function(texte, c1, c2, c3) {\r
+         return '<strong>' + c1 + c2 + c3 + '</strong>';\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.Formatter.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.regexDomain.exec(url);\r
+      if (rechercheDomaine && rechercheDomaine.length >= 2) {\r
+         versionShort = rechercheDomaine[1];\r
+      } else {\r
+         var protocolName = this.regexProtocolName.exec(url);\r
+         if (protocolName && protocolName.length >= 2) {\r
+            versionShort = protocolName[1];\r
+         }\r
+      }\r
+   }\r
+   \r
+   return [versionShort ? versionShort : "url", estUneImage];\r
+ };\r
\r
+euphorik.Formatter.prototype.supprimerSmiles = function(m) {\r
+   objectEach(this.smiles, function(name, 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 nick 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 'processSmiles'.\r
+  * TODO : trouver un moyen pour que les smiles puissent être conservés\r
+  */\r
+euphorik.Formatter.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