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

血红的双手。 提交于 2019-11-26 12:35:14

问题


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:

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.

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


回答1:


There seems to be an HTML::FromANSI Perl module.




回答2:


aha is a C-language program, available in an Ubuntu package, at http://ziz.delphigl.com/tool_aha.php or 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:

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.

It would be great to get Feedback. ;-)




回答3:


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

Alternatively, for the Bourne shell: ansi2html.sh



来源:https://stackoverflow.com/questions/245121/a-library-to-convert-ansi-escapes-terminal-formatting-color-codes-to-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!