This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Proposed fix for Pod::Man
[perl5.git] / lib / Pod / Man.pm
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 $
3 #
4 # Copyright 1999, 2000, 2001 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.16;
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 .    \" fudge factors for nroff and troff
128 .if n \{\
129 .    ds #H 0
130 .    ds #V .8m
131 .    ds #F .3m
132 .    ds #[ \f1
133 .    ds #] \fP
134 .\}
135 .if t \{\
136 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
137 .    ds #V .6m
138 .    ds #F 0
139 .    ds #[ \&
140 .    ds #] \&
141 .\}
142 .    \" simple accents for nroff and troff
143 .if n \{\
144 .    ds ' \&
145 .    ds ` \&
146 .    ds ^ \&
147 .    ds , \&
148 .    ds ~ ~
149 .    ds /
150 .\}
151 .if t \{\
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'
158 .\}
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 \
174 \{\
175 .    ds : e
176 .    ds 8 ss
177 .    ds o a
178 .    ds d- d\h'-1'\(ga
179 .    ds D- D\h'-1'\(hy
180 .    ds th \o'bp'
181 .    ds Th \o'LP'
182 .    ds ae ae
183 .    ds Ae AE
184 .\}
185 .rm #[ #] #H #V #F C
186 ----END OF PREAMBLE----
187 #`# for cperl-mode
188
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.
194 %ESCAPES = (
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
201
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
264 );
265
266
267 ############################################################################
268 # Static helper functions
269 ############################################################################
270
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.
275 sub protect {
276     local $_ = shift;
277     s/^([.\'\\])/\\&$1/mg;
278     $_;
279 }
280
281 # Translate a font string into an escape.
282 sub toescape { (length ($_[0]) > 1 ? '\f(' : '\f') . $_[0] }
283
284
285 ############################################################################
286 # Initialization
287 ############################################################################
288
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().
295 sub initialize {
296     my $self = shift;
297
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{$_}");
305             }
306         } else {
307             $$self{$_} = '';
308         }
309     }
310
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';
317
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})};
326
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};
331
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})$/);
338         $version[2] ||= 0;
339         $version[2] *= 10 ** (3 - length $version[2]);
340         for (@version) { $_ += 0 }
341         $$self{release} = 'perl v' . join ('.', @version);
342     }
343
344     # Double quotes in things that will be quoted.
345     for (qw/center date release/) {
346         $$self{$_} =~ s/\"/\"\"/g if $$self{$_};
347     }
348
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} =~ /^(..)(..)$/) {
357         $$self{LQUOTE} = $1;
358         $$self{RQUOTE} = $2;
359     } else {
360         croak qq(Invalid quote specification "$$self{quotes}");
361     }
362
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/\"/\"\"/;
368
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.
373
374     $self->SUPER::initialize;
375 }
376
377 # For each document we process, output the preamble first.
378 sub begin_pod {
379     my $self = shift;
380
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);
391         } else {
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.
398             for ($name) {
399                 s%//+%/%g;
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
405                 }
406                 s%^lib/%%;
407                 s%/%::%g;
408             }
409         }
410     }
411
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/);
415
416     # Modification date header.  Try to use the modification time of our
417     # input.
418     if (!defined $$self{date}) {
419         my $time = (stat $self->input_file)[9] || time;
420         my ($day, $month, $year) = (localtime $time)[3,4,5];
421         $month++;
422         $year += 1900;
423         $$self{date} = sprintf ('%4d-%02d-%02d', $year, $month, $day);
424     }
425
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}/;
431     chomp $_;
432     print { $self->output_handle } <<"----END OF HEADER----";
433 .\\" Automatically generated by Pod::Man version $VERSION
434 .\\" @{[ scalar localtime ]}
435 .\\"
436 .\\" Standard preamble:
437 .\\" ======================================================================
438 $_
439 .\\" ======================================================================
440 .\\"
441 .IX Title "$name $section"
442 .TH $name $section "$$self{release}" "$$self{date}" "$$self{center}"
443 .UC
444 ----END OF HEADER----
445 #"# for cperl-mode
446
447     # Initialize a few per-file variables.
448     $$self{INDENT} = 0;
449     $$self{NEEDSPACE} = 0;
450 }
451
452
453 ############################################################################
454 # Core overrides
455 ############################################################################
456
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.
461 sub command {
462     my $self = shift;
463     my $command = shift;
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 (@_);
469      } else {
470         my ($text, $line, $paragraph) = @_;
471         my $file;
472         ($file, $line) = $paragraph->file_line;
473         $text =~ s/\n+\z//;
474         $text = " $text" if ($text =~ /^\S/);
475         warn qq($file:$line: Unknown command paragraph "=$command$text"\n);
476         return;
477     }
478 }
479
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.
484 sub verbatim {
485     my $self = shift;
486     return if $$self{EXCLUDE};
487     local $_ = shift;
488     return if /^\s+$/;
489     s/\s+$/\n/;
490     my $lines = tr/\n/\n/;
491     1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
492     s/\\/\\e/g;
493     s/^(\s*\S)/'\&' . $1/gme;
494     $self->makespace;
495     $self->output (".Vb $lines\n$_.Ve\n");
496     $$self{NEEDSPACE} = 0;
497 }
498
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.
501 sub textblock {
502     my $self = shift;
503     return if $$self{EXCLUDE};
504     $self->output ($_[0]), return if $$self{VERBATIM};
505
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.
509     my $text = shift;
510     $text =~ s{
511         (L<                     # A link of the form L</something>.
512               /
513               (
514                   [:\w]+        # The item has to be a simple word...
515                   (\(\))?       # ...or simple function.
516               )
517           >
518           (
519               ,?\s+(and\s+)?    # Allow lots of them, conjuncted.
520               L<
521                   /
522                   ( [:\w]+ ( \(\) )? )
523               >
524           )+
525         )
526     } {
527         local $_ = $1;
528         s{ L< / ( [^>]+ ) > } {$1}xg;
529         my @items = split /(?:,?\s+(?:and\s+)?)/;
530         my $string = 'the ';
531         my $i;
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);
537         }
538         $string .= ' entries elsewhere in this document';
539         $string;
540     }gex;
541
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/;
546     $self->makespace;
547     $self->output (protect $self->textmapfonts ($text));
548     $self->outindex;
549     $$self{NEEDSPACE} = 1;
550 }
551
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.
556 sub sequence {
557     my ($self, $seq) = @_;
558     my $command = $seq->cmd_name;
559
560     # Zero-width characters.
561     if ($command eq 'Z') {
562         # Workaround to generate a blessable reference, needed by 5.005.
563         my $tmp = '\&';
564         return bless \ "$tmp", 'Pod::Man::String';
565     }
566
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);
572
573     # Handle E<> escapes.
574     if ($command eq 'E') {
575         if (/^\d+$/) {
576             return bless \ chr ($_), 'Pod::Man::String';
577         } elsif (exists $ESCAPES{$_}) {
578             return bless \ "$ESCAPES{$_}", 'Pod::Man::String';
579         } else {
580             carp "Unknown escape E<$1>";
581             return bless \ "E<$_>", 'Pod::Man::String';
582         }
583     }
584
585     # For all the other sequences, empty content produces no output.
586     return '' if $_ eq '';
587
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"),
597             'Pod::Man::String';
598     }
599
600     # Handle links.
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';
605     }
606
607     # Whitespace protection replaces whitespace with "\ ".
608     if ($command eq 'S') {
609         s/\s+/\\ /g;
610         return bless \ "$_", 'Pod::Man::String';
611     }
612
613     # Add an index entry to the list of ones waiting to be output.
614     if ($command eq 'X') { push (@{ $$self{INDEX} }, $_); return '' }
615
616     # Anything else is unknown.
617     carp "Unknown sequence $command<$_>";
618 }
619
620
621 ############################################################################
622 # Command paragraphs
623 ############################################################################
624
625 # All command paragraphs take the paragraph and the line number.
626
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
630 # them.
631 sub cmd_head1 {
632     my $self = shift;
633     local $_ = $self->parse (@_);
634     s/\s+$//;
635     s/\\s-?\d//g;
636     s/\s*\n\s*/ /g;
637     if ($$self{ITEMS} > 1) {
638         $$self{ITEMS} = 0;
639         $self->output (".PD\n");
640     }
641     $self->output ($self->switchquotes ('.SH', $self->mapfonts ($_)));
642     $self->outindex (($_ eq 'NAME') ? () : ('Header', $_));
643     $$self{NEEDSPACE} = 0;
644 }
645
646 # Second level heading.
647 sub cmd_head2 {
648     my $self = shift;
649     local $_ = $self->parse (@_);
650     s/\s+$//;
651     s/\s*\n\s*/ /g;
652     if ($$self{ITEMS} > 1) {
653         $$self{ITEMS} = 0;
654         $self->output (".PD\n");
655     }
656     $self->output ($self->switchquotes ('.Sh', $self->mapfonts ($_)));
657     $self->outindex ('Subsection', $_);
658     $$self{NEEDSPACE} = 0;
659 }
660
661 # Third level heading.
662 sub cmd_head3 {
663     my $self = shift;
664     local $_ = $self->parse (@_);
665     s/\s+$//;
666     s/\s*\n\s*/ /g;
667     if ($$self{ITEMS} > 1) {
668         $$self{ITEMS} = 0;
669         $self->output (".PD\n");
670     }
671     $self->makespace;
672     $self->output ($self->switchquotes ('.I', $self->mapfonts ($_)));
673     $self->outindex ('Subsection', $_);
674     $$self{NEEDSPACE} = 1;
675 }
676
677 # Fourth level heading.
678 sub cmd_head4 {
679     my $self = shift;
680     local $_ = $self->parse (@_);
681     s/\s+$//;
682     s/\s*\n\s*/ /g;
683     if ($$self{ITEMS} > 1) {
684         $$self{ITEMS} = 0;
685         $self->output (".PD\n");
686     }
687     $self->makespace;
688     $self->output ($self->textmapfonts ($_) . "\n");
689     $self->outindex ('Subsection', $_);
690     $$self{NEEDSPACE} = 1;
691 }
692
693 # Start a list.  For indents after the first, wrap the outside indent in .RS
694 # so that hanging paragraph tags will be correct.
695 sub cmd_over {
696     my $self = shift;
697     local $_ = shift;
698     unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
699     if (@{ $$self{INDENTS} } > 0) {
700         $self->output (".RS $$self{INDENT}\n");
701     }
702     push (@{ $$self{INDENTS} }, $$self{INDENT});
703     $$self{INDENT} = ($_ + 0);
704 }
705
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.
709 sub cmd_back {
710     my $self = shift;
711     $$self{INDENT} = pop @{ $$self{INDENTS} };
712     unless (defined $$self{INDENT}) {
713         carp "Unmatched =back";
714         $$self{INDENT} = 0;
715     }
716     if ($$self{WEIRDINDENT}) {
717         $self->output (".RE\n");
718         $$self{WEIRDINDENT} = 0;
719     }
720     if (@{ $$self{INDENTS} } > 0) {
721         $self->output (".RE\n");
722         $self->output (".RS $$self{INDENT}\n");
723         $$self{WEIRDINDENT} = 1;
724     }
725     $$self{NEEDSPACE} = 1;
726 }
727
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
733 # embedded.
734 sub cmd_item {
735     my $self = shift;
736     local $_ = $self->parse (@_);
737     s/\s+$//;
738     s/\s*\n\s*/ /g;
739     my $index;
740     if (/\w/ && !/^\w[.\)]\s*$/) {
741         $index = $_;
742         $index =~ s/^\s*[-*+o.]?(?:\s+|\Z)//;
743     }
744     s/^\*(\s|\Z)/\\\(bu$1/;
745     if ($$self{WEIRDINDENT}) {
746         $self->output (".RE\n");
747         $$self{WEIRDINDENT} = 0;
748     }
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;
754     $$self{ITEMS}++;
755 }
756
757 # Begin a block for a particular translator.  Setting VERBATIM triggers
758 # special handling in textblock().
759 sub cmd_begin {
760     my $self = shift;
761     local $_ = shift;
762     my ($kind) = /^(\S+)/ or return;
763     if ($kind eq 'man' || $kind eq 'roff') {
764         $$self{VERBATIM} = 1;
765     } else {
766         $$self{EXCLUDE} = 1;
767     }
768 }
769
770 # End a block for a particular translator.  We assume that all =begin/=end
771 # pairs are properly closed.
772 sub cmd_end {
773     my $self = shift;
774     $$self{EXCLUDE} = 0;
775     $$self{VERBATIM} = 0;
776 }
777
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.
780 sub cmd_for {
781     my $self = shift;
782     local $_ = shift;
783     return unless s/^(?:man|roff)\b[ \t]*\n?//;
784     $self->output ($_);
785 }
786
787
788 ############################################################################
789 # Link handling
790 ############################################################################
791
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.
794 sub buildlink {
795     my $self = shift;
796     local $_ = shift;
797
798     # Smash whitespace in case we were split across multiple lines.
799     s/\s+/ /g;
800
801     # If we were given any explicit text, just output it.
802     if (m{ ^ ([^|]+) \| }x) { return $1 }
803
804     # Okay, leading and trailing whitespace isn't important.
805     s/^\s+//;
806     s/\s+$//;
807
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 $_ }
811
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;
824     } elsif (m%/%) {
825         ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
826         if ($manpage =~ /^[-:.\w]+(?:\(\S+\))?$/) {
827             $manpage =~ s/^([^\(]+)\(/'\f(IS' . $1 . '\f(IE\|'/e;
828         }
829         $section =~ s/^\"\s*//;
830         $section =~ s/\s*\"$//;
831     }
832     if ($manpage && $manpage !~ /\\f\(IS/) {
833         $manpage = "the $manpage manpage";
834     }
835
836     # Now build the actual output text.
837     my $text = '';
838     if (!length ($section) && !length ($manpage)) {
839         carp "Invalid link $_";
840     } elsif (!length ($section)) {
841         $text = $manpage;
842     } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
843         $text .= 'the ' . $section . ' entry';
844         $text .= (length $manpage) ? " in $manpage"
845                                    : " elsewhere in this document";
846     } else {
847         if ($section !~ /^".*"$/) { $section = '"' . $section . '"' }
848         $text .= 'the section on ' . $section;
849         $text .= " in $manpage" if length $manpage;
850     }
851     $text;
852 }
853
854
855 ############################################################################
856 # Escaping and fontification
857 ############################################################################
858
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.
866 #
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.
872 sub mapfonts {
873     my $self = shift;
874     local $_ = shift;
875
876     my ($fixed, $bold, $italic) = (0, 0, 0);
877     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
878     my $last = '\fR';
879     s { \\f\((.)(.) } {
880         my $sequence = '';
881         my $f;
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)};
885         if ($f eq $last) {
886             '';
887         } else {
888             if ($f ne '\fR') { $sequence .= $f }
889             $last = $f;
890             $sequence;
891         }
892     }gxe;
893     $_;
894 }
895
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.
901 sub textmapfonts {
902     my $self = shift;
903     local $_ = shift;
904
905     my ($fixed, $bold, $italic) = (0, 0, 0);
906     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
907     s { \\f\((.)(.) } {
908         ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
909         $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
910     }gxe;
911     $_;
912 }
913
914
915 ############################################################################
916 # *roff-specific parsing
917 ############################################################################
918
919 # Called instead of parse_text, calls parse_text with the right flags.
920 sub parse {
921     my $self = shift;
922     $self->parse_text ({ -expand_seq   => 'sequence',
923                          -expand_ptree => 'collapse' }, @_);
924 }
925
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.
934 sub collapse {
935     my ($self, $ptree, $literal) = @_;
936     if ($literal) {
937         return join ('', map {
938             if (ref $_) {
939                 $$_;
940             } else {
941                 s/\\/\\e/g;
942                 s/-/\\-/g    if $literal > 1;
943                 s/__/_\\|_/g if $literal > 1;
944                 $_;
945             }
946         } $ptree->children);
947     } else {
948         return join ('', map {
949             ref ($_) ? $$_ : $self->guesswork ($_)
950         } $ptree->children);
951     }
952 }
953
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
956 # done.
957 sub guesswork {
958     my $self = shift;
959     local $_ = shift;
960
961     # rofficate backslashes.
962     s/\\/\\e/g;
963
964     # Ensure double underbars have a tiny space between them.
965     s/__/_\\|_/g;
966
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.
970     s{
971         ( ^ | [\s\(\"\'\`\[\{<>] )
972         ( [A-Z] [A-Z] [/A-Z+:\d_\$&-]* )
973         (?: (?= [\s>\}\]\(\)\'\".?!,;] | -- ) | $ )
974     } { $1 . '\s-1' . $2 . '\s0' }egx;
975
976     # Turn PI into a pretty pi.
977     s{ (?: \\s-1 | \b ) PI (?: \\s0 | \b ) } {\\*\(PI}gx;
978
979     # Italize functions in the form func().
980     s{
981         ( \b | \\s-1 )
982         (
983             [A-Za-z_] ([:\w]|\\s-?[01])+ \(\)
984         )
985     } { $1 . '\f(IS' . $2 . '\f(IE' }egx;
986
987     # func(n) is a reference to a manual page.  Make it \fIfunc\fR\|(n).
988     s{
989         ( \b | \\s-1 )
990         ( [A-Za-z_] (?:[-:.\w]|\\s-?[01])+ )
991         (
992             \( \d [a-z]* \)
993         )
994     } { $1 . '\f(IS' . $2 . '\f(IE\|' . $3 }egx;
995
996     # Convert simple Perl variable references to a fixed-width font.
997     s{
998         ( \s+ )
999         ( [\$\@%] [\w:]+ )
1000         (?! \( )
1001     } { $1 . '\f(FS' . $2 . '\f(FE'}egx;
1002
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 '='))) {
1015             "$pre\\*(--$post";
1016         } else {
1017             $pre . ('\-' x length $dash) . $post;
1018         }
1019     }egxs;
1020
1021     # Fix up double quotes.
1022     s{ \" ([^\"]+) \" } { '\*(L"' . $1 . '\*(R"' }egx;
1023
1024     # Make C++ into \*(C+, which is a squinched version.
1025     s{ \b C\+\+ } {\\*\(C+}gx;
1026
1027     # All done.
1028     $_;
1029 }
1030
1031
1032 ############################################################################
1033 # Output formatting
1034 ############################################################################
1035
1036 # Make vertical whitespace.
1037 sub makespace {
1038     my $self = shift;
1039     $self->output (".PD\n") if ($$self{ITEMS} > 1);
1040     $$self{ITEMS} = 0;
1041     $self->output ($$self{INDENT} > 0 ? ".Sp\n" : ".PP\n")
1042         if $$self{NEEDSPACE};
1043 }
1044
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.
1048 sub outindex {
1049     my ($self, $section, $index) = @_;
1050     my @entries = map { split m%\s*/\s*% } @{ $$self{INDEX} };
1051     return unless ($section || @entries);
1052     $$self{INDEX} = [];
1053     my $output;
1054     if (@entries) {
1055         my $output = '.IX Xref "'
1056             . join (' ', map { s/\"/\"\"/; $_ } @entries)
1057             . '"' . "\n";
1058     }
1059     if ($section) {
1060         $index =~ s/\"/\"\"/;
1061         $index =~ s/\\-/-/g;
1062         $index =~ s/\\(?:s-?\d|.\(..|.)//g;
1063         $output .= ".IX $section " . '"' . $index . '"' . "\n";
1064     }
1065     $self->output ($output);
1066 }
1067
1068 # Output text to the output device.
1069 sub output { print { $_[0]->output_handle } $_[1] }
1070
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.
1078 sub switchquotes {
1079     my $self = shift;
1080     my $command = shift;
1081     local $_ = shift;
1082     my $extra = shift;
1083     s/\\\*\([LR]\"/\"/g;
1084
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[\'\`]/)) {
1092         s/\"/\"\"/g;
1093         my $troff = $_;
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";
1103     } else {
1104         $_ = qq("$_") . ($extra ? " $extra" : '');
1105         return "$command $_\n";
1106     }
1107 }
1108
1109 __END__
1110
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.
1115 .if n \{\
1116 .    ds ? ?
1117 .    ds ! !
1118 .    ds q
1119 .\}
1120 .if t \{\
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'
1124 .\}
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 \
1132 \{\
1133 .    ds v \h'-1'\o'\(aa\(ga'
1134 .    ds _ \h'-1'^
1135 .    ds . \h'-1'.
1136 .    ds 3 3
1137 .    ds oe oe
1138 .    ds Oe OE
1139 .\}
1140
1141 ############################################################################
1142 # Documentation
1143 ############################################################################
1144
1145 =head1 NAME
1146
1147 Pod::Man - Convert POD data to formatted *roff input
1148
1149 =head1 SYNOPSIS
1150
1151     use Pod::Man;
1152     my $parser = Pod::Man->new (release => $VERSION, section => 8);
1153
1154     # Read POD from STDIN and write to STDOUT.
1155     $parser->parse_from_filehandle;
1156
1157     # Read POD from file.pod and write to file.1.
1158     $parser->parse_from_file ('file.pod', 'file.1');
1159
1160 =head1 DESCRIPTION
1161
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
1167 be used directly.
1168
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().
1173
1174 new() can take options, in the form of key/value pairs that control the
1175 behavior of the parser.  See below for details.
1176
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
1183 STDIN for input).
1184
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
1189 fixed-width output.
1190
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.
1200
1201 The recognized options to new() are as follows.  All options take a single
1202 argument.
1203
1204 =over 4
1205
1206 =item center
1207
1208 Sets the centered page header to use instead of "User Contributed Perl
1209 Documentation".
1210
1211 =item date
1212
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
1216 YYYY-MM-DD.
1217
1218 =item fixed
1219
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.
1222
1223 =item fixedbold
1224
1225 Bold version of the fixed-width font.  Defaults to CB.  Only matters for
1226 troff(1) output.
1227
1228 =item fixeditalic
1229
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.
1233
1234 =item fixedbolditalic
1235
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)
1239 output.
1240
1241 =item quotes
1242
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.
1248
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
1251 output).
1252
1253 =item release
1254
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.
1260
1261 =item section
1262
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.
1271
1272 By default, section 1 will be used unless the file ends in .pm in which case
1273 section 3 will be selected.
1274
1275 =back
1276
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
1283 details.
1284
1285 =head1 DIAGNOSTICS
1286
1287 =over 4
1288
1289 =item roff font should be 1 or 2 chars, not "%s"
1290
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).
1295
1296 =item Invalid link %s
1297
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.
1301
1302 =item Invalid quote specification "%s"
1303
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.
1306
1307 =item %s:%d: Unknown command paragraph "%s".
1308
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.
1311
1312 =item Unknown escape EE<lt>%sE<gt>
1313
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.
1316
1317 =item Unknown sequence %s
1318
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.
1321
1322 =item %s: Unknown command paragraph "%s" on line %d.
1323
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.
1326
1327 =item Unmatched =back
1328
1329 (W) Pod::Man encountered a C<=back> command that didn't correspond to an
1330 C<=over> command.
1331
1332 =back
1333
1334 =head1 BUGS
1335
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>
1338 option.
1339
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
1343 page processors.
1344
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.
1348
1349 When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
1350 necessarily get it right.
1351
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.
1355
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.
1360
1361 Some of the automagic applied to file names assumes Unix directory
1362 separators.
1363
1364 Pod::Man is excessively slow.
1365
1366 =head1 SEE ALSO
1367
1368 L<Pod::Parser|Pod::Parser>, perlpod(1), pod2man(1), nroff(1), troff(1),
1369 man(1), man(7)
1370
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.
1375
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
1379 the conventions.
1380
1381 =head1 AUTHOR
1382
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>.
1385
1386 =cut