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