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