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