Upgrade to podlators 1.16.
[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.
b4558dc4 2# $Id: Termcap.pm,v 1.4 2001/11/26 07:54:54 eagle Exp $
6055f9d4 3#
59548eca 4# Copyright 1999, 2001 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.
b4558dc4 33$VERSION = 1.04;
6055f9d4 34
35
3c014959 36##############################################################################
6055f9d4 37# Overrides
3c014959 38##############################################################################
6055f9d4 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;
44
45 # The default Term::Cap path won't work on Solaris.
46 $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap"
47 . ":/usr/share/misc/termcap:/usr/share/lib/termcap";
48
49 my $termios = POSIX::Termios->new;
50 $termios->getattr;
51 my $ospeed = $termios->getospeed;
b4558dc4 52 my $term;
53 eval { $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed } };
54 $$self{BOLD} = $$term{_md} || "\e[1m";
55 $$self{UNDL} = $$term{_us} || "\e[4m";
56 $$self{NORM} = $$term{_me} || "\e[m";
6055f9d4 57
58 unless (defined $$self{width}) {
59 $$self{width} = $ENV{COLUMNS} || $$term{_co} || 78;
60 $$self{width} -= 2;
61 }
62
63 $self->SUPER::initialize;
64}
65
66# Make level one headings bold.
67sub cmd_head1 {
68 my $self = shift;
69 local $_ = shift;
70 s/\s+$//;
71 $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}");
72}
73
74# Make level two headings bold.
75sub cmd_head2 {
76 my $self = shift;
77 local $_ = shift;
78 s/\s+$//;
79 $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}");
80}
81
82# Fix up B<> and I<>. Note that we intentionally don't do F<>.
83sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
84sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
85
59548eca 86# Output any included code in bold.
87sub output_code {
88 my ($self, $code) = @_;
89 $self->output ($$self{BOLD} . $code . $$self{NORM});
90}
91
6055f9d4 92# Override the wrapping code to igore the special sequences.
93sub wrap {
94 my $self = shift;
95 local $_ = shift;
96 my $output = '';
97 my $spaces = ' ' x $$self{MARGIN};
98 my $width = $$self{width} - $$self{MARGIN};
99 my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
100 while (length > $width) {
101 if (s/^((?:$code?[^\n]){0,$width})\s+//
102 || s/^((?:$code?[^\n]){$width})//) {
103 $output .= $spaces . $1 . "\n";
104 } else {
105 last;
106 }
107 }
108 $output .= $spaces . $_;
109 $output =~ s/\s+$/\n\n/;
110 $output;
111}
112
113
3c014959 114##############################################################################
6055f9d4 115# Module return value and documentation
3c014959 116##############################################################################
6055f9d4 117
1181;
119__END__
120
121=head1 NAME
122
123Pod::Text::Color - Convert POD data to ASCII text with format escapes
124
125=head1 SYNOPSIS
126
127 use Pod::Text::Termcap;
128 my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
129
130 # Read POD from STDIN and write to STDOUT.
131 $parser->parse_from_filehandle;
132
133 # Read POD from file.pod and write to file.txt.
134 $parser->parse_from_file ('file.pod', 'file.txt');
135
136=head1 DESCRIPTION
137
9741dab0 138Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
139text using the correct termcap escape sequences for the current terminal.
140Apart from the format codes, it in all ways functions like Pod::Text. See
141L<Pod::Text> for details and available options.
6055f9d4 142
b4558dc4 143=head NOTES
144
145This module uses Term::Cap to retrieve the formatting escape sequences for
146the current terminal, and falls back on the ECMA-48 (the same in this
147regard as ANSI X3.64 and ISO 6429, the escape codes also used by DEC VT100
148terminals) if the bold, underline, and reset codes aren't set in the
149termcap information.
150
6055f9d4 151=head1 SEE ALSO
152
b4558dc4 153L<Pod::Text>, L<Pod::Parser>, L<Term::Cap>
6055f9d4 154
155=head1 AUTHOR
156
3c014959 157Russ Allbery <rra@stanford.edu>.
158
159=head1 COPYRIGHT AND LICENSE
160
59548eca 161Copyright 1999, 2001 by Russ Allbery <rra@stanford.edu>.
3c014959 162
163This program is free software; you may redistribute it and/or modify it
164under the same terms as Perl itself.
6055f9d4 165
166=cut