Actions Status MetaCPAN Release

NAME

Text::ANSI::Highlight - syntax highlight for the terminal, by a highlighter of your choice

SYNOPSIS

use Text::ANSI::Highlight;

my $hl = Text::ANSI::Highlight->new('pandoc');

# texts with their languages, all at once
my($c, $perl) = $hl->highlight([ c => $text1 ], [ perl => $text2 ]);

# a file, by its name
print $hl->file('lib/Foo.pm') // $content;

# a Markdown text: the code in the fences, each by its language
print $hl->markdown($text) // $text;

# as a filter of the tee module of greple
greple -Mtee '&Text::ANSI::Highlight::filter' -- ...

VERSION

Version 0.03

DESCRIPTION

This module colors source code for the terminal, with ANSI sequences. It does not color by itself: it gives the text to a highlighter, a command which is installed, and takes the colors from it. The highlighter can be changed for another, and all of them are used in the same way.

What comes back is the text which was given, with colors, and nothing else: when the text a highlighter gives is not that of the input, the input is left as it is. A method returns undef for a text which is not colored, so that the caller shows the text it has:

print $hl->file($file) // $content;

A text is not colored when the highlighter does not know its language, when it is empty, when it has an escape character already, and when the command of the highlighter is not installed or fails.

HIGHLIGHTERS

The colors of pandoc and skylighting are the 16 colors of the terminal, without background by default.

STYLES

The colors are those of a style of the highlighter, which tells the color of the keywords, the comments, the strings and so on. A style is given by its name, or by a file.

A text is not colored when the highlighter does not have the style.

HIGHLIGHTER MODULES

A highlighter is a module Text::ANSI::Highlight::_name_ which has three functions: language(_name_) returns the name of a language as the highlighter calls it, or undef when it does not know it; highlight([ _language_, _text_ ], ...) returns the texts in colors, as many as given; and markdown() tells whether the highlighter knows Markdown. The style is in the variable $STYLE of its package while they are called. The rest is done here.

METHODS

FUNCTIONS

MARKDOWN AND SKYLIGHTING

The skylighting command colors a Markdown file by a definition of Markdown which finds the fences by itself, and it can take the rest of the file for the code of a fence: after a function of a shell script, or __DATA__ of Perl, in a code block (skylighting 0.15). This module finds the fences by itself, and gives the highlighter the code of each block, and the text around them in pieces which have no fence.

SEE ALSO

ansihighlight, the command.

Text::ANSI::Highlight::IO makes a program which reads files by <> read them in colors, as it is.

App::Greple::tee

SyntaxHighlight::Any is a common interface to highlighters as well.

Syntax::Kamelon and Syntax::Highlight::Engine::Kate color by the syntax definitions of Kate in Perl, and Syntax::SourceHighlight by the library of GNU Source-highlight, without running a command. Text::VimColor colors by Vim.

https://pandoc.org/, https://www.gnu.org/software/src-highlite/, https://github.com/jgm/skylighting

AUTHOR

Kazumasa Utashiro

LICENSE

Copyright © 2026 Kazumasa Utashiro.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.