// 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 .
/**
* Classe permettant de formater du texte par exemple pour la substitution des liens dans les
* message par "[url]".
* TODO : améliorer l'efficacité des méthods notamment lié au smiles.
*/
euphorik.Formateur = function() {
this.smiles = euphorik.conf.smiles;
this.protocoles = "http|https|ed2k";
this.regexUrl = new RegExp("(?:(?:" + this.protocoles + ")://|www\\.)[^ ]*", "gi");
this.regexImg = new RegExp("^.*?\\.(gif|jpg|png|jpeg|bmp|tiff)$", "i");
this.regexDomaine = new RegExp("^(?:(?:" + this.protocoles + ")://|www\\.).*?([^/.]+\\.[^/.]+)(?:$|/).*$", "i");
this.regexTestProtocoleExiste = new RegExp("^(?:" + this.protocoles + ")://.*$", "i");
this.regexNomProtocole = new RegExp("^(.*?)://");
};
/**
* Formate un pseudo saise par l'utilisateur.
* @param pseudo le pseudo brut
* @return le pseudo filtré
*/
euphorik.Formateur.prototype.filtrerInputPseudo = function(pseudo) {
return pseudo.replace(/\{|\}/g, "").trim();
};
euphorik.Formateur.prototype.getSmilesHTML = function() {
var XHTML = "";
objectEach(this.smiles, function(nom) {
XHTML += "";
});
return XHTML;
};
/**
* Formatage complet d'un texte.
* @m le message
* @pseudo facultatif, permet de contruire le label des images sous la forme : " : "
*/
euphorik.Formateur.prototype.traitementComplet = function(m, pseudo) {
return this.traiterLiensConv(this.traiterSmiles(this.traiterURL(this.traiterWikiSyntaxe(this.remplacerBalisesHTML(m)), pseudo)));
};
/**
* Transforme les liens en entités clickables.
* Un lien vers une conversation permet d'ouvrire celle ci, elle se marque comme ceci dans un message :
* "{5F}" ou 5F est la racine de la conversation.
* Ce lien sera transformer en {5F} pouvant être clické pour créer la conv 5F.
*/
euphorik.Formateur.prototype.traiterLiensConv = function(m) {
return m.replace(
/\{\w+\}/g,
function(lien) {
return "" + lien + "";
}
);
};
/**
* FIXME : Cette méthode est attrocement lourde ! A optimiser.
* moyenne sur échantillon : 234ms
*/
euphorik.Formateur.prototype.traiterSmiles = function(m) {
objectEach(this.smiles, function(nom, smiles) {
for (var i = 0; i < smiles.length; i++) {
m = m.replace(smiles[i], "");
}
});
return m;
};
euphorik.Formateur.prototype.remplacerBalisesHTML = function(m) {
return m.replace(//g, ">").replace(/"/g, """);
};
euphorik.Formateur.prototype.traiterURL = function(m, pseudo) {
var thisFormateur = this;
var traitementUrl = function(url) {
// si ya pas de protocole on rajoute "http://"
if (!thisFormateur.regexTestProtocoleExiste.test(url)) {
url = "http://" + url;
}
var extension = thisFormateur.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.Formateur.prototype.traiterWikiSyntaxe = function(m) {
return m.replace(
/_(.*?)_/g,
function(texte, capture) {
return '' + capture + '';
}
).replace(
/\*(.*?)\*/g,
function(texte, capture) {
return '' + capture + '';
}
);
};
/**
* Renvoie une version courte de l'url.
* par exemple : http://en.wikipedia.org/wiki/Yakov_Smirnoff devient wikipedia.org
*/
euphorik.Formateur.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.regexDomaine.exec(url);
if (rechercheDomaine && rechercheDomaine.length >= 2) {
versionShort = rechercheDomaine[1];
} else {
var nomProtocole = this.regexNomProtocole.exec(url);
if (nomProtocole && nomProtocole.length >= 2) {
versionShort = nomProtocole[1];
}
}
}
return [versionShort ? versionShort : "url", estUneImage];
};
euphorik.Formateur.prototype.supprimerSmiles = function(m) {
objectEach(this.smiles, function(nom, smiles) {
for (var i = 0; i < smiles.length; i++) {
m = m.replace(smiles[i], "");
}
});
return m;
}
/**
* Traite les pseudo 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 'traiterSmiles'.
* TODO : trouver un moyen pour que les smiles puissent être conservés
*/
euphorik.Formateur.prototype.traiterPourFenetreLightBox = function(M, urlCourante) {
var thisFormateur = this;
var traitementUrl = function(url) {
return "[" + thisFormateur.getShort(url)[0] + (urlCourante === url ? "*" : "") + "]";
};
return this.remplacerBalisesHTML(this.supprimerSmiles(M)).replace(this.regexUrl, traitementUrl);
};