Upgrade to Test::Simple 0.64_03
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text / Color.pm
1 # Pod::Text::Color -- Convert POD data to formatted color ASCII text
2 # $Id: Color.pm,v 2.3 2006-01-25 23:56:54 eagle Exp $
3 #
4 # Copyright 1999, 2001, 2004, 2006 by Russ Allbery <rra@stanford.edu>
5 #
6 # This program is free software; you may redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
9 # This is just a basic proof of concept.  It should later be modified to make
10 # better use of color, take options changing what colors are used for what
11 # text, and the like.
12
13 ##############################################################################
14 # Modules and declarations
15 ##############################################################################
16
17 package Pod::Text::Color;
18
19 require 5.004;
20
21 use Pod::Text ();
22 use Term::ANSIColor qw(colored);
23
24 use strict;
25 use vars qw(@ISA $VERSION);
26
27 @ISA = qw(Pod::Text);
28
29 # Don't use the CVS revision as the version, since this module is also in Perl
30 # core and too many things could munge CVS magic revision strings.  This
31 # number should ideally be the same as the CVS revision in podlators, however.
32 $VERSION = 2.03;
33
34 ##############################################################################
35 # Overrides
36 ##############################################################################
37
38 # Make level one headings bold.
39 sub cmd_head1 {
40     my ($self, $attrs, $text) = @_;
41     $text =~ s/\s+$//;
42     $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold'));
43 }
44
45 # Make level two headings bold.
46 sub cmd_head2 {
47     my ($self, $attrs, $text) = @_;
48     $text =~ s/\s+$//;
49     $self->SUPER::cmd_head2 ($attrs, colored ($text, 'bold'));
50 }
51
52 # Fix the various formatting codes.
53 sub cmd_b { return colored ($_[2], 'bold')   }
54 sub cmd_f { return colored ($_[2], 'cyan')   }
55 sub cmd_i { return colored ($_[2], 'yellow') }
56
57 # Output any included code in green.
58 sub output_code {
59     my ($self, $code) = @_;
60     $code = colored ($code, 'green');
61     $self->output ($code);
62 }
63
64 # We unfortunately have to override the wrapping code here, since the normal
65 # wrapping code gets really confused by all the escape sequences.
66 sub wrap {
67     my $self = shift;
68     local $_ = shift;
69     my $output = '';
70     my $spaces = ' ' x $$self{MARGIN};
71     my $width = $$self{opt_width} - $$self{MARGIN};
72
73     # We have to do $shortchar and $longchar in variables because the
74     # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
75     my $char = '(?:(?:\e\[[\d;]+m)*[^\n])';
76     my $shortchar = $char . "{0,$width}";
77     my $longchar = $char . "{$width}";
78     while (length > $width) {
79         if (s/^($shortchar)\s+// || s/^($longchar)//) {
80             $output .= $spaces . $1 . "\n";
81         } else {
82             last;
83         }
84     }
85     $output .= $spaces . $_;
86     $output =~ s/\s+$/\n\n/;
87     $output;
88 }
89
90 ##############################################################################
91 # Module return value and documentation
92 ##############################################################################
93
94 1;
95 __END__
96
97 =head1 NAME
98
99 Pod::Text::Color - Convert POD data to formatted color ASCII text
100
101 =head1 SYNOPSIS
102
103     use Pod::Text::Color;
104     my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
105
106     # Read POD from STDIN and write to STDOUT.
107     $parser->parse_from_filehandle;
108
109     # Read POD from file.pod and write to file.txt.
110     $parser->parse_from_file ('file.pod', 'file.txt');
111
112 =head1 DESCRIPTION
113
114 Pod::Text::Color is a simple subclass of Pod::Text that highlights output
115 text using ANSI color escape sequences.  Apart from the color, it in all
116 ways functions like Pod::Text.  See L<Pod::Text> for details and available
117 options.
118
119 Term::ANSIColor is used to get colors and therefore must be installed to use
120 this module.
121
122 =head1 BUGS
123
124 This is just a basic proof of concept.  It should be seriously expanded to
125 support configurable coloration via options passed to the constructor, and
126 B<pod2text> should be taught about those.
127
128 =head1 SEE ALSO
129
130 L<Pod::Text>, L<Pod::Simple>
131
132 The current version of this module is always available from its web site at
133 L<http://www.eyrie.org/~eagle/software/podlators/>.  It is also part of the
134 Perl core distribution as of 5.6.0.
135
136 =head1 AUTHOR
137
138 Russ Allbery <rra@stanford.edu>.
139
140 =head1 COPYRIGHT AND LICENSE
141
142 Copyright 1999, 2001, 2004, 2006 by Russ Allbery <rra@stanford.edu>.
143
144 This program is free software; you may redistribute it and/or modify it
145 under the same terms as Perl itself.
146
147 =cut