将 ANSI 转义(终端格式/颜色代码)转换为 HTML 的库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:36:14  来源:igfitidea点击:

A library to convert ANSI escapes (terminal formatting/color codes) to HTML

htmlterminal-coloransi-escape

提问by Myrddin Emrys

I'm looking for a code library that converts ANSI escape sequences into HTML color, via plain tags or CSS. For example, something that would convert this:

我正在寻找一个代码库,通过普通标签或 CSS 将 ANSI 转义序列转换为 HTML 颜色。例如,可以转换此内容的内容:

ESC[00mESC[01;34mbinESC[00m
ESC[01;34mcodeESC[00m
ESC[01;31mdropbox-lnx.x86-0.6.404.tar.gzESC[00m
ESC[00mfooESC[00m

Into this:

进入这个:

<span style="color:blue">bin</span>
<span style="color:blue">code</span>
<span style="color:red">dropbox-lnx.x86-0.6.404.tar.gz</span>
foo

Converting breaks into <br/> isn't necessary, it's just the escape codes that I don't know. I could hack it together myself, but I'd probably miss something important like underlines or mess up how background colors work. I'd rather just sit on top of someone else's code.

将中断转换为 <br/> 不是必需的,它只是我不知道的转义码。我可以自己把它拼凑起来,但我可能会错过一些重要的东西,比如下划线或弄乱背景颜色的工作方式。我宁愿坐在别人的代码之上。

Does such a tool (command line linux) or library (perl, python, or ruby preferably) exist?

是否存在这样的工具(命令行 linux)或库(最好是 perl、python 或 ruby​​)?

采纳答案by Pistos

回答by Alexander Matthes

ahais a C-language program, available in an Ubuntu package, at http://ziz.delphigl.com/tool_aha.phpor on github https://github.com/theZiz/aha, that takes an input with terminal colors by pipe or file and puts a (w3c conform) HTML-File in stdout. Example:

aha是一个 C 语言程序,在 Ubuntu 包中可用,位于http://ziz.delphigl.com/tool_aha.php或在 github https://github.com/theZiz/aha,它接受带有终端颜色的输入管道或文件,并在标准输出中放置一个(符合 w3c 的)HTML 文件。例子:

ls --color=always | aha > ls-output.htm

or

或者

ls --color=always | aha --black > ls-output.htm

for a terminal-like look with black background.

用于黑色背景的终端式外观。

Compile it by "make" and put it where ever you want.

通过“make”编译它并把它放在任何你想要的地方。

It would be great to get Feedback. ;-)

得到反馈会很棒。;-)

回答by Janus Troelsen

Mature Python library and command line tool which is still maintained: ralphbean/ansi2html

仍在维护的成熟 Python 库和命令行工具:ralphbean/ansi2html

Alternatively, for the Bourne shell: ansi2html.sh

或者,对于 Bourne shell:ansi2html.sh