From 24552b4b6de235e30179d7709251e1920596d730 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Fri, 16 Sep 2011 14:52:53 +0100 Subject: [PATCH] Text formatting (i.e. bold, underline, colour) --- js/front.js | 118 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 37 deletions(-) diff --git a/js/front.js b/js/front.js index b8c5ba0..619125d 100644 --- a/js/front.js +++ b/js/front.js @@ -635,7 +635,7 @@ kiwi.front = { return parseInt(channel.num_users, 10); }).reverse(); for (chan in chanList) { - body.append($('' + chanList[chan].channel + '' + chanList[chan].num_users + '' + chanList[chan].topic + '')); + body.append($('' + chanList[chan].channel + '' + chanList[chan].num_users + '' + kiwi.front.format(chanList[chan].topic) + '')); } tab.div.append(table); tab.div.css('overflow-y', 'scroll'); @@ -1090,16 +1090,10 @@ kiwi.front = { setTopicText: function (new_topic) { kiwi.front.original_topic = new_topic; - $('#kiwi .cur_topic .topic').text(new_topic); + $('#kiwi .cur_topic .topic').text(kiwi.front.format(new_topic)); kiwi.front.doLayoutSize(); }, - - - - - - nickStripPrefix: function (nick) { var tmp = nick, i, prefix; @@ -1203,7 +1197,85 @@ kiwi.front = { barsHide: function () { $('#kiwi .toolbars').slideUp(); $('#kiwi .control').slideUp(); + }, + + format: function (msg) { + var re; + + // bold + if (msg.indexOf(String.fromCharCode(2)) !== -1) { + next = ''; + while (msg.indexOf(String.fromCharCode(2)) !== -1) { + msg = msg.replace(String.fromCharCode(2), next); + next = (next === '') ? '' : ''; + } + if (next === '') { + msg = msg + ''; + } + } + + // underline + if (msg.indexOf(String.fromCharCode(31)) !== -1) { + next = ''; + while (msg.indexOf(String.fromCharCode(31)) !== -1) { + msg = msg.replace(String.fromCharCode(31), next); + next = (next === '') ? '' : ''; + } + if (next === '') { + msg = msg + ''; + } + } + + // colour + re = /\x03([0-9][0-5]?)(,([0-9][0-5]?))?(.*?)\x03/g; + + msg = msg.replace(re, function (str, p1, p2, p3, p4) { + var fg, bg, + col = function (num) { + switch (parseInt(num, 10)) { + case 0: + return '#FFFFFF'; + case 1: + return '#000000'; + case 2: + return '#000080'; + case 3: + return '#008000'; + case 4: + return '#FF0000'; + case 5: + return '#800040'; + case 6: + return '#800080'; + case 7: + return '#FF8040'; + case 8: + return '#FFFF00'; + case 9: + return '#80FF00'; + case 10: + return '#008080'; + case 11: + return '#00FFFF'; + case 12: + return '#0000FF'; + case 13: + return '#FF55FF'; + case 14: + return '#808080'; + case 15: + return '#C0C0C0'; + default: + return null; + } + }; + fg = col(p1); + bg = col(p3); + return '' + p4 + ''; + }); + return msg; } + }; @@ -1460,35 +1532,7 @@ Tabview.prototype.addMsg = function (time, nick, msg, type, style) { msg = ''; } - // Text formatting - // bold - if (msg.indexOf(String.fromCharCode(2)) !== -1) { - next = ''; - while (msg.indexOf(String.fromCharCode(2)) !== -1) { - msg = msg.replace(String.fromCharCode(2), next); - next = (next === '') ? '' : ''; - } - if (next === '') { - msg = msg + ''; - } - } - - // Wierd thing noticed by Dux0r on the irc.esper.net server - if (typeof msg !== "string") { - msg = ''; - } - - // underline - if (msg.indexOf(String.fromCharCode(31)) !== -1) { - next = ''; - while (msg.indexOf(String.fromCharCode(31)) !== -1) { - msg = msg.replace(String.fromCharCode(31), next); - next = (next === '') ? '' : ''; - } - if (next === '') { - msg = msg + ''; - } - } + msg = kiwi.front.format(msg); // Make the channels clickable re = new RegExp('\\B(' + kiwi.gateway.channel_prefix + '[^ ,.\\007]+)', 'g'); -- 2.25.1