Upgrade to Pod::Perldoc 3.13
[p5sagit/p5-mst-13.2.git] / lib / Pod / Perldoc / ToMan.pm
CommitLineData
1a67fee7 1
2require 5;
3package Pod::Perldoc::ToMan;
4use strict;
5use warnings;
6
7# This class is unlike ToText.pm et al, because we're NOT paging thru
a60a0c74 8# the output in our particular format -- we make the output and
1a67fee7 9# then we run nroff (or whatever) on it, and then page thru the
10# (plaintext) output of THAT!
11
12use base qw(Pod::Perldoc::BaseTo);
13sub is_pageable { 1 }
14sub write_with_binmode { 0 }
15sub output_extension { 'txt' }
16
17sub __filter_nroff { shift->_perldoc_elem('__filter_nroff' , @_) }
18sub __nroffer { shift->_perldoc_elem('__nroffer' , @_) }
19sub __bindir { shift->_perldoc_elem('__bindir' , @_) }
20sub __pod2man { shift->_perldoc_elem('__pod2man' , @_) }
21sub __output_file { shift->_perldoc_elem('__output_file' , @_) }
22
23sub center { shift->_perldoc_elem('center' , @_) }
24sub date { shift->_perldoc_elem('date' , @_) }
25sub fixed { shift->_perldoc_elem('fixed' , @_) }
26sub fixedbold { shift->_perldoc_elem('fixedbold' , @_) }
27sub fixeditalic { shift->_perldoc_elem('fixeditalic' , @_) }
28sub fixedbolditalic { shift->_perldoc_elem('fixedbolditalic', @_) }
29sub quotes { shift->_perldoc_elem('quotes' , @_) }
30sub release { shift->_perldoc_elem('release' , @_) }
31sub section { shift->_perldoc_elem('section' , @_) }
32
33sub new { return bless {}, ref($_[0]) || $_[0] }
34
35use File::Spec::Functions qw(catfile);
36
37sub parse_from_file {
38 my $self = shift;
39 my($file, $outfh) = @_;
40
41 my $render = $self->{'__nroffer'} || die "no nroffer set!?";
42
43 # turn the switches into CLIs
44 my $switches = join ' ',
45 map qq{"--$_=$self->{$_}"},
46 grep !m/^_/s,
47 keys %$self
48 ;
19006a1d 49
50 my $pod2man =
1a67fee7 51 catfile(
52 ($self->{'__bindir'} || die "no bindir set?!" ),
53 ($self->{'__pod2man'} || die "no pod2man set?!" ),
54 )
19006a1d 55 ;
56 unless(-e $pod2man) {
57 # This is rarely needed, I think.
58 $pod2man = $self->{'__pod2man'} || die "no pod2man set?!";
59 die "Can't find a pod2man?! (". $self->{'__pod2man'} .")\nAborting"
60 unless -e $pod2man;
61 }
62
63 my $command = "$pod2man $switches --lax $file | $render -man";
64 # no temp file, just a pipe!
a60a0c74 65
537c2f98 66 # Thanks to Brendan O'Dea for contributing the following block
67 if(Pod::Perldoc::IS_Linux and -t STDOUT
68 and my ($cols) = `stty -a` =~ m/\bcolumns\s+(\d+)/
69 ) {
70 my $c = $cols * 39 / 40;
71 $cols = $c > $cols - 2 ? $c : $cols -2;
72 $command .= ' -rLL=' . (int $c) . 'n' if $cols > 80;
73 }
74
a60a0c74 75 # I hear persistent reports that adding a -c switch to $render
76 # solves many people's problems. But I also hear that some mans
77 # don't have a -c switch, so that adding it here would presumably
78 # be a Bad Thing -- sburke@cpan.org
79
537c2f98 80 $command .= " | col -x" if Pod::Perldoc::IS_HPUX;
1a67fee7 81
82 defined(&Pod::Perldoc::DEBUG)
83 and Pod::Perldoc::DEBUG()
84 and print "About to run $command\n";
85 ;
86
87 my $rslt = `$command`;
88
89 my $err;
90
91 if( $self->{'__filter_nroff'} ) {
92 defined(&Pod::Perldoc::DEBUG)
93 and &Pod::Perldoc::DEBUG()
94 and print "filter_nroff is set, so filtering...\n";
95 $rslt = $self->___Do_filter_nroff($rslt);
96 } else {
97 defined(&Pod::Perldoc::DEBUG)
98 and Pod::Perldoc::DEBUG()
99 and print "filter_nroff isn't set, so not filtering.\n";
100 }
101
102 if (($err = $?)) {
103 defined(&Pod::Perldoc::DEBUG)
104 and Pod::Perldoc::DEBUG()
105 and print "Nonzero exit ($?) while running $command.\n",
106 "Falling back to Pod::Perldoc::ToPod\n ",
107 ;
108 # A desperate fallthru:
109 require Pod::Perldoc::ToPod;
110 return Pod::Perldoc::ToPod->new->parse_from_file(@_);
111
112 } else {
113 print $outfh $rslt
114 or die "Can't print to $$self{__output_file}: $!";
115 }
116
117 return;
118}
119
120
121sub ___Do_filter_nroff {
122 my $self = shift;
123 my @data = split /\n{2,}/, shift;
124
125 shift @data while @data and $data[0] !~ /\S/; # Go to header
126 shift @data if @data and $data[0] =~ /Contributed\s+Perl/; # Skip header
127 pop @data if @data and $data[-1] =~ /^\w/; # Skip footer, like
128 # 28/Jan/99 perl 5.005, patch 53 1
129 join "\n\n", @data;
130}
131
1321;
133
134__END__
135
136=head1 NAME
137
138Pod::Perldoc::ToMan - let Perldoc render Pod as man pages
139
140=head1 SYNOPSIS
141
142 perldoc -o man Some::Modulename
143
144=head1 DESCRIPTION
145
146This is a "plug-in" class that allows Perldoc to use
147Pod::Man and C<nroff> for reading Pod pages.
148
149The following options are supported: center, date, fixed, fixedbold,
150fixeditalic, fixedbolditalic, quotes, release, section
151
152(Those options are explained in L<Pod::Man>.)
153
154For example:
155
156 perldoc -o man -w center:Pod Some::Modulename
157
158=head1 CAVEAT
159
160This module may change to use a different pod-to-nroff formatter class
161in the future, and this may change what options are supported.
162
163=head1 SEE ALSO
164
165L<Pod::Man>, L<Pod::Perldoc>, L<Pod::Perldoc::ToNroff>
166
167=head1 COPYRIGHT AND DISCLAIMERS
168
19006a1d 169Copyright (c) 2002,3,4 Sean M. Burke. All rights reserved.
1a67fee7 170
171This library is free software; you can redistribute it and/or modify it
172under the same terms as Perl itself.
173
174This program is distributed in the hope that it will be useful, but
175without any warranty; without even the implied warranty of
176merchantability or fitness for a particular purpose.
177
178=head1 AUTHOR
179
180Sean M. Burke C<sburke@cpan.org>
181
182=cut
183