app/assets/javascripts/ansi_up.js in tailog-0.4.3 vs app/assets/javascripts/ansi_up.js in tailog-0.4.5

- old
+ new

@@ -1,326 +1,6 @@ // ansi_up.js // version : 1.3.0 // author : Dru Nelson // license : MIT // http://github.com/drudru/ansi_up - -(function (Date, undefined) { - - var ansi_up, - VERSION = "1.3.0", - - // check for nodeJS - hasModule = (typeof module !== 'undefined'), - - // Normal and then Bright - ANSI_COLORS = [ - [ - { color: "27, 27, 27", 'class': "ansi-black" }, - { color: "182, 80, 47", 'class': "ansi-red" }, - { color: "141, 161, 88", 'class': "ansi-green" }, - { color: "220, 175, 95", 'class': "ansi-yellow" }, - { color: "126, 170, 199", 'class': "ansi-blue" }, - { color: "176, 101, 152", 'class': "ansi-magenta" }, - { color: "141, 220, 217", 'class': "ansi-cyan" }, - { color: "217, 217, 217", 'class': "ansi-white" } - ], [ - { color: "27, 27, 27", 'class': "ansi-bright-black" }, - { color: "182, 80, 47", 'class': "ansi-bright-red" }, - { color: "141, 161, 88", 'class': "ansi-bright-green" }, - { color: "220, 175, 95", 'class': "ansi-bright-yellow" }, - { color: "126, 170, 199", 'class': "ansi-bright-blue" }, - { color: "176, 101, 152", 'class': "ansi-bright-magenta" }, - { color: "141, 220, 217", 'class': "ansi-bright-cyan" }, - { color: "217, 217, 217", 'class': "ansi-bright-white" } - ] - ], - - // 256 Colors Palette - PALETTE_COLORS; - - function Ansi_Up() { - this.fg = this.bg = this.fg_truecolor = this.bg_truecolor = null; - this.bright = 0; - } - - Ansi_Up.prototype.setup_palette = function() { - PALETTE_COLORS = []; - // Index 0..15 : System color - (function() { - var i, j; - for (i = 0; i < 2; ++i) { - for (j = 0; j < 8; ++j) { - PALETTE_COLORS.push(ANSI_COLORS[i][j]['color']); - } - } - })(); - - // Index 16..231 : RGB 6x6x6 - // https://gist.github.com/jasonm23/2868981#file-xterm-256color-yaml - (function() { - var levels = [0, 95, 135, 175, 215, 255]; - var format = function (r, g, b) { return levels[r] + ', ' + levels[g] + ', ' + levels[b] }; - var r, g, b; - for (r = 0; r < 6; ++r) { - for (g = 0; g < 6; ++g) { - for (b = 0; b < 6; ++b) { - PALETTE_COLORS.push(format.call(this, r, g, b)); - } - } - } - })(); - - // Index 232..255 : Grayscale - (function() { - var level = 8; - var format = function(level) { return level + ', ' + level + ', ' + level }; - var i; - for (i = 0; i < 24; ++i, level += 10) { - PALETTE_COLORS.push(format.call(this, level)); - } - })(); - }; - - Ansi_Up.prototype.escape_for_html = function (txt) { - return txt.replace(/[&<>]/gm, function(str) { - if (str == "&") return "&amp;"; - if (str == "<") return "&lt;"; - if (str == ">") return "&gt;"; - }); - }; - - Ansi_Up.prototype.linkify = function (txt) { - return txt.replace(/(https?:\/\/[^\s]+)/gm, function(str) { - return "<a href=\"" + str + "\">" + str + "</a>"; - }); - }; - - Ansi_Up.prototype.ansi_to_html = function (txt, options) { - return this.process(txt, options, true); - }; - - Ansi_Up.prototype.ansi_to_text = function (txt) { - var options = {}; - return this.process(txt, options, false); - }; - - Ansi_Up.prototype.process = function (txt, options, markup) { - var self = this; - var raw_text_chunks = txt.split(/\033\[/); - var first_chunk = raw_text_chunks.shift(); // the first chunk is not the result of the split - - var color_chunks = raw_text_chunks.map(function (chunk) { - return self.process_chunk(chunk, options, markup); - }); - - color_chunks.unshift(first_chunk); - - return color_chunks.join(''); - }; - - Ansi_Up.prototype.process_chunk = function (text, options, markup) { - - // Are we using classes or styles? - options = typeof options == 'undefined' ? {} : options; - var use_classes = typeof options.use_classes != 'undefined' && options.use_classes; - var key = use_classes ? 'class' : 'color'; - - // Each 'chunk' is the text after the CSI (ESC + '[') and before the next CSI/EOF. - // - // This regex matches four groups within a chunk. - // - // The first and third groups match code type. - // We supported only SGR command. It has empty first group and 'm' in third. - // - // The second group matches all of the number+semicolon command sequences - // before the 'm' (or other trailing) character. - // These are the graphics or SGR commands. - // - // The last group is the text (including newlines) that is colored by - // the other group's commands. - var matches = text.match(/^([!\x3c-\x3f]*)([\d;]*)([\x20-\x2c]*[\x40-\x7e])([\s\S]*)/m); - - if (!matches) return text; - - var orig_txt = matches[4]; - var nums = matches[2].split(';'); - - // We currently support only "SGR" (Select Graphic Rendition) - // Simply ignore if not a SGR command. - if (matches[1] !== '' || matches[3] !== 'm') { - return orig_txt; - } - - if (!markup) { - return orig_txt; - } - - var self = this; - - while (nums.length > 0) { - var num_str = nums.shift(); - var num = parseInt(num_str); - - if (isNaN(num) || num === 0) { - self.fg = self.bg = null; - self.bright = 0; - } else if (num === 1) { - self.bright = 1; - } else if (num == 39) { - self.fg = null; - } else if (num == 49) { - self.bg = null; - } else if ((num >= 30) && (num < 38)) { - self.fg = ANSI_COLORS[self.bright][(num % 10)][key]; - } else if ((num >= 90) && (num < 98)) { - self.fg = ANSI_COLORS[1][(num % 10)][key]; - } else if ((num >= 40) && (num < 48)) { - self.bg = ANSI_COLORS[0][(num % 10)][key]; - } else if ((num >= 100) && (num < 108)) { - self.bg = ANSI_COLORS[1][(num % 10)][key]; - } else if (num === 38 || num === 48) { // extend color (38=fg, 48=bg) - (function() { - var is_foreground = (num === 38); - if (nums.length >= 1) { - var mode = nums.shift(); - if (mode === '5' && nums.length >= 1) { // palette color - var palette_index = parseInt(nums.shift()); - if (palette_index >= 0 && palette_index <= 255) { - if (!use_classes) { - if (!PALETTE_COLORS) { - self.setup_palette.call(self); - } - if (is_foreground) { - self.fg = PALETTE_COLORS[palette_index]; - } else { - self.bg = PALETTE_COLORS[palette_index]; - } - } else { - var klass = (palette_index >= 16) - ? ('ansi-palette-' + palette_index) - : ANSI_COLORS[palette_index > 7 ? 1 : 0][palette_index % 8]['class']; - if (is_foreground) { - self.fg = klass; - } else { - self.bg = klass; - } - } - } - } 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; - } - } - } - } - } - })(); - } - } - - if ((self.fg === null) && (self.bg === null)) { - return orig_txt; - } else { - 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 + ")"); - } - } - 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 '<span class="' + classes.join(' ') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>'; - } else { - return '<span style="' + styles.join(';') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>'; - } - } - }; - - // Module exports - ansi_up = { - - escape_for_html: function (txt) { - var a2h = new Ansi_Up(); - return a2h.escape_for_html(txt); - }, - - linkify: function (txt) { - var a2h = new Ansi_Up(); - return a2h.linkify(txt); - }, - - ansi_to_html: function (txt, options) { - var a2h = new Ansi_Up(); - return a2h.ansi_to_html(txt, options); - }, - - ansi_to_text: function (txt) { - var a2h = new Ansi_Up(); - return a2h.ansi_to_text(txt); - }, - - ansi_to_html_obj: function () { - return new Ansi_Up(); - } - }; - - // CommonJS module is defined - if (hasModule) { - module.exports = ansi_up; - } - /*global ender:false */ - if (typeof window !== 'undefined' && typeof ender === 'undefined') { - window.ansi_up = ansi_up; - } - /*global define:false */ - if (typeof define === "function" && define.amd) { - define("ansi_up", [], function () { - return ansi_up; - }); - } -})(Date); +!function(r,t){function n(){this.fg=this.bg=this.fg_truecolor=this.bg_truecolor=null,this.bright=0}var o,s,e="undefined"!=typeof module,i=[[{color:"27, 27, 27","class":"ansi-black"},{color:"182, 80, 47","class":"ansi-red"},{color:"141, 161, 88","class":"ansi-green"},{color:"220, 175, 95","class":"ansi-yellow"},{color:"126, 170, 199","class":"ansi-blue"},{color:"176, 101, 152","class":"ansi-magenta"},{color:"141, 220, 217","class":"ansi-cyan"},{color:"217, 217, 217","class":"ansi-white"}],[{color:"27, 27, 27","class":"ansi-bright-black"},{color:"182, 80, 47","class":"ansi-bright-red"},{color:"141, 161, 88","class":"ansi-bright-green"},{color:"220, 175, 95","class":"ansi-bright-yellow"},{color:"126, 170, 199","class":"ansi-bright-blue"},{color:"176, 101, 152","class":"ansi-bright-magenta"},{color:"141, 220, 217","class":"ansi-bright-cyan"},{color:"217, 217, 217","class":"ansi-bright-white"}]];n.prototype.setup_palette=function(){s=[],function(){var r,t;for(r=0;2>r;++r)for(t=0;8>t;++t)s.push(i[r][t].color)}(),function(){var r,t,n,o=[0,95,135,175,215,255],e=function(r,t,n){return o[r]+", "+o[t]+", "+o[n]};for(r=0;6>r;++r)for(t=0;6>t;++t)for(n=0;6>n;++n)s.push(e.call(this,r,t,n))}(),function(){var r,t=8,n=function(r){return r+", "+r+", "+r};for(r=0;24>r;++r,t+=10)s.push(n.call(this,t))}()},n.prototype.escape_for_html=function(r){return r.replace(/[&<>]/gm,function(r){return"&"==r?"&amp;":"<"==r?"&lt;":">"==r?"&gt;":t})},n.prototype.linkify=function(r){return r.replace(/(https?:\/\/[^\s]+)/gm,function(r){return'<a href="'+r+'">'+r+"</a>"})},n.prototype.ansi_to_html=function(r,t){return this.process(r,t,!0)},n.prototype.ansi_to_text=function(r){var t={};return this.process(r,t,!1)},n.prototype.process=function(r,t,n){var o=this,s=r.split(/\033\[/),e=s.shift(),i=s.map(function(r){return o.process_chunk(r,t,n)});return i.unshift(e),i.join("")},n.prototype.process_chunk=function(r,n,o){n=t===n?{}:n;var e=t!==n.use_classes&&n.use_classes,l=e?"class":"color",a=r.match(/^([!\x3c-\x3f]*)([\d;]*)([\x20-\x2c]*[\x40-\x7e])([\s\S]*)/m);if(!a)return r;var c=a[4],u=a[2].split(";");if(""!==a[1]||"m"!==a[3])return c;if(!o)return c;for(var f=this;u.length>0;){var g=u.shift(),h=parseInt(g);isNaN(h)||0===h?(f.fg=f.bg=null,f.bright=0):1===h?f.bright=1:39==h?f.fg=null:49==h?f.bg=null:h>=30&&38>h?f.fg=i[f.bright][h%10][l]:h>=90&&98>h?f.fg=i[1][h%10][l]:h>=40&&48>h?f.bg=i[0][h%10][l]:h>=100&&108>h?f.bg=i[1][h%10][l]:(38===h||48===h)&&!function(){var r=38===h;if(u.length>=1){var t=u.shift();if("5"!==t||u.length<1){if("2"===t&&u.length>=3){var n=parseInt(u.shift()),o=parseInt(u.shift()),l=parseInt(u.shift());if(!(0>n||n>255||0>o||o>255||0>l||l>255)){var a=n+", "+o+", "+l;e?r?(f.fg="ansi-truecolor",f.fg_truecolor=a):(f.bg="ansi-truecolor",f.bg_truecolor=a):r?f.fg=a:f.bg=a}}}else{var c=parseInt(u.shift());if(c>=0&&255>=c)if(e){var g=16>c?i[c>7?1:0][c%8]["class"]:"ansi-palette-"+c;r?f.fg=g:f.bg=g}else s||f.setup_palette.call(f),r?f.fg=s[c]:f.bg=s[c]}}}()}if(null===f.fg&&null===f.bg)return c;var p=[],_=[],b={},v=function(r){var t,n=[];for(t in r)r.hasOwnProperty(t)&&n.push("data-"+t+'="'+this.escape_for_html(r[t])+'"');return n.length>0?" "+n.join(" "):""};return f.fg&&(e?(_.push(f.fg+"-fg"),null!==f.fg_truecolor&&(b["ansi-truecolor-fg"]=f.fg_truecolor,f.fg_truecolor=null)):p.push("color:rgb("+f.fg+")")),f.bg&&(e?(_.push(f.bg+"-bg"),null!==f.bg_truecolor&&(b["ansi-truecolor-bg"]=f.bg_truecolor,f.bg_truecolor=null)):p.push("background-color:rgb("+f.bg+")")),e?'<span class="'+_.join(" ")+'"'+v.call(f,b)+">"+c+"</span>":'<span style="'+p.join(";")+'"'+v.call(f,b)+">"+c+"</span>"},o={escape_for_html:function(r){var t=new n;return t.escape_for_html(r)},linkify:function(r){var t=new n;return t.linkify(r)},ansi_to_html:function(r,t){var o=new n;return o.ansi_to_html(r,t)},ansi_to_text:function(r){var t=new n;return t.ansi_to_text(r)},ansi_to_html_obj:function(){return new n}},e&&(module.exports=o),"undefined"!=typeof window&&"undefined"==typeof ender&&(window.ansi_up=o),"function"==typeof define&&define.amd&&define("ansi_up",[],function(){return o})}(Date);