GithubHelp home page GithubHelp logo

ansi-to-html's Introduction

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:

<html>
    <body>
        <pre style="background-color: black; overflow: auto; padding: 10px 15px; font-family: monospace;"
        ><?php echo $html ?></pre>
    </body>
</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:

<html>
    <head>
        <style>
            <?php echo $styles ?>

            .ansi_box { overflow: auto; padding: 10px 15px; font-family: monospace; }
        </style>
    </head>
    <body>
        <pre class="ansi_color_bg_black ansi_color_fg_white ansi_box"><?php echo $html ?></pre>
    </body>
</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:

<html>
    <head>
        <style>
            {# This is only need if the inline styling is disabled #}
            {{ ansi_css }}
        </style>
    </head>
    <body>
        {{ some_ansi_code|ansi_to_html }}
    </body>
</html>

ansi-to-html's People

Contributors

adam187 avatar cordoval avatar fabpot avatar jackbentley avatar lyrixx avatar skolodyazhnyy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.