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