19

Is there any easy way to convert bash output to HTML? For example, if I have some colorized output in bash (something like htop), how can I convert it to HTML, with corresponding stylings/tags/css/etc.

Michel Gokan Khan
  • 2,525
  • 3
  • 30
  • 54
  • 1
    What would the conversion results look like? There is no recognizable structure here that could be converted into HTML tags. What exactly are you trying to achieve? – Pekka Jan 09 '10 at 12:08
  • Your data seems to be full of ANSI control sequences. Is that intentional? –  Jan 09 '10 at 12:09
  • 2
    Related: http://stackoverflow.com/questions/245121/a-library-to-convert-ansi-escapes-terminal-formatting-color-codes-to-html – Tobu Jan 09 '10 at 12:20
  • If you are using Konsole terminal emulator you can File -> Save Output As... then choose File type: HTML document, to save everything to HTML – Almir Jan 24 '23 at 13:53

5 Answers5

25

There's ansifilter plus some tools like highlight will produce colorized html from plain text such as source files.

Both available here.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
9

Yes, you need to pipe the result through a tool like ansi2html.

kusma
  • 6,516
  • 2
  • 22
  • 26
3

Without any pretty-printing, the simplest thing you can always do is to escape everything that needs escaping, and wrap a basic HTML shell around (the following should be valid minimal HTML5). For example, get a hold of fastesc: http://raa.ruby-lang.org/project/fastesc/, and that wrap it into an HTML shell.

If you want to preserve the ANSI magic, then you need to convert that to HTML, perhaps with http://ansi-sys.rubyforge.org/

And then do something like this, depending on your needs:

require 'ansisys'


def ansi_escape(string)
    terminal = AnsiSys::Terminal.new
    terminal.echo(string)
    terminal.render 
end

def to_html(string)
    %Q{ <!DOCTYPE html>
        <title>Converted to html</title>
        <pre>
        #{ansi_escape(string)}
        </pre>
    } 
end
nes1983
  • 15,209
  • 4
  • 44
  • 64
3

As suggested in @eli's answer, you can use script to capture the colored output to a file.

You can then convert the output to HTML with aha, the "Ansi HTML Adapter", which should be available in your distribution's repositories: apt install aha or yum install aha for Debian-based or Redhat-based distributions.

If you want to capture only a single command, use the -c option:

script -c "grep --color ..."

This will save the output to a file named typescript in your current directory.

To save to a different file, add the file name at the end:

script -c "grep --color ..." my_grep_colored_output

See man script for other options.

To capture several commands from an interactive session, just start script without a command, and enter Ctrl-d to exit script when done.

To just view the file in color, use less -R.

To convert it to HTML with aha :

aha -s -f typescript > output.html

or

aha -s < typescript > output.html

The -s option makes it write a style sheet in the html header instead of using inline styles through the file. That makes it easier to change colors if some background/foreground combinations turn out hard to read in a browser.

mivk
  • 13,452
  • 5
  • 76
  • 69
2

1.) check that ansifilter and script programs are installed

2.) script -c 'ansible-playbook -i /tmp/or/some /tmp/other/command.yml' -O /tmp/myTypescriptOutput.file

(script writes the output of a command to a file in typescript notation)

3.) convert to html, e.g. to view with your browser:

ansifilter -i /tmp/myTypescriptOutput.file -H -o output.html

convert to rtf, e.g. to view with libreoffice or word

ansifilter -i /tmp/myTypescriptOutput.file -R -o output.rtf

see man ansifilter for other options (latex, tex, svg, pangot, txt, ...)


(additional, if highlight is installed:

highlight /tmp/myTypescriptOutput.file --force

... this will print the saved-shell output in color, like if it was executed at first time)

MacMartin
  • 2,366
  • 1
  • 24
  • 27