File Coverage

blib/lib/Pod/Text/Ansi.pm
Criterion Covered Total %
statement 14 61 22.9
branch 0 4 0.0
condition 0 6 0.0
subroutine 6 21 28.5
pod 0 15 0.0
total 20 107 18.6


line stmt bran cond sub pod time code
1             package Pod::Text::Ansi;
2             BEGIN {
3 1     1   820 $Pod::Text::Ansi::AUTHORITY = 'cpan:HINRIK';
4             }
5             BEGIN {
6 1     1   17 $Pod::Text::Ansi::VERSION = '0.05';
7             }
8              
9 1     1   8 use strict;
  1         2  
  1         35  
10 1     1   4 use warnings FATAL => 'all';
  1         10  
  1         47  
11 1     1   995 use Term::ANSIColor qw(colored);
  1         8699  
  1         487  
12              
13 1     1   13 use base 'Pod::Text';
  1         1  
  1         1215  
14              
15             # wrap every line in Ansi color codes
16             sub color {
17 0     0 0   my ($text, @codes) = @_;
18 0           my @lines = split/\n/, $text;
19 0           $_ = colored($_, @codes) for @lines;
20 0           return join "\n", @lines;
21             }
22              
23             # the same, but only if the entire text is not already colored
24             sub only_color {
25 0     0 0   my ($text, @codes) = @_;
26 0 0 0       return $text if $text =~ /^\e\[/ && $text =~ /\e\[0?m$/;
27 0           return color($text, @codes);
28             }
29              
30             # Make level one headings bold.
31             sub cmd_head1 {
32 0     0 0   my ($self, $attrs, $text) = @_;
33 0           $text =~ s/\s+$//;
34 0           $self->SUPER::cmd_head1($attrs, only_color($text, 'bold'));
35             }
36              
37             # Make level two headings bold.
38             sub cmd_head2 {
39 0     0 0   my ($self, $attrs, $text) = @_;
40 0           $text =~ s/\s+$//;
41 0           $self->SUPER::cmd_head2($attrs, only_color($text, 'bold'));
42             }
43              
44             # Make level three headings bold.
45             sub cmd_head3 {
46 0     0 0   my ($self, $attrs, $text) = @_;
47 0           $text =~ s/\s+$//;
48 0           $self->SUPER::cmd_head3($attrs, only_color("$text", 'bold'));
49             }
50              
51             # Make level four headings bold.
52             sub cmd_head4 {
53 0     0 0   my ($self, $attrs, $text) = @_;
54 0           $text =~ s/\s+$//;
55 0           $self->SUPER::cmd_head4($attrs, only_color("$text", 'bold'));
56             }
57              
58             sub cmd_verbatim {
59 0     0 0   my ($self, $attrs, $text) = @_;
60 0           $text = join("\n", map { color($_, 'yellow') } split(/\n/, $text));
  0            
61 0           $self->SUPER::cmd_verbatim($attrs, color($text, 'yellow'));
62             }
63              
64             # Fix the various formatting codes.
65 0     0 0   sub cmd_c { return color($_[2], 'yellow') }
66 0     0 0   sub cmd_b { return color($_[2], 'bold') }
67 0     0 0   sub cmd_e { return color($_[2], 'green') }
68 0     0 0   sub cmd_f { return color($_[2], 'cyan') }
69 0     0 0   sub cmd_i { return color($_[2], 'green') }
70 0     0 0   sub cmd_l { return color($_[2], 'blue') }
71              
72             # Output any included code in magenta
73             sub output_code {
74 0     0 0   my ($self, $code) = @_;
75 0           $code = color($code, 'magenta');
76 0           $self->output ($code);
77             }
78              
79             # We unfortunately have to override the wrapping code here, since the normal
80             # wrapping code gets really confused by all the escape sequences.
81             sub wrap {
82 0     0 0   my $self = shift;
83 0           local $_ = shift;
84 0           my $output = '';
85 0           my $spaces = ' ' x $$self{MARGIN};
86 0           my $width = $$self{opt_width} - $$self{MARGIN};
87              
88             # We have to do $shortchar and $longchar in variables because the
89             # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
90 0           my $char = '(?:(?:\e\[[\d;]+m)*[^\n])';
91 0           my $shortchar = $char . "{0,$width}";
92 0           my $longchar = $char . "{$width}";
93 0           while (length > $width) {
94 0 0 0       if (s/^($shortchar)\s+// || s/^($longchar)//) {
95 0           $output .= $spaces . $1 . "\n";
96             } else {
97 0           last;
98             }
99             }
100 0           $output .= $spaces . $_;
101 0           $output =~ s/\s+$/\n\n/;
102 0           $output;
103             }
104              
105             1;
106              
107             =encoding utf8
108              
109             =head1 NAME
110              
111             Pod::Text::Ansi - Convert POD to ANSI-colored text
112              
113             =head1 SYNOPSIS
114              
115             use Pod::Text::Ansi;
116             my $parser = Pod::Text::Ansi->new (sentence => 0, width => 78);
117              
118             # Read POD from STDIN and write to STDOUT.
119             $parser->parse_from_filehandle;
120              
121             # Read POD from file.pod and write to file.txt.
122             $parser->parse_from_file ('file.pod', 'file.txt');
123              
124             =head1 DESCRIPTION
125              
126             Pod::Text::Ansi is a simple subclass of Pod::Text that highlights output
127             text using ANSI color escape sequences. Apart from the color, it in all
128             ways functions like Pod::Text. See L for details and available
129             options.
130              
131             =head1 SEE ALSO
132              
133             L, L,
134             L
135              
136             =head1 AUTHOR
137              
138             Hinrik Örn Sigurðsson, L
139              
140             Based on L by Russ Allbery
141             L.
142              
143             =head1 CAVEATS
144              
145             =over
146              
147             =item * It currently doesn't respect some forms of nesting.
148              
149             Example:
150              
151             I<'italic', C<'italic code'>, 'italic'>.
152              
153             Contrary to what the three terms above say, they will be rendered as italic
154             only, then code only, then normal, respectively.
155              
156             Non-overlapping nesting such as the following does work, though:
157              
158             I< C >
159              
160             =item * The wrapping code isn't perfect.
161              
162             Some formatting codes that stretch over multiple lines will break. One example
163             would be an C<< LZ<><> >> code that's too long to fit on one line.
164              
165             =back
166              
167             =head1 LICENSE AND COPYRIGHT
168              
169             Copyright 1999, 2001, 2004, 2006 by Russ Allbery L.
170              
171             Copyright (c) 2009, Hinrik Örn Sigurðsson L.
172              
173             This program is free software; you may redistribute it and/or modify it
174             under the same terms as Perl itself. See L.
175              
176             =cut