I'm fetching output from the exec()
function and I would like to have some syntax highlighting in the results.
Current output is raw:
* [35mmanu[m/etc/init.d/mast: line 105: /var/log/mast/mast-all.log: Permission denied
Text such as [35m
are color highlighting in shell context.
I want to do it in HTML, I already have a colored shell script.
The question A library to convert ANSI escapes (terminal formatting/color codes) to HTML has an answer to this question.
aha is a Ansi to HTML Adapter written in C. It's available in an Ubuntu package and on github theZiz/aha.
My code is then simply:
exec("$command | aha", $output, $exitCode);
foreach($output as $k => $line) {
if ($line == '1') { continue; }
echo "$line";
}
aha takes SGR-colored Input and prints W3C conform HTML-Code.
aha reads the Input from a file or stdin and writes HTML-Code to stdout.
There is some nice options:
--black
, -b
: Black Background and white "standard color"--word-wrap
, -w
: Wrap long lines in the html file. This works with CSS3 supporting browsers as well as many older ones.--no-header
, -n
: Don't include header into generated HTML, useful for inclusion in full HTML files.