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