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