Upgrade to Term::ANSIColor 1.04.
[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
73849855 2# $Id: Color.pm,v 0.6 2000/12/25 12:52:39 eagle Exp $
6055f9d4 3#
4# Copyright 1999 by Russ Allbery <rra@stanford.edu>
5#
6# This program is free software; you can 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
10# make better use of color, take options changing what colors are used for
11# what text, and the like.
12
13############################################################################
14# Modules and declarations
15############################################################################
16
17package Pod::Text::Color;
18
19require 5.004;
20
21use Pod::Text ();
22use Term::ANSIColor qw(colored);
23
24use strict;
25use vars qw(@ISA $VERSION);
26
27@ISA = qw(Pod::Text);
28
73849855 29# Don't use the CVS revision as the version, since this module is also in
30# Perl core and too many things could munge CVS magic revision strings.
31# This number should ideally be the same as the CVS revision in podlators,
32# however.
33$VERSION = 0.06;
6055f9d4 34
35
36############################################################################
37# Overrides
38############################################################################
39
40# Make level one headings bold.
41sub cmd_head1 {
42 my $self = shift;
43 local $_ = shift;
44 s/\s+$//;
45 $self->SUPER::cmd_head1 (colored ($_, 'bold'));
46}
47
48# Make level two headings bold.
49sub cmd_head2 {
50 my $self = shift;
51 local $_ = shift;
52 s/\s+$//;
53 $self->SUPER::cmd_head2 (colored ($_, 'bold'));
54}
55
56# Fix the various interior sequences.
57sub seq_b { return colored ($_[1], 'bold') }
58sub seq_f { return colored ($_[1], 'cyan') }
59sub seq_i { return colored ($_[1], 'yellow') }
60
61# We unfortunately have to override the wrapping code here, since the normal
62# wrapping code gets really confused by all the escape sequences.
63sub wrap {
64 my $self = shift;
65 local $_ = shift;
66 my $output = '';
67 my $spaces = ' ' x $$self{MARGIN};
68 my $width = $$self{width} - $$self{MARGIN};
69 while (length > $width) {
70 if (s/^((?:(?:\e\[[\d;]+m)?[^\n]){0,$width})\s+//
71 || s/^((?:(?:\e\[[\d;]+m)?[^\n]){$width})//) {
72 $output .= $spaces . $1 . "\n";
73 } else {
74 last;
75 }
76 }
77 $output .= $spaces . $_;
78 $output =~ s/\s+$/\n\n/;
79 $output;
80}
81
82############################################################################
83# Module return value and documentation
84############################################################################
85
861;
87__END__
88
89=head1 NAME
90
91Pod::Text::Color - Convert POD data to formatted color ASCII text
92
93=head1 SYNOPSIS
94
95 use Pod::Text::Color;
96 my $parser = Pod::Text::Color->new (sentence => 0, width => 78);
97
98 # Read POD from STDIN and write to STDOUT.
99 $parser->parse_from_filehandle;
100
101 # Read POD from file.pod and write to file.txt.
102 $parser->parse_from_file ('file.pod', 'file.txt');
103
104=head1 DESCRIPTION
105
9741dab0 106Pod::Text::Color is a simple subclass of Pod::Text that highlights output
107text using ANSI color escape sequences. Apart from the color, it in all
108ways functions like Pod::Text. See L<Pod::Text> for details and available
109options.
110
111Term::ANSIColor is used to get colors and therefore must be installed to use
112this module.
113
114=head1 BUGS
115
116This is just a basic proof of concept. It should be seriously expanded to
117support configurable coloration via options passed to the constructor, and
118B<pod2text> should be taught about those.
6055f9d4 119
120=head1 SEE ALSO
121
122L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
123
124=head1 AUTHOR
125
126Russ Allbery E<lt>rra@stanford.eduE<gt>.
127
128=cut