| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- /**
- * replace all :emoji: codes with corresponding image
- * @param {string} inputString
- * @return {string}
- **/
- function formatEmojis(inputString) {
- return inputString.replace(/:([^ \t:]+):/g, function(returnFailed, emoji) {
- var emojiDom = makeEmojiDom(emoji);
- if (emojiDom) {
- var domParent = document.createElement("span");
- domParent.className = returnFailed === inputString ? R.klass.emoji.medium : R.klass.emoji.small;
- domParent.appendChild(emojiDom);
- return domParent.outerHTML;
- }
- return returnFailed;
- });
- }
- /** @type {function(string):string} */
- var formatText = (function() {
- /**
- * @constructor
- * @param {string} fullText
- */
- function MessagePart(fullText) {
- /** @type {Array.<MessagePart>} */
- this.subParts = [];
- /** @type {string} */
- this.text = "";
- /** @type {string} */
- this.fullText = fullText;
- /** @type {boolean} */
- this.italic = false;
- /** @type {boolean} */
- this.bold = false;
- /** @type {boolean} */
- this.strike = false;
- /** @type {boolean} */
- this.code = false;
- /** @type {boolean} */
- this.longCode = false;
- /** @type {boolean} */
- this.quote = false;
- }
- 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++;
- }
- return false;
- }
- MessagePart.prototype.isFinished = function() {
- var result = false;
- if (this.longCode)
- return this.text.substr(this.text.length -3) === '```';
- if (this.code)
- return this.text[this.text.length -1] === '`' && this.text.length > 1;
- if (this.bold)
- result |= this.text[this.text.length -1] === '*' && this.text.length > 1;
- if (this.strike)
- result |= this.text[this.text.length -1] === '~' && this.text.length > 1;
- if (this.italic)
- result |= this.text[this.text.length -1] === '_' && this.text.length > 1;
- if (this.quote)
- result |= this.text[this.text.length -1] === '\n';
- return result || this.isOnlyText();
- };
- MessagePart.prototype.addChar = function(c) {
- this.text += c;
- return this;
- };
- MessagePart.prototype.isOnlyText = function() {
- return !this.italic &&
- !this.bold &&
- !this.strike &&
- !this.code &&
- !this.longCode &&
- !this.quote;
- };
- /**
- * @return {MessagePart}
- **/
- MessagePart.prototype.finalize = function() {
- if (this.longCode)
- this.text = this.text.substr(0, this.text.length -3);
- else if (this.code)
- this.text = this.text.substr(0, this.text.length -1);
- else if (this.bold)
- this.text = this.text.substr(0, this.text.length -1);
- else if (this.strike)
- this.text = this.text.substr(0, this.text.length -1);
- else if (this.italic)
- this.text = this.text.substr(0, this.text.length -1);
- else if (this.quote)
- this.text = this.text.substr(0, this.text.length -1);
- return this;
- };
- MessagePart.prototype.cloneType = function() {
- var clone = new MessagePart(this.fullText);
- clone.italic = this.italic;
- clone.bold = this.bold;
- clone.strike = this.strike;
- clone.code = this.code;
- clone.longCode = this.longCode;
- clone.quote = this.quote;
- return clone;
- };
- /**
- * @param {boolean=} skipFirst
- **/
- MessagePart.prototype.formatText = function(skipFirst) {
- var lastChild = new MessagePart(this.fullText);
- for (var i =skipFirst === true ? 1 : 0, textLength =this.fullText.length; i < textLength; i++) {
- //First, check if we have a ``` code block
- if (!lastChild.longCode && this.fullText.substr(i, 3) === '```') {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i));
- lastChild.longCode = true;
- i += 2;
- } else if (!lastChild.longCode && !lastChild.code && this.fullText[i] === '`') {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i));
- lastChild.code = true;
- } else if (!lastChild.longCode && !lastChild.italic && this.fullText[i] === '_') {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i));
- lastChild.italic = true;
- } else if (!lastChild.longCode && !lastChild.bold && this.fullText[i] === '*') {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i));
- lastChild.bold = true;
- } else if (!lastChild.longCode && !lastChild.strike && this.fullText[i] === '~') {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i));
- lastChild.strike = true;
- } else {
- lastChild.addChar(this.fullText[i]);
- if (!lastChild.isOnlyText() && lastChild.isFinished()) {
- this.subParts.push(lastChild.finalize());
- lastChild = new MessagePart(this.fullText.substr(i +1));
- }
- }
- }
- if (lastChild.isFinished()) {
- this.subParts.push(lastChild.finalize());
- } else {
- // Unterminated sequence
- var textChild = new MessagePart(lastChild.fullText[0]);
- textChild.text = lastChild.fullText[0];
- this.subParts.push(textChild.finalize());
- lastChild.formatText(true);
- this.subParts.push(lastChild);
- }
- /*
- var _msgContent = ""
- ,currentMods = {}
- ,quote = false
- ,i =0
- var msgContent = this.text.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];
- }
- 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;
- }
- return '<a href="' +sub[0] +'" class="' +sub[2] +'"' +(!type ? ' target="_blank"' : '') +'>' +sub[1] +'</a>';
- });
- msgContent = formatEmojis(msgContent);
- var msgLength = msgContent.length,
- 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;
- }
- 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;
- 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;
- */
- };
- MessagePart.prototype.getOuterClass = function() {
- var classList = '';
- if (this.longCode) {
- classList += ' ' +R.klass.msg.style.longCode;
- }
- if (this.bold) {
- classList += ' ' +R.klass.msg.style.bold;
- }
- if (this.code) {
- classList += ' ' +R.klass.msg.style.code;
- }
- if (this.italic) {
- classList += ' ' +R.klass.msg.style.italic;
- }
- if (this.strike) {
- classList += ' ' +R.klass.msg.style.strike;
- }
- if (this.quote) {
- classList += ' ' +R.klass.msg.style.quote;
- }
- return classList;
- };
- /** @return {string} */
- MessagePart.prototype.getInnerHTML = function() {
- return this.text
- .replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ' ') // replace trailing spaces par non-secable spaces
- .replace(/\n/g, '<br/>');
- };
- /** @return {string} */
- MessagePart.prototype.toHTML = function() {
- if (this.subParts.length) {
- var result = "";
- this.subParts.forEach(function(part) {
- result += part.toHTML();
- });
- return result;
- } else {
- if (this.isOnlyText() && !this.text.length)
- return "";
- return '<span class="' +this.getOuterClass() +'">' +this.getInnerHTML() +'</span>';
- }
- };
- return function (fullText) {
- // trivial empty string
- var text = (fullText || "").trim();
- if (text == "")
- return "";
- var msgContent = new MessagePart(text);
- msgContent.formatText();
- console.log(msgContent);
- return msgContent.toHTML();
- }
- })();
|