linux shell output to html

后端 未结 5 1809
时光说笑
时光说笑 2020-12-08 05:45

is there any way to convert bash output to html ? for example if I had some colorized output in bash ( something like htop ), how can I convert it to html tags ... ( somethi

相关标签:
5条回答
  • 2020-12-08 05:56

    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)

    0 讨论(0)
  • 2020-12-08 06:02

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

    0 讨论(0)
  • 2020-12-08 06:05

    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
    
    0 讨论(0)
  • 2020-12-08 06:09

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

    Both available here.

    0 讨论(0)
  • 2020-12-08 06:20

    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.

    0 讨论(0)
提交回复
热议问题