Thursday, 21 April 2011

converting ANSI to HTML. How to convert to html the colored shell output

The main aim of this was able to put in html the ouptut of git log and diff.

Googling around I have found that Perl CPAN has the HTML::FromANSI module. Also, this module installs ansi2html which accepts input from stdin.  

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 from pixelbeat

Unfortunately the ls --color get properly converted to HTML but the git one not. No matter which script I use. Could it be bacause the color is defined in the config as color.ui=auto?

git diff HEAD master -- ensembl/sql/CVS/Tag | ansi2html -p  > ~/public_html/htdocs_dev/diff1.html

git diff HEAD master -- ensembl/sql/CVS/Tag | --bg=dark  > ~/public_html/htdocs_dev/diff2.html

Yes! the problem would be that I have in the configuration color.ui=auto because explicitly having --color in the command make it work:

$ git diff --color HEAD master -- ensembl/sql/CVS/Tag | --bg=dark > ~/public_html/htdocs_dev/diff3.html


Anonymous said...

libcaca can do this too with good UTF-8 support as well.

garu said...

Nice post, I'm sure this bit a lot of people too =)

The problem you had was due to git being clever and stripping colors from the output when piped to another command. This is pretty useful since some pagers (like "less") don't understand ANSI colors, so:

$ git diff --color HEAD HEAD^1 | less

becomes a huge mess to see, while:

$ git diff --color=auto HEAD HEAD^1 | less

shows up ok. Same for ls --color=auto. Even "ack" does that very same trick.

For those interested in the implementation, you just test if STDOUT (or STDERR) is opened to a tty, using the -t file test operator. Something like:

# colors only if we're not being piped

Pablo Marin-Garcia said...

thanks @garu for your comment, you are right, exept in the 'less' bit. There are some less-terminal-setup that are broken and this does not work, but in my linux systems 'less' accepts the color with a direct pipe or reading a file that have the redirected output. In this last case 'less' ask if I want to open a binary file, I say YES and less show the text with the colors correctly.

But as you say having the color=auto is a good thing and only if I really want the redirection with color I will add the --color option.

Pablo Marin-Garcia said...

@anonymous. Caca labs (despite its scatological naming tendency) has very nice tools like libcaca. But despite I was tempted to explore it, I had the impression that it was going to take me more time that I had for that task. So I tried only the ready made solutions. For example I used H::FA because it has the ready made script. When I have more time I will explore more possibilities and other libraries better. By the way do you have a ready made example using libcaca?

Raimund said...

Garu, less understands ansi colours. Try "less -r" (or -R) :)

Royce Souther said...

Great tool, thanks for sharing.