blib/lib/Pod/Text/Ansi.pm

filter: Statementstmt 24.1% 15 / 62 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 28.5% 6 / 21 Podpod 0.0% 0 / 15 totaltotal 18.4% 21 / 114 CC 7 SCAR 17.7
CC
7
Cov
20%
wrap
20.0
only_color
12.0
color
6.0
CRAP 5.9
↑ Summary
1 package Pod::Text::Ansi;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Pod::Text::Ansi::AUTHORITY = 'cpan:HINRIK';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Pod::Text::Ansi::VERSION = '0.05';
7 }
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use base 'Pod::Text';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # wrap every line in Ansi color codes
16 0 ▶sub color {
Subroutine color: not called CC=2 SCAR=17.9
Pod undocumented
17 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 0 ▶sub only_color {
Subroutine only_color: not called CC=3 SCAR=24.8
Pod undocumented
25 0 my ($text, @codes) = @_;
26 0 ▶ return $text if $text =~ /^\e\[/ && $text =~ /\e\[0?m$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\e\[/ and $text =~ /\e\[0?m$/ 0 0 0
Condition0%0/3
$text =~ /^\e\[/ and $text =~ /\e\[0?m$/
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /^\e\[/ and $text =~ /\e\[0?m$/
$text =~ /^\e\[/ $text =~ /\e\[0?m$/
Truth table0%0/3
A and B
A= $text =~ /^\e\[/B= $text =~ /\e\[0?m$/
ABresult
0X0
100
111
27 0 return color($text, @codes);
28 }
29  
30 # Make level one headings bold.
31 0 ▶sub cmd_head1 {
Subroutine cmd_head1: not called CC=1 SCAR=6.9
Pod undocumented
32 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 0 ▶sub cmd_head2 {
Subroutine cmd_head2: not called CC=1 SCAR=6.9
Pod undocumented
39 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 0 ▶sub cmd_head3 {
Subroutine cmd_head3: not called CC=1 SCAR=6.9
Pod undocumented
46 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 0 ▶sub cmd_head4 {
Subroutine cmd_head4: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ($self, $attrs, $text) = @_;
54 0 $text =~ s/\s+$//;
55 0 $self->SUPER::cmd_head4($attrs, only_color("$text", 'bold'));
56 }
57  
58 0 ▶sub cmd_verbatim {
Subroutine cmd_verbatim: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my ($self, $attrs, $text) = @_;
60 0 $text = join("\n", map { color($_, 'yellow') } split(/\n/, $text));
61 0 $self->SUPER::cmd_verbatim($attrs, color($text, 'yellow'));
62 }
63  
64 # Fix the various formatting codes.
65 0 ▶sub cmd_c { return color($_[2], 'yellow') }
Subroutine cmd_c: not called CC=1 SCAR=6.9
Pod undocumented
66 0 ▶sub cmd_b { return color($_[2], 'bold') }
Subroutine cmd_b: not called CC=1 SCAR=6.9
Pod undocumented
67 0 ▶sub cmd_e { return color($_[2], 'green') }
Subroutine cmd_e: not called CC=1 SCAR=6.9
Pod undocumented
68 0 ▶sub cmd_f { return color($_[2], 'cyan') }
Subroutine cmd_f: not called CC=1 SCAR=6.9
Pod undocumented
69 0 ▶sub cmd_i { return color($_[2], 'green') }
Subroutine cmd_i: not called CC=1 SCAR=6.9
Pod undocumented
70 0 ▶sub cmd_l { return color($_[2], 'blue') }
Subroutine cmd_l: not called CC=1 SCAR=6.9
Pod undocumented
71  
72 # Output any included code in magenta
73 0 ▶sub output_code {
Subroutine output_code: not called CC=1 SCAR=6.9
Pod undocumented
74 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 0 ▶sub wrap {
Subroutine wrap: not called CC=4 SCAR=30.0
Pod undocumented
82 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) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $width) 0 0 0
94 0 ▶ if (s/^($shortchar)\s+// || s/^($longchar)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($shortchar)\s+// or s/^($longchar)//) { } 0 0 0
Condition0%0/3
s/^($shortchar)\s+// or s/^($longchar)//
l!l&&r!l&&!r
000
MC/DC0%0/2
s/^($shortchar)\s+// or s/^($longchar)//
s/^($shortchar)\s+// s/^($longchar)//
Truth table0%0/3
A or B
A= s/^($shortchar)\s+//B= s/^($longchar)//
ABresult
1X1
011
000
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 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<Pod::Text> for details and available
129 options.
130  
131 =head1 SEE ALSO
132  
133 L<Pod::Text::Color|Pod::Text::Color>, L<Pod::Text|Pod::Text>,
134 L<Pod::Simple|Pod::Simple>
135  
136 =head1 AUTHOR
137  
138 Hinrik Örn Sigurðsson, L<hinrik.sig@gmail.com>
139  
140 Based on L<Pod::Text::Color|Pod::Text::Color> by Russ Allbery
141 L<rra@stanford.edu>.
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<italic code> >
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<rra@stanford.edu>.
170  
171 Copyright (c) 2009, Hinrik Örn Sigurðsson L<hinrik.sig@gmail.com>.
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<perlartistic>.
175  
176 =cut
↑ Summary