podlators 1.06 released
[p5sagit/p5-mst-13.2.git] / lib / Pod / Man.pm
1 # Pod::Man -- Convert POD data to formatted *roff input.
2 # $Id: Man.pm,v 1.12 2000/12/25 12:56:12 eagle Exp $
3 #
4 # Copyright 1999, 2000 by Russ Allbery <rra@stanford.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
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
13 # to subclass.
14 #
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.
19
20 ############################################################################
21 # Modules and declarations
22 ############################################################################
23
24 package Pod::Man;
25
26 require 5.004;
27
28 use Carp qw(carp croak);
29 use Pod::Parser ();
30
31 use strict;
32 use subs qw(makespace);
33 use vars qw(@ISA %ESCAPES $PREAMBLE $VERSION);
34
35 @ISA = qw(Pod::Parser);
36
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,
40 # however.
41 $VERSION = 1.12;
42
43
44 ############################################################################
45 # Preamble and *roff output tables
46 ############################################################################
47
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
53 # output.
54 $PREAMBLE = <<'----END OF PREAMBLE----';
55 .de Sh \" Subsection heading
56 .br
57 .if t .Sp
58 .ne 5
59 .PP
60 \fB\\$1\fR
61 .PP
62 ..
63 .de Sp \" Vertical space (when we can't use .PP)
64 .if t .sp .5v
65 .if n .sp
66 ..
67 .de Ip \" List item
68 .br
69 .ie \\n(.$>=3 .ne \\$3
70 .el .ne 3
71 .IP "\\$1" \\$2
72 ..
73 .de Vb \" Begin verbatim text
74 .ft @CFONT@
75 .nf
76 .ne \\$1
77 ..
78 .de Ve \" End verbatim text
79 .ft R
80
81 .fi
82 ..
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<>
89 .tr \(*W-|\(bv\*(Tr
90 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
91 .ie n \{\
92 .    ds -- \(*W-
93 .    ds PI pi
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
96 .    ds L" ""
97 .    ds R" ""
98 .    ds C` @LQUOTE@
99 .    ds C' @RQUOTE@
100 'br\}
101 .el\{\
102 .    ds -- \|\(em\|
103 .    ds PI \(*p
104 .    ds L" ``
105 .    ds R" ''
106 'br\}
107 .\"
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.
112 .if \nF \{\
113 .    de IX
114 .    tm Index:\\$1\t\\n%\t"\\$2"
115 ..
116 .    nr % 0
117 .    rr F
118 .\}
119 .\"
120 .\" For nroff, turn off justification.  Always turn off hyphenation; it
121 .\" makes way too many mistakes in technical documents.
122 .hy 0
123 .if n .na
124 .\"
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 .bd B 3
128 .    \" fudge factors for nroff and troff
129 .if n \{\
130 .    ds #H 0
131 .    ds #V .8m
132 .    ds #F .3m
133 .    ds #[ \f1
134 .    ds #] \fP
135 .\}
136 .if t \{\
137 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
138 .    ds #V .6m
139 .    ds #F 0
140 .    ds #[ \&
141 .    ds #] \&
142 .\}
143 .    \" simple accents for nroff and troff
144 .if n \{\
145 .    ds ' \&
146 .    ds ` \&
147 .    ds ^ \&
148 .    ds , \&
149 .    ds ~ ~
150 .    ds /
151 .\}
152 .if t \{\
153 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
154 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
155 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
156 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
157 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
158 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
159 .\}
160 .    \" troff and (daisy-wheel) nroff accents
161 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
162 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
163 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
164 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
165 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
166 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
167 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
168 .ds ae a\h'-(\w'a'u*4/10)'e
169 .ds Ae A\h'-(\w'A'u*4/10)'E
170 .    \" corrections for vroff
171 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
172 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
173 .    \" for low resolution devices (crt and lpr)
174 .if \n(.H>23 .if \n(.V>19 \
175 \{\
176 .    ds : e
177 .    ds 8 ss
178 .    ds o a
179 .    ds d- d\h'-1'\(ga
180 .    ds D- D\h'-1'\(hy
181 .    ds th \o'bp'
182 .    ds Th \o'LP'
183 .    ds ae ae
184 .    ds Ae AE
185 .\}
186 .rm #[ #] #H #V #F C
187 ----END OF PREAMBLE----
188 #`# for cperl-mode
189
190 # This table is taken nearly verbatim from Tom Christiansen's pod2man.  It
191 # assumes that the standard preamble has already been printed, since that's
192 # what defines all of the accent marks.  Note that some of these are quoted
193 # with double quotes since they contain embedded single quotes, so use \\
194 # uniformly for backslash for readability.
195 %ESCAPES = (
196     'amp'       =>    '&',      # ampersand
197     'lt'        =>    '<',      # left chevron, less-than
198     'gt'        =>    '>',      # right chevron, greater-than
199     'quot'      =>    '"',      # double quote
200     'sol'       =>    '/',      # solidus (forward slash)
201     'verbar'    =>    '|',      # vertical bar
202
203     'Aacute'    =>    "A\\*'",  # capital A, acute accent
204     'aacute'    =>    "a\\*'",  # small a, acute accent
205     'Acirc'     =>    'A\\*^',  # capital A, circumflex accent
206     'acirc'     =>    'a\\*^',  # small a, circumflex accent
207     'AElig'     =>    '\*(AE',  # capital AE diphthong (ligature)
208     'aelig'     =>    '\*(ae',  # small ae diphthong (ligature)
209     'Agrave'    =>    "A\\*`",  # capital A, grave accent
210     'agrave'    =>    "A\\*`",  # small a, grave accent
211     'Aring'     =>    'A\\*o',  # capital A, ring
212     'aring'     =>    'a\\*o',  # small a, ring
213     'Atilde'    =>    'A\\*~',  # capital A, tilde
214     'atilde'    =>    'a\\*~',  # small a, tilde
215     'Auml'      =>    'A\\*:',  # capital A, dieresis or umlaut mark
216     'auml'      =>    'a\\*:',  # small a, dieresis or umlaut mark
217     'Ccedil'    =>    'C\\*,',  # capital C, cedilla
218     'ccedil'    =>    'c\\*,',  # small c, cedilla
219     'Eacute'    =>    "E\\*'",  # capital E, acute accent
220     'eacute'    =>    "e\\*'",  # small e, acute accent
221     'Ecirc'     =>    'E\\*^',  # capital E, circumflex accent
222     'ecirc'     =>    'e\\*^',  # small e, circumflex accent
223     'Egrave'    =>    'E\\*`',  # capital E, grave accent
224     'egrave'    =>    'e\\*`',  # small e, grave accent
225     'ETH'       =>    '\\*(D-', # capital Eth, Icelandic
226     'eth'       =>    '\\*(d-', # small eth, Icelandic
227     'Euml'      =>    'E\\*:',  # capital E, dieresis or umlaut mark
228     'euml'      =>    'e\\*:',  # small e, dieresis or umlaut mark
229     'Iacute'    =>    "I\\*'",  # capital I, acute accent
230     'iacute'    =>    "i\\*'",  # small i, acute accent
231     'Icirc'     =>    'I\\*^',  # capital I, circumflex accent
232     'icirc'     =>    'i\\*^',  # small i, circumflex accent
233     'Igrave'    =>    'I\\*`',  # capital I, grave accent
234     'igrave'    =>    'i\\*`',  # small i, grave accent
235     'Iuml'      =>    'I\\*:',  # capital I, dieresis or umlaut mark
236     'iuml'      =>    'i\\*:',  # small i, dieresis or umlaut mark
237     'Ntilde'    =>    'N\*~',   # capital N, tilde
238     'ntilde'    =>    'n\*~',   # small n, tilde
239     'Oacute'    =>    "O\\*'",  # capital O, acute accent
240     'oacute'    =>    "o\\*'",  # small o, acute accent
241     'Ocirc'     =>    'O\\*^',  # capital O, circumflex accent
242     'ocirc'     =>    'o\\*^',  # small o, circumflex accent
243     'Ograve'    =>    'O\\*`',  # capital O, grave accent
244     'ograve'    =>    'o\\*`',  # small o, grave accent
245     'Oslash'    =>    'O\\*/',  # capital O, slash
246     'oslash'    =>    'o\\*/',  # small o, slash
247     'Otilde'    =>    'O\\*~',  # capital O, tilde
248     'otilde'    =>    'o\\*~',  # small o, tilde
249     'Ouml'      =>    'O\\*:',  # capital O, dieresis or umlaut mark
250     'ouml'      =>    'o\\*:',  # small o, dieresis or umlaut mark
251     'szlig'     =>    '\*8',    # small sharp s, German (sz ligature)
252     'THORN'     =>    '\\*(Th', # capital THORN, Icelandic
253     'thorn'     =>    '\\*(th', # small thorn, Icelandic
254     'Uacute'    =>    "U\\*'",  # capital U, acute accent
255     'uacute'    =>    "u\\*'",  # small u, acute accent
256     'Ucirc'     =>    'U\\*^',  # capital U, circumflex accent
257     'ucirc'     =>    'u\\*^',  # small u, circumflex accent
258     'Ugrave'    =>    'U\\*`',  # capital U, grave accent
259     'ugrave'    =>    'u\\*`',  # small u, grave accent
260     'Uuml'      =>    'U\\*:',  # capital U, dieresis or umlaut mark
261     'uuml'      =>    'u\\*:',  # small u, dieresis or umlaut mark
262     'Yacute'    =>    "Y\\*'",  # capital Y, acute accent
263     'yacute'    =>    "y\\*'",  # small y, acute accent
264     'yuml'      =>    'y\\*:',  # small y, dieresis or umlaut mark
265 );
266
267
268 ############################################################################
269 # Static helper functions
270 ############################################################################
271
272 # Protect leading quotes and periods against interpretation as commands.
273 # Also protect anything starting with a backslash, since it could expand
274 # or hide something that *roff would interpret as a command.  This is
275 # overkill, but it's much simpler than trying to parse *roff here.
276 sub protect {
277     local $_ = shift;
278     s/^([.\'\\])/\\&$1/mg;
279     $_;
280 }
281
282 # Translate a font string into an escape.
283 sub toescape { (length ($_[0]) > 1 ? '\f(' : '\f') . $_[0] }
284
285
286 ############################################################################
287 # Initialization
288 ############################################################################
289
290 # Initialize the object.  Here, we also process any additional options
291 # passed to the constructor or set up defaults if none were given.  center
292 # is the centered title, release is the version number, and date is the date
293 # for the documentation.  Note that we can't know what file name we're
294 # processing due to the architecture of Pod::Parser, so that *has* to either
295 # be passed to the constructor or set separately with Pod::Man::name().
296 sub initialize {
297     my $self = shift;
298
299     # Figure out the fixed-width font.  If user-supplied, make sure that
300     # they are the right length.
301     for (qw/fixed fixedbold fixeditalic fixedbolditalic/) {
302         if (defined $$self{$_}) {
303             if (length ($$self{$_}) < 1 || length ($$self{$_}) > 2) {
304                 croak qq(roff font should be 1 or 2 chars,)
305                     . qq( not "$$self{$_}");
306             }
307         } else {
308             $$self{$_} = '';
309         }
310     }
311
312     # Set the default fonts.  We can't be sure what fixed bold-italic is
313     # going to be called, so default to just bold.
314     $$self{fixed}           ||= 'CW';
315     $$self{fixedbold}       ||= 'CB';
316     $$self{fixeditalic}     ||= 'CI';
317     $$self{fixedbolditalic} ||= 'CB';
318
319     # Set up a table of font escapes.  First number is fixed-width, second
320     # is bold, third is italic.
321     $$self{FONTS} = { '000' => '\fR', '001' => '\fI',
322                       '010' => '\fB', '011' => '\f(BI',
323                       '100' => toescape ($$self{fixed}),
324                       '101' => toescape ($$self{fixeditalic}),
325                       '110' => toescape ($$self{fixedbold}),
326                       '111' => toescape ($$self{fixedbolditalic})};
327
328     # Extra stuff for page titles.
329     $$self{center} = 'User Contributed Perl Documentation'
330         unless defined $$self{center};
331     $$self{indent}  = 4 unless defined $$self{indent};
332
333     # We used to try first to get the version number from a local binary,
334     # but we shouldn't need that any more.  Get the version from the running
335     # Perl.  Work a little magic to handle subversions correctly under both
336     # the pre-5.6 and the post-5.6 version numbering schemes.
337     if (!defined $$self{release}) {
338         my @version = ($] =~ /^(\d+)\.(\d{3})(\d{0,3})$/);
339         $version[2] ||= 0;
340         $version[2] *= 10 ** (3 - length $version[2]);
341         for (@version) { $_ += 0 }
342         $$self{release} = 'perl v' . join ('.', @version);
343     }
344
345     # Double quotes in things that will be quoted.
346     for (qw/center date release/) {
347         $$self{$_} =~ s/\"/\"\"/g if $$self{$_};
348     }
349
350     # Figure out what quotes we'll be using for C<> text.
351     $$self{quotes} ||= '"';
352     if ($$self{quotes} eq 'none') {
353         $$self{LQUOTE} = $$self{RQUOTE} = '';
354     } elsif (length ($$self{quotes}) == 1) {
355         $$self{LQUOTE} = $$self{RQUOTE} = $$self{quotes};
356     } elsif ($$self{quotes} =~ /^(.)(.)$/
357              || $$self{quotes} =~ /^(..)(..)$/) {
358         $$self{LQUOTE} = $1;
359         $$self{RQUOTE} = $2;
360     } else {
361         croak qq(Invalid quote specification "$$self{quotes}");
362     }
363
364     # Double the first quote; note that this should not be s///g as two
365     # double quotes is represented in *roff as three double quotes, not
366     # four.  Weird, I know.
367     $$self{LQUOTE} =~ s/\"/\"\"/;
368     $$self{RQUOTE} =~ s/\"/\"\"/;
369
370     $$self{INDENT}  = 0;        # Current indentation level.
371     $$self{INDENTS} = [];       # Stack of indentations.
372     $$self{INDEX}   = [];       # Index keys waiting to be printed.
373     $$self{ITEMS}   = 0;        # The number of consecutive =items.
374
375     $self->SUPER::initialize;
376 }
377
378 # For each document we process, output the preamble first.
379 sub begin_pod {
380     my $self = shift;
381
382     # Try to figure out the name and section from the file name.
383     my $section = $$self{section} || 1;
384     my $name = $$self{name};
385     if (!defined $name) {
386         $name = $self->input_file;
387         $section = 3 if (!$$self{section} && $name =~ /\.pm\z/i);
388         $name =~ s/\.p(od|[lm])\z//i;
389         if ($section =~ /^1/) {
390             require File::Basename;
391             $name = uc File::Basename::basename ($name);
392         } else {
393             # Lose everything up to the first of
394             #     */lib/*perl*      standard or site_perl module
395             #     */*perl*/lib      from -D prefix=/opt/perl
396             #     */*perl*/         random module hierarchy
397             # which works.  Should be fixed to use File::Spec.  Also handle
398             # a leading lib/ since that's what ExtUtils::MakeMaker creates.
399             for ($name) {
400                 s%//+%/%g;
401                 if (     s%^.*?/lib/[^/]*perl[^/]*/%%si
402                       or s%^.*?/[^/]*perl[^/]*/(?:lib/)?%%si) {
403                     s%^site(_perl)?/%%s;      # site and site_perl
404                     s%^(.*-$^O|$^O-.*)/%%so;  # arch
405                     s%^\d+\.\d+%%s;           # version
406                 }
407                 s%^lib/%%;
408                 s%/%::%g;
409             }
410         }
411     }
412
413     # Modification date header.  Try to use the modification time of our
414     # input.
415     if (!defined $$self{date}) {
416         my $time = (stat $self->input_file)[9] || time;
417         my ($day, $month, $year) = (localtime $time)[3,4,5];
418         $month++;
419         $year += 1900;
420         $$self{date} = sprintf ('%4d-%02d-%02d', $year, $month, $day);
421     }
422
423     # Now, print out the preamble and the title.
424     local $_ = $PREAMBLE;
425     s/\@CFONT\@/$$self{fixed}/;
426     s/\@LQUOTE\@/$$self{LQUOTE}/;
427     s/\@RQUOTE\@/$$self{RQUOTE}/;
428     chomp $_;
429     print { $self->output_handle } <<"----END OF HEADER----";
430 .\\" Automatically generated by Pod::Man version $VERSION
431 .\\" @{[ scalar localtime ]}
432 .\\"
433 .\\" Standard preamble:
434 .\\" ======================================================================
435 $_
436 .\\" ======================================================================
437 .\\"
438 .IX Title "$name $section"
439 .TH $name $section "$$self{release}" "$$self{date}" "$$self{center}"
440 .UC
441 ----END OF HEADER----
442 #"# for cperl-mode
443
444     # Initialize a few per-file variables.
445     $$self{INDENT} = 0;
446     $$self{NEEDSPACE} = 0;
447 }
448
449
450 ############################################################################
451 # Core overrides
452 ############################################################################
453
454 # Called for each command paragraph.  Gets the command, the associated
455 # paragraph, the line number, and a Pod::Paragraph object.  Just dispatches
456 # the command to a method named the same as the command.  =cut is handled
457 # internally by Pod::Parser.
458 sub command {
459     my $self = shift;
460     my $command = shift;
461     return if $command eq 'pod';
462    return if ($$self{EXCLUDE} && $command ne 'end');
463     if ($self->can ('cmd_' . $command)) {
464         $command = 'cmd_' . $command;
465         $self->$command (@_);
466      } else {
467         my ($text, $line, $paragraph) = @_;
468         my $file;
469         ($file, $line) = $paragraph->file_line;
470         $text =~ s/\n+\z//;
471         $text = " $text" if ($text =~ /^\S/);
472         warn qq($file:$line: Unknown command paragraph "=$command$text"\n);
473         return;
474     }
475 }
476
477 # Called for a verbatim paragraph.  Gets the paragraph, the line number, and
478 # a Pod::Paragraph object.  Rofficate backslashes, untabify, put a
479 # zero-width character at the beginning of each line to protect against
480 # commands, and wrap in .Vb/.Ve.
481 sub verbatim {
482     my $self = shift;
483     return if $$self{EXCLUDE};
484     local $_ = shift;
485     return if /^\s+$/;
486     s/\s+$/\n/;
487     my $lines = tr/\n/\n/;
488     1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
489     s/\\/\\e/g;
490     s/^(\s*\S)/'\&' . $1/gme;
491     $self->makespace;
492     $self->output (".Vb $lines\n$_.Ve\n");
493     $$self{NEEDSPACE} = 0;
494 }
495
496 # Called for a regular text block.  Gets the paragraph, the line number, and
497 # a Pod::Paragraph object.  Perform interpolation and output the results.
498 sub textblock {
499     my $self = shift;
500     return if $$self{EXCLUDE};
501     $self->output ($_[0]), return if $$self{VERBATIM};
502
503     # Perform a little magic to collapse multiple L<> references.  We'll
504     # just rewrite the whole thing into actual text at this part, bypassing
505     # the whole internal sequence parsing thing.
506     my $text = shift;
507     $text =~ s{
508         (L<                     # A link of the form L</something>.
509               /
510               (
511                   [:\w]+        # The item has to be a simple word...
512                   (\(\))?       # ...or simple function.
513               )
514           >
515           (
516               ,?\s+(and\s+)?    # Allow lots of them, conjuncted.
517               L<
518                   /
519                   ( [:\w]+ ( \(\) )? )
520               >
521           )+
522         )
523     } {
524         local $_ = $1;
525         s{ L< / ( [^>]+ ) > } {$1}xg;
526         my @items = split /(?:,?\s+(?:and\s+)?)/;
527         my $string = 'the ';
528         my $i;
529         for ($i = 0; $i < @items; $i++) {
530             $string .= $items[$i];
531             $string .= ', ' if @items > 2 && $i != $#items;
532             $string .= ' ' if @items == 2 && $i == 2;
533             $string .= 'and ' if ($i == $#items - 1);
534         }
535         $string .= ' entries elsewhere in this document';
536         $string;
537     }gex;
538
539     # Parse the tree and output it.  collapse knows about references to
540     # scalars as well as scalars and does the right thing with them.
541     $text = $self->parse ($text, @_);
542     $text =~ s/\n\s*$/\n/;
543     $self->makespace;
544     $self->output (protect $self->textmapfonts ($text));
545     $self->outindex;
546     $$self{NEEDSPACE} = 1;
547 }
548
549 # Called for an interior sequence.  Takes a Pod::InteriorSequence object and
550 # returns a reference to a scalar.  This scalar is the final formatted text.
551 # It's returned as a reference so that other interior sequences above us
552 # know that the text has already been processed.
553 sub sequence {
554     my ($self, $seq) = @_;
555     my $command = $seq->cmd_name;
556
557     # Zero-width characters.
558     if ($command eq 'Z') {
559         # Workaround to generate a blessable reference, needed by 5.005.
560         my $tmp = '\&';
561         return bless \ "$tmp", 'Pod::Man::String';
562     }
563
564     # C<>, L<>, X<>, and E<> don't apply guesswork to their contents.  C<>
565     # needs some additional special handling.
566     my $literal = ($command =~ /^[CELX]$/);
567     $literal++ if $command eq 'C';
568     local $_ = $self->collapse ($seq->parse_tree, $literal);
569
570     # Handle E<> escapes.
571     if ($command eq 'E') {
572         if (/^\d+$/) {
573             return bless \ chr ($_), 'Pod::Man::String';
574         } elsif (exists $ESCAPES{$_}) {
575             return bless \ "$ESCAPES{$_}", 'Pod::Man::String';
576         } else {
577             carp "Unknown escape E<$1>";
578             return bless \ "E<$_>", 'Pod::Man::String';
579         }
580     }
581
582     # For all the other sequences, empty content produces no output.
583     return '' if $_ eq '';
584
585     # Handle formatting sequences.
586     if ($command eq 'B') {
587         return bless \ ('\f(BS' . $_ . '\f(BE'), 'Pod::Man::String';
588     } elsif ($command eq 'F') {
589         return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
590     } elsif ($command eq 'I') {
591         return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
592     } elsif ($command eq 'C') {
593         return bless \ ('\f(FS\*(C`' . $_ . "\\*(C'\\f(FE"),
594             'Pod::Man::String';
595     }
596
597     # Handle links.
598     if ($command eq 'L') {
599         # A bug in lvalue subs in 5.6 requires the temporary variable.
600         my $tmp = $self->buildlink ($_);
601         return bless \ "$tmp", 'Pod::Man::String';
602     }
603
604     # Whitespace protection replaces whitespace with "\ ".
605     if ($command eq 'S') {
606         s/\s+/\\ /g;
607         return bless \ "$_", 'Pod::Man::String';
608     }
609
610     # Add an index entry to the list of ones waiting to be output.
611     if ($command eq 'X') { push (@{ $$self{INDEX} }, $_); return '' }
612
613     # Anything else is unknown.
614     carp "Unknown sequence $command<$_>";
615 }
616
617
618 ############################################################################
619 # Command paragraphs
620 ############################################################################
621
622 # All command paragraphs take the paragraph and the line number.
623
624 # First level heading.  We can't output .IX in the NAME section due to a bug
625 # in some versions of catman, so don't output a .IX for that section.  .SH
626 # already uses small caps, so remove any E<> sequences that would cause
627 # them.
628 sub cmd_head1 {
629     my $self = shift;
630     local $_ = $self->parse (@_);
631     s/\s+$//;
632     s/\\s-?\d//g;
633     if ($$self{ITEMS} > 1) {
634         $$self{ITEMS} = 0;
635         $self->output (".PD\n");
636     }
637     $self->output ($self->switchquotes ('.SH', $self->mapfonts ($_)));
638     $self->outindex (($_ eq 'NAME') ? () : ('Header', $_));
639     $$self{NEEDSPACE} = 0;
640 }
641
642 # Second level heading.
643 sub cmd_head2 {
644     my $self = shift;
645     local $_ = $self->parse (@_);
646     s/\s+$//;
647     if ($$self{ITEMS} > 1) {
648         $$self{ITEMS} = 0;
649         $self->output (".PD\n");
650     }
651     $self->output ($self->switchquotes ('.Sh', $self->mapfonts ($_)));
652     $self->outindex ('Subsection', $_);
653     $$self{NEEDSPACE} = 0;
654 }
655
656 # Third level heading.
657 sub cmd_head3 {
658     my $self = shift;
659     local $_ = $self->parse (@_);
660     s/\s+$//;
661     if ($$self{ITEMS} > 1) {
662         $$self{ITEMS} = 0;
663         $self->output (".PD\n");
664     }
665     $self->makespace;
666     $self->output ($self->switchquotes ('.I', $self->mapfonts ($_)));
667     $self->outindex ('Subsection', $_);
668     $$self{NEEDSPACE} = 1;
669 }
670
671 # Fourth level heading.
672 sub cmd_head4 {
673     my $self = shift;
674     local $_ = $self->parse (@_);
675     s/\s+$//;
676     if ($$self{ITEMS} > 1) {
677         $$self{ITEMS} = 0;
678         $self->output (".PD\n");
679     }
680     $self->makespace;
681     $self->output ($self->textmapfonts ($_) . "\n");
682     $self->outindex ('Subsection', $_);
683     $$self{NEEDSPACE} = 1;
684 }
685
686 # Start a list.  For indents after the first, wrap the outside indent in .RS
687 # so that hanging paragraph tags will be correct.
688 sub cmd_over {
689     my $self = shift;
690     local $_ = shift;
691     unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
692     if (@{ $$self{INDENTS} } > 0) {
693         $self->output (".RS $$self{INDENT}\n");
694     }
695     push (@{ $$self{INDENTS} }, $$self{INDENT});
696     $$self{INDENT} = ($_ + 0);
697 }
698
699 # End a list.  If we've closed an embedded indent, we've mangled the hanging
700 # paragraph indent, so temporarily replace it with .RS and set WEIRDINDENT.
701 # We'll close that .RS at the next =back or =item.
702 sub cmd_back {
703     my $self = shift;
704     $$self{INDENT} = pop @{ $$self{INDENTS} };
705     unless (defined $$self{INDENT}) {
706         carp "Unmatched =back";
707         $$self{INDENT} = 0;
708     }
709     if ($$self{WEIRDINDENT}) {
710         $self->output (".RE\n");
711         $$self{WEIRDINDENT} = 0;
712     }
713     if (@{ $$self{INDENTS} } > 0) {
714         $self->output (".RE\n");
715         $self->output (".RS $$self{INDENT}\n");
716         $$self{WEIRDINDENT} = 1;
717     }
718     $$self{NEEDSPACE} = 1;
719 }
720
721 # An individual list item.  Emit an index entry for anything that's
722 # interesting, but don't emit index entries for things like bullets and
723 # numbers.  rofficate bullets too while we're at it (so for nice output, use
724 # * for your lists rather than o or . or - or some other thing).  Newlines
725 # in an item title are turned into spaces since *roff can't handle them
726 # embedded.
727 sub cmd_item {
728     my $self = shift;
729     local $_ = $self->parse (@_);
730     s/\s+$//;
731     s/\s*\n\s*/ /g;
732     my $index;
733     if (/\w/ && !/^\w[.\)]\s*$/) {
734         $index = $_;
735         $index =~ s/^\s*[-*+o.]?(?:\s+|\Z)//;
736     }
737     s/^\*(\s|\Z)/\\\(bu$1/;
738     if ($$self{WEIRDINDENT}) {
739         $self->output (".RE\n");
740         $$self{WEIRDINDENT} = 0;
741     }
742     $_ = $self->textmapfonts ($_);
743     $self->output (".PD 0\n") if ($$self{ITEMS} == 1);
744     $self->output ($self->switchquotes ('.Ip', $_, $$self{INDENT}));
745     $self->outindex ($index ? ('Item', $index) : ());
746     $$self{NEEDSPACE} = 0;
747     $$self{ITEMS}++;
748 }
749
750 # Begin a block for a particular translator.  Setting VERBATIM triggers
751 # special handling in textblock().
752 sub cmd_begin {
753     my $self = shift;
754     local $_ = shift;
755     my ($kind) = /^(\S+)/ or return;
756     if ($kind eq 'man' || $kind eq 'roff') {
757         $$self{VERBATIM} = 1;
758     } else {
759         $$self{EXCLUDE} = 1;
760     }
761 }
762
763 # End a block for a particular translator.  We assume that all =begin/=end
764 # pairs are properly closed.
765 sub cmd_end {
766     my $self = shift;
767     $$self{EXCLUDE} = 0;
768     $$self{VERBATIM} = 0;
769 }
770
771 # One paragraph for a particular translator.  Ignore it unless it's intended
772 # for man or roff, in which case we output it verbatim.
773 sub cmd_for {
774     my $self = shift;
775     local $_ = shift;
776     return unless s/^(?:man|roff)\b[ \t]*\n?//;
777     $self->output ($_);
778 }
779
780
781 ############################################################################
782 # Link handling
783 ############################################################################
784
785 # Handle links.  We can't actually make real hyperlinks, so this is all to
786 # figure out what text and formatting we print out.
787 sub buildlink {
788     my $self = shift;
789     local $_ = shift;
790
791     # Smash whitespace in case we were split across multiple lines.
792     s/\s+/ /g;
793
794     # If we were given any explicit text, just output it.
795     if (m{ ^ ([^|]+) \| }x) { return $1 }
796
797     # Okay, leading and trailing whitespace isn't important.
798     s/^\s+//;
799     s/\s+$//;
800
801     # Default to using the whole content of the link entry as a section
802     # name.  Note that L<manpage/> forces a manpage interpretation, as does
803     # something looking like L<manpage(section)>.  Do the same thing to
804     # L<manpage(section)> as we would to manpage(section) without the L<>;
805     # see guesswork().  If we've added italics, don't add the "manpage"
806     # text; markup is sufficient.
807     my ($manpage, $section) = ('', $_);
808     if (/^"\s*(.*?)\s*"$/) {
809         $section = '"' . $1 . '"';
810     } elsif (m{ ^ [-:.\w]+ (?: \( \S+ \) )? $ }x) {
811         ($manpage, $section) = ($_, '');
812         $manpage =~ s/^([^\(]+)\(/'\f(IS' . $1 . '\f(IE\|('/e;
813     } elsif (m%/%) {
814         ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
815         if ($manpage =~ /^[-:.\w]+(?:\(\S+\))?$/) {
816             $manpage =~ s/^([^\(]+)\(/'\f(IS' . $1 . '\f(IE\|'/e;
817         }
818         $section =~ s/^\"\s*//;
819         $section =~ s/\s*\"$//;
820     }
821     if ($manpage && $manpage !~ /\\f\(IS/) {
822         $manpage = "the $manpage manpage";
823     }
824
825     # Now build the actual output text.
826     my $text = '';
827     if (!length ($section) && !length ($manpage)) {
828         carp "Invalid link $_";
829     } elsif (!length ($section)) {
830         $text = $manpage;
831     } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
832         $text .= 'the ' . $section . ' entry';
833         $text .= (length $manpage) ? " in $manpage"
834                                    : " elsewhere in this document";
835     } else {
836         if ($section !~ /^".*"$/) { $section = '"' . $section . '"' }
837         $text .= 'the section on ' . $section;
838         $text .= " in $manpage" if length $manpage;
839     }
840     $text;
841 }
842
843
844 ############################################################################
845 # Escaping and fontification
846 ############################################################################
847
848 # At this point, we'll have embedded font codes of the form \f(<font>[SE]
849 # where <font> is one of B, I, or F.  Turn those into the right font start
850 # or end codes.  The old pod2man didn't get B<someI<thing> else> right;
851 # after I<> it switched back to normal text rather than bold.  We take care
852 # of this by using variables as a combined pointer to our current font
853 # sequence, and set each to the number of current nestings of start tags for
854 # that font.  Use them as a vector to look up what font sequence to use.
855 #
856 # \fP changes to the previous font, but only one previous font is kept.  We
857 # don't know what the outside level font is; normally it's R, but if we're
858 # inside a heading it could be something else.  So arrange things so that
859 # the outside font is always the "previous" font and end with \fP instead of
860 # \fR.  Idea from Zack Weinberg.
861 sub mapfonts {
862     my $self = shift;
863     local $_ = shift;
864
865     my ($fixed, $bold, $italic) = (0, 0, 0);
866     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
867     my $last = '\fR';
868     s { \\f\((.)(.) } {
869         my $sequence = '';
870         my $f;
871         if ($last ne '\fR') { $sequence = '\fP' }
872         ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
873         $f = $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
874         if ($f eq $last) {
875             '';
876         } else {
877             if ($f ne '\fR') { $sequence .= $f }
878             $last = $f;
879             $sequence;
880         }
881     }gxe;
882     $_;
883 }
884
885 # Unfortunately, there is a bug in Solaris 2.6 nroff (not present in GNU
886 # groff) where the sequence \fB\fP\f(CW\fP leaves the font set to B rather
887 # than R, presumably because \f(CW doesn't actually do a font change.  To
888 # work around this, use a separate textmapfonts for text blocks where the
889 # default font is always R and only use the smart mapfonts for headings.
890 sub textmapfonts {
891     my $self = shift;
892     local $_ = shift;
893
894     my ($fixed, $bold, $italic) = (0, 0, 0);
895     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
896     s { \\f\((.)(.) } {
897         ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
898         $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
899     }gxe;
900     $_;
901 }
902
903
904 ############################################################################
905 # *roff-specific parsing
906 ############################################################################
907
908 # Called instead of parse_text, calls parse_text with the right flags.
909 sub parse {
910     my $self = shift;
911     $self->parse_text ({ -expand_seq   => 'sequence',
912                          -expand_ptree => 'collapse' }, @_);
913 }
914
915 # Takes a parse tree and a flag saying whether or not to treat it as literal
916 # text (not call guesswork on it), and returns the concatenation of all of
917 # the text strings in that parse tree.  If the literal flag isn't true,
918 # guesswork() will be called on all plain scalars in the parse tree.
919 # Otherwise, just escape backslashes in the normal case.  If collapse is
920 # being called on a C<> sequence, literal is set to 2, and we do some
921 # additional cleanup.  Assumes that everything in the parse tree is either a
922 # scalar or a reference to a scalar.
923 sub collapse {
924     my ($self, $ptree, $literal) = @_;
925     if ($literal) {
926         return join ('', map {
927             if (ref $_) {
928                 $$_;
929             } else {
930                 s/\\/\\e/g;
931                 s/-/\\-/g    if $literal > 1;
932                 s/__/_\\|_/g if $literal > 1;
933                 $_;
934             }
935         } $ptree->children);
936     } else {
937         return join ('', map {
938             ref ($_) ? $$_ : $self->guesswork ($_)
939         } $ptree->children);
940     }
941 }
942
943 # Takes a text block to perform guesswork on; this is guaranteed not to
944 # contain any interior sequences.  Returns the text block with remapping
945 # done.
946 sub guesswork {
947     my $self = shift;
948     local $_ = shift;
949
950     # rofficate backslashes.
951     s/\\/\\e/g;
952
953     # Ensure double underbars have a tiny space between them.
954     s/__/_\\|_/g;
955
956     # Make all caps a little smaller.  Be careful here, since we don't want
957     # to make @ARGV into small caps, nor do we want to fix the MIME in
958     # MIME-Version, since it looks weird with the full-height V.
959     s{
960         ( ^ | [\s\(\"\'\`\[\{<>] )
961         ( [A-Z] [A-Z] [/A-Z+:\d_\$&-]* )
962         (?: (?= [\s>\}\]\)\'\".?!,;:] | -- ) | $ )
963     } { $1 . '\s-1' . $2 . '\s0' }egx;
964
965     # Turn PI into a pretty pi.
966     s{ (?: \\s-1 | \b ) PI (?: \\s0 | \b ) } {\\*\(PI}gx;
967
968     # Italize functions in the form func().
969     s{
970         \b
971         (
972             [:\w]+ (?:\\s-1)? \(\)
973         )
974     } { '\f(IS' . $1 . '\f(IE' }egx;
975
976     # func(n) is a reference to a manual page.  Make it \fIfunc\fR\|(n).
977     s{
978         \b
979         (\w[-:.\w]+ (?:\\s-1)?)
980         (
981             \( [^\)] \)
982         )
983     } { '\f(IS' . $1 . '\f(IE\|' . $2 }egx;
984
985     # Convert simple Perl variable references to a fixed-width font.
986     s{
987         ( \s+ )
988         ( [\$\@%] [\w:]+ )
989         (?! \( )
990     } { $1 . '\f(FS' . $2 . '\f(FE'}egx;
991
992     # Translate -- into a real em dash if it's used like one and fix up
993     # dashes, but keep hyphens hyphens.
994     s{ (\G|^|.) (-+) (\b|.) } {
995         my ($pre, $dash, $post) = ($1, $2, $3);
996         if (length ($dash) == 1) {
997             ($pre =~ /[a-zA-Z]/) ? "$pre-$post" : "$pre\\-$post";
998         } elsif (length ($dash) == 2
999                  && ((!$pre && !$post)
1000                      || ($pre =~ /\w/ && !$post)
1001                      || ($pre eq ' ' && $post eq ' ')
1002                      || ($pre eq '=' && $post ne '=')
1003                      || ($pre ne '=' && $post eq '='))) {
1004             "$pre\\*(--$post";
1005         } else {
1006             $pre . ('\-' x length $dash) . $post;
1007         }
1008     }egxs;
1009
1010     # Fix up double quotes.
1011     s{ \" ([^\"]+) \" } { '\*(L"' . $1 . '\*(R"' }egx;
1012
1013     # Make C++ into \*(C+, which is a squinched version.
1014     s{ \b C\+\+ } {\\*\(C+}gx;
1015
1016     # All done.
1017     $_;
1018 }
1019
1020
1021 ############################################################################
1022 # Output formatting
1023 ############################################################################
1024
1025 # Make vertical whitespace.
1026 sub makespace {
1027     my $self = shift;
1028     $self->output (".PD\n") if ($$self{ITEMS} > 1);
1029     $$self{ITEMS} = 0;
1030     $self->output ($$self{INDENT} > 0 ? ".Sp\n" : ".PP\n")
1031         if $$self{NEEDSPACE};
1032 }
1033
1034 # Output any pending index entries, and optionally an index entry given as
1035 # an argument.  Support multiple index entries in X<> separated by slashes,
1036 # and strip special escapes from index entries.
1037 sub outindex {
1038     my ($self, $section, $index) = @_;
1039     my @entries = map { split m%\s*/\s*% } @{ $$self{INDEX} };
1040     return unless ($section || @entries);
1041     $$self{INDEX} = [];
1042     my $output;
1043     if (@entries) {
1044         my $output = '.IX Xref "'
1045             . join (' ', map { s/\"/\"\"/; $_ } @entries)
1046             . '"' . "\n";
1047     }
1048     if ($section) {
1049         $index =~ s/\"/\"\"/;
1050         $index =~ s/\\-/-/g;
1051         $index =~ s/\\(?:s-?\d|.\(..|.)//g;
1052         $output .= ".IX $section " . '"' . $index . '"' . "\n";
1053     }
1054     $self->output ($output);
1055 }
1056
1057 # Output text to the output device.
1058 sub output { print { $_[0]->output_handle } $_[1] }
1059
1060 # Given a command and a single argument that may or may not contain double
1061 # quotes, handle double-quote formatting for it.  If there are no double
1062 # quotes, just return the command followed by the argument in double quotes.
1063 # If there are double quotes, use an if statement to test for nroff, and for
1064 # nroff output the command followed by the argument in double quotes with
1065 # embedded double quotes doubled.  For other formatters, remap paired double
1066 # quotes to LQUOTE and RQUOTE.
1067 sub switchquotes {
1068     my $self = shift;
1069     my $command = shift;
1070     local $_ = shift;
1071     my $extra = shift;
1072     s/\\\*\([LR]\"/\"/g;
1073
1074     # We also have to deal with \*C` and \*C', which are used to add the
1075     # quotes around C<> text, since they may expand to " and if they do this
1076     # confuses the .SH macros and the like no end.  Expand them ourselves.
1077     # If $extra is set, we're dealing with =item, which in most nroff macro
1078     # sets requires an extra level of quoting of double quotes.
1079     my $c_is_quote = ($$self{LQUOTE} =~ /\"/) || ($$self{RQUOTE} =~ /\"/);
1080     if (/\"/ || ($c_is_quote && /\\\*\(C[\'\`]/)) {
1081         s/\"/\"\"/g;
1082         my $troff = $_;
1083         $troff =~ s/\"\"([^\"]*)\"\"/\`\`$1\'\'/g;
1084         s/\\\*\(C\`/$$self{LQUOTE}/g;
1085         s/\\\*\(C\'/$$self{RQUOTE}/g;
1086         $troff =~ s/\\\*\(C[\'\`]//g;
1087         s/\"/\"\"/g if $extra;
1088         $troff =~ s/\"/\"\"/g if $extra;
1089         $_ = qq("$_") . ($extra ? " $extra" : '');
1090         $troff = qq("$troff") . ($extra ? " $extra" : '');
1091         return ".if n $command $_\n.el $command $troff\n";
1092     } else {
1093         $_ = qq("$_") . ($extra ? " $extra" : '');
1094         return "$command $_\n";
1095     }
1096 }
1097
1098 __END__
1099
1100 .\" These are some extra bits of roff that I don't want to lose track of
1101 .\" but that have been removed from the preamble to make it a bit shorter
1102 .\" since they're not currently being used.  They're accents and special
1103 .\" characters we don't currently have escapes for.
1104 .if n \{\
1105 .    ds ? ?
1106 .    ds ! !
1107 .    ds q
1108 .\}
1109 .if t \{\
1110 .    ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
1111 .    ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
1112 .    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'
1113 .\}
1114 .ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
1115 .ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
1116 .ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
1117 .ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
1118 .ds oe o\h'-(\w'o'u*4/10)'e
1119 .ds Oe O\h'-(\w'O'u*4/10)'E
1120 .if \n(.H>23 .if \n(.V>19 \
1121 \{\
1122 .    ds v \h'-1'\o'\(aa\(ga'
1123 .    ds _ \h'-1'^
1124 .    ds . \h'-1'.
1125 .    ds 3 3
1126 .    ds oe oe
1127 .    ds Oe OE
1128 .\}
1129
1130 ############################################################################
1131 # Documentation
1132 ############################################################################
1133
1134 =head1 NAME
1135
1136 Pod::Man - Convert POD data to formatted *roff input
1137
1138 =head1 SYNOPSIS
1139
1140     use Pod::Man;
1141     my $parser = Pod::Man->new (release => $VERSION, section => 8);
1142
1143     # Read POD from STDIN and write to STDOUT.
1144     $parser->parse_from_filehandle;
1145
1146     # Read POD from file.pod and write to file.1.
1147     $parser->parse_from_file ('file.pod', 'file.1');
1148
1149 =head1 DESCRIPTION
1150
1151 Pod::Man is a module to convert documentation in the POD format (the
1152 preferred language for documenting Perl) into *roff input using the man
1153 macro set.  The resulting *roff code is suitable for display on a terminal
1154 using nroff(1), normally via man(1), or printing using troff(1).  It is
1155 conventionally invoked using the driver script B<pod2man>, but it can also
1156 be used directly.
1157
1158 As a derived class from Pod::Parser, Pod::Man supports the same methods and
1159 interfaces.  See L<Pod::Parser> for all the details; briefly, one creates a
1160 new parser with C<Pod::Man-E<gt>new()> and then calls either
1161 parse_from_filehandle() or parse_from_file().
1162
1163 new() can take options, in the form of key/value pairs that control the
1164 behavior of the parser.  See below for details.
1165
1166 If no options are given, Pod::Man uses the name of the input file with any
1167 trailing C<.pod>, C<.pm>, or C<.pl> stripped as the man page title, to
1168 section 1 unless the file ended in C<.pm> in which case it defaults to
1169 section 3, to a centered title of "User Contributed Perl Documentation", to
1170 a centered footer of the Perl version it is run with, and to a left-hand
1171 footer of the modification date of its input (or the current date if given
1172 STDIN for input).
1173
1174 Pod::Man assumes that your *roff formatters have a fixed-width font named
1175 CW.  If yours is called something else (like CR), use the C<fixed> option to
1176 specify it.  This generally only matters for troff output for printing.
1177 Similarly, you can set the fonts used for bold, italic, and bold italic
1178 fixed-width output.
1179
1180 Besides the obvious pod conversions, Pod::Man also takes care of formatting
1181 func(), func(n), and simple variable references like $foo or @bar so you
1182 don't have to use code escapes for them; complex expressions like
1183 C<$fred{'stuff'}> will still need to be escaped, though.  It also translates
1184 dashes that aren't used as hyphens into en dashes, makes long dashes--like
1185 this--into proper em dashes, fixes "paired quotes," makes C++ and PI look
1186 right, puts a little space between double underbars, makes ALLCAPS a teeny
1187 bit smaller in troff(1), and escapes stuff that *roff treats as special so
1188 that you don't have to.
1189
1190 The recognized options to new() are as follows.  All options take a single
1191 argument.
1192
1193 =over 4
1194
1195 =item center
1196
1197 Sets the centered page header to use instead of "User Contributed Perl
1198 Documentation".
1199
1200 =item date
1201
1202 Sets the left-hand footer.  By default, the modification date of the input
1203 file will be used, or the current date if stat() can't find that file (the
1204 case if the input is from STDIN), and the date will be formatted as
1205 YYYY-MM-DD.
1206
1207 =item fixed
1208
1209 The fixed-width font to use for vertabim text and code.  Defaults to CW.
1210 Some systems may want CR instead.  Only matters for troff(1) output.
1211
1212 =item fixedbold
1213
1214 Bold version of the fixed-width font.  Defaults to CB.  Only matters for
1215 troff(1) output.
1216
1217 =item fixeditalic
1218
1219 Italic version of the fixed-width font (actually, something of a misnomer,
1220 since most fixed-width fonts only have an oblique version, not an italic
1221 version).  Defaults to CI.  Only matters for troff(1) output.
1222
1223 =item fixedbolditalic
1224
1225 Bold italic (probably actually oblique) version of the fixed-width font.
1226 Pod::Man doesn't assume you have this, and defaults to CB.  Some systems
1227 (such as Solaris) have this font available as CX.  Only matters for troff(1)
1228 output.
1229
1230 =item quotes
1231
1232 Sets the quote marks used to surround CE<lt>> text.  If the value is a
1233 single character, it is used as both the left and right quote; if it is two
1234 characters, the first character is used as the left quote and the second as
1235 the right quoted; and if it is four characters, the first two are used as
1236 the left quote and the second two as the right quote.
1237
1238 This may also be set to the special value C<none>, in which case no quote
1239 marks are added around CE<lt>> text (but the font is still changed for troff
1240 output).
1241
1242 =item release
1243
1244 Set the centered footer.  By default, this is the version of Perl you run
1245 Pod::Man under.  Note that some system an macro sets assume that the
1246 centered footer will be a modification date and will prepend something like
1247 "Last modified: "; if this is the case, you may want to set C<release> to
1248 the last modified date and C<date> to the version number.
1249
1250 =item section
1251
1252 Set the section for the C<.TH> macro.  The standard section numbering
1253 convention is to use 1 for user commands, 2 for system calls, 3 for
1254 functions, 4 for devices, 5 for file formats, 6 for games, 7 for
1255 miscellaneous information, and 8 for administrator commands.  There is a lot
1256 of variation here, however; some systems (like Solaris) use 4 for file
1257 formats, 5 for miscellaneous information, and 7 for devices.  Still others
1258 use 1m instead of 8, or some mix of both.  About the only section numbers
1259 that are reliably consistent are 1, 2, and 3.
1260
1261 By default, section 1 will be used unless the file ends in .pm in which case
1262 section 3 will be selected.
1263
1264 =back
1265
1266 The standard Pod::Parser method parse_from_filehandle() takes up to two
1267 arguments, the first being the file handle to read POD from and the second
1268 being the file handle to write the formatted output to.  The first defaults
1269 to STDIN if not given, and the second defaults to STDOUT.  The method
1270 parse_from_file() is almost identical, except that its two arguments are the
1271 input and output disk files instead.  See L<Pod::Parser> for the specific
1272 details.
1273
1274 =head1 DIAGNOSTICS
1275
1276 =over 4
1277
1278 =item roff font should be 1 or 2 chars, not "%s"
1279
1280 (F) You specified a *roff font (using C<fixed>, C<fixedbold>, etc.) that
1281 wasn't either one or two characters.  Pod::Man doesn't support *roff fonts
1282 longer than two characters, although some *roff extensions do (the canonical
1283 versions of nroff(1) and troff(1) don't either).
1284
1285 =item Invalid link %s
1286
1287 (W) The POD source contained a C<LE<lt>E<gt>> sequence that Pod::Man was
1288 unable to parse.  You should never see this error message; it probably
1289 indicates a bug in Pod::Man.
1290
1291 =item Invalid quote specification "%s"
1292
1293 (F) The quote specification given (the quotes option to the constructor) was
1294 invalid.  A quote specification must be one, two, or four characters long.
1295
1296 =item %s:%d: Unknown command paragraph "%s".
1297
1298 (W) The POD source contained a non-standard command paragraph (something of
1299 the form C<=command args>) that Pod::Man didn't know about.  It was ignored.
1300
1301 =item Unknown escape EE<lt>%sE<gt>
1302
1303 (W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Man didn't
1304 know about.  C<EE<lt>%sE<gt>> was printed verbatim in the output.
1305
1306 =item Unknown sequence %s
1307
1308 (W) The POD source contained a non-standard interior sequence (something of
1309 the form C<XE<lt>E<gt>>) that Pod::Man didn't know about.  It was ignored.
1310
1311 =item %s: Unknown command paragraph "%s" on line %d.
1312
1313 (W) The POD source contained a non-standard command paragraph (something of
1314 the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1315
1316 =item Unmatched =back
1317
1318 (W) Pod::Man encountered a C<=back> command that didn't correspond to an
1319 C<=over> command.
1320
1321 =back
1322
1323 =head1 BUGS
1324
1325 The lint-like features and strict POD format checking done by B<pod2man> are
1326 not yet implemented and should be, along with the corresponding C<lax>
1327 option.
1328
1329 The NAME section should be recognized specially and index entries emitted
1330 for everything in that section.  This would have to be deferred until the
1331 next section, since extraneous things in NAME tends to confuse various man
1332 page processors.
1333
1334 The handling of hyphens, en dashes, and em dashes is somewhat fragile, and
1335 one may get the wrong one under some circumstances.  This should only matter
1336 for troff(1) output.
1337
1338 When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
1339 necessarily get it right.
1340
1341 Pod::Man doesn't handle font names longer than two characters.  Neither do
1342 most troff(1) implementations, but GNU troff does as an extension.  It would
1343 be nice to support as an option for those who want to use it.
1344
1345 The preamble added to each output file is rather verbose, and most of it is
1346 only necessary in the presence of EE<lt>E<gt> escapes for non-ASCII
1347 characters.  It would ideally be nice if all of those definitions were only
1348 output if needed, perhaps on the fly as the characters are used.
1349
1350 Some of the automagic applied to file names assumes Unix directory
1351 separators.
1352
1353 Pod::Man is excessively slow.
1354
1355 =head1 SEE ALSO
1356
1357 L<Pod::Parser|Pod::Parser>, perlpod(1), pod2man(1), nroff(1), troff(1),
1358 man(1), man(7)
1359
1360 Ossanna, Joseph F., and Brian W. Kernighan.  "Troff User's Manual,"
1361 Computing Science Technical Report No. 54, AT&T Bell Laboratories.  This is
1362 the best documentation of standard nroff(1) and troff(1).  At the time of
1363 this writing, it's available at http://www.cs.bell-labs.com/cm/cs/cstr.html.
1364
1365 The man page documenting the man macro set may be man(5) instead of man(7)
1366 on your system.  Also, please see pod2man(1) for extensive documentation on
1367 writing manual pages if you've not done it before and aren't familiar with
1368 the conventions.
1369
1370 =head1 AUTHOR
1371
1372 Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
1373 original B<pod2man> by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>.
1374
1375 =cut