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.28'; 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 || $self->is_freebsd || $self->is_bitrig ) { 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 = map { $_, $self->{$_} } 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 # Then again, do *not* comment it out on OpenBSD: 148 # mandoc handles UTF-8 input just fine. 149 push @switches, 'utf8' => 1; 150 151 $self->debug( "Pod::Man switches are [@switches]\n" ); 152 153 return @switches; 154 } 155 156sub _parse_with_pod_man { 157 my( $self, $file ) = @_; 158 159 #->output_fh and ->output_string from Pod::Simple aren't 160 # working, apparently, so there's this ugly hack: 161 local *STDOUT; 162 open STDOUT, '>', $self->{_text_ref}; 163 my $parser = Pod::Man->new( $self->_get_podman_switches ); 164 $self->debug( "Parsing $file\n" ); 165 $parser->parse_from_file( $file ); 166 $self->debug( "Done parsing $file\n" ); 167 close STDOUT; 168 169 $self->die( "No output from Pod::Man!\n" ) 170 unless length $self->{_text_ref}; 171 172 $self->_save_pod_man_output if $self->debugging; 173 174 return SUCCESS; 175 } 176 177sub _save_pod_man_output { 178 my( $self, $fh ) = @_; 179 180 $fh = do { 181 my $file = "podman.out.$$.txt"; 182 $self->debug( "Writing $file with Pod::Man output\n" ); 183 open my $fh2, '>', $file; 184 $fh2; 185 } unless $fh; 186 187 print { $fh } ${ $self->{_text_ref} }; 188 } 189 190sub _have_groff_with_utf8 { 191 my( $self ) = @_; 192 193 return 0 unless $self->_is_groff; 194 my $roffer = $self->__nroffer; 195 196 my $minimum_groff_version = '1.20.1'; 197 198 my $version_string = `$roffer -v`; 199 my( $version ) = $version_string =~ /\(?groff\)? version (\d+\.\d+(?:\.\d+)?)/; 200 $self->debug( "Found groff $version\n" ); 201 202 # is a string comparison good enough? 203 if( $version lt $minimum_groff_version ) { 204 $self->warn( 205 "You have an old groff." . 206 " Update to version $minimum_groff_version for good Unicode support.\n" . 207 "If you don't upgrade, wide characters may come out oddly.\n" 208 ); 209 } 210 211 $version ge $minimum_groff_version; 212 } 213 214sub _collect_nroff_switches { 215 my( $self ) = shift; 216 217 my @render_switches = ('-man', $self->_get_device_switches); 218 219 # Thanks to Brendan O'Dea for contributing the following block 220 if( $self->_is_roff and -t STDOUT and my ($cols) = $self->_get_columns ) { 221 my $c = $cols * 39 / 40; 222 $cols = $c > $cols - 2 ? $c : $cols -2; 223 push @render_switches, '-rLL=' . (int $c) . 'n' if $cols > 80; 224 } 225 226 if( $self->_is_mandoc ) { 227 push @render_switches, '-Owidth=' . $self->_get_columns; 228 } 229 230 # I hear persistent reports that adding a -c switch to $render 231 # solves many people's problems. But I also hear that some mans 232 # don't have a -c switch, so that unconditionally adding it here 233 # would presumably be a Bad Thing -- sburke@cpan.org 234 push @render_switches, '-c' if( $self->_is_roff and $self->is_cygwin ); 235 236 return @render_switches; 237 } 238 239sub _get_device_switches { 240 my( $self ) = @_; 241 242 if( $self->_is_nroff ) { qw() } 243 elsif( $self->_have_groff_with_utf8 ) { qw(-Kutf8 -Tutf8) } 244 elsif( $self->_is_ebcdic ) { qw(-Tcp1047) } 245 elsif( $self->_is_mandoc ) { qw() } 246 else { qw(-Tlatin1) } 247 } 248 249sub _is_roff { 250 my( $self ) = @_; 251 252 $self->_is_nroff or $self->_is_groff; 253 } 254 255sub _is_nroff { 256 my( $self ) = @_; 257 258 $self->__nroffer =~ /\bnroff\b/; 259 } 260 261sub _is_groff { 262 my( $self ) = @_; 263 264 $self->__nroffer =~ /\bgroff\b/; 265 } 266 267sub _is_mandoc { 268 my ( $self ) = @_; 269 270 $self->__nroffer =~ /\bmandoc\b/; 271 } 272 273sub _is_ebcdic { 274 my( $self ) = @_; 275 276 return 0; 277 } 278 279sub _filter_through_nroff { 280 my( $self ) = shift; 281 $self->debug( "Filtering through " . $self->__nroffer() . "\n" ); 282 283 # Maybe someone set rendering switches as part of the opt_n value 284 # Deal with that here. 285 286 my ($render, $switches) = $self->__nroffer() =~ /\A([\/a-zA-Z0-9_\.-]+)\b(.+)?\z/; 287 288 $self->die("no nroffer!?") unless $render; 289 my @render_switches = $self->_collect_nroff_switches; 290 291 if ( $switches ) { 292 # Eliminate whitespace 293 $switches =~ s/\s//g; 294 295 # Then separate the switches with a zero-width positive 296 # lookahead on the dash. 297 # 298 # See: 299 # http://www.effectiveperlprogramming.com/blog/1411 300 # for a good discussion of this technique 301 302 push @render_switches, split(/(?=-)/, $switches); 303 } 304 305 $self->debug( "render is $render\n" ); 306 $self->debug( "render options are @render_switches\n" ); 307 308 require Symbol; 309 require IPC::Open3; 310 require IO::Handle; 311 312 my $pid = IPC::Open3::open3( 313 my $writer, 314 my $reader, 315 my $err = Symbol::gensym(), 316 $render, 317 @render_switches 318 ); 319 320 $reader->autoflush(1); 321 322 use IO::Select; 323 my $selector = IO::Select->new( $reader ); 324 325 $self->debug( "Writing to pipe to $render\n" ); 326 327 my $offset = 0; 328 my $chunk_size = 4096; 329 my $length = length( ${ $self->{_text_ref} } ); 330 my $chunks = $length / $chunk_size; 331 my $done; 332 my $buffer; 333 while( $offset <= $length ) { 334 $self->debug( "Writing chunk $chunks\n" ); $chunks++; 335 syswrite $writer, ${ $self->{_text_ref} }, $chunk_size, $offset 336 or $self->die( $! ); 337 $offset += $chunk_size; 338 $self->debug( "Checking read\n" ); 339 READ: { 340 last READ unless $selector->can_read( 0.01 ); 341 $self->debug( "Reading\n" ); 342 my $bytes = sysread $reader, $buffer, 4096; 343 $self->debug( "Read $bytes bytes\n" ); 344 $done .= $buffer; 345 $self->debug( sprintf "Output is %d bytes\n", 346 length $done 347 ); 348 next READ; 349 } 350 } 351 close $writer; 352 $self->debug( "Done writing\n" ); 353 354 # read any leftovers 355 $done .= do { local $/; <$reader> }; 356 $self->debug( sprintf "Done reading. Output is %d bytes\n", 357 length $done 358 ); 359 360 # wait for it to exit 361 waitpid( $pid, 0 ); 362 363 if( $? ) { 364 $self->warn( "Error from pipe to $render!\n" ); 365 $self->debug( 'Error: ' . do { local $/; <$err> } ); 366 } 367 368 369 close $reader; 370 if( my $err = $? ) { 371 $self->debug( 372 "Nonzero exit ($?) while running `$render @render_switches`.\n" . 373 "Falling back to Pod::Perldoc::ToPod\n" 374 ); 375 return $self->_fallback_to_pod( @_ ); 376 } 377 378 $self->debug( "Output:\n----\n$done\n----\n" ); 379 380 ${ $self->{_text_ref} } = $done; 381 382 return length ${ $self->{_text_ref} } ? SUCCESS : FAILED; 383 } 384 385sub parse_from_file { 386 my( $self, $file, $outfh) = @_; 387 388 # We have a pipeline of filters each affecting the reference 389 # in $self->{_text_ref} 390 $self->{_text_ref} = \my $output; 391 392 $self->_parse_with_pod_man( $file ); 393 # so far, nroff is an external command so we ensure it worked 394 my $result = $self->_filter_through_nroff; 395 return $self->_fallback_to_pod( @_ ) unless $result == SUCCESS; 396 397 $self->_post_nroff_processing; 398 399 print { $outfh } $output or 400 $self->die( "Can't print to $$self{__output_file}: $!" ); 401 402 return; 403 } 404 405sub _fallback_to_pod { 406 my( $self, @args ) = @_; 407 $self->warn( "Falling back to Pod because there was a problem!\n" ); 408 require Pod::Perldoc::ToPod; 409 return Pod::Perldoc::ToPod->new->parse_from_file(@_); 410 } 411 412# maybe there's a user setting we should check? 413sub _get_tab_width { 4 } 414 415sub _expand_tabs { 416 my( $self ) = @_; 417 418 my $tab_width = ' ' x $self->_get_tab_width; 419 420 ${ $self->{_text_ref} } =~ s/\t/$tab_width/g; 421 } 422 423sub _post_nroff_processing { 424 my( $self ) = @_; 425 426 if( $self->is_hpux ) { 427 $self->debug( "On HP-UX, I'm going to expand tabs for you\n" ); 428 # this used to be a pipe to `col -x` for HP-UX 429 $self->_expand_tabs; 430 } 431 432 if( $self->{'__filter_nroff'} ) { 433 $self->debug( "filter_nroff is set, so filtering\n" ); 434 $self->_remove_nroff_header; 435 $self->_remove_nroff_footer; 436 } 437 else { 438 $self->debug( "filter_nroff is not set, so not filtering\n" ); 439 } 440 441 $self->_handle_unicode; 442 443 return 1; 444 } 445 446# I don't think this does anything since there aren't two consecutive 447# newlines in the Pod::Man output 448sub _remove_nroff_header { 449 my( $self ) = @_; 450 $self->debug( "_remove_nroff_header is still a stub!\n" ); 451 return 1; 452 453# my @data = split /\n{2,}/, shift; 454# shift @data while @data and $data[0] !~ /\S/; # Go to header 455# shift @data if @data and $data[0] =~ /Contributed\s+Perl/; # Skip header 456 } 457 458# I don't think this does anything since there aren't two consecutive 459# newlines in the Pod::Man output 460sub _remove_nroff_footer { 461 my( $self ) = @_; 462 $self->debug( "_remove_nroff_footer is still a stub!\n" ); 463 return 1; 464 ${ $self->{_text_ref} } =~ s/\n\n+.*\w.*\Z//m; 465 466# my @data = split /\n{2,}/, shift; 467# pop @data if @data and $data[-1] =~ /^\w/; # Skip footer, like 468 # 28/Jan/99 perl 5.005, patch 53 1 469 } 470 471sub _unicode_already_handled { 472 my( $self ) = @_; 473 474 $self->_have_groff_with_utf8 || 475 1 # so, we don't have a case that needs _handle_unicode 476 ; 477 } 478 479sub _handle_unicode { 480# this is the job of preconv 481# we don't need this with groff 1.20 and later. 482 my( $self ) = @_; 483 484 return 1 if $self->_unicode_already_handled; 485 486 require Encode; 487 488 # it's UTF-8 here, but we need character data 489 my $text = Encode::decode( 'UTF-8', ${ $self->{_text_ref} } ) ; 490 491# http://www.mail-archive.com/groff@gnu.org/msg01378.html 492# http://linux.die.net/man/7/groff_char 493# http://www.gnu.org/software/groff/manual/html_node/Using-Symbols.html 494# http://lists.gnu.org/archive/html/groff/2011-05/msg00007.html 495# http://www.simplicidade.org/notes/archives/2009/05/fixing_the_pod.html 496# http://lists.freebsd.org/pipermail/freebsd-questions/2011-July/232239.html 497 $text =~ s/(\P{ASCII})/ 498 sprintf '\\[u%04X]', ord $1 499 /eg; 500 501 # should we encode? 502 ${ $self->{_text_ref} } = $text; 503 } 504 5051; 506 507__END__ 508 509=head1 NAME 510 511Pod::Perldoc::ToMan - let Perldoc render Pod as man pages 512 513=head1 SYNOPSIS 514 515 perldoc -o man Some::Modulename 516 517=head1 DESCRIPTION 518 519This is a "plug-in" class that allows Perldoc to use 520Pod::Man and C<groff> for reading Pod pages. 521 522The following options are supported: center, date, fixed, fixedbold, 523fixeditalic, fixedbolditalic, quotes, release, section 524 525(Those options are explained in L<Pod::Man>.) 526 527For example: 528 529 perldoc -o man -w center:Pod Some::Modulename 530 531=head1 CAVEAT 532 533This module may change to use a different pod-to-nroff formatter class 534in the future, and this may change what options are supported. 535 536=head1 SEE ALSO 537 538L<Pod::Man>, L<Pod::Perldoc>, L<Pod::Perldoc::ToNroff> 539 540=head1 COPYRIGHT AND DISCLAIMERS 541 542Copyright (c) 2011 brian d foy. All rights reserved. 543 544Copyright (c) 2002,3,4 Sean M. Burke. All rights reserved. 545 546This library is free software; you can redistribute it and/or modify it 547under the same terms as Perl itself. 548 549This program is distributed in the hope that it will be useful, but 550without any warranty; without even the implied warranty of 551merchantability or fitness for a particular purpose. 552 553=head1 AUTHOR 554 555Current maintainer: Mark Allen C<< <mallen@cpan.org> >> 556 557Past contributions from: 558brian d foy C<< <bdfoy@cpan.org> >> 559Adriano R. Ferreira C<< <ferreira@cpan.org> >>, 560Sean M. Burke C<< <sburke@cpan.org> >> 561 562=cut 563 564