Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Pod / Text / Color.pm
1 # Pod::Text::Color -- Convert POD data to formatted color ASCII text
2 #
3 # Copyright 1999, 2001, 2004, 2006, 2008 Russ Allbery <rra@stanford.edu>
4 #
5 # This program is free software; you may redistribute it and/or modify it
6 # under the same terms as Perl itself.
7 #
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
10 # text, and the like.
11
12 ##############################################################################
13 # Modules and declarations
14 ##############################################################################
15
16 package Pod::Text::Color;
17
18 require 5.004;
19
20 use Pod::Text ();
21 use Term::ANSIColor qw(colored);
22
23 use strict;
24 use vars qw(@ISA $VERSION);
25
26 @ISA = qw(Pod::Text);
27
28 $VERSION = '2.05';
29
30 ##############################################################################
31 # Overrides
32 ##############################################################################
33
34 # Make level one headings bold.
35 sub cmd_head1 {
36     my ($self, $attrs, $text) = @_;
37     $text =~ s/\s+$//;
38     $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold'));
39 }
40
41 # Make level two headings bold.
42 sub cmd_head2 {
43     my ($self, $attrs, $text) = @_;
44     $text =~ s/\s+$//;
45     $self->SUPER::cmd_head2 ($attrs, colored ($text, 'bold'));
46 }
47
48 # Fix the various formatting codes.
49 sub cmd_b { return colored ($_[2], 'bold')   }
50 sub cmd_f { return colored ($_[2], 'cyan')   }
51 sub cmd_i { return colored ($_[2], 'yellow') }
52
53 # Output any included code in green.
54 sub output_code {
55     my ($self, $code) = @_;
56     $code = colored ($code, 'green');
57     $self->output ($code);
58 }
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{opt_width} - $$self{MARGIN};
68
69     # We have to do $shortchar and $longchar in variables because the
70     # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
71     my $char = '(?:(?:\e\[[\d;]+m)*[^\n])';
72     my $shortchar = $char . "{0,$width}";
73     my $longchar = $char . "{$width}";
74     while (length > $width) {
75         if (s/^($shortchar)\s+// || s/^($longchar)//) {
76             $output .= $spaces . $1 . "\n";
77         } else {
78             last;
79         }
80     }
81     $output .= $spaces . $_;
82     $output =~ s/\s+$/\n\n/;
83     $output;
84 }
85
86 ##############################################################################
87 # Module return value and documentation
88 ##############################################################################
89
90 1;
91 __END__
92
93 =head1 NAME
94
95 Pod::Text::Color - Convert POD data to formatted color ASCII text
96
97 =for stopwords
98 Allbery
99
100 =head1 SYNOPSIS
101
102     use Pod::Text::Color;
103     my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
104
105     # Read POD from STDIN and write to STDOUT.
106     $parser->parse_from_filehandle;
107
108     # Read POD from file.pod and write to file.txt.
109     $parser->parse_from_file ('file.pod', 'file.txt');
110
111 =head1 DESCRIPTION
112
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
116 options.
117
118 Term::ANSIColor is used to get colors and therefore must be installed to use
119 this module.
120
121 =head1 BUGS
122
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.
126
127 =head1 SEE ALSO
128
129 L<Pod::Text>, L<Pod::Simple>
130
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.
134
135 =head1 AUTHOR
136
137 Russ Allbery <rra@stanford.edu>.
138
139 =head1 COPYRIGHT AND LICENSE
140
141 Copyright 1999, 2001, 2004, 2006, 2008 Russ Allbery <rra@stanford.edu>.
142
143 This program is free software; you may redistribute it and/or modify it
144 under the same terms as Perl itself.
145
146 =cut