1 # Pod::Text::Color -- Convert POD data to formatted color ASCII text
2 # $Id: Color.pm,v 2.1 2004/12/31 21:50:00 eagle Exp $
4 # Copyright 1999, 2001, 2004 by Russ Allbery <rra@stanford.edu>
6 # This program is free software; you may redistribute it and/or modify it
7 # under the same terms as Perl itself.
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
13 ##############################################################################
14 # Modules and declarations
15 ##############################################################################
17 package Pod::Text::Color;
22 use Term::ANSIColor qw(colored);
25 use vars qw(@ISA $VERSION);
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.
34 ##############################################################################
36 ##############################################################################
38 # Make level one headings bold.
40 my ($self, $attrs, $text) = @_;
42 $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold'));
45 # Make level two headings bold.
47 my ($self, $attrs, $text) = @_;
49 $self->SUPER::cmd_head2 ($attrs, colored ($text, 'bold'));
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') }
57 # Output any included code in green.
59 my ($self, $code) = @_;
60 $code = colored ($code, 'green');
61 $self->output ($code);
64 # We unfortunately have to override the wrapping code here, since the normal
65 # wrapping code gets really confused by all the escape sequences.
70 my $spaces = ' ' x $$self{MARGIN};
71 my $width = $$self{opt_width} - $$self{MARGIN};
72 my $char = '(?:(?:\e\[[\d;]+m)*[^\n])';
73 while (length > $width) {
74 if (s/^(${char}{0,$width})\s+// || s/^(${char}{$width})//) {
75 $output .= $spaces . $1 . "\n";
80 $output .= $spaces . $_;
81 $output =~ s/\s+$/\n\n/;
85 ##############################################################################
86 # Module return value and documentation
87 ##############################################################################
94 Pod::Text::Color - Convert POD data to formatted color ASCII text
99 my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
101 # Read POD from STDIN and write to STDOUT.
102 $parser->parse_from_filehandle;
104 # Read POD from file.pod and write to file.txt.
105 $parser->parse_from_file ('file.pod', 'file.txt');
109 Pod::Text::Color is a simple subclass of Pod::Text that highlights output
110 text using ANSI color escape sequences. Apart from the color, it in all
111 ways functions like Pod::Text. See L<Pod::Text> for details and available
114 Term::ANSIColor is used to get colors and therefore must be installed to use
119 This is just a basic proof of concept. It should be seriously expanded to
120 support configurable coloration via options passed to the constructor, and
121 B<pod2text> should be taught about those.
125 L<Pod::Text>, L<Pod::Simple>
127 The current version of this module is always available from its web site at
128 L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
129 Perl core distribution as of 5.6.0.
133 Russ Allbery <rra@stanford.edu>.
135 =head1 COPYRIGHT AND LICENSE
137 Copyright 1999, 2001, 2004 by Russ Allbery <rra@stanford.edu>.
139 This program is free software; you may redistribute it and/or modify it
140 under the same terms as Perl itself.