93

I'm looking for a code library that converts ANSI escape sequences into HTML color, via plain tags or CSS. For example, something that would convert this:

ESC[00mESC[01;34mbinESC[00m
ESC[01;34mcodeESC[00m
ESC[01;31mdropbox-lnx.x86-0.6.404.tar.gzESC[00m
ESC[00mfooESC[00m

Into this:

<span style="color:blue">bin</span>
<span style="color:blue">code</span>
<span style="color:red">dropbox-lnx.x86-0.6.404.tar.gz</span>
foo

Converting breaks into <br/> isn't necessary, it's just the escape codes that I don't know. I could hack it together myself, but I'd probably miss something important like underlines or mess up how background colors work. I'd rather just sit on top of someone else's code.

Does such a tool (command line linux) or library (perl, python, or ruby preferably) exist?

Myrddin Emrys
  • 42,126
  • 11
  • 38
  • 51
  • 1
    Great question and a valuable set of answers! – cxw Sep 22 '16 at 14:55
  • 1
    [ANSI4J](https://github.com/PavelKastornyy/ansi4j) is a Java library that parses ANSI functions and can generate CSS declarations. So, we have separately text and CSS styles for this text. – Pavel_K Mar 28 '22 at 07:56

3 Answers3

106

aha is a C-language program, available in an Ubuntu package, at http://ziz.delphigl.com/tool_aha.php or on github https://github.com/theZiz/aha, that takes an input with terminal colors by pipe or file and puts a (w3c conform) HTML-File in stdout. Example:

ls --color=always | aha > ls-output.htm

or

ls --color=always | aha --black > ls-output.htm

for a terminal-like look with black background.

Compile it by "make" and put it where ever you want.

It would be great to get Feedback. ;-)

Highway of Life
  • 22,803
  • 16
  • 52
  • 80
Alexander Matthes
  • 1,069
  • 2
  • 7
  • 2
  • 2
    IcanDivideBy0 missed something: the "german link" in Alexander Matthes' post remains valid. The orig post has an erroneous ' ' before "ziz" in it; use http://ziz.delphigl.com/tool_aha.php Further, Alexander's link points to: - source at http://ziz.delphigl.com/data/aha-src-0.4.4.tar.gz, and - other debian packages at http: //packages.debian.org/search?keywords=aha *(remove the space (' ') added after ':', as I don't have priveleges to include more than 2 links per post.)* – Der Schley Jan 19 '12 at 16:06
  • 7
    There is an ubuntu package for aha – Maxim Razin Feb 16 '13 at 00:08
  • 1
    Just wanted to note for others that the github repo for this app compiles and runs without problem on os x. :) – masukomi Apr 09 '13 at 18:59
  • yep, compiled with a pretty oddball version of gcc that i have to use at work. nice and simple. – gcb Apr 19 '13 at 22:55
  • 3
    I just added a [Homebrew Pull Request for aha](https://github.com/Homebrew/homebrew/pull/26921). If it gets accepted, installing aha will be a simple `brew update && brew install aha` on Macs. – Nightscape Feb 22 '14 at 18:20
  • Exactly what I needed for a project! – Jeff Mar 14 '14 at 14:55
  • 2
    Seems to handle only 16 colors, as far as I can tell. – Gringo Suave May 09 '15 at 18:26
24

Mature Python library and command line tool which is still maintained: pycontribs/ansi2html

Alternatively, for the Bourne shell: ansi2html.sh

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
  • 2
    "Inspired by and developed off of the work of pixelbeat and blackjack". I like this one better, and the -i option is great :) – DavidGamba Jan 16 '14 at 18:44
  • Great! Both handle 256 colors too. Upvoted. – jamadagni Dec 31 '15 at 03:54
  • The pixelbeat [ansi2html.sh](https://github.com/pixelb/scripts/blob/master/scripts/ansi2html.sh) worked nicely for me! – cxw Sep 22 '16 at 14:54
  • Not limited to Bourne anymore and deb, yum, apt packages available: https://pypi.python.org/pypi/ansi2html/1.0.7 – hobs Oct 06 '16 at 22:20
17

There seems to be an HTML::FromANSI Perl module.

cjm
  • 61,471
  • 9
  • 126
  • 175
Pistos
  • 23,070
  • 14
  • 64
  • 77
  • 10
    this module installs ansi2html that you can pipe input. `ls --color | ansi2html -p > my_web_page.html` `ls --color | ansi2html > my_snpipet_code-no_header-footer.html` BUT I prefer the default output from [ansi2html.sh from pixelbeat](http://www.pixelbeat.org/scripts/ansi2html.sh) – Pablo Marin-Garcia Apr 21 '11 at 15:57