Quick integration of mainline changes to date
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text / Termcap.pm
1 # Pod::Text::Termcap -- Convert POD data to ASCII text with format escapes.
2 # $Id: Termcap.pm,v 0.4 1999/09/20 10:17:45 eagle Exp $
3 #
4 # Copyright 1999 by Russ Allbery <rra@stanford.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
9 # This is a simple subclass of Pod::Text that overrides a few key methods to
10 # output the right termcap escape sequences for formatted text on the
11 # current terminal type.
12
13 ############################################################################
14 # Modules and declarations
15 ############################################################################
16
17 package Pod::Text::Termcap;
18
19 require 5.004;
20
21 use Pod::Text ();
22 use POSIX ();
23 use Term::Cap;
24
25 use strict;
26 use vars qw(@ISA $VERSION);
27
28 @ISA = qw(Pod::Text);
29
30 # Use the CVS revision of this file as its version number.
31 ($VERSION = (split (' ', q$Revision: 0.4 $ ))[1]) =~ s/\.(\d)$/.0$1/;
32
33
34 ############################################################################
35 # Overrides
36 ############################################################################
37
38 # In the initialization method, grab our terminal characteristics as well as
39 # do all the stuff we normally do.
40 sub initialize {
41     my $self = shift;
42
43     # The default Term::Cap path won't work on Solaris.
44     $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap"
45         . ":/usr/share/misc/termcap:/usr/share/lib/termcap";
46
47     my $termios = POSIX::Termios->new;
48     $termios->getattr;
49     my $ospeed = $termios->getospeed;
50     my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
51     $$self{BOLD} = $$term{_md} or die 'BOLD';
52     $$self{UNDL} = $$term{_us} or die 'UNDL';
53     $$self{NORM} = $$term{_me} or die 'NORM';
54
55     unless (defined $$self{width}) {
56         $$self{width} = $ENV{COLUMNS} || $$term{_co} || 78;
57         $$self{width} -= 2;
58     }
59
60     $self->SUPER::initialize;
61 }
62
63 # Make level one headings bold.
64 sub cmd_head1 {
65     my $self = shift;
66     local $_ = shift;
67     s/\s+$//;
68     $self->SUPER::cmd_head1 ("$$self{BOLD}$_$$self{NORM}");
69 }
70
71 # Make level two headings bold.
72 sub cmd_head2 {
73     my $self = shift;
74     local $_ = shift;
75     s/\s+$//;
76     $self->SUPER::cmd_head2 ("$$self{BOLD}$_$$self{NORM}");
77 }
78
79 # Fix up B<> and I<>.  Note that we intentionally don't do F<>.
80 sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
81 sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
82
83 # Override the wrapping code to igore the special sequences.
84 sub wrap {
85     my $self = shift;
86     local $_ = shift;
87     my $output = '';
88     my $spaces = ' ' x $$self{MARGIN};
89     my $width = $$self{width} - $$self{MARGIN};
90     my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
91     while (length > $width) {
92         if (s/^((?:$code?[^\n]){0,$width})\s+//
93             || s/^((?:$code?[^\n]){$width})//) {
94             $output .= $spaces . $1 . "\n";
95         } else {
96             last;
97         }
98     }
99     $output .= $spaces . $_;
100     $output =~ s/\s+$/\n\n/;
101     $output;
102 }
103
104
105 ############################################################################
106 # Module return value and documentation
107 ############################################################################
108
109 1;
110 __END__
111
112 =head1 NAME
113
114 Pod::Text::Color - Convert POD data to ASCII text with format escapes
115
116 =head1 SYNOPSIS
117
118     use Pod::Text::Termcap;
119     my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
120
121     # Read POD from STDIN and write to STDOUT.
122     $parser->parse_from_filehandle;
123
124     # Read POD from file.pod and write to file.txt.
125     $parser->parse_from_file ('file.pod', 'file.txt');
126
127 =head1 DESCRIPTION
128
129 Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
130 text using the correct termcap escape sequences for the current terminal.
131 Apart from the format codes, it in all ways functions like Pod::Text.  See
132 L<Pod::Text> for details and available options.
133
134 =head1 SEE ALSO
135
136 L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
137
138 =head1 AUTHOR
139
140 Russ Allbery E<lt>rra@stanford.eduE<gt>.
141
142 =cut