From: Ummon Date: Sat, 13 Apr 2019 18:00:45 +0000 (+0200) Subject: 'formater' -> 'formatter' X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=commitdiff_plain;h=6926aecb1cdd777ef8c52f63291341a540670a2c 'formater' -> 'formatter' --- diff --git a/js/formater.js b/js/formater.js deleted file mode 100644 index a4fbc23..0000000 --- a/js/formater.js +++ /dev/null @@ -1,183 +0,0 @@ -// 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 */ - -/** - * An object for text formatting like Wiki syntax or smiles substitution. - * TODO : improve the performance of the smiles substitution - */ -euphorik.Formater = function() { - this.smiles = euphorik.conf.smiles; - this.protocols = "http|https|ed2k"; - - this.regexUrl = new RegExp("(?:(?:" + this.protocols + ")://|www\\.)[^ ]*", "gi"); - this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i"); - this.regexDomain = new RegExp("^(?:(?:" + this.protocols + ")://)(.*?)(?:$|/).*$", "i"); - this.regexTestIfProtocolExists = new RegExp("^(?:" + this.protocols + ")://.*$", "i"); - this.regexProtocolName = new RegExp("^(.*?)://"); -}; - -/** - * Formats a nick given by the user. - * Trim and remove "{..}". - * @param nick the given nick - * @return the cleaned nick - */ -euphorik.Formater.prototype.formatNick = function(nick) { - return nick.replace(/\{|\}/g, "").trim(); -}; - -euphorik.Formater.prototype.getSmilesHTML = function() { - var XHTML = ""; - objectEach(this.smiles, function(name) { - XHTML += "\"""; - }); - return XHTML; -}; - -/** - * Complete fomratting process applied to a text. - * - Remove HTML markups - * - Substitutes wiki syntax with HTML - * - Replaces URL with 'a' tag - * - Replaces smiles with HTML - * - Replaces the link to a conversation with HTML - * @m the raw message - * @nick optional, attaches the nick and the message to each images like " : " - */ -euphorik.Formater.prototype.completeProcessing = function(m, nick) { - return this.processConversationLinks(this.processSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), nick))); -}; - -/** - * Processes all conversation links. - * The user can click on a conversation link to open it. - * A link is a number in between brackets like that : "{5F}" where '5F' is the id of the root message. - * This link will be turn in '{5F}' which can be clicked to open the '5F' conversation. - */ -euphorik.Formater.prototype.processConversationLinks = function(m) { - return m.replace( - /\{\w+\}/g, - function(lien) { - return "" + lien + ""; - } - ); -}; - -/** - * Substitute the smiles (':)', ':P', etc.) with HTML. - * FIXME : This function is very heavy, to optimize ! - * Average : 234ms - */ -euphorik.Formater.prototype.processSmiles = function(m) { - objectEach(this.smiles, function(name, smiles) { - for (var i = 0; i < smiles.length; i++) { - m = m.replace(smiles[i], "\"""); - } - }); - return m; -}; - -euphorik.Formater.prototype.remplacerBalisesHTML = function(m) { - return m.replace(//g, ">").replace(/"/g, """); -}; - -euphorik.Formater.prototype.traiterURL = function(m, nick) { - var thisFormater = this; - var traitementUrl = function(url) { - // si ya pas de protocole on rajoute "http://" - if (!thisFormater.regexTestIfProtocolExists.test(url)) { - url = "http://" + url; - } - var extension = thisFormater.getShort(url); - return "[" + extension[0] + "]"; - }; - return m.replace(this.regexUrl, traitementUrl); -}; - -/** - * Formatage en utilisant un sous-ensemble des règles de Textile : http://en.wikipedia.org/wiki/Textile_(markup_language). - * par exemple _italic_ devient italic. - */ -euphorik.Formater.prototype.traiterWikiSyntaxe = function(m) { - return m.replace( - /(?:^| )_(.*?)_(?:$| )/g, - function(texte, c1, c2, c3) { - return '' + c1 + c2 + c3 + ''; - } - ).replace( - /(?:^| )\*(.*?)\*(?:$| )/g, - function(texte, c1, c2, c3) { - return '' + c1 + c2 + c3 + ''; - } - ); -}; - -/** - * Renvoie une version courte de l'url. - * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient en.wikipedia.org - */ -euphorik.Formater.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.regexDomain.exec(url); - if (rechercheDomaine && rechercheDomaine.length >= 2) { - versionShort = rechercheDomaine[1]; - } else { - var protocolName = this.regexProtocolName.exec(url); - if (protocolName && protocolName.length >= 2) { - versionShort = protocolName[1]; - } - } - } - - return [versionShort ? versionShort : "url", estUneImage]; - }; - -euphorik.Formater.prototype.supprimerSmiles = function(m) { - objectEach(this.smiles, function(name, smiles) { - for (var i = 0; i < smiles.length; i++) { - m = m.replace(smiles[i], ""); - } - }); - return m; -}; - -/** - * Traite les nick et messages à être affiché dans le titre d'une image visualisé avec lightbox. - * Supprime les smiles pour pas qu'ils puissent être remplacés par la fonction 'processSmiles'. - * TODO : trouver un moyen pour que les smiles puissent être conservés - */ -euphorik.Formater.prototype.traiterPourFenetreLightBox = function(M, urlCourante) { - var thisFormater = this; - var traitementUrl = function(url) { - return "[" + thisFormater.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]"; - }; - - return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl); -}; diff --git a/js/formatter.js b/js/formatter.js new file mode 100644 index 0000000..8c3bf05 --- /dev/null +++ b/js/formatter.js @@ -0,0 +1,183 @@ +// 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 */ + +/** + * An object for text formatting like Wiki syntax or smiles substitution. + * TODO : improve the performance of the smiles substitution + */ +euphorik.Formatter = function() { + this.smiles = euphorik.conf.smiles; + this.protocols = "http|https|ed2k"; + + this.regexUrl = new RegExp("(?:(?:" + this.protocols + ")://|www\\.)[^ ]*", "gi"); + this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i"); + this.regexDomain = new RegExp("^(?:(?:" + this.protocols + ")://)(.*?)(?:$|/).*$", "i"); + this.regexTestIfProtocolExists = new RegExp("^(?:" + this.protocols + ")://.*$", "i"); + this.regexProtocolName = new RegExp("^(.*?)://"); +}; + +/** + * Formats a nick given by the user. + * Trim and remove "{..}". + * @param nick the given nick + * @return the cleaned nick + */ +euphorik.Formatter.prototype.formatNick = function(nick) { + return nick.replace(/\{|\}/g, "").trim(); +}; + +euphorik.Formatter.prototype.getSmilesHTML = function() { + var XHTML = ""; + objectEach(this.smiles, function(name) { + XHTML += "\"""; + }); + return XHTML; +}; + +/** + * Complete fomratting process applied to a text. + * - Remove HTML markups + * - Substitutes wiki syntax with HTML + * - Replaces URL with 'a' tag + * - Replaces smiles with HTML + * - Replaces the link to a conversation with HTML + * @m the raw message + * @nick optional, attaches the nick and the message to each images like " : " + */ +euphorik.Formatter.prototype.completeProcessing = function(m, nick) { + return this.processConversationLinks(this.processSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), nick))); +}; + +/** + * Processes all conversation links. + * The user can click on a conversation link to open it. + * A link is a number in between brackets like that : "{5F}" where '5F' is the id of the root message. + * This link will be turn in '{5F}' which can be clicked to open the '5F' conversation. + */ +euphorik.Formatter.prototype.processConversationLinks = function(m) { + return m.replace( + /\{\w+\}/g, + function(lien) { + return "" + lien + ""; + } + ); +}; + +/** + * Substitute the smiles (':)', ':P', etc.) with HTML. + * FIXME : This function is very heavy, to optimize ! + * Average : 234ms + */ +euphorik.Formatter.prototype.processSmiles = function(m) { + objectEach(this.smiles, function(name, smiles) { + for (var i = 0; i < smiles.length; i++) { + m = m.replace(smiles[i], "\"""); + } + }); + return m; +}; + +euphorik.Formatter.prototype.remplacerBalisesHTML = function(m) { + return m.replace(//g, ">").replace(/"/g, """); +}; + +euphorik.Formatter.prototype.traiterURL = function(m, nick) { + var thisFormater = this; + var traitementUrl = function(url) { + // si ya pas de protocole on rajoute "http://" + if (!thisFormater.regexTestIfProtocolExists.test(url)) { + url = "http://" + url; + } + var extension = thisFormater.getShort(url); + return "[" + extension[0] + "]"; + }; + return m.replace(this.regexUrl, traitementUrl); +}; + +/** + * Formatage en utilisant un sous-ensemble des règles de Textile : http://en.wikipedia.org/wiki/Textile_(markup_language). + * par exemple _italic_ devient italic. + */ +euphorik.Formatter.prototype.traiterWikiSyntaxe = function(m) { + return m.replace( + /(?:^| )_(.*?)_(?:$| )/g, + function(texte, c1, c2, c3) { + return '' + c1 + c2 + c3 + ''; + } + ).replace( + /(?:^| )\*(.*?)\*(?:$| )/g, + function(texte, c1, c2, c3) { + return '' + c1 + c2 + c3 + ''; + } + ); +}; + +/** + * Renvoie une version courte de l'url. + * par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient en.wikipedia.org + */ +euphorik.Formatter.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.regexDomain.exec(url); + if (rechercheDomaine && rechercheDomaine.length >= 2) { + versionShort = rechercheDomaine[1]; + } else { + var protocolName = this.regexProtocolName.exec(url); + if (protocolName && protocolName.length >= 2) { + versionShort = protocolName[1]; + } + } + } + + return [versionShort ? versionShort : "url", estUneImage]; + }; + +euphorik.Formatter.prototype.supprimerSmiles = function(m) { + objectEach(this.smiles, function(name, smiles) { + for (var i = 0; i < smiles.length; i++) { + m = m.replace(smiles[i], ""); + } + }); + return m; +}; + +/** + * Traite les nick et messages à être affiché dans le titre d'une image visualisé avec lightbox. + * Supprime les smiles pour pas qu'ils puissent être remplacés par la fonction 'processSmiles'. + * TODO : trouver un moyen pour que les smiles puissent être conservés + */ +euphorik.Formatter.prototype.traiterPourFenetreLightBox = function(M, urlCourante) { + var thisFormater = this; + var traitementUrl = function(url) { + return "[" + thisFormater.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]"; + }; + + return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl); +};