diff --git a/ansi_up.js b/ansi_up.js index 564c3cc..0cb72d5 100644 --- a/ansi_up.js +++ b/ansi_up.js @@ -40,7 +40,7 @@ PALETTE_COLORS; function Ansi_Up() { - this.fg = this.bg = null; + this.fg = this.bg = this.fg_truecolor = this.bg_truecolor = null; this.bright = 0; } @@ -191,7 +191,28 @@ } } } - // } else if(mode === '2' && num.length >= 3) { // true color (not supported yet) + } else if(mode === '2' && nums.length >= 3) { // true color + var r = parseInt(nums.shift()); + var g = parseInt(nums.shift()); + var b = parseInt(nums.shift()); + if ((r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)) { + var color = r + ', ' + g + ', ' + b; + if (!use_classes) { + if (is_foreground) { + self.fg = color; + } else { + self.bg = color; + } + } else { + if (is_foreground) { + self.fg = 'ansi-truecolor'; + self.fg_truecolor = color; + } else { + self.bg = 'ansi-truecolor'; + self.bg_truecolor = color; + } + } + } } } })(); @@ -201,10 +222,27 @@ if ((self.fg === null) && (self.bg === null)) { return orig_txt; } else { - var styles = classes = []; + var styles = []; + var classes = []; + var data = {}; + var render_data = function (data) { + var fragments = []; + var key; + for (key in data) { + if (data.hasOwnProperty(key)) { + fragments.push('data-' + key + '="' + this.escape_for_html(data[key]) + '"'); + } + } + return fragments.length > 0 ? ' ' + fragments.join(' ') : ''; + }; + if (self.fg) { if (use_classes) { classes.push(self.fg + "-fg"); + if (self.fg_truecolor !== null) { + data['ansi-truecolor-fg'] = self.fg_truecolor; + self.fg_truecolor = null; + } } else { styles.push("color:rgb(" + self.fg + ")"); } @@ -212,14 +250,18 @@ if (self.bg) { if (use_classes) { classes.push(self.bg + "-bg"); + if (self.bg_truecolor !== null) { + data['ansi-truecolor-bg'] = self.bg_truecolor; + self.bg_truecolor = null; + } } else { styles.push("background-color:rgb(" + self.bg + ")"); } } if (use_classes) { - return "" + orig_txt + ""; + return '' + orig_txt + ''; } else { - return "" + orig_txt + ""; + return '' + orig_txt + ''; } } }; diff --git a/test/ansi_up-test.js b/test/ansi_up-test.js index 9c5cd47..fc1f6e1 100644 --- a/test/ansi_up-test.js +++ b/test/ansi_up-test.js @@ -274,6 +274,27 @@ describe('ansi_up', function() { l.should.eql(expected); }); }); + + describe('transform extend colors (true color)', function() { + it('foreground', function() { + var start = "\033[38;2;42;142;242m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start); + l.should.eql(expected); + }); + it('background', function() { + var start = "\033[48;2;42;142;242m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start); + l.should.eql(expected); + }); + it('both foreground and background', function() { + var start = "\033[38;2;42;142;242;48;2;1;2;3m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start); + l.should.eql(expected); + }); + }); }); describe('themed colors', function() { @@ -392,6 +413,27 @@ describe('ansi_up', function() { l.should.eql(expected); }); }); + + describe('transform extend colors (true color)', function() { + it('foreground', function() { + var start = "\033[38;2;42;142;242m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start, {use_classes: true}); + l.should.eql(expected); + }); + it('background', function() { + var start = "\033[48;2;42;142;242m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start, {use_classes: true}); + l.should.eql(expected); + }); + it('both foreground and background', function() { + var start = "\033[38;2;42;142;242;48;2;1;2;3m" + "foo" + "\033[0m"; + var expected = 'foo'; + var l = ansi_up.ansi_to_html(start, {use_classes: true}); + l.should.eql(expected); + }); + }); }); describe('ignore unsupported CSI', function() { it('should correctly convert a string similar to CSI', function() {