82

There are tools providing coloured output:

dwdiff -c File1 File2 # word level diff
grep --color=always # we all know this guy
...

The question is: How to convert their colored output of arbitrary program into coloured html file?

Other output formats might be suitable as well (LaTeX would be great). I think html is good starting point, as it's easy to convert it to other formats.

(For curious how to keep terminal colour codes, please follow answer: https://unix.stackexchange.com/a/10832/9689 ... | unbuffer command_with_colours arg1 arg2 | ... - tool unbuffer is part of expect )

  • 1
    If you like dwdiff, you might also like colordiff, a wrapper that colorizes diff's output. I've aliased diff=colordiff for years, never had any problems. – Jonathan Hartley Sep 13 '17 at 19:36

10 Answers10

56

The answer to this question is probably what you want.

It links to these tools, which do the conversion you're looking for:

user17591
  • 1,098
  • 12
    aha is also available in Ubuntu: sudo apt install aha. But note that some commands will suppress colors when the output is a pipe. So when sending output to aha, you may need to add options to your commands. For ls or grep it would be --color=always. – mivk Mar 28 '18 at 20:22
  • 3
    Also available for MacOS: brew install aha – Markus Dec 15 '20 at 13:21
  • 1
    for introducing me to aha ☺. Thanks. – Geremia Jan 10 '22 at 20:31
  • aha seems buggy, it isn't escaping html entities in the original text before converting it. – user10489 Apr 04 '23 at 11:33
31

Or just a shell script

https://github.com/pixelb/scripts/blob/master/scripts/ansi2html.sh

More details of this script http://pablomarin-garcia.blogspot.com/2011/04/converting-ansi-to-html-how-to-convert.html

aap
  • 103
Dreampuf
  • 411
  • Welcome to Unix & Linux Stack Exchange! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – slm Aug 29 '13 at 11:43
  • 1
    This is the only answer I could get to work on Amazon Linux 2 – David Blevins Apr 20 '21 at 03:14
  • "just" a shell script... 500 lines long, with a giant script for awk. Just why?! – Yarek T Jan 30 '24 at 14:50
  • @YarekT I'm doubting that you can re-implement it in fewer hundred lines of code. Maybe more than 800 hundred lines of Python code? https://github.com/pycontribs/ansi2html/blob/main/src/ansi2html/converter.py Or 12k lines of C code? https://github.com/theZiz/aha/blob/master/aha.c, or the best you can choose is 400 lines of Perl? https://fastapi.metacpan.org/source/NUFFIN/HTML-FromANSI-2.03/lib/HTML/FromANSI.pm – Dreampuf Feb 01 '24 at 01:23
  • I'm pretty sure that if 500 lines of Shell code can not make you comfortable, any of those are neither. But if you are happy with a pip install CLI from a nobody, we shouldn't talk. I'm not that guy who arrogantly thinks that these details should be comprehensively solved by a 3rd party library. – Dreampuf Feb 01 '24 at 01:23
  • Shell scripts, while useful in many contexts, have their limit. There are better languages that offer a bit more sanity when dealing with complicated problems. Props for solving this in a shell script at the time. I think collectively we have better tooling now (readable + testable + maintainable + nicely portable maybe?). Alas I don't have time, so not trying to take anything away from the answer, or solution. Hopefully that explains the my reaction from my previous comment. – Yarek T Feb 09 '24 at 09:37
  • Let's walk the walk. An HTML render for ANSI Escape Codes needs a parser and a rewriter at minimal implementation. These implementations I posted have the same logic. The only difference is the language's manipulation of string. Everyone is busy with their stuff, and it's easy to answer these "standard" answers. But please really think about it. – Dreampuf Feb 20 '24 at 04:01
12

ansi2html, on pypi.

One may derive immoderate amounts of enjoyment from piping regular shell output through lolcat then through ansi2html. Some thing like:

… | lolcat -f | ansi2html -ip
scruss
  • 314
8

You can try vim with AnsiEsc.vim plugin to view ANSI colors through escape codes, then redirect to standard output to vim - (make sure you've activated :syntax on). Then convert the file to HTML by vim command: :TOhtml. The generated HTML file should have coloured output.


To convert source code non-interactively into html, try the following command:

vim -E -s -c "let g:html_no_progress=1" -c "syntax on" -c "set ft=c" -c "runtime syntax/2html.vim" -cwqa myfile.c

Source: :help g:html_no_progress (part of :help TOhtml).

Note: You can use - instead of the myfile.c, to convert code from standard input.

kenorb
  • 20,988
  • This didn't work for me. It would help if you would explain the command. – Ken Ingram Jan 01 '19 at 03:12
  • This command has been explained in Vim manual, check by :help TOhtml or :help g:html_no_progress. – kenorb Jan 02 '19 at 10:26
  • The way you wrote that is confusing. Use AnsiEsc.vim plugin. Activate it with :syntax on, and then you can see the text properly styled in the ANSI formatting? After that, save the file as html using :TOhtml? – Ken Ingram Jan 03 '19 at 07:19
  • I got it to work, so your answer was the simplest, but understanding your instructions took more work than the solution. :-) – Ken Ingram Jan 03 '19 at 07:20
  • 1
    If you've any suggestions on how to improve the steps, feel free to improve. – kenorb Jan 03 '19 at 11:35
  • 2
    I offered them in my comment. If I understood the instruction correctly. I guess you're suggesting I edit the answer? Which is fine. I can do that. – Ken Ingram Jan 03 '19 at 20:40
4

You can use vim. This is part of a script I use to convert diff output to HTML.

vim -n \
    -c ':%s%^+  %+++    ' \
    -c ':%s%^-  %---    ' \
    -c ':%s%^   %       ' \
    -c ':set nu' \
    -c ':let html_use_css=1' \
    -c ':so $VIMRUNTIME/syntax/2html.vim' \
    -c ':wq' -c ':qa!' $input > /dev/null 2> /dev/null

After this, I run sed to change the CSS and the title to be exactly what I want them to be.

*Edit: I should have mentioned that to get color, you need to have syntax highlighting turned on. I do that in my .vimrc, but if you wanted to add it here it would just be another line like

-c ':syntax on' \
user17591
  • 1,098
  • Ok. So how would you like to convert output of arbitrary program, let's say : grep ? – Grzegorz Wierzowiecki Aug 08 '12 at 07:37
  • 1
    Ok, I didn't understand at first. If you want to convert ANSI color codes, then the answer here is probably what you want. If you just need a quick hack, then creating a sed script with a bunch of lines that replace ANSI codes with HTML would work. Something like 's,^[\[31m,,g s,^[\[m,,g' would be quick and dirty, but possibly sufficient. – user17591 Aug 08 '12 at 15:26
  • Could you @user17591 put this link as one more answer? I'd love to select it as answer to my question, and higher your reputation. – Grzegorz Wierzowiecki Aug 12 '12 at 19:31
  • Ok. I added a direct link to the Perl module so that any subsequent user will be able to find it with one less level of indirection, but also a link to the original question/answer for attribution. – user17591 Aug 13 '12 at 14:21
  • @user17591, nice solution! (+1) I just end-up adding a 2 year old vimshell followup variant of your solution! – JJoao Jan 26 '15 at 23:19
2

Update: I've added a perl method, using module HTML::FromANSI from CPAN .. To install it, just extract it to a directory, and run, as root: perl -MCPAN -e 'install HTML::FromANSI' from that directory. The particular feature to suit your request is a script called ansi2html. Interestingly enough, it shows the same loss-of-color after the overlaid k in the filenames, as does the elisp-shell script... Here is an example usage:

ls -l --color=always /bin/*k* | 
 grep --color=always "k\|x\|2010" |
  ansi2html  >/tmp/example.html
firefox /tmp/example.html

Here is the html output. as seen in Firefox:

enter image description here


Here is a method using an emacs elisp-shell script... for the example, called htmlize ...Emacs does not need to be running.

I originally tested it on a black background, but I noticed that for some reason, a white background doesn't play well with one of the introduced Escape Codes,\e[K, which seems to be ERASE_LINE (Erase the current line of terminal output). I've added a line to remove this Escape Code. It now works for a white background.

Here is an example of color highlighted output from ls being piped to grep for further color highlighting.

ls -l --color=always /bin/*k* | 
 grep --color=always "k\|x\|2010" >/tmp/example
htmlize /tmp/example
firefox /tmp/example.html

this is the elsip-shell script.

#!/bin/sh
":"; exec /usr/bin/emacs -Q --script "$0" -- "$@" # -*-emacs-lisp-*-
(require 'ansi-color) (require 'htmlize)
(find-file (setq path-in (cadr argv)))
(ansi-color-apply-on-region (point-min) (point-max))
(switch-to-buffer (buffer-name (htmlize-buffer)))
(write-file (concat path-in ".html"))

Here is a sample of the html output, in Firefox... If I get time, I'll look further into the overlaying ANSI codes issue. where the red ks overlap the green filenames, but that is only there because of a hurriedly chosen test regex for grep... (maybe that is something \e[K influences...

enter image description here

Peter.O
  • 32,916
2

As of 2021, there also is ansi2html which is avaliable in the Debian package "colorized-logs".

Hermann
  • 6,148
  • which is incompatible with/breaks the pypi version I mentioned earlier – scruss Jun 23 '21 at 16:57
  • I fail to see the point. https://github.com/kilobyte/colorized-logs and https://github.com/pycontribs/ansi2html are completely different projects. You may use one or the other. – Hermann Jun 23 '21 at 19:10
  • If you've previously been using the pypi ansi2html, then install the Debian package colorized-logs, it will replace the existing ansi2html command with an incompatible one. This was a problem for me, so I make sure that colorized-logs is blocked from my Debian systems – scruss Jun 24 '21 at 14:26
  • Strange. Executables installed by pip usually go into ~/.local/bin and apt installs to /usr/bin. – Hermann Jun 24 '21 at 22:35
2

There is simple and versatile npm command line tool ansi-to-html that can convert easily shell colored output to html.

git log --color | ansi-to-html --newline > ansi.html

There are options to change the foreground and background color also.

    -f, --fg         The background color used for resets (#000)
    -b, --bg         The foreground color used for resets (#FFF)

ansi-to-html npm module.

It can also be used without installing, with npx command.

git log --color | npx ansi-to-html --newline > ansi.html
Gagan
  • 131
  • 1
    This is a lifesaver. Just note if you don't explicitly add --color then piping will prevent coloring (git will assume pipelines need pure plaintext output). – Sridhar Sarnobat Dec 15 '23 at 21:12
1

For those who have a command that is written on the assumption that piped output will never have color (e.g. some testing frameworks) you may find it useful to use the script utility to first save the ansi output. This can then be pushed to aha or the other utilities mentioned.

1

This is just a followup of @user17591 solution -- vim scripting:

#!/usr/bin/vim -ns
:%s%^+  %+++
:%s%^-  %---
:%s%^   %
:set nu
:let html_use_css=1
:so $VIMRUNTIME/syntax/2html.vim
:wq
:qa!

chmod it and

Usage: htmlvim file (to produce file.html)

JJoao
  • 12,170
  • 1
  • 23
  • 45