linux shell output to html

2020-01-30 07:00发布

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 ... ( something like this: <p style="color: red">some text</p>)

标签: html linux shell
3条回答
在下西门庆
2楼-- · 2020-01-30 07:38

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

查看更多
爷、活的狠高调
3楼-- · 2020-01-30 07:41

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
查看更多
劳资没心,怎么记你
4楼-- · 2020-01-30 07:50

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

Both available here.

查看更多
登录 后发表回答