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