1 # Pod::Man -- Convert POD data to formatted *roff input.
2 # $Id: Man.pm,v 1.16 2001/04/09 13:06:02 eagle Exp $
4 # Copyright 1999, 2000, 2001 by Russ Allbery <rra@stanford.edu>
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
9 # This module is intended to be a replacement for the pod2man script
10 # distributed with versions of Perl prior to 5.6, and attempts to match its
11 # output except for some specific circumstances where other decisions seemed
12 # to produce better output. It uses Pod::Parser and is designed to be easy
15 # Perl core hackers, please note that this module is also separately
16 # maintained outside of the Perl core as part of the podlators. Please send
17 # me any patches at the address above in addition to sending them to the
18 # standard Perl mailing lists.
20 ############################################################################
21 # Modules and declarations
22 ############################################################################
28 use Carp qw(carp croak);
32 use subs qw(makespace);
33 use vars qw(@ISA %ESCAPES $PREAMBLE $VERSION);
35 @ISA = qw(Pod::Parser);
37 # Don't use the CVS revision as the version, since this module is also in
38 # Perl core and too many things could munge CVS magic revision strings.
39 # This number should ideally be the same as the CVS revision in podlators,
44 ############################################################################
45 # Preamble and *roff output tables
46 ############################################################################
48 # The following is the static preamble which starts all *roff output we
49 # generate. It's completely static except for the font to use as a
50 # fixed-width font, which is designed by @CFONT@, and the left and right
51 # quotes to use for C<> text, designated by @LQOUTE@ and @RQUOTE@.
52 # $PREAMBLE should therefore be run through s/\@CFONT\@/<font>/g before
54 $PREAMBLE = <<'----END OF PREAMBLE----';
55 .de Sh \" Subsection heading
63 .de Sp \" Vertical space (when we can't use .PP)
69 .ie \\n(.$>=3 .ne \\$3
73 .de Vb \" Begin verbatim text
78 .de Ve \" End verbatim text
83 .\" Set up some character translations and predefined strings. \*(-- will
84 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
85 .\" double quote, and \*(R" will give a right double quote. | will give a
86 .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used
87 .\" to do unbreakable dashes and therefore won't be available. \*(C` and
88 .\" \*(C' expand to `' in nroff, nothing in troff, for use with C<>
90 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
94 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
95 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
108 .\" If the F register is turned on, we'll generate index entries on stderr
109 .\" for titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and
110 .\" index entries marked with X<> in POD. Of course, you'll have to process
111 .\" the output yourself in some meaningful fashion.
114 . tm Index:\\$1\t\\n%\t"\\$2"
120 .\" For nroff, turn off justification. Always turn off hyphenation; it
121 .\" makes way too many mistakes in technical documents.
125 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
126 .\" Fear. Run. Save yourself. No user-serviceable parts.
127 . \" fudge factors for nroff and troff
136 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
142 . \" simple accents for nroff and troff
152 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
153 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
154 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
155 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
156 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
157 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
159 . \" troff and (daisy-wheel) nroff accents
160 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
161 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
162 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
163 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
164 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
165 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
166 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
167 .ds ae a\h'-(\w'a'u*4/10)'e
168 .ds Ae A\h'-(\w'A'u*4/10)'E
169 . \" corrections for vroff
170 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
171 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
172 . \" for low resolution devices (crt and lpr)
173 .if \n(.H>23 .if \n(.V>19 \
186 ----END OF PREAMBLE----
189 # This table is taken nearly verbatim from Tom Christiansen's pod2man. It
190 # assumes that the standard preamble has already been printed, since that's
191 # what defines all of the accent marks. Note that some of these are quoted
192 # with double quotes since they contain embedded single quotes, so use \\
193 # uniformly for backslash for readability.
195 'amp' => '&', # ampersand
196 'lt' => '<', # left chevron, less-than
197 'gt' => '>', # right chevron, greater-than
198 'quot' => '"', # double quote
199 'sol' => '/', # solidus (forward slash)
200 'verbar' => '|', # vertical bar
202 'Aacute' => "A\\*'", # capital A, acute accent
203 'aacute' => "a\\*'", # small a, acute accent
204 'Acirc' => 'A\\*^', # capital A, circumflex accent
205 'acirc' => 'a\\*^', # small a, circumflex accent
206 'AElig' => '\*(AE', # capital AE diphthong (ligature)
207 'aelig' => '\*(ae', # small ae diphthong (ligature)
208 'Agrave' => "A\\*`", # capital A, grave accent
209 'agrave' => "A\\*`", # small a, grave accent
210 'Aring' => 'A\\*o', # capital A, ring
211 'aring' => 'a\\*o', # small a, ring
212 'Atilde' => 'A\\*~', # capital A, tilde
213 'atilde' => 'a\\*~', # small a, tilde
214 'Auml' => 'A\\*:', # capital A, dieresis or umlaut mark
215 'auml' => 'a\\*:', # small a, dieresis or umlaut mark
216 'Ccedil' => 'C\\*,', # capital C, cedilla
217 'ccedil' => 'c\\*,', # small c, cedilla
218 'Eacute' => "E\\*'", # capital E, acute accent
219 'eacute' => "e\\*'", # small e, acute accent
220 'Ecirc' => 'E\\*^', # capital E, circumflex accent
221 'ecirc' => 'e\\*^', # small e, circumflex accent
222 'Egrave' => 'E\\*`', # capital E, grave accent
223 'egrave' => 'e\\*`', # small e, grave accent
224 'ETH' => '\\*(D-', # capital Eth, Icelandic
225 'eth' => '\\*(d-', # small eth, Icelandic
226 'Euml' => 'E\\*:', # capital E, dieresis or umlaut mark
227 'euml' => 'e\\*:', # small e, dieresis or umlaut mark
228 'Iacute' => "I\\*'", # capital I, acute accent
229 'iacute' => "i\\*'", # small i, acute accent
230 'Icirc' => 'I\\*^', # capital I, circumflex accent
231 'icirc' => 'i\\*^', # small i, circumflex accent
232 'Igrave' => 'I\\*`', # capital I, grave accent
233 'igrave' => 'i\\*`', # small i, grave accent
234 'Iuml' => 'I\\*:', # capital I, dieresis or umlaut mark
235 'iuml' => 'i\\*:', # small i, dieresis or umlaut mark
236 'Ntilde' => 'N\*~', # capital N, tilde
237 'ntilde' => 'n\*~', # small n, tilde
238 'Oacute' => "O\\*'", # capital O, acute accent
239 'oacute' => "o\\*'", # small o, acute accent
240 'Ocirc' => 'O\\*^', # capital O, circumflex accent
241 'ocirc' => 'o\\*^', # small o, circumflex accent
242 'Ograve' => 'O\\*`', # capital O, grave accent
243 'ograve' => 'o\\*`', # small o, grave accent
244 'Oslash' => 'O\\*/', # capital O, slash
245 'oslash' => 'o\\*/', # small o, slash
246 'Otilde' => 'O\\*~', # capital O, tilde
247 'otilde' => 'o\\*~', # small o, tilde
248 'Ouml' => 'O\\*:', # capital O, dieresis or umlaut mark
249 'ouml' => 'o\\*:', # small o, dieresis or umlaut mark
250 'szlig' => '\*8', # small sharp s, German (sz ligature)
251 'THORN' => '\\*(Th', # capital THORN, Icelandic
252 'thorn' => '\\*(th', # small thorn, Icelandic
253 'Uacute' => "U\\*'", # capital U, acute accent
254 'uacute' => "u\\*'", # small u, acute accent
255 'Ucirc' => 'U\\*^', # capital U, circumflex accent
256 'ucirc' => 'u\\*^', # small u, circumflex accent
257 'Ugrave' => 'U\\*`', # capital U, grave accent
258 'ugrave' => 'u\\*`', # small u, grave accent
259 'Uuml' => 'U\\*:', # capital U, dieresis or umlaut mark
260 'uuml' => 'u\\*:', # small u, dieresis or umlaut mark
261 'Yacute' => "Y\\*'", # capital Y, acute accent
262 'yacute' => "y\\*'", # small y, acute accent
263 'yuml' => 'y\\*:', # small y, dieresis or umlaut mark
267 ############################################################################
268 # Static helper functions
269 ############################################################################
271 # Protect leading quotes and periods against interpretation as commands.
272 # Also protect anything starting with a backslash, since it could expand
273 # or hide something that *roff would interpret as a command. This is
274 # overkill, but it's much simpler than trying to parse *roff here.
277 s/^([.\'\\])/\\&$1/mg;
281 # Translate a font string into an escape.
282 sub toescape { (length ($_[0]) > 1 ? '\f(' : '\f') . $_[0] }
285 ############################################################################
287 ############################################################################
289 # Initialize the object. Here, we also process any additional options
290 # passed to the constructor or set up defaults if none were given. center
291 # is the centered title, release is the version number, and date is the date
292 # for the documentation. Note that we can't know what file name we're
293 # processing due to the architecture of Pod::Parser, so that *has* to either
294 # be passed to the constructor or set separately with Pod::Man::name().
298 # Figure out the fixed-width font. If user-supplied, make sure that
299 # they are the right length.
300 for (qw/fixed fixedbold fixeditalic fixedbolditalic/) {
301 if (defined $$self{$_}) {
302 if (length ($$self{$_}) < 1 || length ($$self{$_}) > 2) {
303 croak qq(roff font should be 1 or 2 chars,)
304 . qq( not "$$self{$_}");
311 # Set the default fonts. We can't be sure what fixed bold-italic is
312 # going to be called, so default to just bold.
313 $$self{fixed} ||= 'CW';
314 $$self{fixedbold} ||= 'CB';
315 $$self{fixeditalic} ||= 'CI';
316 $$self{fixedbolditalic} ||= 'CB';
318 # Set up a table of font escapes. First number is fixed-width, second
319 # is bold, third is italic.
320 $$self{FONTS} = { '000' => '\fR', '001' => '\fI',
321 '010' => '\fB', '011' => '\f(BI',
322 '100' => toescape ($$self{fixed}),
323 '101' => toescape ($$self{fixeditalic}),
324 '110' => toescape ($$self{fixedbold}),
325 '111' => toescape ($$self{fixedbolditalic})};
327 # Extra stuff for page titles.
328 $$self{center} = 'User Contributed Perl Documentation'
329 unless defined $$self{center};
330 $$self{indent} = 4 unless defined $$self{indent};
332 # We used to try first to get the version number from a local binary,
333 # but we shouldn't need that any more. Get the version from the running
334 # Perl. Work a little magic to handle subversions correctly under both
335 # the pre-5.6 and the post-5.6 version numbering schemes.
336 if (!defined $$self{release}) {
337 my @version = ($] =~ /^(\d+)\.(\d{3})(\d{0,3})$/);
339 $version[2] *= 10 ** (3 - length $version[2]);
340 for (@version) { $_ += 0 }
341 $$self{release} = 'perl v' . join ('.', @version);
344 # Double quotes in things that will be quoted.
345 for (qw/center date release/) {
346 $$self{$_} =~ s/\"/\"\"/g if $$self{$_};
349 # Figure out what quotes we'll be using for C<> text.
350 $$self{quotes} ||= '"';
351 if ($$self{quotes} eq 'none') {
352 $$self{LQUOTE} = $$self{RQUOTE} = '';
353 } elsif (length ($$self{quotes}) == 1) {
354 $$self{LQUOTE} = $$self{RQUOTE} = $$self{quotes};
355 } elsif ($$self{quotes} =~ /^(.)(.)$/
356 || $$self{quotes} =~ /^(..)(..)$/) {
360 croak qq(Invalid quote specification "$$self{quotes}");
363 # Double the first quote; note that this should not be s///g as two
364 # double quotes is represented in *roff as three double quotes, not
365 # four. Weird, I know.
366 $$self{LQUOTE} =~ s/\"/\"\"/;
367 $$self{RQUOTE} =~ s/\"/\"\"/;
369 $$self{INDENT} = 0; # Current indentation level.
370 $$self{INDENTS} = []; # Stack of indentations.
371 $$self{INDEX} = []; # Index keys waiting to be printed.
372 $$self{ITEMS} = 0; # The number of consecutive =items.
374 $self->SUPER::initialize;
377 # For each document we process, output the preamble first.
381 # Try to figure out the name and section from the file name.
382 my $section = $$self{section} || 1;
383 my $name = $$self{name};
384 if (!defined $name) {
385 $name = $self->input_file;
386 $section = 3 if (!$$self{section} && $name =~ /\.pm\z/i);
387 $name =~ s/\.p(od|[lm])\z//i;
388 if ($section =~ /^1/) {
389 require File::Basename;
390 $name = uc File::Basename::basename ($name);
392 # Lose everything up to the first of
393 # */lib/*perl* standard or site_perl module
394 # */*perl*/lib from -D prefix=/opt/perl
395 # */*perl*/ random module hierarchy
396 # which works. Should be fixed to use File::Spec. Also handle
397 # a leading lib/ since that's what ExtUtils::MakeMaker creates.
400 if ( s%^.*?/lib/[^/]*perl[^/]*/%%si
401 or s%^.*?/[^/]*perl[^/]*/(?:lib/)?%%si) {
402 s%^site(_perl)?/%%s; # site and site_perl
403 s%^(.*-$^O|$^O-.*)/%%so; # arch
404 s%^\d+\.\d+%%s; # version
412 # If $name contains spaces, quote it; this mostly comes up in the case
413 # of input from stdin.
414 $name = '"' . $name . '"' if ($name =~ /\s/);
416 # Modification date header. Try to use the modification time of our
418 if (!defined $$self{date}) {
419 my $time = (stat $self->input_file)[9] || time;
420 my ($day, $month, $year) = (localtime $time)[3,4,5];
423 $$self{date} = sprintf ('%4d-%02d-%02d', $year, $month, $day);
426 # Now, print out the preamble and the title.
427 local $_ = $PREAMBLE;
428 s/\@CFONT\@/$$self{fixed}/;
429 s/\@LQUOTE\@/$$self{LQUOTE}/;
430 s/\@RQUOTE\@/$$self{RQUOTE}/;
432 print { $self->output_handle } <<"----END OF HEADER----";
433 .\\" Automatically generated by Pod::Man version $VERSION
434 .\\" @{[ scalar localtime ]}
436 .\\" Standard preamble:
437 .\\" ======================================================================
439 .\\" ======================================================================
441 .IX Title "$name $section"
442 .TH $name $section "$$self{release}" "$$self{date}" "$$self{center}"
444 ----END OF HEADER----
447 # Initialize a few per-file variables.
449 $$self{NEEDSPACE} = 0;
453 ############################################################################
455 ############################################################################
457 # Called for each command paragraph. Gets the command, the associated
458 # paragraph, the line number, and a Pod::Paragraph object. Just dispatches
459 # the command to a method named the same as the command. =cut is handled
460 # internally by Pod::Parser.
464 return if $command eq 'pod';
465 return if ($$self{EXCLUDE} && $command ne 'end');
466 if ($self->can ('cmd_' . $command)) {
467 $command = 'cmd_' . $command;
468 $self->$command (@_);
470 my ($text, $line, $paragraph) = @_;
472 ($file, $line) = $paragraph->file_line;
474 $text = " $text" if ($text =~ /^\S/);
475 warn qq($file:$line: Unknown command paragraph "=$command$text"\n);
480 # Called for a verbatim paragraph. Gets the paragraph, the line number, and
481 # a Pod::Paragraph object. Rofficate backslashes, untabify, put a
482 # zero-width character at the beginning of each line to protect against
483 # commands, and wrap in .Vb/.Ve.
486 return if $$self{EXCLUDE};
490 my $lines = tr/\n/\n/;
491 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
493 s/^(\s*\S)/'\&' . $1/gme;
495 $self->output (".Vb $lines\n$_.Ve\n");
496 $$self{NEEDSPACE} = 0;
499 # Called for a regular text block. Gets the paragraph, the line number, and
500 # a Pod::Paragraph object. Perform interpolation and output the results.
503 return if $$self{EXCLUDE};
504 $self->output ($_[0]), return if $$self{VERBATIM};
506 # Perform a little magic to collapse multiple L<> references. We'll
507 # just rewrite the whole thing into actual text at this part, bypassing
508 # the whole internal sequence parsing thing.
511 (L< # A link of the form L</something>.
514 [:\w]+ # The item has to be a simple word...
515 (\(\))? # ...or simple function.
519 ,?\s+(and\s+)? # Allow lots of them, conjuncted.
528 s{ L< / ( [^>]+ ) > } {$1}xg;
529 my @items = split /(?:,?\s+(?:and\s+)?)/;
532 for ($i = 0; $i < @items; $i++) {
533 $string .= $items[$i];
534 $string .= ', ' if @items > 2 && $i != $#items;
535 $string .= ' ' if @items == 2 && $i == 2;
536 $string .= 'and ' if ($i == $#items - 1);
538 $string .= ' entries elsewhere in this document';
542 # Parse the tree and output it. collapse knows about references to
543 # scalars as well as scalars and does the right thing with them.
544 $text = $self->parse ($text, @_);
545 $text =~ s/\n\s*$/\n/;
547 $self->output (protect $self->textmapfonts ($text));
549 $$self{NEEDSPACE} = 1;
552 # Called for an interior sequence. Takes a Pod::InteriorSequence object and
553 # returns a reference to a scalar. This scalar is the final formatted text.
554 # It's returned as a reference so that other interior sequences above us
555 # know that the text has already been processed.
557 my ($self, $seq) = @_;
558 my $command = $seq->cmd_name;
560 # Zero-width characters.
561 if ($command eq 'Z') {
562 # Workaround to generate a blessable reference, needed by 5.005.
564 return bless \ "$tmp", 'Pod::Man::String';
567 # C<>, L<>, X<>, and E<> don't apply guesswork to their contents. C<>
568 # needs some additional special handling.
569 my $literal = ($command =~ /^[CELX]$/);
570 $literal++ if $command eq 'C';
571 local $_ = $self->collapse ($seq->parse_tree, $literal);
573 # Handle E<> escapes.
574 if ($command eq 'E') {
576 return bless \ chr ($_), 'Pod::Man::String';
577 } elsif (exists $ESCAPES{$_}) {
578 return bless \ "$ESCAPES{$_}", 'Pod::Man::String';
580 carp "Unknown escape E<$1>";
581 return bless \ "E<$_>", 'Pod::Man::String';
585 # For all the other sequences, empty content produces no output.
586 return '' if $_ eq '';
588 # Handle formatting sequences.
589 if ($command eq 'B') {
590 return bless \ ('\f(BS' . $_ . '\f(BE'), 'Pod::Man::String';
591 } elsif ($command eq 'F') {
592 return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
593 } elsif ($command eq 'I') {
594 return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
595 } elsif ($command eq 'C') {
596 return bless \ ('\f(FS\*(C`' . $_ . "\\*(C'\\f(FE"),
601 if ($command eq 'L') {
602 # A bug in lvalue subs in 5.6 requires the temporary variable.
603 my $tmp = $self->buildlink ($_);
604 return bless \ "$tmp", 'Pod::Man::String';
607 # Whitespace protection replaces whitespace with "\ ".
608 if ($command eq 'S') {
610 return bless \ "$_", 'Pod::Man::String';
613 # Add an index entry to the list of ones waiting to be output.
614 if ($command eq 'X') { push (@{ $$self{INDEX} }, $_); return '' }
616 # Anything else is unknown.
617 carp "Unknown sequence $command<$_>";
621 ############################################################################
623 ############################################################################
625 # All command paragraphs take the paragraph and the line number.
627 # First level heading. We can't output .IX in the NAME section due to a bug
628 # in some versions of catman, so don't output a .IX for that section. .SH
629 # already uses small caps, so remove any E<> sequences that would cause
633 local $_ = $self->parse (@_);
637 if ($$self{ITEMS} > 1) {
639 $self->output (".PD\n");
641 $self->output ($self->switchquotes ('.SH', $self->mapfonts ($_)));
642 $self->outindex (($_ eq 'NAME') ? () : ('Header', $_));
643 $$self{NEEDSPACE} = 0;
646 # Second level heading.
649 local $_ = $self->parse (@_);
652 if ($$self{ITEMS} > 1) {
654 $self->output (".PD\n");
656 $self->output ($self->switchquotes ('.Sh', $self->mapfonts ($_)));
657 $self->outindex ('Subsection', $_);
658 $$self{NEEDSPACE} = 0;
661 # Third level heading.
664 local $_ = $self->parse (@_);
667 if ($$self{ITEMS} > 1) {
669 $self->output (".PD\n");
672 $self->output ($self->switchquotes ('.I', $self->mapfonts ($_)));
673 $self->outindex ('Subsection', $_);
674 $$self{NEEDSPACE} = 1;
677 # Fourth level heading.
680 local $_ = $self->parse (@_);
683 if ($$self{ITEMS} > 1) {
685 $self->output (".PD\n");
688 $self->output ($self->textmapfonts ($_) . "\n");
689 $self->outindex ('Subsection', $_);
690 $$self{NEEDSPACE} = 1;
693 # Start a list. For indents after the first, wrap the outside indent in .RS
694 # so that hanging paragraph tags will be correct.
698 unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
699 if (@{ $$self{INDENTS} } > 0) {
700 $self->output (".RS $$self{INDENT}\n");
702 push (@{ $$self{INDENTS} }, $$self{INDENT});
703 $$self{INDENT} = ($_ + 0);
706 # End a list. If we've closed an embedded indent, we've mangled the hanging
707 # paragraph indent, so temporarily replace it with .RS and set WEIRDINDENT.
708 # We'll close that .RS at the next =back or =item.
711 $$self{INDENT} = pop @{ $$self{INDENTS} };
712 unless (defined $$self{INDENT}) {
713 carp "Unmatched =back";
716 if ($$self{WEIRDINDENT}) {
717 $self->output (".RE\n");
718 $$self{WEIRDINDENT} = 0;
720 if (@{ $$self{INDENTS} } > 0) {
721 $self->output (".RE\n");
722 $self->output (".RS $$self{INDENT}\n");
723 $$self{WEIRDINDENT} = 1;
725 $$self{NEEDSPACE} = 1;
728 # An individual list item. Emit an index entry for anything that's
729 # interesting, but don't emit index entries for things like bullets and
730 # numbers. rofficate bullets too while we're at it (so for nice output, use
731 # * for your lists rather than o or . or - or some other thing). Newlines
732 # in an item title are turned into spaces since *roff can't handle them
736 local $_ = $self->parse (@_);
740 if (/\w/ && !/^\w[.\)]\s*$/) {
742 $index =~ s/^\s*[-*+o.]?(?:\s+|\Z)//;
744 s/^\*(\s|\Z)/\\\(bu$1/;
745 if ($$self{WEIRDINDENT}) {
746 $self->output (".RE\n");
747 $$self{WEIRDINDENT} = 0;
749 $_ = $self->textmapfonts ($_);
750 $self->output (".PD 0\n") if ($$self{ITEMS} == 1);
751 $self->output ($self->switchquotes ('.Ip', $_, $$self{INDENT}));
752 $self->outindex ($index ? ('Item', $index) : ());
753 $$self{NEEDSPACE} = 0;
757 # Begin a block for a particular translator. Setting VERBATIM triggers
758 # special handling in textblock().
762 my ($kind) = /^(\S+)/ or return;
763 if ($kind eq 'man' || $kind eq 'roff') {
764 $$self{VERBATIM} = 1;
770 # End a block for a particular translator. We assume that all =begin/=end
771 # pairs are properly closed.
775 $$self{VERBATIM} = 0;
778 # One paragraph for a particular translator. Ignore it unless it's intended
779 # for man or roff, in which case we output it verbatim.
783 return unless s/^(?:man|roff)\b[ \t]*\n?//;
788 ############################################################################
790 ############################################################################
792 # Handle links. We can't actually make real hyperlinks, so this is all to
793 # figure out what text and formatting we print out.
798 # Smash whitespace in case we were split across multiple lines.
801 # If we were given any explicit text, just output it.
802 if (m{ ^ ([^|]+) \| }x) { return $1 }
804 # Okay, leading and trailing whitespace isn't important.
808 # If the argument looks like a URL, return it verbatim. This only
809 # handles URLs that use the server syntax.
810 if (m%^[a-z]+://\S+$%) { return $_ }
812 # Default to using the whole content of the link entry as a section
813 # name. Note that L<manpage/> forces a manpage interpretation, as does
814 # something looking like L<manpage(section)>. Do the same thing to
815 # L<manpage(section)> as we would to manpage(section) without the L<>;
816 # see guesswork(). If we've added italics, don't add the "manpage"
817 # text; markup is sufficient.
818 my ($manpage, $section) = ('', $_);
819 if (/^"\s*(.*?)\s*"$/) {
820 $section = '"' . $1 . '"';
821 } elsif (m{ ^ [-:.\w]+ (?: \( \S+ \) )? $ }x) {
822 ($manpage, $section) = ($_, '');
823 $manpage =~ s/^([^\(]+)\(/'\f(IS' . $1 . '\f(IE\|('/e;
825 ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
826 if ($manpage =~ /^[-:.\w]+(?:\(\S+\))?$/) {
827 $manpage =~ s/^([^\(]+)\(/'\f(IS' . $1 . '\f(IE\|'/e;
829 $section =~ s/^\"\s*//;
830 $section =~ s/\s*\"$//;
832 if ($manpage && $manpage !~ /\\f\(IS/) {
833 $manpage = "the $manpage manpage";
836 # Now build the actual output text.
838 if (!length ($section) && !length ($manpage)) {
839 carp "Invalid link $_";
840 } elsif (!length ($section)) {
842 } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
843 $text .= 'the ' . $section . ' entry';
844 $text .= (length $manpage) ? " in $manpage"
845 : " elsewhere in this document";
847 if ($section !~ /^".*"$/) { $section = '"' . $section . '"' }
848 $text .= 'the section on ' . $section;
849 $text .= " in $manpage" if length $manpage;
855 ############################################################################
856 # Escaping and fontification
857 ############################################################################
859 # At this point, we'll have embedded font codes of the form \f(<font>[SE]
860 # where <font> is one of B, I, or F. Turn those into the right font start
861 # or end codes. The old pod2man didn't get B<someI<thing> else> right;
862 # after I<> it switched back to normal text rather than bold. We take care
863 # of this by using variables as a combined pointer to our current font
864 # sequence, and set each to the number of current nestings of start tags for
865 # that font. Use them as a vector to look up what font sequence to use.
867 # \fP changes to the previous font, but only one previous font is kept. We
868 # don't know what the outside level font is; normally it's R, but if we're
869 # inside a heading it could be something else. So arrange things so that
870 # the outside font is always the "previous" font and end with \fP instead of
871 # \fR. Idea from Zack Weinberg.
876 my ($fixed, $bold, $italic) = (0, 0, 0);
877 my %magic = (F => \$fixed, B => \$bold, I => \$italic);
882 if ($last ne '\fR') { $sequence = '\fP' }
883 ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
884 $f = $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
888 if ($f ne '\fR') { $sequence .= $f }
896 # Unfortunately, there is a bug in Solaris 2.6 nroff (not present in GNU
897 # groff) where the sequence \fB\fP\f(CW\fP leaves the font set to B rather
898 # than R, presumably because \f(CW doesn't actually do a font change. To
899 # work around this, use a separate textmapfonts for text blocks where the
900 # default font is always R and only use the smart mapfonts for headings.
905 my ($fixed, $bold, $italic) = (0, 0, 0);
906 my %magic = (F => \$fixed, B => \$bold, I => \$italic);
908 ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
909 $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
915 ############################################################################
916 # *roff-specific parsing
917 ############################################################################
919 # Called instead of parse_text, calls parse_text with the right flags.
922 $self->parse_text ({ -expand_seq => 'sequence',
923 -expand_ptree => 'collapse' }, @_);
926 # Takes a parse tree and a flag saying whether or not to treat it as literal
927 # text (not call guesswork on it), and returns the concatenation of all of
928 # the text strings in that parse tree. If the literal flag isn't true,
929 # guesswork() will be called on all plain scalars in the parse tree.
930 # Otherwise, just escape backslashes in the normal case. If collapse is
931 # being called on a C<> sequence, literal is set to 2, and we do some
932 # additional cleanup. Assumes that everything in the parse tree is either a
933 # scalar or a reference to a scalar.
935 my ($self, $ptree, $literal) = @_;
937 return join ('', map {
942 s/-/\\-/g if $literal > 1;
943 s/__/_\\|_/g if $literal > 1;
948 return join ('', map {
949 ref ($_) ? $$_ : $self->guesswork ($_)
954 # Takes a text block to perform guesswork on; this is guaranteed not to
955 # contain any interior sequences. Returns the text block with remapping
961 # rofficate backslashes.
964 # Ensure double underbars have a tiny space between them.
967 # Make all caps a little smaller. Be careful here, since we don't want
968 # to make @ARGV into small caps, nor do we want to fix the MIME in
969 # MIME-Version, since it looks weird with the full-height V.
971 ( ^ | [\s\(\"\'\`\[\{<>] )
972 ( [A-Z] [A-Z] [/A-Z+:\d_\$&-]* )
973 (?: (?= [\s>\}\]\(\)\'\".?!,;] | -- ) | $ )
974 } { $1 . '\s-1' . $2 . '\s0' }egx;
976 # Turn PI into a pretty pi.
977 s{ (?: \\s-1 | \b ) PI (?: \\s0 | \b ) } {\\*\(PI}gx;
979 # Italize functions in the form func().
983 [A-Za-z_] ([:\w]|\\s-?[01])+ \(\)
985 } { $1 . '\f(IS' . $2 . '\f(IE' }egx;
987 # func(n) is a reference to a manual page. Make it \fIfunc\fR\|(n).
990 ( [A-Za-z_] (?:[-:.\w]|\\s-?[01])+ )
994 } { $1 . '\f(IS' . $2 . '\f(IE\|' . $3 }egx;
996 # Convert simple Perl variable references to a fixed-width font.
1001 } { $1 . '\f(FS' . $2 . '\f(FE'}egx;
1003 # Translate -- into a real em dash if it's used like one and fix up
1004 # dashes, but keep hyphens hyphens.
1005 s{ (\G|^|.) (-+) (\b|.) } {
1006 my ($pre, $dash, $post) = ($1, $2, $3);
1007 if (length ($dash) == 1) {
1008 ($pre =~ /[a-zA-Z]/) ? "$pre-$post" : "$pre\\-$post";
1009 } elsif (length ($dash) == 2
1010 && ((!$pre && !$post)
1011 || ($pre =~ /\w/ && !$post)
1012 || ($pre eq ' ' && $post eq ' ')
1013 || ($pre eq '=' && $post ne '=')
1014 || ($pre ne '=' && $post eq '='))) {
1017 $pre . ('\-' x length $dash) . $post;
1021 # Fix up double quotes.
1022 s{ \" ([^\"]+) \" } { '\*(L"' . $1 . '\*(R"' }egx;
1024 # Make C++ into \*(C+, which is a squinched version.
1025 s{ \b C\+\+ } {\\*\(C+}gx;
1032 ############################################################################
1034 ############################################################################
1036 # Make vertical whitespace.
1039 $self->output (".PD\n") if ($$self{ITEMS} > 1);
1041 $self->output ($$self{INDENT} > 0 ? ".Sp\n" : ".PP\n")
1042 if $$self{NEEDSPACE};
1045 # Output any pending index entries, and optionally an index entry given as
1046 # an argument. Support multiple index entries in X<> separated by slashes,
1047 # and strip special escapes from index entries.
1049 my ($self, $section, $index) = @_;
1050 my @entries = map { split m%\s*/\s*% } @{ $$self{INDEX} };
1051 return unless ($section || @entries);
1055 my $output = '.IX Xref "'
1056 . join (' ', map { s/\"/\"\"/; $_ } @entries)
1060 $index =~ s/\"/\"\"/;
1061 $index =~ s/\\-/-/g;
1062 $index =~ s/\\(?:s-?\d|.\(..|.)//g;
1063 $output .= ".IX $section " . '"' . $index . '"' . "\n";
1065 $self->output ($output);
1068 # Output text to the output device.
1069 sub output { print { $_[0]->output_handle } $_[1] }
1071 # Given a command and a single argument that may or may not contain double
1072 # quotes, handle double-quote formatting for it. If there are no double
1073 # quotes, just return the command followed by the argument in double quotes.
1074 # If there are double quotes, use an if statement to test for nroff, and for
1075 # nroff output the command followed by the argument in double quotes with
1076 # embedded double quotes doubled. For other formatters, remap paired double
1077 # quotes to LQUOTE and RQUOTE.
1080 my $command = shift;
1083 s/\\\*\([LR]\"/\"/g;
1085 # We also have to deal with \*C` and \*C', which are used to add the
1086 # quotes around C<> text, since they may expand to " and if they do this
1087 # confuses the .SH macros and the like no end. Expand them ourselves.
1088 # If $extra is set, we're dealing with =item, which in most nroff macro
1089 # sets requires an extra level of quoting of double quotes.
1090 my $c_is_quote = ($$self{LQUOTE} =~ /\"/) || ($$self{RQUOTE} =~ /\"/);
1091 if (/\"/ || ($c_is_quote && /\\\*\(C[\'\`]/)) {
1094 $troff =~ s/\"\"([^\"]*)\"\"/\`\`$1\'\'/g;
1095 s/\\\*\(C\`/$$self{LQUOTE}/g;
1096 s/\\\*\(C\'/$$self{RQUOTE}/g;
1097 $troff =~ s/\\\*\(C[\'\`]//g;
1098 s/\"/\"\"/g if $extra;
1099 $troff =~ s/\"/\"\"/g if $extra;
1100 $_ = qq("$_") . ($extra ? " $extra" : '');
1101 $troff = qq("$troff") . ($extra ? " $extra" : '');
1102 return ".if n $command $_\n.el $command $troff\n";
1104 $_ = qq("$_") . ($extra ? " $extra" : '');
1105 return "$command $_\n";
1111 .\" These are some extra bits of roff that I don't want to lose track of
1112 .\" but that have been removed from the preamble to make it a bit shorter
1113 .\" since they're not currently being used. They're accents and special
1114 .\" characters we don't currently have escapes for.
1121 . ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
1122 . ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
1123 . ds q o\h'-\w'o'u*8/10'\s-4\v'.4m'\z\(*i\v'-.4m'\s+4\h'\w'o'u*8/10'
1125 .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
1126 .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
1127 .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
1128 .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
1129 .ds oe o\h'-(\w'o'u*4/10)'e
1130 .ds Oe O\h'-(\w'O'u*4/10)'E
1131 .if \n(.H>23 .if \n(.V>19 \
1133 . ds v \h'-1'\o'\(aa\(ga'
1141 ############################################################################
1143 ############################################################################
1147 Pod::Man - Convert POD data to formatted *roff input
1152 my $parser = Pod::Man->new (release => $VERSION, section => 8);
1154 # Read POD from STDIN and write to STDOUT.
1155 $parser->parse_from_filehandle;
1157 # Read POD from file.pod and write to file.1.
1158 $parser->parse_from_file ('file.pod', 'file.1');
1162 Pod::Man is a module to convert documentation in the POD format (the
1163 preferred language for documenting Perl) into *roff input using the man
1164 macro set. The resulting *roff code is suitable for display on a terminal
1165 using nroff(1), normally via man(1), or printing using troff(1). It is
1166 conventionally invoked using the driver script B<pod2man>, but it can also
1169 As a derived class from Pod::Parser, Pod::Man supports the same methods and
1170 interfaces. See L<Pod::Parser> for all the details; briefly, one creates a
1171 new parser with C<Pod::Man-E<gt>new()> and then calls either
1172 parse_from_filehandle() or parse_from_file().
1174 new() can take options, in the form of key/value pairs that control the
1175 behavior of the parser. See below for details.
1177 If no options are given, Pod::Man uses the name of the input file with any
1178 trailing C<.pod>, C<.pm>, or C<.pl> stripped as the man page title, to
1179 section 1 unless the file ended in C<.pm> in which case it defaults to
1180 section 3, to a centered title of "User Contributed Perl Documentation", to
1181 a centered footer of the Perl version it is run with, and to a left-hand
1182 footer of the modification date of its input (or the current date if given
1185 Pod::Man assumes that your *roff formatters have a fixed-width font named
1186 CW. If yours is called something else (like CR), use the C<fixed> option to
1187 specify it. This generally only matters for troff output for printing.
1188 Similarly, you can set the fonts used for bold, italic, and bold italic
1191 Besides the obvious pod conversions, Pod::Man also takes care of formatting
1192 func(), func(n), and simple variable references like $foo or @bar so you
1193 don't have to use code escapes for them; complex expressions like
1194 C<$fred{'stuff'}> will still need to be escaped, though. It also translates
1195 dashes that aren't used as hyphens into en dashes, makes long dashes--like
1196 this--into proper em dashes, fixes "paired quotes," makes C++ and PI look
1197 right, puts a little space between double underbars, makes ALLCAPS a teeny
1198 bit smaller in troff(1), and escapes stuff that *roff treats as special so
1199 that you don't have to.
1201 The recognized options to new() are as follows. All options take a single
1208 Sets the centered page header to use instead of "User Contributed Perl
1213 Sets the left-hand footer. By default, the modification date of the input
1214 file will be used, or the current date if stat() can't find that file (the
1215 case if the input is from STDIN), and the date will be formatted as
1220 The fixed-width font to use for vertabim text and code. Defaults to CW.
1221 Some systems may want CR instead. Only matters for troff(1) output.
1225 Bold version of the fixed-width font. Defaults to CB. Only matters for
1230 Italic version of the fixed-width font (actually, something of a misnomer,
1231 since most fixed-width fonts only have an oblique version, not an italic
1232 version). Defaults to CI. Only matters for troff(1) output.
1234 =item fixedbolditalic
1236 Bold italic (probably actually oblique) version of the fixed-width font.
1237 Pod::Man doesn't assume you have this, and defaults to CB. Some systems
1238 (such as Solaris) have this font available as CX. Only matters for troff(1)
1243 Sets the quote marks used to surround CE<lt>> text. If the value is a
1244 single character, it is used as both the left and right quote; if it is two
1245 characters, the first character is used as the left quote and the second as
1246 the right quoted; and if it is four characters, the first two are used as
1247 the left quote and the second two as the right quote.
1249 This may also be set to the special value C<none>, in which case no quote
1250 marks are added around CE<lt>> text (but the font is still changed for troff
1255 Set the centered footer. By default, this is the version of Perl you run
1256 Pod::Man under. Note that some system an macro sets assume that the
1257 centered footer will be a modification date and will prepend something like
1258 "Last modified: "; if this is the case, you may want to set C<release> to
1259 the last modified date and C<date> to the version number.
1263 Set the section for the C<.TH> macro. The standard section numbering
1264 convention is to use 1 for user commands, 2 for system calls, 3 for
1265 functions, 4 for devices, 5 for file formats, 6 for games, 7 for
1266 miscellaneous information, and 8 for administrator commands. There is a lot
1267 of variation here, however; some systems (like Solaris) use 4 for file
1268 formats, 5 for miscellaneous information, and 7 for devices. Still others
1269 use 1m instead of 8, or some mix of both. About the only section numbers
1270 that are reliably consistent are 1, 2, and 3.
1272 By default, section 1 will be used unless the file ends in .pm in which case
1273 section 3 will be selected.
1277 The standard Pod::Parser method parse_from_filehandle() takes up to two
1278 arguments, the first being the file handle to read POD from and the second
1279 being the file handle to write the formatted output to. The first defaults
1280 to STDIN if not given, and the second defaults to STDOUT. The method
1281 parse_from_file() is almost identical, except that its two arguments are the
1282 input and output disk files instead. See L<Pod::Parser> for the specific
1289 =item roff font should be 1 or 2 chars, not "%s"
1291 (F) You specified a *roff font (using C<fixed>, C<fixedbold>, etc.) that
1292 wasn't either one or two characters. Pod::Man doesn't support *roff fonts
1293 longer than two characters, although some *roff extensions do (the canonical
1294 versions of nroff(1) and troff(1) don't either).
1296 =item Invalid link %s
1298 (W) The POD source contained a C<LE<lt>E<gt>> sequence that Pod::Man was
1299 unable to parse. You should never see this error message; it probably
1300 indicates a bug in Pod::Man.
1302 =item Invalid quote specification "%s"
1304 (F) The quote specification given (the quotes option to the constructor) was
1305 invalid. A quote specification must be one, two, or four characters long.
1307 =item %s:%d: Unknown command paragraph "%s".
1309 (W) The POD source contained a non-standard command paragraph (something of
1310 the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1312 =item Unknown escape EE<lt>%sE<gt>
1314 (W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Man didn't
1315 know about. C<EE<lt>%sE<gt>> was printed verbatim in the output.
1317 =item Unknown sequence %s
1319 (W) The POD source contained a non-standard interior sequence (something of
1320 the form C<XE<lt>E<gt>>) that Pod::Man didn't know about. It was ignored.
1322 =item %s: Unknown command paragraph "%s" on line %d.
1324 (W) The POD source contained a non-standard command paragraph (something of
1325 the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1327 =item Unmatched =back
1329 (W) Pod::Man encountered a C<=back> command that didn't correspond to an
1336 The lint-like features and strict POD format checking done by B<pod2man> are
1337 not yet implemented and should be, along with the corresponding C<lax>
1340 The NAME section should be recognized specially and index entries emitted
1341 for everything in that section. This would have to be deferred until the
1342 next section, since extraneous things in NAME tends to confuse various man
1345 The handling of hyphens, en dashes, and em dashes is somewhat fragile, and
1346 one may get the wrong one under some circumstances. This should only matter
1347 for troff(1) output.
1349 When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
1350 necessarily get it right.
1352 Pod::Man doesn't handle font names longer than two characters. Neither do
1353 most troff(1) implementations, but GNU troff does as an extension. It would
1354 be nice to support as an option for those who want to use it.
1356 The preamble added to each output file is rather verbose, and most of it is
1357 only necessary in the presence of EE<lt>E<gt> escapes for non-ASCII
1358 characters. It would ideally be nice if all of those definitions were only
1359 output if needed, perhaps on the fly as the characters are used.
1361 Some of the automagic applied to file names assumes Unix directory
1364 Pod::Man is excessively slow.
1368 L<Pod::Parser|Pod::Parser>, perlpod(1), pod2man(1), nroff(1), troff(1),
1371 Ossanna, Joseph F., and Brian W. Kernighan. "Troff User's Manual,"
1372 Computing Science Technical Report No. 54, AT&T Bell Laboratories. This is
1373 the best documentation of standard nroff(1) and troff(1). At the time of
1374 this writing, it's available at http://www.cs.bell-labs.com/cm/cs/cstr.html.
1376 The man page documenting the man macro set may be man(5) instead of man(7)
1377 on your system. Also, please see pod2man(1) for extensive documentation on
1378 writing manual pages if you've not done it before and aren't familiar with
1383 Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
1384 original B<pod2man> by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>.