sensiolabs / ansi-to-html Goto Github PK
View Code? Open in Web Editor NEWAn ANSI to HTML5 converter
License: MIT License
An ANSI to HTML5 converter
License: MIT License
Hi, I did a git clone of the project. How do I integrate it into my project? with the composer autoloader?
I get 'Uncaught error: Class 'AnsiToHtmlConverter' not found when I run it.
As I found out the very hard way (half of my output not showing and an hour of debugging), some programs throw a color change in their output between a CR and a LF. I.e. [CR][ESC][31m[LF]
AnsiToHtmlConverter.php erases a line followd by [CR][color change][LF] since it thinks the color change is text at the beginning of a new line so it "overwrites" (deletes) the old line. It isn't Terminals like Putty will handle this correctly.
Proposed fix: for me this change in AnsiToHtmlConverter.php (line 50) worked.
$text = preg_replace('#^.*\r((\x9B|\x1B\[)[0-?]*[ -\/]*[@-~])*(?!\n)#ms', '', $text);
Probably the regex can be a bit simpler (I included all ANSI codes in the regex, that's overkill. I took it from https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python#33925425 ).
I came across a weird behavior while using the library - if background color (e.g. 42 - green) is specified before foreground (e.g. 30 - black) the render will contain black text on black background. Weirdly enough this happens only for separated sequences (e.g. ^[[0m^[[42m^[[30m
) and not while parsing their short forms (e.g. ^[[42;30m
).
I used following example to demonstrate the problem (also attached):
Download raw
^[[30;42mWORKS1^[[0m
^[[0m^[[42m^[[30mDOESNT1^[[0m
^[[0m^[[30m^[[42mWORKS2^[[0m
^[[42;30mWORKS1^[[0m
Rendering in GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
HTML from ansi-to-html v1.1.3
<?php
$output = (new AnsiToHtmlConverter())->convert($test);
<span style="background-color: green; color: black">WORKS1</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: black; color: black">DOESNT1</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: green; color: white">WORKS2</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: green; color: black">WORKS1</span><span style="background-color: black; color: white"><br />
</span>
I tried to shorten the output by defining the css prefix as 'ac', but realized that it will never work due to the fact that the converter has ansi_color_ hardcoded and there is no connection between the Theme and the Converter.
$converter = new AnsiToHtmlConverter();
$converter->convert('some random cli output'); // returns classes with "ansi_color" prefix
$converter->getTheme()->asCss('css_prefix_for_ansi_color'); // returns styles with given prefix
If you try to render <fg=yellow;href=https://github.com/>github</>
for instance, the html render is broken.
<?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\Theme;
/**
* Hacker theme.
*
* @see http://ethanschoonover.com/solarized
*/
class HackerTheme extends Theme
{
public function asArray()
{
return array(
// normal
'black' => '#000000',
'red' => '#800000',
'green' => '#008000',
'yellow' => '#808000',
'blue' => '#000080',
'magenta' => '#800080',
'cyan' => '#008080',
'white' => '#808080',
// bright
'brblack' => '#606060',
'brred' => '#FF0000',
'brgreen' => '#00FF00',
'bryellow' => '#FFFF00',
'brblue' => '#0000FF',
'brmagenta' => '#FF00FF',
'brcyan' => '#00FFFF',
'brwhite' => '#FFFFFF',
);
}
}
Hello,
Is there any chance to see a 1.1.1 release including 3f08b01 or should I stick with dev-master ?
My command prints a bunch of lines using writeln
function, but the ansi-to-html converter does not make separate lines out of them.
Is there a way to achieve this?
In convert() method, if detect \r, erase the entire line :
replace:
$text = preg_replace('#^.*\r(?!\n)#m', '', $text);
by:
$text = preg_replace('#^(.*)\r(?!\n)#m', '$1', $text);
Console display :
$ ./app/console project:ota:update
Check if new firmware is available...
Found embeded Firmware for Xxxxx on 0.2.6 (206)
But actualy Html display :
<span style="background-color: black;color: yellow;display: block;">Check if new firmware is available...</span><span style="background-color: black;color: white;display: block;"></span><span style="background-color: black;color: yellow;display: block;">Found embeded Firmware for Xxxxx (heater) on 0.2.6 (206)</span><span style="background-color: black; color: white"></span>
Html should be display :
<span style="background-color: black;color: yellow;">Check if new firmware is available...</span><br/><span style="background-color: black;color: yellow;">Found embeded Firmware for Xxxxx (heater) on 0.2.6 (206)</span>
my code :
In ContainerAwareCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<comment>Check if new firmware is available...</comment>');
$output->writeln("<comment>Found Alfred embeded Firmware for $board on $ver ($version)</comment>");
}
In Controller
public function scanAction()
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true );
... Call my command ...
// return the output
$converter = new AnsiToHtmlConverter(); //new SolarizedTheme(), true);
$content = $converter->convert($output->fetch());
$this->addFlash('sonata_flash_success', "$content");
}
With :
I thing in the README.md the following line :
$twig->addExtension(AnsiExtension($converter));
should be corriged to :
$twig->addExtension(new AnsiExtension($converter));
AnsiExtension
is a class and not a function
Here's the fix:
foreach ($options as $option) {
if ($option >= 30 && $option < 38) {
$fg = $option - 30;
} elseif ($option >= 40 && $option < 48) {
$bg = $option - 40;
} elseif ($option >= 90 && $option < 98) { // This needs to be added
$fg = $option - 80; // This needs to be added
} elseif (39 == $option) {
...
}
For one of my project, I had to create a twig extension.
Do you want a PR?
<?php
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
class AnsiExtension extends \Twig_Extension
{
private $converter;
public function __construct(AnsiToHtmlConverter $converter = null)
{
$this->converter = $converter ?: new AnsiToHtmlConverter();
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('ansi_to_html', array($this, 'ansiToHtml'), array('is_safe' => array('html'))),
);
}
public function ansiToHtml($string)
{
return $this->converter->convert($string);
}
public function getName()
{
return 'ansi';
}
}
It would be nice to add a twig function ansi_css
that will ouput the inline CSS.
But to do that, we need AnsiToHtmlConverter::getTheme
string = '� �� ��[KGi0/0/0/8 admin-down admin-down '
Output after replacing = in-down admin-down (Gi0/0/0/8 is lost)
[31] => Array
(
[0] => color
[1] => KGi0/0/0/8 ad
)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.