More encoding mapping magic.
[p5sagit/p5-mst-13.2.git] / lib / Pod / Man.pm
CommitLineData
9741dab0 1# Pod::Man -- Convert POD data to formatted *roff input.
ee89c1da 2# $Id: Man.pm,v 1.16 2001/04/09 13:06:02 eagle Exp $
9741dab0 3#
77003bb1 4# Copyright 1999, 2000, 2001 by Russ Allbery <rra@stanford.edu>
9741dab0 5#
6# This program is free software; you can redistribute it and/or modify it
7# under the same terms as Perl itself.
8#
c9abbd5d 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.
9741dab0 19
20############################################################################
21# Modules and declarations
22############################################################################
23
24package Pod::Man;
25
26require 5.004;
27
28use Carp qw(carp croak);
29use Pod::Parser ();
30
31use strict;
32use subs qw(makespace);
33use vars qw(@ISA %ESCAPES $PREAMBLE $VERSION);
34
35@ISA = qw(Pod::Parser);
36
c9abbd5d 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.
ee89c1da 41$VERSION = 1.16;
9741dab0 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
ab1f1d91 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.
9741dab0 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" ""
ab1f1d91 98. ds C` @LQUOTE@
99. ds C' @RQUOTE@
9741dab0 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"
f3248e50 115..
9741dab0 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.
9741dab0 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----
5cdeb5a2 187#`# for cperl-mode
188
9741dab0 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
ab1f1d91 199 'sol' => '/', # solidus (forward slash)
be3174d2 200 'verbar' => '|', # vertical bar
9741dab0 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
44464a02 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.
c9abbd5d 275sub protect {
276 local $_ = shift;
44464a02 277 s/^([.\'\\])/\\&$1/mg;
c9abbd5d 278 $_;
279}
5cdeb5a2 280
9741dab0 281# Translate a font string into an escape.
282sub toescape { (length ($_[0]) > 1 ? '\f(' : '\f') . $_[0] }
283
5cdeb5a2 284
9741dab0 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().
295sub 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) {
ab1f1d91 303 croak qq(roff font should be 1 or 2 chars,)
304 . qq( not "$$self{$_}");
9741dab0 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
c9abbd5d 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.
9741dab0 336 if (!defined $$self{release}) {
c9abbd5d 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);
9741dab0 342 }
343
344 # Double quotes in things that will be quoted.
c9abbd5d 345 for (qw/center date release/) {
346 $$self{$_} =~ s/\"/\"\"/g if $$self{$_};
347 }
9741dab0 348
ab1f1d91 349 # Figure out what quotes we'll be using for C<> text.
5cdeb5a2 350 $$self{quotes} ||= '"';
ab1f1d91 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
9741dab0 369 $$self{INDENT} = 0; # Current indentation level.
370 $$self{INDENTS} = []; # Stack of indentations.
371 $$self{INDEX} = []; # Index keys waiting to be printed.
5cdeb5a2 372 $$self{ITEMS} = 0; # The number of consecutive =items.
9741dab0 373
374 $self->SUPER::initialize;
375}
376
ab1f1d91 377# For each document we process, output the preamble first.
9741dab0 378sub 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;
fe6f1558 386 $section = 3 if (!$$self{section} && $name =~ /\.pm\z/i);
387 $name =~ s/\.p(od|[lm])\z//i;
9741dab0 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
c88ded47 396 # which works. Should be fixed to use File::Spec. Also handle
397 # a leading lib/ since that's what ExtUtils::MakeMaker creates.
9741dab0 398 for ($name) {
399 s%//+%/%g;
c9abbd5d 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
9741dab0 405 }
c88ded47 406 s%^lib/%%;
9741dab0 407 s%/%::%g;
408 }
409 }
410 }
411
77003bb1 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
9741dab0 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;
c9abbd5d 423 $$self{date} = sprintf ('%4d-%02d-%02d', $year, $month, $day);
9741dab0 424 }
425
426 # Now, print out the preamble and the title.
ab1f1d91 427 local $_ = $PREAMBLE;
428 s/\@CFONT\@/$$self{fixed}/;
429 s/\@LQUOTE\@/$$self{LQUOTE}/;
430 s/\@RQUOTE\@/$$self{RQUOTE}/;
431 chomp $_;
9741dab0 432 print { $self->output_handle } <<"----END OF HEADER----";
433.\\" Automatically generated by Pod::Man version $VERSION
434.\\" @{[ scalar localtime ]}
435.\\"
436.\\" Standard preamble:
437.\\" ======================================================================
ab1f1d91 438$_
9741dab0 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.
461sub command {
462 my $self = shift;
463 my $command = shift;
464 return if $command eq 'pod';
ab1f1d91 465 return if ($$self{EXCLUDE} && $command ne 'end');
466 if ($self->can ('cmd_' . $command)) {
467 $command = 'cmd_' . $command;
844b31e3 468 $self->$command (@_);
ab1f1d91 469 } else {
470 my ($text, $line, $paragraph) = @_;
5cdeb5a2 471 my $file;
472 ($file, $line) = $paragraph->file_line;
ab1f1d91 473 $text =~ s/\n+\z//;
474 $text = " $text" if ($text =~ /^\S/);
475 warn qq($file:$line: Unknown command paragraph "=$command$text"\n);
476 return;
844b31e3 477 }
9741dab0 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.
484sub 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;
5cdeb5a2 494 $self->makespace;
9741dab0 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.
501sub 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.
c9abbd5d 509 my $text = shift;
510 $text =~ s{
9741dab0 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.
5cdeb5a2 520 L<
9741dab0 521 /
522 ( [:\w]+ ( \(\) )? )
523 >
524 )+
525 )
526 } {
527 local $_ = $1;
c9abbd5d 528 s{ L< / ( [^>]+ ) > } {$1}xg;
9741dab0 529 my @items = split /(?:,?\s+(?:and\s+)?)/;
c9abbd5d 530 my $string = 'the ';
9741dab0 531 my $i;
532 for ($i = 0; $i < @items; $i++) {
533 $string .= $items[$i];
c9abbd5d 534 $string .= ', ' if @items > 2 && $i != $#items;
535 $string .= ' ' if @items == 2 && $i == 2;
536 $string .= 'and ' if ($i == $#items - 1);
9741dab0 537 }
c9abbd5d 538 $string .= ' entries elsewhere in this document';
9741dab0 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.
c9abbd5d 544 $text = $self->parse ($text, @_);
545 $text =~ s/\n\s*$/\n/;
5cdeb5a2 546 $self->makespace;
50a3fd2a 547 $self->output (protect $self->textmapfonts ($text));
9741dab0 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.
556sub sequence {
557 my ($self, $seq) = @_;
558 my $command = $seq->cmd_name;
559
560 # Zero-width characters.
f5daac4a 561 if ($command eq 'Z') {
c9abbd5d 562 # Workaround to generate a blessable reference, needed by 5.005.
563 my $tmp = '\&';
564 return bless \ "$tmp", 'Pod::Man::String';
f5daac4a 565 }
9741dab0 566
a3e04946 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);
9741dab0 572
573 # Handle E<> escapes.
574 if ($command eq 'E') {
2e20e14f 575 if (/^\d+$/) {
576 return bless \ chr ($_), 'Pod::Man::String';
577 } elsif (exists $ESCAPES{$_}) {
9741dab0 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') {
9741dab0 596 return bless \ ('\f(FS\*(C`' . $_ . "\\*(C'\\f(FE"),
597 'Pod::Man::String';
598 }
599
600 # Handle links.
601 if ($command eq 'L') {
c9abbd5d 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';
9741dab0 605 }
5cdeb5a2 606
9741dab0 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.
631sub cmd_head1 {
632 my $self = shift;
633 local $_ = $self->parse (@_);
634 s/\s+$//;
635 s/\\s-?\d//g;
77003bb1 636 s/\s*\n\s*/ /g;
5cdeb5a2 637 if ($$self{ITEMS} > 1) {
638 $$self{ITEMS} = 0;
639 $self->output (".PD\n");
640 }
50a3fd2a 641 $self->output ($self->switchquotes ('.SH', $self->mapfonts ($_)));
9741dab0 642 $self->outindex (($_ eq 'NAME') ? () : ('Header', $_));
643 $$self{NEEDSPACE} = 0;
644}
645
646# Second level heading.
647sub cmd_head2 {
648 my $self = shift;
649 local $_ = $self->parse (@_);
650 s/\s+$//;
77003bb1 651 s/\s*\n\s*/ /g;
5cdeb5a2 652 if ($$self{ITEMS} > 1) {
653 $$self{ITEMS} = 0;
654 $self->output (".PD\n");
655 }
50a3fd2a 656 $self->output ($self->switchquotes ('.Sh', $self->mapfonts ($_)));
9741dab0 657 $self->outindex ('Subsection', $_);
658 $$self{NEEDSPACE} = 0;
659}
660
50a3fd2a 661# Third level heading.
662sub cmd_head3 {
663 my $self = shift;
664 local $_ = $self->parse (@_);
665 s/\s+$//;
77003bb1 666 s/\s*\n\s*/ /g;
50a3fd2a 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.
678sub cmd_head4 {
679 my $self = shift;
680 local $_ = $self->parse (@_);
681 s/\s+$//;
77003bb1 682 s/\s*\n\s*/ /g;
50a3fd2a 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
9741dab0 693# Start a list. For indents after the first, wrap the outside indent in .RS
694# so that hanging paragraph tags will be correct.
695sub 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.
709sub 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
46bce7d0 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.
9741dab0 734sub cmd_item {
735 my $self = shift;
736 local $_ = $self->parse (@_);
737 s/\s+$//;
46bce7d0 738 s/\s*\n\s*/ /g;
9741dab0 739 my $index;
740 if (/\w/ && !/^\w[.\)]\s*$/) {
741 $index = $_;
fe2227f0 742 $index =~ s/^\s*[-*+o.]?(?:\s+|\Z)//;
9741dab0 743 }
744 s/^\*(\s|\Z)/\\\(bu$1/;
745 if ($$self{WEIRDINDENT}) {
746 $self->output (".RE\n");
747 $$self{WEIRDINDENT} = 0;
748 }
50a3fd2a 749 $_ = $self->textmapfonts ($_);
5cdeb5a2 750 $self->output (".PD 0\n") if ($$self{ITEMS} == 1);
50a3fd2a 751 $self->output ($self->switchquotes ('.Ip', $_, $$self{INDENT}));
9741dab0 752 $self->outindex ($index ? ('Item', $index) : ());
753 $$self{NEEDSPACE} = 0;
5cdeb5a2 754 $$self{ITEMS}++;
9741dab0 755}
756
757# Begin a block for a particular translator. Setting VERBATIM triggers
758# special handling in textblock().
759sub 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.
772sub 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.
780sub cmd_for {
781 my $self = shift;
782 local $_ = shift;
9741dab0 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.
794sub 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
16ba52cf 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
9741dab0 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 {
2e20e14f 847 if ($section !~ /^".*"$/) { $section = '"' . $section . '"' }
848 $text .= 'the section on ' . $section;
9741dab0 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
50a3fd2a 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.
9741dab0 872sub 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);
50a3fd2a 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.
901sub 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);
9741dab0 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.
920sub parse {
921 my $self = shift;
922 $self->parse_text ({ -expand_seq => 'sequence',
923 -expand_ptree => 'collapse' }, @_);
924}
5cdeb5a2 925
9741dab0 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.
a3e04946 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.
9741dab0 934sub collapse {
935 my ($self, $ptree, $literal) = @_;
936 if ($literal) {
937 return join ('', map {
938 if (ref $_) {
939 $$_;
940 } else {
941 s/\\/\\e/g;
a3e04946 942 s/-/\\-/g if $literal > 1;
943 s/__/_\\|_/g if $literal > 1;
9741dab0 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.
957sub 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_\$&-]* )
ee89c1da 973 (?: (?= [\s>\}\]\(\)\'\".?!,;] | -- ) | $ )
c9abbd5d 974 } { $1 . '\s-1' . $2 . '\s0' }egx;
9741dab0 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{
ee89c1da 981 ( \b | \\s-1 )
9741dab0 982 (
ee89c1da 983 [A-Za-z_] ([:\w]|\\s-?[01])+ \(\)
9741dab0 984 )
ee89c1da 985 } { $1 . '\f(IS' . $2 . '\f(IE' }egx;
9741dab0 986
987 # func(n) is a reference to a manual page. Make it \fIfunc\fR\|(n).
988 s{
ee89c1da 989 ( \b | \\s-1 )
990 ( [A-Za-z_] (?:[-:.\w]|\\s-?[01])+ )
9741dab0 991 (
ee89c1da 992 \( \d [a-z]* \)
9741dab0 993 )
ee89c1da 994 } { $1 . '\f(IS' . $2 . '\f(IE\|' . $3 }egx;
9741dab0 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.
1037sub makespace {
1038 my $self = shift;
5cdeb5a2 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};
9741dab0 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.
1048sub 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.
1069sub output { print { $_[0]->output_handle } $_[1] }
1070
50a3fd2a 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
73849855 1077# quotes to LQUOTE and RQUOTE.
50a3fd2a 1078sub 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
73849855 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.
50a3fd2a 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;
50a3fd2a 1095 s/\\\*\(C\`/$$self{LQUOTE}/g;
1096 s/\\\*\(C\'/$$self{RQUOTE}/g;
1097 $troff =~ s/\\\*\(C[\'\`]//g;
73849855 1098 s/\"/\"\"/g if $extra;
1099 $troff =~ s/\"/\"\"/g if $extra;
50a3fd2a 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
9741dab0 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
1147Pod::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
1162Pod::Man is a module to convert documentation in the POD format (the
1163preferred language for documenting Perl) into *roff input using the man
1164macro set. The resulting *roff code is suitable for display on a terminal
1165using nroff(1), normally via man(1), or printing using troff(1). It is
9e107c59 1166conventionally invoked using the driver script B<pod2man>, but it can also
9741dab0 1167be used directly.
1168
1169As a derived class from Pod::Parser, Pod::Man supports the same methods and
1170interfaces. See L<Pod::Parser> for all the details; briefly, one creates a
1171new parser with C<Pod::Man-E<gt>new()> and then calls either
1172parse_from_filehandle() or parse_from_file().
1173
1174new() can take options, in the form of key/value pairs that control the
1175behavior of the parser. See below for details.
1176
1177If no options are given, Pod::Man uses the name of the input file with any
1178trailing C<.pod>, C<.pm>, or C<.pl> stripped as the man page title, to
1179section 1 unless the file ended in C<.pm> in which case it defaults to
1180section 3, to a centered title of "User Contributed Perl Documentation", to
1181a centered footer of the Perl version it is run with, and to a left-hand
1182footer of the modification date of its input (or the current date if given
1183STDIN for input).
1184
1185Pod::Man assumes that your *roff formatters have a fixed-width font named
1186CW. If yours is called something else (like CR), use the C<fixed> option to
1187specify it. This generally only matters for troff output for printing.
1188Similarly, you can set the fonts used for bold, italic, and bold italic
1189fixed-width output.
1190
1191Besides the obvious pod conversions, Pod::Man also takes care of formatting
1192func(), func(n), and simple variable references like $foo or @bar so you
1193don't have to use code escapes for them; complex expressions like
1194C<$fred{'stuff'}> will still need to be escaped, though. It also translates
1195dashes that aren't used as hyphens into en dashes, makes long dashes--like
1196this--into proper em dashes, fixes "paired quotes," makes C++ and PI look
1197right, puts a little space between double underbars, makes ALLCAPS a teeny
1198bit smaller in troff(1), and escapes stuff that *roff treats as special so
1199that you don't have to.
1200
1201The recognized options to new() are as follows. All options take a single
1202argument.
1203
1204=over 4
1205
1206=item center
1207
1208Sets the centered page header to use instead of "User Contributed Perl
1209Documentation".
1210
1211=item date
1212
1213Sets the left-hand footer. By default, the modification date of the input
1214file will be used, or the current date if stat() can't find that file (the
1215case if the input is from STDIN), and the date will be formatted as
1216YYYY-MM-DD.
1217
1218=item fixed
1219
1220The fixed-width font to use for vertabim text and code. Defaults to CW.
1221Some systems may want CR instead. Only matters for troff(1) output.
1222
1223=item fixedbold
1224
1225Bold version of the fixed-width font. Defaults to CB. Only matters for
1226troff(1) output.
1227
1228=item fixeditalic
1229
1230Italic version of the fixed-width font (actually, something of a misnomer,
1231since most fixed-width fonts only have an oblique version, not an italic
1232version). Defaults to CI. Only matters for troff(1) output.
1233
1234=item fixedbolditalic
1235
1236Bold italic (probably actually oblique) version of the fixed-width font.
1237Pod::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)
1239output.
1240
ab1f1d91 1241=item quotes
1242
1243Sets the quote marks used to surround CE<lt>> text. If the value is a
1244single character, it is used as both the left and right quote; if it is two
1245characters, the first character is used as the left quote and the second as
1246the right quoted; and if it is four characters, the first two are used as
1247the left quote and the second two as the right quote.
1248
1249This may also be set to the special value C<none>, in which case no quote
1250marks are added around CE<lt>> text (but the font is still changed for troff
1251output).
1252
9741dab0 1253=item release
1254
1255Set the centered footer. By default, this is the version of Perl you run
1256Pod::Man under. Note that some system an macro sets assume that the
1257centered 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
1259the last modified date and C<date> to the version number.
1260
1261=item section
1262
1263Set the section for the C<.TH> macro. The standard section numbering
1264convention is to use 1 for user commands, 2 for system calls, 3 for
1265functions, 4 for devices, 5 for file formats, 6 for games, 7 for
1266miscellaneous information, and 8 for administrator commands. There is a lot
1267of variation here, however; some systems (like Solaris) use 4 for file
1268formats, 5 for miscellaneous information, and 7 for devices. Still others
1269use 1m instead of 8, or some mix of both. About the only section numbers
1270that are reliably consistent are 1, 2, and 3.
1271
1272By default, section 1 will be used unless the file ends in .pm in which case
1273section 3 will be selected.
1274
1275=back
1276
1277The standard Pod::Parser method parse_from_filehandle() takes up to two
1278arguments, the first being the file handle to read POD from and the second
1279being the file handle to write the formatted output to. The first defaults
1280to STDIN if not given, and the second defaults to STDOUT. The method
1281parse_from_file() is almost identical, except that its two arguments are the
1282input and output disk files instead. See L<Pod::Parser> for the specific
1283details.
1284
1285=head1 DIAGNOSTICS
1286
1287=over 4
1288
ab1f1d91 1289=item roff font should be 1 or 2 chars, not "%s"
9741dab0 1290
1291(F) You specified a *roff font (using C<fixed>, C<fixedbold>, etc.) that
1292wasn't either one or two characters. Pod::Man doesn't support *roff fonts
1293longer than two characters, although some *roff extensions do (the canonical
1294versions 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
1299unable to parse. You should never see this error message; it probably
1300indicates a bug in Pod::Man.
1301
ab1f1d91 1302=item Invalid quote specification "%s"
1303
1304(F) The quote specification given (the quotes option to the constructor) was
1305invalid. 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
1310the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1311
9741dab0 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
1315know 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
1320the form C<XE<lt>E<gt>>) that Pod::Man didn't know about. It was ignored.
1321
844b31e3 1322=item %s: Unknown command paragraph "%s" on line %d.
1323
1324(W) The POD source contained a non-standard command paragraph (something of
1325the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1326
9741dab0 1327=item Unmatched =back
1328
1329(W) Pod::Man encountered a C<=back> command that didn't correspond to an
1330C<=over> command.
1331
1332=back
1333
1334=head1 BUGS
1335
1336The lint-like features and strict POD format checking done by B<pod2man> are
1337not yet implemented and should be, along with the corresponding C<lax>
1338option.
1339
1340The NAME section should be recognized specially and index entries emitted
1341for everything in that section. This would have to be deferred until the
1342next section, since extraneous things in NAME tends to confuse various man
1343page processors.
1344
1345The handling of hyphens, en dashes, and em dashes is somewhat fragile, and
1346one may get the wrong one under some circumstances. This should only matter
1347for troff(1) output.
1348
1349When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
1350necessarily get it right.
1351
1352Pod::Man doesn't handle font names longer than two characters. Neither do
1353most troff(1) implementations, but GNU troff does as an extension. It would
1354be nice to support as an option for those who want to use it.
1355
1356The preamble added to each output file is rather verbose, and most of it is
1357only necessary in the presence of EE<lt>E<gt> escapes for non-ASCII
1358characters. It would ideally be nice if all of those definitions were only
1359output if needed, perhaps on the fly as the characters are used.
1360
1361Some of the automagic applied to file names assumes Unix directory
1362separators.
1363
1364Pod::Man is excessively slow.
1365
9741dab0 1366=head1 SEE ALSO
1367
9e107c59 1368L<Pod::Parser|Pod::Parser>, perlpod(1), pod2man(1), nroff(1), troff(1),
9741dab0 1369man(1), man(7)
1370
1371Ossanna, Joseph F., and Brian W. Kernighan. "Troff User's Manual,"
1372Computing Science Technical Report No. 54, AT&T Bell Laboratories. This is
1373the best documentation of standard nroff(1) and troff(1). At the time of
1374this writing, it's available at http://www.cs.bell-labs.com/cm/cs/cstr.html.
1375
1376The man page documenting the man macro set may be man(5) instead of man(7)
9e107c59 1377on your system. Also, please see pod2man(1) for extensive documentation on
9741dab0 1378writing manual pages if you've not done it before and aren't familiar with
1379the conventions.
1380
1381=head1 AUTHOR
1382
1383Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
1384original B<pod2man> by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>.
1385
1386=cut