5

I am trying to output embedded Pod as ANSI text to the terminal. In Perl 5 I can use Pod::Text::Termcap:

use strict;
use warnings;
use Pod::Text::Termcap;

my $str = do {local $/; <DATA>};
my $parser = Pod::Text::Termcap->new();
$parser->parse_string_document( $str, \*STDERR );

__DATA__

=head1 SYNOPSIS

my_test_command I<filename> [OPTIONS]

=head1 ARGUMENTS

=over 4

=item I<filename>

File name to test

=back

=head1 OPTIONS

=over 4

=item B<--help>

Prints help

=back

=head1 DESCRIPTION

A sample test command with embedded Pod

Output:

enter image description here

I tried to achieve the same in Perl 6:

use v6;

%*ENV<POD_TO_TEXT_ANSI> = 1;
my @lines;
for $=pod -> $pod-block {
    for $pod-block.contents -> $pod-item {
        use Pod::To::Text;
        push @lines, pod2text($pod-item);
    }
}
say @lines.join("\n\n");

=begin pod

=head1 SYNOPSIS

my_test_command I<filename> [OPTIONS]

=head1 ARGUMENTS

=item I<filename>

File name to test

=head1 OPTIONS

=item B<--help>

Prints help

=head1 DESCRIPTION

A sample test command with embedded Pod

=end pod

Output:

enter image description here

As seen the ANSI termcap escapes are missing in the Perl 6 output. How can I get ANSI features like bold face and underlined text in Perl 6?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • I'm not sure POD_TO_TEXT_ANSI applies (despite its name) to Pod::To::Text. Documentation says [it applies to p6doc](https://docs.perl6.org/programs/02-reading-docs), which indeed it does. I'd have to check the source to see if it does what you want, but in principle there's nothing that says it does. – jjmerelo Apr 18 '19 at 15:21
  • 1
    For starters, you can golf it down to say pod2text(=pod); result will be the same. Looking at [the source](https://github.com/rakudo/rakudo/blob/master/lib/Pod/To/Text.pm6), POD_TO_TEXT_ANSI seems to affect only formatting_to_text (which uses `colored` from Terminal::ANSIColor). That's called for code. But even if I add some code, it does not seem to work. The thing is, it does not work either with perl6 --doc, that is, no color. So I'm afraid you're on your own here... – jjmerelo Apr 18 '19 at 15:29

2 Answers2

4

Pod::To::Text accepts an environment variable POD_TO_TEXT_ANSI that turns this on. Setting that env var inside of a DOC phaser might be too late, though, if the selected Pod::To module is loaded before the perl 6 code is parsed however.

timotimo
  • 4,299
  • 19
  • 23
2

Regarding your question:

How can I get ANSI features like bold face and underlined text in Perl 6?

You might want to give Terminal::ANSIColor a try but you will need to add the ANSI escape codes yourself; it's not going to work automatically on PODs

jjmerelo
  • 22,578
  • 8
  • 40
  • 86