pozemka / ANSIEsc2HTML

Converts ANSI escape sequences to HTML. Library and CLI program.
MIT License
3 stars 0 forks source link
ansi-escape-sequences html sgr-parameters

ANSI Esc to HTML

Library to convert limited part of SGR (Select Graphic Rendition) sequence of ANSI escape sequences to HTML.

Supported SGR parameters:

Limitations:

See more info on wiki.

Usage:

  1. include src/ansi_esc2html.h
  2. either
    • add src/ansi_esc2html.cpp to your project
    • or link against ansi_esc2html library
  3. Example:
    ANSI_SGR2HTML ansisgr2html;
    std::string ansi = "\x1b[48;5;141m background color \x1b[49m";
    std::string html = ansisgr2html.simpleParse(ansi);
    //or
    std::string html_strict = ansisgr2html.strictParse(ansi);

    result HTML will be:

    <body style="background-color:#111111;font-family:'Consolas','Droid Sans Mono',monospace; color:#eeeeee; white-space:pre"><span style="background-color:#af87ff"> background color </span></body>

Functions:

simpleParse

Can only disable SGR parameters in reverse order they where enabled. Terminal emulators can disable them in any order. See example on wiki

This function is suitable for ANSI code generated from markup languages like XML or Markdown.

strictParse

Can disable SGR parameters in any order. However it is ~40% slower than simpleParse.

Compilation:

This will produce release versions of static and dynamic libraries and test executable:

mkdir build
cd build/
cmake -DCMAKE_BUILD_TYPE=Release ..
  # or for Windows
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release .. 
make

Performance:

Tested on:

Results:

Build and test