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