跳转到主要内容

ANSI代码到HTML转换器

项目描述

特性

  • 可以作为模块或命令行工具使用。

  • 支持大多数‘m’代码(颜色和属性)。

  • 可以通过非常方便和强大的方式使用样式自定义外观。

    • ANSI属性映射到可样式的HTML类(ansi_yellowansi_bright…)

    • 具有相同ANSI属性集的文本序列被封装在具有这些类激活的单个span中。

    • 可以定义类或特定类组合的样式。

    • 可以根据封装容器定义样式,以便在单个文档中存在不同的样式。

  • 它已经过驱动开发并进行了前后测试。

作为Python模块的使用

  • deansi.styleSheet():返回可以定制的ANSI类的默认样式表。

  • deansi.deansi(consoleText):返回HTML转换

以下示例使用它们构建简单的控制台输出外观

import deansi

html_template = """\
<style>
.ansi_terminal {{ background-color: #222; color: #cfc; }}
{defaultStyle}
</style>
<div class="ansi_terminal">{ansiText}</div>
"""
ansiInput = "\033[31mHello World!!\033[m"

print html_template.format(
    defaultStyle = deansi.styleSheet(),
    ansiText = deansi.deansi(ansiInput),
    )

命令行使用

deansi可以作为基于管道的命令行工具使用。一个相当简单的用法可能是

$ ls --color | deansi.py > ls.html

此外,我们可以使用一些选项来修改其行为

$ deansy.py --help

usage: deansi.py [-h] [-s FILE] [-t FILE] [--dark] [INPUT_FILE] [OUTPUT_FILE]

Converts coloured console output into equivalent HTML

positional arguments:
  INPUT_FILE            the console input to convert (default stdin)
  OUTPUT_FILE           the file where to drop the html output (default
                        stdout)

optional arguments:
  -h, --help            show this help message and exit
  -s FILE, --style FILE
                        use FILE as stylesheet
  -t FILE, --template FILE
                        use FILE as html template
  --dark                use the dark background style

自定义样式表

默认样式表看起来像这样

.ansi_terminal { background-color: #222; color: #cfc; }
.ansi_terminal { white-space: pre; font-family: monospace; }
.ansi_black { color: black; }
.ansi_red { color: darkred; }
.ansi_green { color: darkgreen; }
.ansi_yellow { color: orange; }
.ansi_blue { color: darkblue; }
.ansi_magenta { color: purple; }
.ansi_cyan { color: darkcyan; }
.ansi_white { color: lightgray; }
.ansi_bright.ansi_black { color: gray; }
.ansi_bright.ansi_red { color: red; }
.ansi_bright.ansi_green { color: green; }
.ansi_bright.ansi_yellow { color: yellow; }
.ansi_bright.ansi_blue { color: blue; }
.ansi_bright.ansi_magenta { color: magenta; }
.ansi_bright.ansi_cyan { color: cyan; }
.ansi_bright.ansi_white { color: white; }
.ansi_bgblack { background-color: black; }
.ansi_bgred { background-color: red; }
.ansi_bggreen { background-color: green; }
.ansi_bgyellow { background-color: yellow; }
.ansi_bgblue { background-color: blue; }
.ansi_bgmagenta { background-color: magenta; }
.ansi_bgcyan { background-color: cyan; }
.ansi_bgwhite { background-color: white; }
.ansi_bright { font-weight: bold; }
.ansi_faint { opacity: .5; }
.ansi_italic { font-style: italic; }
.ansi_underscore { text-decoration: underline; }
.ansi_blink { text-decoration: blink; }
.ansi_reverse { border: 1pt solid; }
.ansi_hide { opacity: 0; }
.ansi_strike { text-decoration: line-through; }

由于CSS的级联行为,任何默认样式规则之后的样式规则将覆盖这些默认样式。例如,如果您想更改当ansi亮色属性应用时的黄色颜色,而不是应用粗体字体,您可以这样说:

.ansi_yellow.ansi_bright { color: #FF7; font-weight: inherit; }

如果您想在同一个HTML中使用多个行为,可以使用如下CSS魔法:

.my_own_ansi_enviroment .ansi_inverse { font-style: italic; border: none; }

项目详情


下载文件

下载适用于您平台的应用文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

deansi-1.2.tar.gz (5.8 kB 查看哈希值)

上传时间: 源代码

构建分发

deansi-1.2.linux-i686.tar.gz (7.7 kB 查看哈希值)

上传时间: 源代码

由以下支持