NAME
Syntax::Highlight::Basic::Output::Ansi - Render highlighted code with ANSI terminal colors
SYNOPSIS
use Syntax::Highlight::Basic::Parser;
use Syntax::Highlight::Basic::Output::Ansi;
my $parser = Syntax::Highlight::Basic::Parser->new(language => 'perl');
my $tokens = $parser->parse('if ($x) { print "hello"; }');
my $output = Syntax::Highlight::Basic::Output::Ansi->new();
my $string = $output->convert($tokens);
print $string; # outputs ANSI-colored text to terminal
DESCRIPTION
Syntax::Highlight::Basic::Output::Ansi converts parser token output into a string with embedded ANSI 256-color escape codes. Each non-whitespace token is wrapped in \e[38;5;NNNm ... \e[0m sequences.
Bold groups (keywords, statements, types) use \e[1;38;5;NNNm to combine bold with color.
No HTML escaping is needed since the output is intended for terminal display.
CONSTRUCTOR
new(%options)
Creates a new Output::Ansi instance.
Options:
colors-
A hash reference of group name to color entry overrides. Keys are Vim group names. Values are hash references with two fields:
User-provided colors take precedence over the built-in defaults.
Valid group names (sub-groups take priority over parent groups):
Sub-groups:
String,Character,Number,Boolean,Float,Function,Conditional,Repeat,Label,Operator,Keyword,Exception,Include,Define,Macro,PreCondit,StorageClass,Structure,Typedef,Tag,SpecialChar,Delimiter,SpecialComment,DebugParent groups:
Comment,Constant,Identifier,Statement,PreProc,Type,Special,Underlined,Error,TodoExample:
my $output = Syntax::Highlight::Basic::Output::Ansi->new( colors => { Comment => { color => 245, bold => 0 }, Keyword => { color => 33, bold => 1 }, }, );
METHODS
convert($parse_result)
Converts parser token output to an ANSI-colored string.
Parameter $parse_result is the array reference returned by Syntax::Highlight::Basic::Parser::parse().
Returns a string with embedded ANSI escape codes suitable for terminal display.
COLOR MAPPING
The following default ANSI 256-color indices are used:
String,Character,Special→ 67 (blue-ish)Number,Float,Constant→ 71 (green-ish)Boolean,Conditional,Repeat,Keyword,Exception,StorageClass,Structure,Typedef,Tag,Statement→ 28 bold (green bold)Function→ 33 (light blue)Label→ 136 (olive)Operator,Delimiter→ 102 (gray)Include,Define,Macro,PreCondit,PreProc→ 130 (brown)SpecialChar→ 130 bold (brown bold)SpecialComment,Debug→ 160 bold (red bold)Comment,Todo→ 73 (teal)Identifier→ 33 (light blue)Type→ 88 (dark red)Underlined→ 4 (blue)Error→ 196 (bright red)
VERSION
0.1.0
AUTHOR
Syntax::Highlight::Basic Contributors
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 196:
Non-ASCII character seen before =encoding in '—'. Assuming UTF-8