Michel Gokan Khan
Michel Gokan Khan

Reputation: 2625

How to convert Linux's shell output to HTML?

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.

Upvotes: 22

Views: 29052

Answers (5)

mivk
mivk

Reputation: 15009

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.

Upvotes: 4

MacMartin
MacMartin

Reputation: 2866

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)

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 360355

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

Both available here.

Upvotes: 28

nes1983
nes1983

Reputation: 15756

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

Upvotes: 3

kusma
kusma

Reputation: 6658

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

Upvotes: 10

Related Questions