github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/public/static/js/ansi_up.js (about)

     1  // ansi_up.js
     2  // version : 1.1.0
     3  // author : Dru Nelson
     4  // license : MIT
     5  // http://github.com/drudru/ansi_up
     6  
     7  (function (Date, undefined) {
     8  
     9      var ansi_up,
    10          VERSION = "1.1.0",
    11  
    12          // check for nodeJS
    13          hasModule = (typeof module !== 'undefined'),
    14  
    15          // Normal and then Bright
    16          ANSI_COLORS = [
    17            [
    18              { color: "0, 0, 0",        class: "ansi-black"   },
    19              { color: "187, 0, 0",      class: "ansi-red"     },
    20              { color: "0, 187, 0",      class: "ansi-green"   },
    21              { color: "187, 187, 0",    class: "ansi-yellow"  },
    22              { color: "0, 0, 187",      class: "ansi-blue"    },
    23              { color: "187, 0, 187",    class: "ansi-magenta" },
    24              { color: "0, 187, 187",    class: "ansi-cyan"    },
    25              { color: "255,255,255",    class: "ansi-white"   }
    26            ],
    27            [
    28              { color: "85, 85, 85",     class: "ansi-bright-black"   },
    29              { color: "255, 85, 85",    class: "ansi-bright-red"     },
    30              { color: "0, 255, 0",      class: "ansi-bright-green"   },
    31              { color: "255, 255, 85",   class: "ansi-bright-yellow"  },
    32              { color: "85, 85, 255",    class: "ansi-bright-blue"    },
    33              { color: "255, 85, 255",   class: "ansi-bright-magenta" },
    34              { color: "85, 255, 255",   class: "ansi-bright-cyan"    },
    35              { color: "255, 255, 255",  class: "ansi-bright-white"   }
    36            ]
    37          ];
    38  
    39      function Ansi_Up() {
    40        this.fg = this.bg = null;
    41        this.bright = 0;
    42      }
    43  
    44      Ansi_Up.prototype.escape_for_html = function (txt) {
    45        return txt.replace(/[&<>]/gm, function(str) {
    46          if (str == "&") return "&amp;";
    47          if (str == "<") return "&lt;";
    48          if (str == ">") return "&gt;";
    49        });
    50      };
    51  
    52      Ansi_Up.prototype.linkify = function (txt) {
    53        return txt.replace(/(http|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:\/~\+#]*[\w\-\@?^=%&amp;\/~\+#])?/ig, function(str) {
    54          return "<a href=\"" + str + "\">" + str + "</a>";
    55        });
    56      };
    57  
    58      Ansi_Up.prototype.ansi_to_html = function (txt, options) {
    59  
    60        var data4 = txt.split(/\033\[/);
    61  
    62        var first = data4.shift(); // the first chunk is not the result of the split
    63  
    64        var self = this;
    65        var data5 = data4.map(function (chunk) {
    66          return self.process_chunk(chunk, options);
    67        });
    68  
    69        data5.unshift(first);
    70  
    71        var flattened_data = data5.reduce( function (a, b) {
    72          if (Array.isArray(b))
    73            return a.concat(b);
    74  
    75          a.push(b);
    76          return a;
    77        }, []);
    78  
    79        var escaped_data = flattened_data.join('');
    80  
    81        return escaped_data;
    82      };
    83  
    84      Ansi_Up.prototype.process_chunk = function (text, options) {
    85  
    86        // Are we using classes or styles?
    87        options = typeof options == 'undefined' ? {} : options;
    88        var use_classes = typeof options.use_classes != 'undefined' && options.use_classes;
    89        var key = use_classes ? 'class' : 'color';
    90  
    91        // Do proper handling of sequences (aka - injest vi split(';') into state machine
    92        //match,codes,txt = text.match(/([\d;]+)m(.*)/m);
    93        var matches = text.match(/([\d;]*)m([^]*)/m);
    94  
    95        if (!matches) return text;
    96  
    97        var orig_txt = matches[2];
    98        var nums = matches[1].split(';');
    99  
   100        var self = this;
   101        nums.map(function (num_str) {
   102  
   103          var num = parseInt(num_str);
   104  
   105          if (isNaN(num) || num === 0) {
   106            self.fg = self.bg = null;
   107            self.bright = 0;
   108          } else if (num === 1) {
   109            self.bright = 1;
   110          } else if ((num >= 30) && (num < 38)) {
   111            self.fg = ANSI_COLORS[self.bright][(num % 10)][key];
   112          } else if ((num >= 40) && (num < 48)) {
   113            self.bg = ANSI_COLORS[0][(num % 10)][key];
   114          }
   115        });
   116  
   117        if ((self.fg === null) && (self.bg === null)) {
   118          return orig_txt;
   119        } else {
   120          var styles = classes = [];
   121          if (self.fg) {
   122            if (use_classes) {
   123              classes.push(self.fg + "-fg");
   124            } else {
   125              styles.push("color:rgb(" + self.fg + ")");
   126            }
   127          }
   128          if (self.bg) {
   129            if (use_classes) {
   130              classes.push(self.bg + "-bg");
   131            } else {
   132              styles.push("background-color:rgb(" + self.bg + ")");
   133            }
   134          }
   135          if (use_classes) {
   136            return ["<span class=\"" + classes.join(' ') + "\">", orig_txt, "</span>"];
   137          } else {
   138            return ["<span style=\"" + styles.join(';') + "\">", orig_txt, "</span>"];
   139          }
   140        }
   141      };
   142  
   143      // Module exports
   144      ansi_up = {
   145  
   146        escape_for_html: function (txt) {
   147          var a2h = new Ansi_Up();
   148          return a2h.escape_for_html(txt);
   149        },
   150  
   151        linkify: function (txt) {
   152          var a2h = new Ansi_Up();
   153          return a2h.linkify(txt);
   154        },
   155  
   156        ansi_to_html: function (txt, options) {
   157          var a2h = new Ansi_Up();
   158          return a2h.ansi_to_html(txt, options);
   159        },
   160  
   161        ansi_to_html_obj: function () {
   162          return new Ansi_Up();
   163        }
   164      };
   165  
   166      // CommonJS module is defined
   167      if (hasModule) {
   168          module.exports = ansi_up;
   169      }
   170      /*global ender:false */
   171      if (typeof window !== 'undefined' && typeof ender === 'undefined') {
   172          window.ansi_up = ansi_up;
   173      }
   174      /*global define:false */
   175      if (typeof define === "function" && define.amd) {
   176          define("ansi_up", [], function () {
   177              return ansi_up;
   178          });
   179      }
   180  })(Date);