자바스크립트 ansi2html() 구현

1 개요[ | ]

JavaScript ansi2html()
자바스크립트 ansi2html() 구현
<style>
.ansi30 { color: #3f3f3f; }
.ansi31 { color: #ff7a88; }
.ansi32 { color: #57bb8a; }
.ansi33 { color: #ffff66; }
.ansi34 { color: #82b1ff; }
.ansi35 { color: #cd00cd; }
.ansi36 { color: #99bbd7; }
.ansi37 { color: #e5e5e5; }
</style>
<script>
function ansi2html(s) {
  o = 0;
  t = c => {
    x = (o?'</span>':'')+(c?`<span class="ansi${c}">`:'');
    o = c;
    return x;
  }
  return s.replace('<','&lt;').replace(/[\u001b]\[(\d+;)?(\d+)m/g, function(){return t(1*arguments[2])}) + t();
}

traceback = `\u001b[0;31m---------------------------------------------------------------------------\u001b[0m
\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)
\u001b[0;32m<ipython-input-9-7c77553f6810>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mzeta\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m
\u001b[0;31mNameError\u001b[0m: name 'zeta' is not defined`;
converted = ansi2html(traceback);
document.write('BEFORE:');
document.write('<pre>'+traceback+'</pre>');
document.write('AFTER:');
document.write('<pre>'+converted+'</pre>');
</script>

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}