php 转 ansi,ANSI to HTML5:ANSI到HTML5的转化库

ANSI to HTML5 Converter

This small library only does one thing: converting a text containing ANSI codes to an HTML5 fragment:

require_once __DIR__.'/vendor/autoload.php';

use SensioLabs\AnsiConverter\AnsiToHtmlConverter;

$converter = new AnsiToHtmlConverter();

$html = $converter->convert($ansi);

The $ansi variable should contain a text with ANSI codes, and $html will contain the converted HTML5 version of it.

You can then output the HTML5 fragment in any HTML document:

 
 

><?php echo $html?>

The converter supports different color themes:

use SensioLabs\AnsiConverter\Theme\SolarizedTheme;

$theme = new SolarizedTheme();

$converter = new AnsiToHtmlConverter($theme);

By default, the colors are inlined into the HTML, but you can also use classes by turning off style inlining:

$converter = new AnsiToHtmlConverter($theme, false);

And the asCss() method of the theme object lets you retrieve the theme styles as a CSS snippet:

$styles = $theme->asCss();

which you can then use in your HTML document:

.ansi_box { overflow: auto; padding: 10px 15px; font-family: monospace; }

<?phpecho $html?>

Twig Integration

Register the extension:

use SensioLabs\AnsiConverter\Bridge\Twig\AnsiExtension;

$twig->addExtension(AnsiExtension());

It's possible to use a custom AnsiToHtmlConverter:

use SensioLabs\AnsiConverter\Bridge\Twig\AnsiExtension;

use SensioLabs\AnsiConverter\Theme\SolarizedTheme;

$theme = new SolarizedTheme();

$converter = new AnsiToHtmlConverter($theme, false);

$twig->addExtension(AnsiExtension($converter));

Then:

{# This is only need if the inline styling is disabled #}

{{ ansi_css }}

{{ some_ansi_code|ansi_to_html }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?php /*  * This file is part of ansi-to-html.  *  * (c) 2013 Fabien Potencier  *  * For the full copyright and license information, please view the LICENSE  * file that was distributed with this source code.  */ namespace SensioLabs\AnsiConverter; use SensioLabs\AnsiConverter\Theme\Theme; /**  * Converts an ANSI text to HTML5.  */ class AnsiToHtmlConverter {     protected $theme;     protected $charset;     protected $inlineStyles;     protected $inlineColors;     protected $colorNames;     public function __construct(Theme $theme = null, $inlineStyles = true, $charset = 'UTF-8')     {         $this->theme = null === $theme ? new Theme() : $theme;         $this->inlineStyles = $inlineStyles;         $this->charset = $charset;         $this->inlineColors = $this->theme->asArray();         $this->colorNames = array(             'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white',             '', '',             'brblack', 'brred', 'brgreen', 'bryellow', 'brblue', 'brmagenta', 'brcyan', 'brwhite',         );ANSI是一种字符代码,为使计算机支持更多语言,通常使用 0x00~0x7f 范围的1 个字节来表示 1 个英文字符。超出此范围的使用0x80~0xFFFF来编码,即扩展的ASCII编码。为使计算机支持更多语言,通常使用 0x80~0xFFFF 范围的 2 个字节来表示 1 个字符。比如:汉字 '中' 在ANSI编码ANSI编码中文操作系统中,使用 [0xD6,0xD0] 这两个字节存储。不同的国家和地区制定了不同的标准,由此产生了 GB2312、GBK、GB18030、Big5、Shift_JIS 等各自的编码标准。这些使用多个字节来代表一个字符的各种汉字延伸编码方式,称为 ANSI 编码。在简体中文Windows操作系统中,ANSI 编码代表 GBK 编码;在繁体中文Windows操作系统中,ANSI编码代表Big5;在日文Windows操作系统中,ANSI 编码代表 Shift_JIS 编码。不同 ANSI 编码之间互不兼容,当信息在国际间交流时,无法将属于两种语言的文字,存储在同一段 ANSI 编码的文本中。ANSI编码表示英文字符时用一个字节,表示中文用两个或四个字节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值