xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Perldoc/lib/Pod/Perldoc/ToMan.pm (revision e9ce384231aabe5c5a622aa68cef46f2c5bfdb4a)
1require 5.006;
2package Pod::Perldoc::ToMan;
3use strict;
4use warnings;
5use parent qw(Pod::Perldoc::BaseTo);
6
7use vars qw($VERSION);
8$VERSION = '3.19';
9
10use File::Spec::Functions qw(catfile);
11use Pod::Man 2.18;
12# This class is unlike ToText.pm et al, because we're NOT paging thru
13# the output in our particular format -- we make the output and
14# then we run nroff (or whatever) on it, and then page thru the
15# (plaintext) output of THAT!
16
17sub SUCCESS () { 1 }
18sub FAILED  () { 0 }
19
20sub is_pageable        { 1 }
21sub write_with_binmode { 0 }
22sub output_extension   { 'txt' }
23
24sub __filter_nroff  { shift->_perldoc_elem('__filter_nroff'  , @_) }
25sub __nroffer       { shift->_perldoc_elem('__nroffer'       , @_) }
26sub __bindir        { shift->_perldoc_elem('__bindir'        , @_) }
27sub __pod2man       { shift->_perldoc_elem('__pod2man'       , @_) }
28sub __output_file   { shift->_perldoc_elem('__output_file'   , @_) }
29
30sub center          { shift->_perldoc_elem('center'         , @_) }
31sub date            { shift->_perldoc_elem('date'           , @_) }
32sub fixed           { shift->_perldoc_elem('fixed'          , @_) }
33sub fixedbold       { shift->_perldoc_elem('fixedbold'      , @_) }
34sub fixeditalic     { shift->_perldoc_elem('fixeditalic'    , @_) }
35sub fixedbolditalic { shift->_perldoc_elem('fixedbolditalic', @_) }
36sub name            { shift->_perldoc_elem('name'           , @_) }
37sub quotes          { shift->_perldoc_elem('quotes'         , @_) }
38sub release         { shift->_perldoc_elem('release'        , @_) }
39sub section         { shift->_perldoc_elem('section'        , @_) }
40
41sub new {
42	my( $either ) = shift;
43	my $self = bless {}, ref($either) || $either;
44	$self->init( @_ );
45	return $self;
46	}
47
48sub init {
49	my( $self, @args ) = @_;
50
51	unless( $self->__nroffer ) {
52		my $roffer = $self->_find_roffer( $self->_roffer_candidates );
53		$self->debug( "Using $roffer\n" );
54		$self->__nroffer( $roffer );
55		}
56    else {
57	    $self->debug( "__nroffer is " . $self->__nroffer() . "\n" );
58        }
59
60	$self->_check_nroffer;
61	}
62
63sub _roffer_candidates {
64	my( $self ) = @_;
65
66	if( $self->is_openbsd ) { qw( mandoc groff nroff ) }
67	else                    { qw( groff nroff mandoc ) }
68	}
69
70sub _find_roffer {
71	my( $self, @candidates ) = @_;
72
73	my @found = ();
74	foreach my $candidate ( @candidates ) {
75		push @found, $self->_find_executable_in_path( $candidate );
76		}
77
78	return wantarray ? @found : $found[0];
79	}
80
81sub _check_nroffer {
82	return 1;
83	# where is it in the PATH?
84
85	# is it executable?
86
87	# what is its real name?
88
89	# what is its version?
90
91	# does it support the flags we need?
92
93	# is it good enough for us?
94	}
95
96sub _get_stty { `stty -a` }
97
98sub _get_columns_from_stty {
99	my $output = $_[0]->_get_stty;
100
101	if(    $output =~ /\bcolumns\s+(\d+)/ )    { return $1 }
102	elsif( $output =~ /;\s*(\d+)\s+columns;/ ) { return $1 }
103	else                                       { return  0 }
104	}
105
106sub _get_columns_from_manwidth {
107	my( $self ) = @_;
108
109	return 0 unless defined $ENV{MANWIDTH};
110
111	unless( $ENV{MANWIDTH} =~ m/\A\d+\z/ ) {
112		$self->warn( "Ignoring non-numeric MANWIDTH ($ENV{MANWIDTH})\n" );
113		return 0;
114		}
115
116	if( $ENV{MANWIDTH} == 0 ) {
117		$self->warn( "Ignoring MANWIDTH of 0. Really? Why even run the program? :)\n" );
118		return 0;
119		}
120
121	if( $ENV{MANWIDTH} =~ m/\A(\d+)\z/ ) { return $1 }
122
123	return 0;
124	}
125
126sub _get_default_width {
127	73
128	}
129
130sub _get_columns {
131	$_[0]->_get_columns_from_manwidth ||
132	$_[0]->_get_columns_from_stty     ||
133	$_[0]->_get_default_width;
134	}
135
136sub _get_podman_switches {
137	my( $self ) = @_;
138
139	my @switches = grep !m/^_/s, keys %$self;
140
141    # There needs to be a cleaner way to handle setting
142    # the UTF-8 flag, but for now, comment out this
143    # line because it often does the wrong thing.
144    #
145    # See RT #77465
146    #
147    #push @switches, 'utf8' => 1;
148
149	$self->debug( "Pod::Man switches are [@switches]\n" );
150
151	return @switches;
152	}
153
154sub _parse_with_pod_man {
155	my( $self, $file ) = @_;
156
157	#->output_fh and ->output_string from Pod::Simple aren't
158	# working, apparently, so there's this ugly hack:
159	local *STDOUT;
160	open STDOUT, '>', $self->{_text_ref};
161	my $parser = Pod::Man->new( $self->_get_podman_switches );
162	$self->debug( "Parsing $file\n" );
163	$parser->parse_from_file( $file );
164	$self->debug( "Done parsing $file\n" );
165	close STDOUT;
166
167	$self->die( "No output from Pod::Man!\n" )
168		unless length $self->{_text_ref};
169
170	$self->_save_pod_man_output if $self->debugging;
171
172	return SUCCESS;
173	}
174
175sub _save_pod_man_output {
176	my( $self, $fh ) = @_;
177
178	$fh = do {
179		my $file = "podman.out.$$.txt";
180		$self->debug( "Writing $file with Pod::Man output\n" );
181		open my $fh2, '>', $file;
182		$fh2;
183		} unless $fh;
184
185	print { $fh } ${ $self->{_text_ref} };
186	}
187
188sub _have_groff_with_utf8 {
189	my( $self ) = @_;
190
191	return 0 unless $self->_is_groff;
192	my $roffer = $self->__nroffer;
193
194	my $minimum_groff_version = '1.20.1';
195
196	my $version_string = `$roffer -v`;
197	my( $version ) = $version_string =~ /\(?groff\)? version (\d+\.\d+(?:\.\d+)?)/;
198	$self->debug( "Found groff $version\n" );
199
200	# is a string comparison good enough?
201	if( $version lt $minimum_groff_version ) {
202		$self->warn(
203			"You have an old groff." .
204			" Update to version $minimum_groff_version for good Unicode support.\n" .
205			"If you don't upgrade, wide characters may come out oddly.\n"
206			 );
207		}
208
209	$version ge $minimum_groff_version;
210	}
211
212sub _have_mandoc_with_utf8 {
213	my( $self ) = @_;
214
215	return 0 unless $self->_is_mandoc;
216	my $roffer = $self->__nroffer;
217
218	my $minimum_mandoc_version = '1.11';
219
220	my $version_string = `$roffer -V`;
221	my( $version ) = $version_string =~ /mandoc ((\d+)\.(\d+))/;
222	$self->debug( "Found mandoc $version\n" );
223
224	# is a string comparison good enough?
225	if( $version lt $minimum_mandoc_version ) {
226		$self->warn(
227			"You have an older mandoc." .
228			" Update to version $minimum_mandoc_version for better Unicode support.\n" .
229			"If you don't upgrade, wide characters may come out oddly.\n" .
230			"Your results still might be odd. If you have groff, that's even better.\n"
231			 );
232		}
233
234	$version ge $minimum_mandoc_version;
235	}
236
237sub _collect_nroff_switches {
238	my( $self ) = shift;
239
240	my @render_switches = $self->_is_mandoc ? qw(-mandoc) : qw(-man);
241
242	push @render_switches, $self->_get_device_switches;
243
244	# Thanks to Brendan O'Dea for contributing the following block
245	if( $self->_is_roff and $self->is_linux and -t STDOUT and my ($cols) = $self->_get_columns ) {
246		my $c = $cols * 39 / 40;
247		$cols = $c > $cols - 2 ? $c : $cols -2;
248		push @render_switches, '-rLL=' . (int $c) . 'n' if $cols > 80;
249		}
250
251	# I hear persistent reports that adding a -c switch to $render
252	# solves many people's problems.  But I also hear that some mans
253	# don't have a -c switch, so that unconditionally adding it here
254	# would presumably be a Bad Thing   -- sburke@cpan.org
255    push @render_switches, '-c' if( $self->_is_roff and $self->is_cygwin );
256
257	return @render_switches;
258	}
259
260sub _get_device_switches {
261	my( $self ) = @_;
262
263	   if( $self->_is_nroff  )             { qw()              }
264	elsif( $self->_have_groff_with_utf8 )  { qw(-Kutf8 -Tutf8) }
265	elsif( $self->_is_ebcdic )             { qw(-Tcp1047)      }
266	elsif( $self->_have_mandoc_with_utf8 ) { qw(-Tlocale)      }
267	elsif( $self->_is_mandoc )             { qw()              }
268	else                                   { qw(-Tlatin1)      }
269	}
270
271sub _is_roff {
272	my( $self ) = @_;
273
274	$self->_is_nroff or $self->_is_groff;
275	}
276
277sub _is_nroff {
278	my( $self ) = @_;
279
280	$self->__nroffer =~ /\bnroff\b/;
281	}
282
283sub _is_groff {
284	my( $self ) = @_;
285
286	$self->__nroffer =~ /\bgroff\b/;
287	}
288
289sub _is_mandoc {
290	my ( $self ) = @_;
291
292	$self->__nroffer =~ /\bmandoc\b/;
293	}
294
295sub _is_ebcdic {
296	my( $self ) = @_;
297
298	return 0;
299	}
300
301sub _filter_through_nroff {
302	my( $self ) = shift;
303	$self->debug( "Filtering through " . $self->__nroffer() . "\n" );
304
305    # Maybe someone set rendering switches as part of the opt_n value
306    # Deal with that here.
307
308    my ($render, $switches) = $self->__nroffer() =~ /\A([\/a-zA-Z0-9_\.-]+)\b(.+)?\z/;
309
310    $self->die("no nroffer!?") unless $render;
311    my @render_switches = $self->_collect_nroff_switches;
312
313    if ( $switches ) {
314        # Eliminate whitespace
315        $switches =~ s/\s//g;
316
317        # Then seperate the switches with a zero-width positive
318        # lookahead on the dash.
319        #
320        # See:
321        # http://www.effectiveperlprogramming.com/blog/1411
322        # for a good discussion of this technique
323
324        push @render_switches, split(/(?=-)/, $switches);
325        }
326
327	$self->debug( "render is $render\n" );
328	$self->debug( "render options are @render_switches\n" );
329
330	require Symbol;
331	require IPC::Open3;
332	require IO::Handle;
333
334	my $pid = IPC::Open3::open3(
335		my $writer,
336		my $reader,
337		my $err = Symbol::gensym(),
338		$render,
339		@render_switches
340		);
341
342	$reader->autoflush(1);
343
344	use IO::Select;
345	my $selector = IO::Select->new( $reader );
346
347	$self->debug( "Writing to pipe to $render\n" );
348
349	my $offset = 0;
350	my $chunk_size = 4096;
351	my $length = length( ${ $self->{_text_ref} } );
352	my $chunks = $length / $chunk_size;
353	my $done;
354	my $buffer;
355	while( $offset <= $length ) {
356		$self->debug( "Writing chunk $chunks\n" ); $chunks++;
357		syswrite $writer, ${ $self->{_text_ref} }, $chunk_size, $offset
358			or $self->die( $! );
359		$offset += $chunk_size;
360		$self->debug( "Checking read\n" );
361		READ: {
362			last READ unless $selector->can_read( 0.01 );
363			$self->debug( "Reading\n" );
364			my $bytes = sysread $reader, $buffer, 4096;
365			$self->debug( "Read $bytes bytes\n" );
366			$done .= $buffer;
367			$self->debug( sprintf "Output is %d bytes\n",
368				length $done
369				);
370			next READ;
371			}
372		}
373	close $writer;
374	$self->debug( "Done writing\n" );
375
376	# read any leftovers
377	$done .= do { local $/; <$reader> };
378	$self->debug( sprintf "Done reading. Output is %d bytes\n",
379		length $done
380		);
381
382	if( $? ) {
383		$self->warn( "Error from pipe to $render!\n" );
384		$self->debug( 'Error: ' . do { local $/; <$err> } );
385		}
386
387
388	close $reader;
389	if( my $err = $? ) {
390		$self->debug(
391			"Nonzero exit ($?) while running `$render @render_switches`.\n" .
392			"Falling back to Pod::Perldoc::ToPod\n"
393			);
394		return $self->_fallback_to_pod( @_ );
395		}
396
397	$self->debug( "Output:\n----\n$done\n----\n" );
398
399	${ $self->{_text_ref} } = $done;
400
401	return length ${ $self->{_text_ref} } ? SUCCESS : FAILED;
402	}
403
404sub parse_from_file {
405	my( $self, $file, $outfh) = @_;
406
407	# We have a pipeline of filters each affecting the reference
408	# in $self->{_text_ref}
409	$self->{_text_ref} = \my $output;
410
411	$self->_parse_with_pod_man( $file );
412	# so far, nroff is an external command so we ensure it worked
413	my $result = $self->_filter_through_nroff;
414	return $self->_fallback_to_pod( @_ ) unless $result == SUCCESS;
415
416	$self->_post_nroff_processing;
417
418	print { $outfh } $output or
419		$self->die( "Can't print to $$self{__output_file}: $!" );
420
421	return;
422	}
423
424sub _fallback_to_pod {
425	my( $self, @args ) = @_;
426	$self->warn( "Falling back to Pod because there was a problem!\n" );
427	require Pod::Perldoc::ToPod;
428	return  Pod::Perldoc::ToPod->new->parse_from_file(@_);
429	}
430
431# maybe there's a user setting we should check?
432sub _get_tab_width { 4 }
433
434sub _expand_tabs {
435	my( $self ) = @_;
436
437	my $tab_width = ' ' x $self->_get_tab_width;
438
439	${ $self->{_text_ref} } =~ s/\t/$tab_width/g;
440	}
441
442sub _post_nroff_processing {
443	my( $self ) = @_;
444
445	if( $self->is_hpux ) {
446	    $self->debug( "On HP-UX, I'm going to expand tabs for you\n" );
447		# this used to be a pipe to `col -x` for HP-UX
448		$self->_expand_tabs;
449		}
450
451	if( $self->{'__filter_nroff'} ) {
452		$self->debug( "filter_nroff is set, so filtering\n" );
453		$self->_remove_nroff_header;
454		$self->_remove_nroff_footer;
455		}
456	else {
457		$self->debug( "filter_nroff is not set, so not filtering\n" );
458		}
459
460	$self->_handle_unicode;
461
462	return 1;
463	}
464
465# I don't think this does anything since there aren't two consecutive
466# newlines in the Pod::Man output
467sub _remove_nroff_header {
468	my( $self ) = @_;
469	$self->debug( "_remove_nroff_header is still a stub!\n" );
470	return 1;
471
472#  my @data = split /\n{2,}/, shift;
473#  shift @data while @data and $data[0] !~ /\S/; # Go to header
474#  shift @data if @data and $data[0] =~ /Contributed\s+Perl/; # Skip header
475	}
476
477# I don't think this does anything since there aren't two consecutive
478# newlines in the Pod::Man output
479sub _remove_nroff_footer {
480	my( $self ) = @_;
481	$self->debug( "_remove_nroff_footer is still a stub!\n" );
482	return 1;
483	${ $self->{_text_ref} } =~ s/\n\n+.*\w.*\Z//m;
484
485#  my @data = split /\n{2,}/, shift;
486#  pop @data if @data and $data[-1] =~ /^\w/; # Skip footer, like
487        # 28/Jan/99 perl 5.005, patch 53 1
488	}
489
490sub _unicode_already_handled {
491	my( $self ) = @_;
492
493	$self->_have_groff_with_utf8 ||
494	1  # so, we don't have a case that needs _handle_unicode
495	;
496	}
497
498sub _handle_unicode {
499# this is the job of preconv
500# we don't need this with groff 1.20 and later.
501	my( $self ) = @_;
502
503	return 1 if $self->_unicode_already_handled;
504
505	require Encode;
506
507	# it's UTF-8 here, but we need character data
508	my $text = Encode::decode( 'UTF-8', ${ $self->{_text_ref} } ) ;
509
510# http://www.mail-archive.com/groff@gnu.org/msg01378.html
511# http://linux.die.net/man/7/groff_char
512# http://www.gnu.org/software/groff/manual/html_node/Using-Symbols.html
513# http://lists.gnu.org/archive/html/groff/2011-05/msg00007.html
514# http://www.simplicidade.org/notes/archives/2009/05/fixing_the_pod.html
515# http://lists.freebsd.org/pipermail/freebsd-questions/2011-July/232239.html
516	$text =~ s/(\P{ASCII})/
517		sprintf '\\[u%04X]', ord $1
518	     /eg;
519
520	# should we encode?
521	${ $self->{_text_ref} } = $text;
522	}
523
5241;
525
526__END__
527
528=head1 NAME
529
530Pod::Perldoc::ToMan - let Perldoc render Pod as man pages
531
532=head1 SYNOPSIS
533
534  perldoc -o man Some::Modulename
535
536=head1 DESCRIPTION
537
538This is a "plug-in" class that allows Perldoc to use
539Pod::Man and C<groff> for reading Pod pages.
540
541The following options are supported:  center, date, fixed, fixedbold,
542fixeditalic, fixedbolditalic, quotes, release, section
543
544(Those options are explained in L<Pod::Man>.)
545
546For example:
547
548  perldoc -o man -w center:Pod Some::Modulename
549
550=head1 CAVEAT
551
552This module may change to use a different pod-to-nroff formatter class
553in the future, and this may change what options are supported.
554
555=head1 SEE ALSO
556
557L<Pod::Man>, L<Pod::Perldoc>, L<Pod::Perldoc::ToNroff>
558
559=head1 COPYRIGHT AND DISCLAIMERS
560
561Copyright (c) 2011 brian d foy. All rights reserved.
562
563Copyright (c) 2002,3,4 Sean M. Burke.  All rights reserved.
564
565This library is free software; you can redistribute it and/or modify it
566under the same terms as Perl itself.
567
568This program is distributed in the hope that it will be useful, but
569without any warranty; without even the implied warranty of
570merchantability or fitness for a particular purpose.
571
572=head1 AUTHOR
573
574Current maintainer: Mark Allen C<< <mallen@cpan.org> >>
575
576Past contributions from:
577brian d foy C<< <bdfoy@cpan.org> >>
578Adriano R. Ferreira C<< <ferreira@cpan.org> >>,
579Sean M. Burke C<< <sburke@cpan.org> >>
580
581=cut
582
583