|
|
@@ -20,144 +20,146 @@ function formatEmojis(inputString) {
|
|
|
* @param {string} fullText
|
|
|
* @return {string}
|
|
|
**/
|
|
|
-function formatText(fullText) {
|
|
|
- if (!fullText || fullText == "")
|
|
|
- return "";
|
|
|
- var msgContents = fullText.split(/\r?\n/g);
|
|
|
+var formatText = (function() {
|
|
|
+ return function (fullText) {
|
|
|
+ if (!fullText || fullText == "")
|
|
|
+ return "";
|
|
|
+ var msgContents = fullText.split(/\r?\n/g);
|
|
|
|
|
|
- for (var msgContentIndex=0, nbMsgContents = msgContents.length; msgContentIndex < nbMsgContents; msgContentIndex++) {
|
|
|
- var msgContent = msgContents[msgContentIndex].trim()
|
|
|
- ,_msgContent = ""
|
|
|
- ,currentMods = {}
|
|
|
- ,quote = false
|
|
|
- ,i =0
|
|
|
+ for (var msgContentIndex=0, nbMsgContents = msgContents.length; msgContentIndex < nbMsgContents; msgContentIndex++) {
|
|
|
+ var msgContent = msgContents[msgContentIndex].trim()
|
|
|
+ ,_msgContent = ""
|
|
|
+ ,currentMods = {}
|
|
|
+ ,quote = false
|
|
|
+ ,i =0
|
|
|
|
|
|
- msgContent = msgContent.replace(new RegExp('<([@#]?)([^>]*)>', 'g'),
|
|
|
- function(matched, type, entity) {
|
|
|
- var sub = entity.split('|');
|
|
|
+ msgContent = msgContent.replace(new RegExp('<([@#]?)([^>]*)>', 'g'),
|
|
|
+ function(matched, type, entity) {
|
|
|
+ var sub = entity.split('|');
|
|
|
|
|
|
- if (type === '@') {
|
|
|
- if (!sub[1]) {
|
|
|
- var user = SLACK.context.users[sub[0]];
|
|
|
- sub[1] = user ? ('@' +user.name) : locale.unknownMember;
|
|
|
- } else if ('@' !== sub[1][0]) {
|
|
|
- sub[1] = '@' +sub[1];
|
|
|
+ if (type === '@') {
|
|
|
+ if (!sub[1]) {
|
|
|
+ var user = SLACK.context.users[sub[0]];
|
|
|
+ sub[1] = user ? ('@' +user.name) : locale.unknownMember;
|
|
|
+ } else if ('@' !== sub[1][0]) {
|
|
|
+ sub[1] = '@' +sub[1];
|
|
|
+ }
|
|
|
+ sub[0] = '#' +sub[0];
|
|
|
+ sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkuser;
|
|
|
+ } else if (type === '#') {
|
|
|
+ if (!sub[1]) {
|
|
|
+ var chan = SLACK.context.channels[sub[0]];
|
|
|
+ sub[1] = chan ? ('#' +chan.name) : locale.unknownChannel;
|
|
|
+ } else if ('#' !== sub[1][0]) {
|
|
|
+ sub[1] = '#' +sub[1];
|
|
|
+ }
|
|
|
+ sub[0] = '#' +sub[0];
|
|
|
+ sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkchan;
|
|
|
+ } else if (sub[0].indexOf("://") !== -1) {
|
|
|
+ if (!sub[1])
|
|
|
+ sub[1] = sub[0];
|
|
|
+ sub[2] = R.klass.msg.link;
|
|
|
+ } else {
|
|
|
+ return matched;
|
|
|
}
|
|
|
- sub[0] = '#' +sub[0];
|
|
|
- sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkuser;
|
|
|
- } else if (type === '#') {
|
|
|
- if (!sub[1]) {
|
|
|
- var chan = SLACK.context.channels[sub[0]];
|
|
|
- sub[1] = chan ? ('#' +chan.name) : locale.unknownChannel;
|
|
|
- } else if ('#' !== sub[1][0]) {
|
|
|
- sub[1] = '#' +sub[1];
|
|
|
+ return '<a href="' +sub[0] +'" class="' +sub[2] +'"' +(!type ? ' target="_blank"' : '') +'>' +sub[1] +'</a>';
|
|
|
+ });
|
|
|
+ msgContent = formatEmojis(msgContent);
|
|
|
+ var msgLength = msgContent.length;
|
|
|
+ var isAlphadec = function(c) {
|
|
|
+ return ((c >= 'A' && c <= 'Z') ||
|
|
|
+ (c >= 'a' && c <= 'z') ||
|
|
|
+ (c >= '0' && c <= '9') ||
|
|
|
+ "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ".indexOf(c) !== -1);
|
|
|
+ }
|
|
|
+ ,checkEnd = function(str, pos, c) {
|
|
|
+ while (str[pos]) {
|
|
|
+ if (isAlphadec(str[pos]) && str[pos] != c && str[pos +1] == c) {
|
|
|
+ return true;
|
|
|
}
|
|
|
- sub[0] = '#' +sub[0];
|
|
|
- sub[2] = R.klass.msg.link +' ' +R.klass.msg.linkchan;
|
|
|
- } else if (sub[0].indexOf("://") !== -1) {
|
|
|
- if (!sub[1])
|
|
|
- sub[1] = sub[0];
|
|
|
- sub[2] = R.klass.msg.link;
|
|
|
- } else {
|
|
|
- return matched;
|
|
|
- }
|
|
|
- return '<a href="' +sub[0] +'" class="' +sub[2] +'"' +(!type ? ' target="_blank"' : '') +'>' +sub[1] +'</a>';
|
|
|
- });
|
|
|
- msgContent = formatEmojis(msgContent);
|
|
|
- var msgLength = msgContent.length;
|
|
|
- var isAlphadec = function(c) {
|
|
|
- return ((c >= 'A' && c <= 'Z') ||
|
|
|
- (c >= 'a' && c <= 'z') ||
|
|
|
- (c >= '0' && c <= '9') ||
|
|
|
- "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ".indexOf(c) !== -1);
|
|
|
- }
|
|
|
- ,checkEnd = function(str, pos, c) {
|
|
|
- while (str[pos]) {
|
|
|
- if (isAlphadec(str[pos]) && str[pos] != c && str[pos +1] == c) {
|
|
|
- return true;
|
|
|
+ pos++;
|
|
|
}
|
|
|
- pos++;
|
|
|
+ return false;
|
|
|
+ } ,appendMod = function(mods) {
|
|
|
+ if (!Object.keys(currentMods).length)
|
|
|
+ return "";
|
|
|
+ return '<span class="' +Object.keys(mods).join(' ') +'">';
|
|
|
+ };
|
|
|
+
|
|
|
+ // Skip trailing
|
|
|
+ while (i < msgLength && (msgContent[i] === ' ' || msgContent[i] === '\t'))
|
|
|
+ i++;
|
|
|
+ if (msgContent.substr(i, 4) === '>') {
|
|
|
+ quote = true;
|
|
|
+ i += 4;
|
|
|
}
|
|
|
- return false;
|
|
|
- } ,appendMod = function(mods) {
|
|
|
- if (!Object.keys(currentMods).length)
|
|
|
- return "";
|
|
|
- return '<span class="' +Object.keys(mods).join(' ') +'">';
|
|
|
- };
|
|
|
+ for (; i < msgLength; i++) {
|
|
|
+ var c = msgContent[i];
|
|
|
|
|
|
- // Skip trailing
|
|
|
- while (i < msgLength && (msgContent[i] === ' ' || msgContent[i] === '\t'))
|
|
|
- i++;
|
|
|
- if (msgContent.substr(i, 4) === '>') {
|
|
|
- quote = true;
|
|
|
- i += 4;
|
|
|
- }
|
|
|
- for (; i < msgLength; i++) {
|
|
|
- var c = msgContent[i];
|
|
|
+ if (c === '<') {
|
|
|
+ do {
|
|
|
+ _msgContent += msgContent[i++];
|
|
|
+ } while (msgContent[i -1] !== '>' && msgContent[i]);
|
|
|
+ i--;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!(currentMods[R.klass.msg.style.bold]) && c === '*' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
+ if (Object.keys(currentMods).length)
|
|
|
+ _msgContent += '</span>';
|
|
|
+ currentMods[R.klass.msg.style.bold] = true;
|
|
|
+ _msgContent += appendMod(currentMods);
|
|
|
+ } else if (!(currentMods[R.klass.msg.style.strike]) && c === '~' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
+ if (Object.keys(currentMods).length)
|
|
|
+ _msgContent += '</span>';
|
|
|
+ currentMods[R.klass.msg.style.strike] = true;
|
|
|
+ _msgContent += appendMod(currentMods);
|
|
|
+ } else if (!(currentMods[R.klass.msg.style.code]) && c === '`' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
+ if (Object.keys(currentMods).length)
|
|
|
+ _msgContent += '</span>';
|
|
|
+ currentMods[R.klass.msg.style.code] = true;
|
|
|
+ _msgContent += appendMod(currentMods);
|
|
|
+ } else if (!(currentMods[R.klass.msg.style.italic]) && c === '_' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
+ if (Object.keys(currentMods).length)
|
|
|
+ _msgContent += '</span>';
|
|
|
+ currentMods[R.klass.msg.style.italic] = true;
|
|
|
+ _msgContent += appendMod(currentMods);
|
|
|
+ } else {
|
|
|
+ var finalFound = false;
|
|
|
+ _msgContent += c;
|
|
|
|
|
|
- if (c === '<') {
|
|
|
- do {
|
|
|
- _msgContent += msgContent[i++];
|
|
|
- } while (msgContent[i -1] !== '>' && msgContent[i]);
|
|
|
- i--;
|
|
|
- continue;
|
|
|
+ do {
|
|
|
+ if ((currentMods[R.klass.msg.style.bold]) && c !== '*' && msgContent[i +1] === '*') {
|
|
|
+ delete currentMods[R.klass.msg.style.bold];
|
|
|
+ finalFound = true;
|
|
|
+ } else if ((currentMods[R.klass.msg.style.strike]) && c !== '~' && msgContent[i +1] === '~') {
|
|
|
+ delete currentMods[R.klass.msg.style.strike];
|
|
|
+ finalFound = true;
|
|
|
+ } else if ((currentMods[R.klass.msg.style.code]) && c !== '`' && msgContent[i +1] === '`') {
|
|
|
+ delete currentMods[R.klass.msg.style.code];
|
|
|
+ finalFound = true;
|
|
|
+ } else if ((currentMods[R.klass.msg.style.italic]) && c !== '_' && msgContent[i +1] === '_') {
|
|
|
+ delete currentMods[R.klass.msg.style.italic];
|
|
|
+ finalFound = true;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ c = msgContent[++i];
|
|
|
+ } while (i < msgLength);
|
|
|
+ if (finalFound)
|
|
|
+ _msgContent += '</span>' +appendMod(currentMods);
|
|
|
+ }
|
|
|
}
|
|
|
- if (!(currentMods[R.klass.msg.style.bold]) && c === '*' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
- if (Object.keys(currentMods).length)
|
|
|
- _msgContent += '</span>';
|
|
|
- currentMods[R.klass.msg.style.bold] = true;
|
|
|
- _msgContent += appendMod(currentMods);
|
|
|
- } else if (!(currentMods[R.klass.msg.style.strike]) && c === '~' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
- if (Object.keys(currentMods).length)
|
|
|
- _msgContent += '</span>';
|
|
|
- currentMods[R.klass.msg.style.strike] = true;
|
|
|
- _msgContent += appendMod(currentMods);
|
|
|
- } else if (!(currentMods[R.klass.msg.style.code]) && c === '`' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
- if (Object.keys(currentMods).length)
|
|
|
- _msgContent += '</span>';
|
|
|
- currentMods[R.klass.msg.style.code] = true;
|
|
|
- _msgContent += appendMod(currentMods);
|
|
|
- } else if (!(currentMods[R.klass.msg.style.italic]) && c === '_' && msgContent[i +1] && checkEnd(msgContent, i, c)) {
|
|
|
- if (Object.keys(currentMods).length)
|
|
|
- _msgContent += '</span>';
|
|
|
- currentMods[R.klass.msg.style.italic] = true;
|
|
|
- _msgContent += appendMod(currentMods);
|
|
|
- } else {
|
|
|
- var finalFound = false;
|
|
|
- _msgContent += c;
|
|
|
-
|
|
|
- do {
|
|
|
- if ((currentMods[R.klass.msg.style.bold]) && c !== '*' && msgContent[i +1] === '*') {
|
|
|
- delete currentMods[R.klass.msg.style.bold];
|
|
|
- finalFound = true;
|
|
|
- } else if ((currentMods[R.klass.msg.style.strike]) && c !== '~' && msgContent[i +1] === '~') {
|
|
|
- delete currentMods[R.klass.msg.style.strike];
|
|
|
- finalFound = true;
|
|
|
- } else if ((currentMods[R.klass.msg.style.code]) && c !== '`' && msgContent[i +1] === '`') {
|
|
|
- delete currentMods[R.klass.msg.style.code];
|
|
|
- finalFound = true;
|
|
|
- } else if ((currentMods[R.klass.msg.style.italic]) && c !== '_' && msgContent[i +1] === '_') {
|
|
|
- delete currentMods[R.klass.msg.style.italic];
|
|
|
- finalFound = true;
|
|
|
- } else {
|
|
|
- break;
|
|
|
- }
|
|
|
- c = msgContent[++i];
|
|
|
- } while (i < msgLength);
|
|
|
- if (finalFound)
|
|
|
- _msgContent += '</span>' +appendMod(currentMods);
|
|
|
+ if (!isObjectEmpty(currentMods)) {
|
|
|
+ // Should not append
|
|
|
+ console.warn("formatter warning");
|
|
|
+ _msgContent += '</span>';
|
|
|
}
|
|
|
+ if (quote)
|
|
|
+ msgContents[msgContentIndex] = '<span class="' +R.klass.msg.style.quote +'">' +_msgContent +'</span>';
|
|
|
+ else
|
|
|
+ msgContents[msgContentIndex] = _msgContent;
|
|
|
}
|
|
|
- if (!isObjectEmpty(currentMods)) {
|
|
|
- // Should not append
|
|
|
- console.warn("formatter warning");
|
|
|
- _msgContent += '</span>';
|
|
|
- }
|
|
|
- if (quote)
|
|
|
- msgContents[msgContentIndex] = '<span class="' +R.klass.msg.style.quote +'">' +_msgContent +'</span>';
|
|
|
- else
|
|
|
- msgContents[msgContentIndex] = _msgContent;
|
|
|
+ return msgContents.length === 0 ? "" : msgContents.join('<br/>');
|
|
|
}
|
|
|
- return msgContents.length === 0 ? "" : msgContents.join('<br/>');
|
|
|
-}
|
|
|
+})();
|
|
|
|