Once again syncing after too long an absence
[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 1.0 2000/12/25 12:52:48 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 # Don't use the CVS revision as the version, since this module is also in
31 # Perl core and too many things could munge CVS magic revision strings.
32 # This number should ideally be the same as the CVS revision in podlators,
33 # however.
34 $VERSION = 1.00;
35
36
37 ############################################################################
38 # Overrides
39 ############################################################################
40
41 # In the initialization method, grab our terminal characteristics as well as
42 # do all the stuff we normally do.
43 sub initialize {
44     my $self = shift;
45
46     # The default Term::Cap path won't work on Solaris.
47     $ENV{TERMPATH} = "$ENV{HOME}/.termcap:/etc/termcap"
48         . ":/usr/share/misc/termcap:/usr/share/lib/termcap";
49
50     my $termios = POSIX::Termios->new;
51     $termios->getattr;
52     my $ospeed = $termios->getospeed;
53     my $term = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
54     $$self{BOLD} = $$term{_md} or die 'BOLD';
55     $$self{UNDL} = $$term{_us} or die 'UNDL';
56     $$self{NORM} = $$term{_me} or die 'NORM';
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.
67 sub 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.
75 sub 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<>.
83 sub seq_b { my $self = shift; return "$$self{BOLD}$_[0]$$self{NORM}" }
84 sub seq_i { my $self = shift; return "$$self{UNDL}$_[0]$$self{NORM}" }
85
86 # Override the wrapping code to igore the special sequences.
87 sub wrap {
88     my $self = shift;
89     local $_ = shift;
90     my $output = '';
91     my $spaces = ' ' x $$self{MARGIN};
92     my $width = $$self{width} - $$self{MARGIN};
93     my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
94     while (length > $width) {
95         if (s/^((?:$code?[^\n]){0,$width})\s+//
96             || s/^((?:$code?[^\n]){$width})//) {
97             $output .= $spaces . $1 . "\n";
98         } else {
99             last;
100         }
101     }
102     $output .= $spaces . $_;
103     $output =~ s/\s+$/\n\n/;
104     $output;
105 }
106
107
108 ############################################################################
109 # Module return value and documentation
110 ############################################################################
111
112 1;
113 __END__
114
115 =head1 NAME
116
117 Pod::Text::Color - Convert POD data to ASCII text with format escapes
118
119 =head1 SYNOPSIS
120
121     use Pod::Text::Termcap;
122     my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
123
124     # Read POD from STDIN and write to STDOUT.
125     $parser->parse_from_filehandle;
126
127     # Read POD from file.pod and write to file.txt.
128     $parser->parse_from_file ('file.pod', 'file.txt');
129
130 =head1 DESCRIPTION
131
132 Pod::Text::Termcap is a simple subclass of Pod::Text that highlights output
133 text using the correct termcap escape sequences for the current terminal.
134 Apart from the format codes, it in all ways functions like Pod::Text.  See
135 L<Pod::Text> for details and available options.
136
137 =head1 SEE ALSO
138
139 L<Pod::Text|Pod::Text>, L<Pod::Parser|Pod::Parser>
140
141 =head1 AUTHOR
142
143 Russ Allbery E<lt>rra@stanford.eduE<gt>.
144
145 =cut