Upgrade to podlators-2.0.2
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text / Termcap.pm
CommitLineData
6055f9d4 1# Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
8f202758 2# $Id: Termcap.pm,v 2.3 2006-01-25 23:56:54 eagle Exp $
6055f9d4 3#
8f202758 4# Copyright 1999, 2001, 2002, 2004, 2006 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#
9741dab0 9# This is a simple subclass of Pod::Text that overrides a few key methods to
3c014959 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 16
17package Pod::Text::Termcap;
18
19require 5.004;
20
21use Pod::Text ();
22use POSIX ();
23use Term::Cap;
9741dab0 24
6055f9d4 25use strict;
26use vars qw(@ISA $VERSION);
27
28@ISA = qw(Pod::Text);
29
3c014959 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.
8f202758 33$VERSION = 2.03;
6055f9d4 34
3c014959 35##############################################################################
6055f9d4 36# Overrides
3c014959 37##############################################################################
6055f9d4 38
39# In the initialization method, grab our terminal characteristics as well as
40# do all the stuff we normally do.
b7ae008f 41sub new {
42 my ($self, @args) = @_;
a97e9142 43 my ($ospeed, $term, $termios);
b7ae008f 44 $self = $self->SUPER::new (@args);
6055f9d4 45
2ccddc90 46 # $ENV{HOME} is usually not set on Windows. The default Term::Cap path
47 # may not work on Solaris.
8bafa735 48 my $home = exists $ENV{HOME} ? "$ENV{HOME}/.termcap:" : '';
49 $ENV{TERMPATH} = $home . '/etc/termcap:/usr/share/misc/termcap'
50 . ':/usr/share/lib/termcap';
6055f9d4 51
a97e9142 52 # Fall back on a hard-coded terminal speed if POSIX::Termios isn't
4989cd70 53 # available (such as on VMS).
a97e9142 54 eval { $termios = POSIX::Termios->new };
55 if ($@) {
2da3dd12 56 $ospeed = 9600;
a97e9142 57 } else {
58 $termios->getattr;
2da3dd12 59 $ospeed = $termios->getospeed || 9600;
8ef7c2e5 60 }
a97e9142 61
62 # Fall back on the ANSI escape sequences if Term::Cap doesn't work.
b4558dc4 63 eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
64 $$self{BOLD} = $$term{_md} || "\e[1m";
65 $$self{UNDL} = $$term{_us} || "\e[4m";
66 $$self{NORM} = $$term{_me} || "\e[m";
6055f9d4 67
68 unless (defined $$self{width}) {
b7ae008f 69 $$self{opt_width} = $ENV{COLUMNS} || $$term{_co} || 80;
70 $$self{opt_width} -= 2;
6055f9d4 71 }
72
b7ae008f 73 return $self;
6055f9d4 74}
75
76# Make level one headings bold.
77sub cmd_head1 {
b7ae008f 78 my ($self, $attrs, $text) = @_;
79 $text =~ s/\s+$//;
80 $self->SUPER::cmd_head1 ($attrs, "$$self{BOLD}$text$$self{NORM}");
6055f9d4 81}
82
83# Make level two headings bold.
84sub cmd_head2 {
b7ae008f 85 my ($self, $attrs, $text) = @_;
86 $text =~ s/\s+$//;
87 $self->SUPER::cmd_head2 ($attrs, "$$self{BOLD}$text$$self{NORM}");
6055f9d4 88}
89
90# Fix up B<> and I<>. Note that we intentionally don't do F<>.
b7ae008f 91sub cmd_b { my $self = shift; return "$$self{BOLD}$_[1]$$self{NORM}" }
92sub cmd_i { my $self = shift; return "$$self{UNDL}$_[1]$$self{NORM}" }
6055f9d4 93
59548eca 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 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};
b7ae008f 106 my $width = $$self{opt_width} - $$self{MARGIN};
107
108 # $codes matches a single special sequence. $char matches any number of
109 # special sequences preceeding a single character other than a newline.
8f202758 110 # We have to do $shortchar and $longchar in variables because the
111 # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x.
b7ae008f 112 my $codes = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
113 my $char = "(?:$codes*[^\\n])";
8f202758 114 my $shortchar = $char . "{0,$width}";
115 my $longchar = $char . "{$width}";
6055f9d4 116 while (length > $width) {
8f202758 117 if (s/^($shortchar)\s+// || s/^($longchar)//) {
6055f9d4 118 $output .= $spaces . $1 . "\n";
119 } else {
120 last;
121 }
122 }
123 $output .= $spaces . $_;
124 $output =~ s/\s+$/\n\n/;
b7ae008f 125 return $output;
6055f9d4 126}
127
3c014959 128##############################################################################
6055f9d4 129# Module return value and documentation
3c014959 130##############################################################################
6055f9d4 131
1321;
133__END__
134
135=head1 NAME
136
fd20da51 137Pod::Text::Termcap - Convert POD data to ASCII text with format escapes
6055f9d4 138
139=head1 SYNOPSIS
140
141 use Pod::Text::Termcap;
142 my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
143
144 # Read POD from STDIN and write to STDOUT.
145 $parser->parse_from_filehandle;
146
147 # Read POD from file.pod and write to file.txt.
148 $parser->parse_from_file ('file.pod', 'file.txt');
149
150=head1 DESCRIPTION
151
9741dab0 152Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
153text using the correct termcap escape sequences for the current terminal.
154Apart from the format codes, it in all ways functions like Pod::Text. See
155L<Pod::Text> for details and available options.
6055f9d4 156
b84d8b9e 157=head1 NOTES
b4558dc4 158
159This module uses Term::Cap to retrieve the formatting escape sequences for
160the current terminal, and falls back on the ECMA-48 (the same in this
161regard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100
162terminals) if the bold, underline, and reset codes aren't set in the
163termcap information.
164
6055f9d4 165=head1 SEE ALSO
166
b7ae008f 167L<Pod::Text>, L<Pod::Simple>, L<Term::Cap>
6055f9d4 168
fd20da51 169The current version of this module is always available from its web site at
170L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
171Perl core distribution as of 5.6.0.
172
6055f9d4 173=head1 AUTHOR
174
3c014959 175Russ Allbery <rra@stanford.edu>.
176
177=head1 COPYRIGHT AND LICENSE
178
8f202758 179Copyright 1999, 2001, 2002, 2004, 2006 by Russ Allbery <rra@stanford.edu>.
3c014959 180
181This program is free software; you may redistribute it and/or modify it
182under the same terms as Perl itself.
6055f9d4 183
184=cut