Print embedded Pod as formatted text with termcap escapes

落花浮王杯 提交于 2019-12-23 10:25:50

问题


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:

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:

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?


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/55746131/print-embedded-pod-as-formatted-text-with-termcap-escapes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!