Upgrade to podlators 1.10.
[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 1.0 2001/07/10 11:03:43 eagle Exp $
3 #
4 # Copyright 1999 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 = 1.00;
33
34
35 ##############################################################################
36 # Overrides
37 ##############################################################################
38
39 # Make level one headings bold.
40 sub cmd_head1 {
41     my $self = shift;
42     local $_ = shift;
43     s/\s+$//;
44     $self->SUPER::cmd_head1 (colored ($_, 'bold'));
45 }
46
47 # Make level two headings bold.
48 sub cmd_head2 {
49     my $self = shift;
50     local $_ = shift;
51     s/\s+$//;
52     $self->SUPER::cmd_head2 (colored ($_, 'bold'));
53 }
54
55 # Fix the various interior sequences.
56 sub seq_b { return colored ($_[1], 'bold')   }
57 sub seq_f { return colored ($_[1], 'cyan')   }
58 sub seq_i { return colored ($_[1], 'yellow') }
59
60 # We unfortunately have to override the wrapping code here, since the normal
61 # wrapping code gets really confused by all the escape sequences.
62 sub wrap {
63     my $self = shift;
64     local $_ = shift;
65     my $output = '';
66     my $spaces = ' ' x $$self{MARGIN};
67     my $width = $$self{width} - $$self{MARGIN};
68     while (length > $width) {
69         if (s/^((?:(?:\e\[[\d;]+m)?[^\n]){0,$width})\s+//
70             || s/^((?:(?:\e\[[\d;]+m)?[^\n]){$width})//) {
71             $output .= $spaces . $1 . "\n";
72         } else {
73             last;
74         }
75     }
76     $output .= $spaces . $_;
77     $output =~ s/\s+$/\n\n/;
78     $output;
79 }
80
81 ##############################################################################
82 # Module return value and documentation
83 ##############################################################################
84
85 1;
86 __END__
87
88 =head1 NAME
89
90 Pod::Text::Color - Convert POD data to formatted color ASCII text
91
92 =head1 SYNOPSIS
93
94     use Pod::Text::Color;
95     my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
96
97     # Read POD from STDIN and write to STDOUT.
98     $parser->parse_from_filehandle;
99
100     # Read POD from file.pod and write to file.txt.
101     $parser->parse_from_file ('file.pod', 'file.txt');
102
103 =head1 DESCRIPTION
104
105 Pod::Text::Color is a simple subclass of Pod::Text that highlights output
106 text using ANSI color escape sequences.  Apart from the color, it in all
107 ways functions like Pod::Text.  See L<Pod::Text> for details and available
108 options.
109
110 Term::ANSIColor is used to get colors and therefore must be installed to use
111 this module.
112
113 =head1 BUGS
114
115 This is just a basic proof of concept.  It should be seriously expanded to
116 support configurable coloration via options passed to the constructor, and
117 B<pod2text> should be taught about those.
118
119 =head1 SEE ALSO
120
121 L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
122
123 =head1 AUTHOR
124
125 Russ Allbery <rra@stanford.edu>.
126
127 =head1 COPYRIGHT AND LICENSE
128
129 Copyright 1999 by Russ Allbery <rra@stanford.edu>.
130
131 This program is free software; you may redistribute it and/or modify it
132 under the same terms as Perl itself.
133
134 =cut