1 # Pod::Text::Color -- Convert POD data to formatted color ASCII text
3 # Copyright 1999, 2001, 2004, 2006 by Russ Allbery <rra@stanford.edu>
5 # This program is free software; you may redistribute it and/or modify it
6 # under the same terms as Perl itself.
8 # This is just a basic proof of concept. It should later be modified to make
9 # better use of color, take options changing what colors are used for what
12 ##############################################################################
13 # Modules and declarations
14 ##############################################################################
16 package Pod::Text::Color;
21 use Term::ANSIColor qw(colored);
24 use vars qw(@ISA $VERSION);
28 # Don't use the CVS revision as the version, since this module is also in Perl
29 # core and too many things could munge CVS magic revision strings. This
30 # number should ideally be the same as the CVS revision in podlators, however.
33 ##############################################################################
35 ##############################################################################
37 # Make level one headings bold.
39 my ($self, $attrs, $text) = @_;
41 $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold'));
44 # Make level two headings bold.
46 my ($self, $attrs, $text) = @_;
48 $self->SUPER::cmd_head2 ($attrs, colored ($text, 'bold'));
51 # Fix the various formatting codes.
52 sub cmd_b { return colored ($_[2], 'bold') }
53 sub cmd_f { return colored ($_[2], 'cyan') }
54 sub cmd_i { return colored ($_[2], 'yellow') }
56 # Output any included code in green.
58 my ($self, $code) = @_;
59 $code = colored ($code, 'green');
60 $self->output ($code);
63 # We unfortunately have to override the wrapping code here, since the normal
64 # wrapping code gets really confused by all the escape sequences.
69 my $spaces = ' ' x $$self{MARGIN};
70 my $width = $$self{opt_width} - $$self{MARGIN};
72 # We have to do $shortchar and $longchar in variables because the
73 # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
74 my $char = '(?:(?:\e\[[\d;]+m)*[^\n])';
75 my $shortchar = $char . "{0,$width}";
76 my $longchar = $char . "{$width}";
77 while (length > $width) {
78 if (s/^($shortchar)\s+// || s/^($longchar)//) {
79 $output .= $spaces . $1 . "\n";
84 $output .= $spaces . $_;
85 $output =~ s/\s+$/\n\n/;
89 ##############################################################################
90 # Module return value and documentation
91 ##############################################################################
98 Pod::Text::Color - Convert POD data to formatted color ASCII text
102 use Pod::Text::Color;
103 my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
105 # Read POD from STDIN and write to STDOUT.
106 $parser->parse_from_filehandle;
108 # Read POD from file.pod and write to file.txt.
109 $parser->parse_from_file ('file.pod', 'file.txt');
113 Pod::Text::Color is a simple subclass of Pod::Text that highlights output
114 text using ANSI color escape sequences. Apart from the color, it in all
115 ways functions like Pod::Text. See L<Pod::Text> for details and available
118 Term::ANSIColor is used to get colors and therefore must be installed to use
123 This is just a basic proof of concept. It should be seriously expanded to
124 support configurable coloration via options passed to the constructor, and
125 B<pod2text> should be taught about those.
129 L<Pod::Text>, L<Pod::Simple>
131 The current version of this module is always available from its web site at
132 L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
133 Perl core distribution as of 5.6.0.
137 Russ Allbery <rra@stanford.edu>.
139 =head1 COPYRIGHT AND LICENSE
141 Copyright 1999, 2001, 2004, 2006 by Russ Allbery <rra@stanford.edu>.
143 This program is free software; you may redistribute it and/or modify it
144 under the same terms as Perl itself.