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