summaryrefslogtreecommitdiff
path: root/js/parse.js
diff options
context:
space:
mode:
authortimb <opuscule@gmail.com>2015-04-16 10:28:33 -0700
committertimb <opuscule@gmail.com>2015-04-16 10:28:33 -0700
commitcc4fb936a8d95b78ff71548d7b6919fedec0dc16 (patch)
tree412db88ae9a909cbc9b66ab13fec130d5b3d60af /js/parse.js
parente56f2ca44bdbf620b36829886399891f83cf3624 (diff)
import mirc/irssi colorcodes
Diffstat (limited to 'js/parse.js')
-rw-r--r--js/parse.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/js/parse.js b/js/parse.js
new file mode 100644
index 0000000..8697afd
--- /dev/null
+++ b/js/parse.js
@@ -0,0 +1,131 @@
+(function(){
+
+var char_color = '\x03';
+var regexp_color = /(^[\d]{1,2})?(?:,([\d]{1,2}))?/;
+
+var style_chars = {
+ '\x02': 'bold',
+ '\x1d': 'italic',
+ '\x1f': 'underline',
+ '\x0f': 'reset',
+ '\x16': 'inverse'
+};
+
+var Style = function(style){
+ this.b = style.b;
+ this.i = style.i;
+ this.u = style.u;
+ this.fg = style.fg;
+ this.bg = style.bg;
+};
+
+var style_fns = {};
+
+style_fns.bold = function(style){ style.b = !style.b };
+
+style_fns.italic = function(style){ style.i = !style.i };
+
+style_fns.underline = function(style){ style.u = !style.u };
+
+style_fns.inverse = function(style){
+ var tmp = style.fg;
+ style.fg = style.bg;
+ style.bg = tmp;
+};
+
+style_fns.reset = function(style, base_style){
+ style.b = base_style.b;
+ style.i = base_style.i;
+ style.u = base_style.u;
+ style.fg = base_style.fg;
+ style.bg = base_style.bg;
+};
+
+var colorcode_to_json = function(string, opts){
+ opts = opts || {};
+ var d = colorcode_to_json.defaults;
+
+ var base_style = {
+ b: "b" in opts ? opts.b : d.b,
+ i: "i" in opts ? opts.i : d.i,
+ u: "u" in opts ? opts.u : d.u,
+ fg: "fg" in opts ? opts.fg : d.fg,
+ bg: "bg" in opts ? opts.bg : d.bg
+ };
+
+ var lines_in = string.split(/\r?\n/);
+ var lines_out = [];
+ var w = 0, h = 0;
+
+ for (var i=0; i<lines_in.length; i++){
+ var line = lines_in[i];
+ if (line.length === 0) continue; // skip blank lines
+ var json_line = line_to_json(line, base_style);
+ if (w < json_line.length) w = json_line.length;
+ lines_out.push(json_line);
+ h++;
+ }
+
+ return {w:w, h:h, lines:lines_out};
+};
+
+colorcode_to_json.defaults = {
+ b: false
+, i: false
+, u: false
+, fg: 0
+, bg: 1
+};
+
+var line_to_json = function(line, base_style){
+ var out = [];
+ var pos = -1;
+ var len = line.length -1;
+ var char;
+ var style = new Style(base_style);
+
+ while (pos < len){ pos++;
+
+ char = line[pos];
+
+ // next char is a styling char
+ if (char in style_chars){
+ style_fns[style_chars[char]](style, base_style);
+ continue;
+ }
+
+ // next char is a color styling char, with possible color nums after
+ if (char === char_color){
+ var matches = line.substr(pos+1,5).match(regexp_color);
+
+ // \x03 without color code is a soft style reset
+ if (matches[1] === undefined && matches[2] === undefined) {
+ style.fg = base_style.fg;
+ style.bg = base_style.bg;
+ continue;
+ }
+
+ if (matches[1] !== undefined)
+ style.fg = Number(matches[1]);
+
+ if (matches[2] !== undefined)
+ style.bg = Number(matches[2]);
+
+ pos += matches[0].length;
+ continue;
+
+ }
+
+ // otherwise, next char is treated as normal content
+ var data = new Style(style);
+ //data.value = char;
+ data.value = char.charCodeAt(0);
+
+ out.push(data);
+ }
+ return out;
+};
+
+window.colorcode_to_json = colorcode_to_json;
+
+})();