This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to podlators-1.19.
[perl5.git] / lib / Pod / Text / Termcap.pm
CommitLineData
6055f9d4 1# Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
2da3dd12 2# $Id: Termcap.pm,v 1.9 2002/01/02 07:59:09 eagle Exp $
6055f9d4 3#
2da3dd12 4# Copyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu>
6055f9d4 5#
3c014959 6# This program is free software; you may redistribute it and/or modify it
6055f9d4
GS
7# under the same terms as Perl itself.
8#
9741dab0 9# This is a simple subclass of Pod::Text that overrides a few key methods to
3c014959
JH
10# output the right termcap escape sequences for formatted text on the current
11# terminal type.
6055f9d4 12
3c014959 13##############################################################################
6055f9d4 14# Modules and declarations
3c014959 15##############################################################################
6055f9d4
GS
16
17package Pod::Text::Termcap;
18
19require 5.004;
20
21use Pod::Text ();
22use POSIX ();
23use Term::Cap;
9741dab0 24
6055f9d4
GS
25use strict;
26use vars qw(@ISA $VERSION);
27
28@ISA = qw(Pod::Text);
29
3c014959
JH
30# Don't use the CVS revision as the version, since this module is also in Perl
31# core and too many things could munge CVS magic revision strings. This
32# number should ideally be the same as the CVS revision in podlators, however.
2da3dd12 33$VERSION = 1.09;
6055f9d4
GS
34
35
3c014959 36##############################################################################
6055f9d4 37# Overrides
3c014959 38##############################################################################
6055f9d4
GS
39
40# In the initialization method, grab our terminal characteristics as well as
41# do all the stuff we normally do.
42sub initialize {
43 my $self = shift;
a97e9142 44 my ($ospeed, $term, $termios);
6055f9d4
GS
45
46 # The default Term::Cap path won't work on Solaris.
47 $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap"
48 . ":/usr/share/misc/termcap:/usr/share/lib/termcap";
49
a97e9142 50 # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
4989cd70 51 # available (such as on VMS).
a97e9142
RA
52 eval { $termios = POSIX::Termios->new };
53 if ($@) {
2da3dd12 54 $ospeed = 9600;
a97e9142
RA
55 } else {
56 $termios->getattr;
2da3dd12 57 $ospeed = $termios->getospeed || 9600;
8ef7c2e5 58 }
a97e9142
RA
59
60 # Fall back on the ANSI escape sequences if Term::Cap doesn't work.
b4558dc4
JH
61 eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
62 $$self{BOLD} = $$term{_md} || "\e[1m";
63 $$self{UNDL} = $$term{_us} || "\e[4m";
64 $$self{NORM} = $$term{_me} || "\e[m";
6055f9d4
GS
65
66 unless (defined $$self{width}) {
a97e9142 67 $$self{width} = $ENV{COLUMNS} || $$term{_co} || 80;
6055f9d4
GS
68 $$self{width} -= 2;
69 }
70
71 $self->SUPER::initialize;
72}
73
74# Make level one headings bold.
75sub cmd_head1 {
76 my $self = shift;
77 local $_ = shift;
78 s/\s+$//;
79 $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}");
80}
81
82# Make level two headings bold.
83sub cmd_head2 {
84 my $self = shift;
85 local $_ = shift;
86 s/\s+$//;
87 $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}");
88}
89
90# Fix up B<> and I<>. Note that we intentionally don't do F<>.
91sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
92sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
93
59548eca
JH
94# Output any included code in bold.
95sub output_code {
96 my ($self, $code) = @_;
97 $self->output ($$self{BOLD} . $code . $$self{NORM});
98}
99
6055f9d4
GS
100# Override the wrapping code to igore the special sequences.
101sub wrap {
102 my $self = shift;
103 local $_ = shift;
104 my $output = '';
105 my $spaces = ' ' x $$self{MARGIN};
106 my $width = $$self{width} - $$self{MARGIN};
107 my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
108 while (length > $width) {
109 if (s/^((?:$code?[^\n]){0,$width})\s+//
110 || s/^((?:$code?[^\n]){$width})//) {
111 $output .= $spaces . $1 . "\n";
112 } else {
113 last;
114 }
115 }
116 $output .= $spaces . $_;
117 $output =~ s/\s+$/\n\n/;
118 $output;
119}
120
121
3c014959 122##############################################################################
6055f9d4 123# Module return value and documentation
3c014959 124##############################################################################
6055f9d4
GS
125
1261;
127__END__
128
129=head1 NAME
130
131Pod::Text::Color - Convert POD data to ASCII text with format escapes
132
133=head1 SYNOPSIS
134
135 use Pod::Text::Termcap;
136 my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
137
138 # Read POD from STDIN and write to STDOUT.
139 $parser->parse_from_filehandle;
140
141 # Read POD from file.pod and write to file.txt.
142 $parser->parse_from_file ('file.pod', 'file.txt');
143
144=head1 DESCRIPTION
145
9741dab0
GS
146Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
147text using the correct termcap escape sequences for the current terminal.
148Apart from the format codes, it in all ways functions like Pod::Text. See
149L<Pod::Text> for details and available options.
6055f9d4 150
b84d8b9e 151=head1 NOTES
b4558dc4
JH
152
153This module uses Term::Cap to retrieve the formatting escape sequences for
154the current terminal, and falls back on the ECMA-48 (the same in this
155regard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100
156terminals) if the bold, underline, and reset codes aren't set in the
157termcap information.
158
6055f9d4
GS
159=head1 SEE ALSO
160
b4558dc4 161L<Pod::Text>, L<Pod::Parser>, L<Term::Cap>
6055f9d4
GS
162
163=head1 AUTHOR
164
3c014959
JH
165Russ Allbery <rra@stanford.edu>.
166
167=head1 COPYRIGHT AND LICENSE
168
2da3dd12 169Copyright 1999, 2001, 2002 by Russ Allbery <rra@stanford.edu>.
3c014959
JH
170
171This program is free software; you may redistribute it and/or modify it
172under the same terms as Perl itself.
6055f9d4
GS
173
174=cut