NAME

Parse::ANSIColor::Tiny - Determine attributes of ANSI-Colored string

VERSION

version 0.302

SYNOPSIS

# output from some command
my $output = "foo\e[31mbar\e[00m";

my $ansi = Parse::ANSIColor::Tiny->new();
my $marked = $ansi->parse($output);

is_deeply
  $marked,
  [
    [ [], 'foo' ],
    [ ['red'], 'bar' ],
  ],
  'parse colored string';

# don't forget to html-encode the string!
my $html = join '',
  '<div>',
  (map { '<span class="' . join(' ', @{ $_->[0] }) . '">' . h($_->[1]) . '</span>' } @$marked),
  '</div>';

is $html,
  '<div><span class="">foo</span><span class="red">bar</span></div>',
  'turned simple ansi into html';

DESCRIPTION

Parse a string colored with ANSI escape sequences into a structure suitable for reformatting (into HTML, for example).

The output of terminal commands can be marked up with colors and formatting that in some instances you'd like to preserve.

This module is essentially the inverse of Term::ANSIColor. The array refs returned from "parse" can be passed back in to Term::ANSIColor::colored. The strings may not match exactly due to different ways the attributes can be specified, but the end result should be colored the same.

This is a ::Tiny module... it attempts to be correct for most cases with a small amount of code. It may not be 100% correct, especially in complex cases. It only handles the m escape sequence (\033[0m) which produces colors and simple attributes (bold, underline) (like what can be produced with Term::ANSIColor).

If you do find bugs please submit tickets (with patches, if possible).

METHODS

new

Constructor.

Takes a hash or hash ref of arguments though currently no options are defined :-)

identify

my @names = $parser->identify('1;31');
  # or $parser->identify('1', '31');
# returns ('bold', 'red')

Identifies attributes by their number; Returns a list of names.

This is similar to uncolor() in Term::ANSIColor.

Unknown codes will be ignored (remove from the output):

$parser->identify('33', '52');
# returns ('yellow') # drops the '52'

normalize

my @norm = $parser->normalize(@attributes);

Takes a list of named attributes (like those returned from "identify") and reduces the list to only those that would have effect.

  • Duplicates will be removed

  • a foreground color will overwrite any previous foreground color (and the previous ones will be removed)

  • same for background colors

  • clear will remove all previous attributes

my @norm = $parser->normalize(qw(red bold green));
# returns ('bold', 'green');

parse

my $marked = $parser->parse($output);

Parse the provided string and return an array ref of array refs describing the formatting:

# [
#   [ [], 'plain words' ],
#   [ ['red'], 'colored words' ],
# [

These array refs are consistent with the arguments to colored() in Term::ANSIColor:

Term::ANSIColor::colored( ['red'], 'colored words' );

FUNCTIONS

identify_ansicolor

Function wrapped around "identify".

normalize_ansicolor

Function wrapped around "normalize".

parse_ansicolor

Function wrapped around "parse".

EXPORTS

Everything listed in "FUNCTIONS" is also available for export upon request.

SEE ALSO

  • Term::ANSIColor - For marking up text that will be printed to the terminal

  • HTML::FromANSI - Specific to (old) HTML; As of 2.03 (released in 2007) tags are not customizable. Uses Term::VT102 which is likely more robust but may be overkill in simple situations (and was difficult to install in the past).

SUPPORT

Perldoc

You can find documentation for this module with the perldoc command.

perldoc Parse::ANSIColor::Tiny

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.

Bugs / Feature Requests

Please report any bugs or feature requests by email to bug-parse-ansicolor-tiny at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-ANSIColor-Tiny. You will be automatically notified of any progress on the request by the system.

Source Code

https://github.com/rwstauner/Parse-ANSIColor-Tiny

git clone https://github.com/rwstauner/Parse-ANSIColor-Tiny.git

AUTHOR

Randy Stauner <rwstauner@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Randy Stauner.

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