Upgrade to podlators 1.13.
[p5sagit/p5-mst-13.2.git] / lib / Pod / Man.pm
CommitLineData
9741dab0 1# Pod::Man -- Convert POD data to formatted *roff input.
bf202ccd 2# $Id: Man.pm,v 1.26 2001/11/15 09:02:06 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#
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
3c014959 12# to produce better output. It uses Pod::Parser and is designed to be easy to
13# subclass.
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
26require 5.004;
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.
bf202ccd 41$VERSION = 1.26;
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};
327 $$self{indent} = 4 unless defined $$self{indent};
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
bf202ccd 366 $$self{INDENT} = 0; # Current indentation level.
367 $$self{INDENTS} = []; # Stack of indentations.
368 $$self{INDEX} = []; # Index keys waiting to be printed.
369 $$self{ITEMS} = 0; # The number of consecutive =items.
370 $$self{NEWINDENT} = 0; # Whether we've seen =over without =item.
9741dab0 371
372 $self->SUPER::initialize;
373}
374
ab1f1d91 375# For each document we process, output the preamble first.
9741dab0 376sub begin_pod {
377 my $self = shift;
378
379 # Try to figure out the name and section from the file name.
380 my $section = $$self{section} || 1;
381 my $name = $$self{name};
382 if (!defined $name) {
383 $name = $self->input_file;
fe6f1558 384 $section = 3 if (!$$self{section} && $name =~ /\.pm\z/i);
385 $name =~ s/\.p(od|[lm])\z//i;
bf202ccd 386 if ($section !~ /^3/) {
9741dab0 387 require File::Basename;
388 $name = uc File::Basename::basename ($name);
389 } else {
bf202ccd 390 # Assume that we're dealing with a module. We want to figure out
391 # the full module name from the path to the file, but we don't
392 # want to include too much of the path into the module name. Lose
393 # everything up to the first of:
394 #
395 # */lib/*perl*/ standard or site_perl module
396 # */*perl*/lib/ from -Dprefix=/opt/perl
397 # */*perl*/ random module hierarchy
398 #
399 # which works. Also strip off a leading site or site_perl
400 # component, any OS-specific component, and any version number
401 # component, and strip off an initial component of "lib" or
402 # "blib/lib" since that's what ExtUtils::MakeMaker creates.
403 # splitdir requires at least File::Spec 0.8.
404 require File::Spec;
405 my ($volume, $dirs, $file) = File::Spec->splitpath ($name);
406 my @dirs = File::Spec->splitdir ($dirs);
407 my $cut = 0;
408 my $i;
409 for ($i = 0; $i < scalar @dirs; $i++) {
410 if ($dirs[$i] eq 'lib' && $dirs[$i + 1] =~ /perl/) {
411 $cut = $i + 2;
412 last;
413 } elsif ($dirs[$i] =~ /perl/) {
414 $cut = $i + 1;
415 $cut++ if $dirs[$i + 1] eq 'lib';
416 last;
9741dab0 417 }
9741dab0 418 }
bf202ccd 419 if ($cut > 0) {
420 splice (@dirs, 0, $cut);
421 shift @dirs if ($dirs[0] =~ /^site(_perl)?$/);
422 shift @dirs if ($dirs[0] =~ /^[\d.]+$/);
423 shift @dirs if ($dirs[0] =~ /^(.*-$^O|$^O-.*)$/);
424 }
425 shift @dirs if $dirs[0] eq 'lib';
426 splice (@dirs, 0, 2) if ($dirs[0] eq 'blib' && $dirs[1] eq 'lib');
427
428 # Remove empty directories when building the module name; they
429 # occur too easily on Unix by doubling slashes.
430 $name = join ('::', (grep { $_ ? $_ : () } @dirs), $file);
9741dab0 431 }
432 }
433
3c014959 434 # If $name contains spaces, quote it; this mostly comes up in the case of
435 # input from stdin.
77003bb1 436 $name = '"' . $name . '"' if ($name =~ /\s/);
437
9741dab0 438 # Modification date header. Try to use the modification time of our
439 # input.
440 if (!defined $$self{date}) {
441 my $time = (stat $self->input_file)[9] || time;
442 my ($day, $month, $year) = (localtime $time)[3,4,5];
443 $month++;
444 $year += 1900;
c9abbd5d 445 $$self{date} = sprintf ('%4d-%02d-%02d', $year, $month, $day);
9741dab0 446 }
447
bf202ccd 448 # Now, print out the preamble and the title. The meaning of the arguments
449 # to .TH unfortunately vary by system; some systems consider the fourth
450 # argument to be a "source" and others use it as a version number.
451 # Generally it's just presented as the left-side footer, though, so it
452 # doesn't matter too much if a particular system gives it another
453 # interpretation.
454 #
455 # The order of date and release used to be reversed in older versions of
456 # this module, but this order is correct for both Solaris and Linux.
ab1f1d91 457 local $_ = $PREAMBLE;
458 s/\@CFONT\@/$$self{fixed}/;
459 s/\@LQUOTE\@/$$self{LQUOTE}/;
460 s/\@RQUOTE\@/$$self{RQUOTE}/;
461 chomp $_;
bf202ccd 462 my $pversion = $Pod::Parser::VERSION;
9741dab0 463 print { $self->output_handle } <<"----END OF HEADER----";
bf202ccd 464.\\" Automatically generated by Pod::Man v$VERSION, Pod::Parser v$pversion
9741dab0 465.\\"
466.\\" Standard preamble:
3c014959 467.\\" ========================================================================
ab1f1d91 468$_
3c014959 469.\\" ========================================================================
9741dab0 470.\\"
471.IX Title "$name $section"
bf202ccd 472.TH $name $section "$$self{date}" "$$self{release}" "$$self{center}"
9741dab0 473.UC
474----END OF HEADER----
9741dab0 475
476 # Initialize a few per-file variables.
477 $$self{INDENT} = 0;
478 $$self{NEEDSPACE} = 0;
479}
480
481
3c014959 482##############################################################################
9741dab0 483# Core overrides
3c014959 484##############################################################################
9741dab0 485
486# Called for each command paragraph. Gets the command, the associated
487# paragraph, the line number, and a Pod::Paragraph object. Just dispatches
488# the command to a method named the same as the command. =cut is handled
489# internally by Pod::Parser.
490sub command {
491 my $self = shift;
492 my $command = shift;
493 return if $command eq 'pod';
3c014959 494 return if ($$self{EXCLUDE} && $command ne 'end');
ab1f1d91 495 if ($self->can ('cmd_' . $command)) {
496 $command = 'cmd_' . $command;
844b31e3 497 $self->$command (@_);
3c014959 498 } else {
ab1f1d91 499 my ($text, $line, $paragraph) = @_;
5cdeb5a2 500 my $file;
501 ($file, $line) = $paragraph->file_line;
ab1f1d91 502 $text =~ s/\n+\z//;
503 $text = " $text" if ($text =~ /^\S/);
504 warn qq($file:$line: Unknown command paragraph "=$command$text"\n);
505 return;
844b31e3 506 }
9741dab0 507}
508
3c014959 509# Called for a verbatim paragraph. Gets the paragraph, the line number, and a
510# Pod::Paragraph object. Rofficate backslashes, untabify, put a zero-width
511# character at the beginning of each line to protect against commands, and
512# wrap in .Vb/.Ve.
9741dab0 513sub verbatim {
514 my $self = shift;
515 return if $$self{EXCLUDE};
516 local $_ = shift;
517 return if /^\s+$/;
518 s/\s+$/\n/;
519 my $lines = tr/\n/\n/;
520 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
521 s/\\/\\e/g;
522 s/^(\s*\S)/'\&' . $1/gme;
5cdeb5a2 523 $self->makespace;
9741dab0 524 $self->output (".Vb $lines\n$_.Ve\n");
525 $$self{NEEDSPACE} = 0;
526}
527
3c014959 528# Called for a regular text block. Gets the paragraph, the line number, and a
529# Pod::Paragraph object. Perform interpolation and output the results.
9741dab0 530sub textblock {
531 my $self = shift;
532 return if $$self{EXCLUDE};
533 $self->output ($_[0]), return if $$self{VERBATIM};
534
bf202ccd 535 # Parse the tree. collapse knows about references to scalars as well as
536 # scalars and does the right thing with them. Tidy up any trailing
537 # whitespace.
c9abbd5d 538 my $text = shift;
c9abbd5d 539 $text = $self->parse ($text, @_);
540 $text =~ s/\n\s*$/\n/;
bf202ccd 541
542 # Output the paragraph. We also have to handle =over without =item. If
543 # there's an =over without =item, NEWINDENT will be set, and we need to
544 # handle creation of the indent here. Set WEIRDINDENT so that it will be
545 # cleaned up on =back.
5cdeb5a2 546 $self->makespace;
bf202ccd 547 if ($$self{NEWINDENT}) {
548 $self->output (".RS $$self{INDENT}\n");
549 $$self{WEIRDINDENT} = 1;
550 $$self{NEWINDENT} = 0;
551 }
50a3fd2a 552 $self->output (protect $self->textmapfonts ($text));
9741dab0 553 $self->outindex;
554 $$self{NEEDSPACE} = 1;
555}
556
557# Called for an interior sequence. Takes a Pod::InteriorSequence object and
558# returns a reference to a scalar. This scalar is the final formatted text.
3c014959 559# It's returned as a reference so that other interior sequences above us know
560# that the text has already been processed.
9741dab0 561sub sequence {
562 my ($self, $seq) = @_;
563 my $command = $seq->cmd_name;
564
bf202ccd 565 # We have to defer processing of the inside of an L<> formatting code. If
566 # this sequence is nested inside an L<> sequence, return the literal raw
567 # text of it.
568 my $parent = $seq->nested;
569 while (defined $parent) {
570 return $seq->raw_text if ($parent->cmd_name eq 'L');
571 $parent = $parent->nested;
572 }
573
9741dab0 574 # Zero-width characters.
f5daac4a 575 if ($command eq 'Z') {
c9abbd5d 576 # Workaround to generate a blessable reference, needed by 5.005.
577 my $tmp = '\&';
578 return bless \ "$tmp", 'Pod::Man::String';
f5daac4a 579 }
9741dab0 580
a3e04946 581 # C<>, L<>, X<>, and E<> don't apply guesswork to their contents. C<>
582 # needs some additional special handling.
583 my $literal = ($command =~ /^[CELX]$/);
584 $literal++ if $command eq 'C';
585 local $_ = $self->collapse ($seq->parse_tree, $literal);
9741dab0 586
bf202ccd 587 # Handle E<> escapes. Numeric escapes that match one of the supported ISO
588 # 8859-1 characters don't work at present.
9741dab0 589 if ($command eq 'E') {
2e20e14f 590 if (/^\d+$/) {
591 return bless \ chr ($_), 'Pod::Man::String';
592 } elsif (exists $ESCAPES{$_}) {
9741dab0 593 return bless \ "$ESCAPES{$_}", 'Pod::Man::String';
594 } else {
aa212ad6 595 my ($file, $line) = $seq->file_line;
596 warn "$file:$line: Unknown escape E<$_>\n";
9741dab0 597 return bless \ "E<$_>", 'Pod::Man::String';
598 }
599 }
600
601 # For all the other sequences, empty content produces no output.
602 return '' if $_ eq '';
603
604 # Handle formatting sequences.
605 if ($command eq 'B') {
606 return bless \ ('\f(BS' . $_ . '\f(BE'), 'Pod::Man::String';
607 } elsif ($command eq 'F') {
608 return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
609 } elsif ($command eq 'I') {
610 return bless \ ('\f(IS' . $_ . '\f(IE'), 'Pod::Man::String';
611 } elsif ($command eq 'C') {
3c014959 612 # A bug in lvalue subs in 5.6 requires the temporary variable.
613 my $tmp = $self->quote_literal ($_);
614 return bless \ "$tmp", 'Pod::Man::String';
9741dab0 615 }
616
617 # Handle links.
618 if ($command eq 'L') {
bf202ccd 619 my ($text, $type) = (parselink ($_))[1,4];
620 return '' unless $text;
621 my ($file, $line) = $seq->file_line;
622 $text = $self->parse ($text, $line);
623 $text = '<' . $text . '>' if $type eq 'url';
624 return bless \ "$text", 'Pod::Man::String';
9741dab0 625 }
5cdeb5a2 626
9741dab0 627 # Whitespace protection replaces whitespace with "\ ".
628 if ($command eq 'S') {
629 s/\s+/\\ /g;
630 return bless \ "$_", 'Pod::Man::String';
631 }
632
633 # Add an index entry to the list of ones waiting to be output.
634 if ($command eq 'X') { push (@{ $$self{INDEX} }, $_); return '' }
635
636 # Anything else is unknown.
aa212ad6 637 my ($file, $line) = $seq->file_line;
638 warn "$file:$line: Unknown sequence $command<$_>\n";
9741dab0 639}
640
641
3c014959 642##############################################################################
9741dab0 643# Command paragraphs
3c014959 644##############################################################################
9741dab0 645
646# All command paragraphs take the paragraph and the line number.
647
648# First level heading. We can't output .IX in the NAME section due to a bug
649# in some versions of catman, so don't output a .IX for that section. .SH
3c014959 650# already uses small caps, so remove any E<> sequences that would cause them.
9741dab0 651sub cmd_head1 {
652 my $self = shift;
653 local $_ = $self->parse (@_);
654 s/\s+$//;
655 s/\\s-?\d//g;
77003bb1 656 s/\s*\n\s*/ /g;
5cdeb5a2 657 if ($$self{ITEMS} > 1) {
658 $$self{ITEMS} = 0;
659 $self->output (".PD\n");
660 }
50a3fd2a 661 $self->output ($self->switchquotes ('.SH', $self->mapfonts ($_)));
9741dab0 662 $self->outindex (($_ eq 'NAME') ? () : ('Header', $_));
663 $$self{NEEDSPACE} = 0;
664}
665
666# Second level heading.
667sub cmd_head2 {
668 my $self = shift;
669 local $_ = $self->parse (@_);
670 s/\s+$//;
77003bb1 671 s/\s*\n\s*/ /g;
5cdeb5a2 672 if ($$self{ITEMS} > 1) {
673 $$self{ITEMS} = 0;
674 $self->output (".PD\n");
675 }
50a3fd2a 676 $self->output ($self->switchquotes ('.Sh', $self->mapfonts ($_)));
9741dab0 677 $self->outindex ('Subsection', $_);
678 $$self{NEEDSPACE} = 0;
679}
680
50a3fd2a 681# Third level heading.
682sub cmd_head3 {
683 my $self = shift;
684 local $_ = $self->parse (@_);
685 s/\s+$//;
77003bb1 686 s/\s*\n\s*/ /g;
50a3fd2a 687 if ($$self{ITEMS} > 1) {
688 $$self{ITEMS} = 0;
689 $self->output (".PD\n");
690 }
691 $self->makespace;
692 $self->output ($self->switchquotes ('.I', $self->mapfonts ($_)));
693 $self->outindex ('Subsection', $_);
694 $$self{NEEDSPACE} = 1;
695}
696
697# Fourth level heading.
698sub cmd_head4 {
699 my $self = shift;
700 local $_ = $self->parse (@_);
701 s/\s+$//;
77003bb1 702 s/\s*\n\s*/ /g;
50a3fd2a 703 if ($$self{ITEMS} > 1) {
704 $$self{ITEMS} = 0;
705 $self->output (".PD\n");
706 }
707 $self->makespace;
708 $self->output ($self->textmapfonts ($_) . "\n");
709 $self->outindex ('Subsection', $_);
710 $$self{NEEDSPACE} = 1;
711}
712
9741dab0 713# Start a list. For indents after the first, wrap the outside indent in .RS
714# so that hanging paragraph tags will be correct.
715sub cmd_over {
716 my $self = shift;
717 local $_ = shift;
718 unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
bf202ccd 719 if (@{ $$self{INDENTS} } > 0 && !$$self{WEIRDINDENT}) {
9741dab0 720 $self->output (".RS $$self{INDENT}\n");
721 }
722 push (@{ $$self{INDENTS} }, $$self{INDENT});
723 $$self{INDENT} = ($_ + 0);
bf202ccd 724 $$self{NEWINDENT} = 1;
9741dab0 725}
726
727# End a list. If we've closed an embedded indent, we've mangled the hanging
728# paragraph indent, so temporarily replace it with .RS and set WEIRDINDENT.
729# We'll close that .RS at the next =back or =item.
730sub cmd_back {
731 my $self = shift;
732 $$self{INDENT} = pop @{ $$self{INDENTS} };
733 unless (defined $$self{INDENT}) {
aa212ad6 734 my ($file, $line, $paragraph) = @_;
735 ($file, $line) = $paragraph->file_line;
736 warn "$file:$line: Unmatched =back\n";
9741dab0 737 $$self{INDENT} = 0;
738 }
739 if ($$self{WEIRDINDENT}) {
740 $self->output (".RE\n");
741 $$self{WEIRDINDENT} = 0;
742 }
743 if (@{ $$self{INDENTS} } > 0) {
744 $self->output (".RE\n");
745 $self->output (".RS $$self{INDENT}\n");
746 $$self{WEIRDINDENT} = 1;
747 }
748 $$self{NEEDSPACE} = 1;
bf202ccd 749 $$self{NEWINDENT} = 0;
9741dab0 750}
751
752# An individual list item. Emit an index entry for anything that's
753# interesting, but don't emit index entries for things like bullets and
3c014959 754# numbers. rofficate bullets too while we're at it (so for nice output, use *
755# for your lists rather than o or . or - or some other thing). Newlines in an
756# item title are turned into spaces since *roff can't handle them embedded.
9741dab0 757sub cmd_item {
758 my $self = shift;
759 local $_ = $self->parse (@_);
760 s/\s+$//;
46bce7d0 761 s/\s*\n\s*/ /g;
9741dab0 762 my $index;
763 if (/\w/ && !/^\w[.\)]\s*$/) {
764 $index = $_;
fe2227f0 765 $index =~ s/^\s*[-*+o.]?(?:\s+|\Z)//;
9741dab0 766 }
bf202ccd 767 $_ = '*' unless $_;
9741dab0 768 s/^\*(\s|\Z)/\\\(bu$1/;
769 if ($$self{WEIRDINDENT}) {
770 $self->output (".RE\n");
771 $$self{WEIRDINDENT} = 0;
772 }
50a3fd2a 773 $_ = $self->textmapfonts ($_);
5cdeb5a2 774 $self->output (".PD 0\n") if ($$self{ITEMS} == 1);
3c014959 775 $self->output ($self->switchquotes ('.IP', $_, $$self{INDENT}));
9741dab0 776 $self->outindex ($index ? ('Item', $index) : ());
777 $$self{NEEDSPACE} = 0;
5cdeb5a2 778 $$self{ITEMS}++;
bf202ccd 779 $$self{NEWINDENT} = 0;
9741dab0 780}
781
782# Begin a block for a particular translator. Setting VERBATIM triggers
783# special handling in textblock().
784sub cmd_begin {
785 my $self = shift;
786 local $_ = shift;
787 my ($kind) = /^(\S+)/ or return;
788 if ($kind eq 'man' || $kind eq 'roff') {
789 $$self{VERBATIM} = 1;
790 } else {
791 $$self{EXCLUDE} = 1;
792 }
793}
794
795# End a block for a particular translator. We assume that all =begin/=end
796# pairs are properly closed.
797sub cmd_end {
798 my $self = shift;
799 $$self{EXCLUDE} = 0;
800 $$self{VERBATIM} = 0;
801}
802
803# One paragraph for a particular translator. Ignore it unless it's intended
804# for man or roff, in which case we output it verbatim.
805sub cmd_for {
806 my $self = shift;
807 local $_ = shift;
9741dab0 808 return unless s/^(?:man|roff)\b[ \t]*\n?//;
809 $self->output ($_);
810}
811
812
3c014959 813##############################################################################
9741dab0 814# Escaping and fontification
3c014959 815##############################################################################
9741dab0 816
817# At this point, we'll have embedded font codes of the form \f(<font>[SE]
3c014959 818# where <font> is one of B, I, or F. Turn those into the right font start or
819# end codes. The old pod2man didn't get B<someI<thing> else> right; after I<>
820# it switched back to normal text rather than bold. We take care of this by
821# using variables as a combined pointer to our current font sequence, and set
822# each to the number of current nestings of start tags for that font. Use
823# them as a vector to look up what font sequence to use.
50a3fd2a 824#
825# \fP changes to the previous font, but only one previous font is kept. We
826# don't know what the outside level font is; normally it's R, but if we're
3c014959 827# inside a heading it could be something else. So arrange things so that the
828# outside font is always the "previous" font and end with \fP instead of \fR.
829# Idea from Zack Weinberg.
9741dab0 830sub mapfonts {
831 my $self = shift;
832 local $_ = shift;
833
834 my ($fixed, $bold, $italic) = (0, 0, 0);
835 my %magic = (F => \$fixed, B => \$bold, I => \$italic);
50a3fd2a 836 my $last = '\fR';
837 s { \\f\((.)(.) } {
838 my $sequence = '';
839 my $f;
840 if ($last ne '\fR') { $sequence = '\fP' }
841 ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
842 $f = $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
843 if ($f eq $last) {
844 '';
845 } else {
846 if ($f ne '\fR') { $sequence .= $f }
847 $last = $f;
848 $sequence;
849 }
850 }gxe;
851 $_;
852}
853
854# Unfortunately, there is a bug in Solaris 2.6 nroff (not present in GNU
855# groff) where the sequence \fB\fP\f(CW\fP leaves the font set to B rather
3c014959 856# than R, presumably because \f(CW doesn't actually do a font change. To work
857# around this, use a separate textmapfonts for text blocks where the default
858# font is always R and only use the smart mapfonts for headings.
50a3fd2a 859sub textmapfonts {
860 my $self = shift;
861 local $_ = shift;
862
863 my ($fixed, $bold, $italic) = (0, 0, 0);
864 my %magic = (F => \$fixed, B => \$bold, I => \$italic);
9741dab0 865 s { \\f\((.)(.) } {
866 ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
867 $$self{FONTS}{($fixed && 1) . ($bold && 1) . ($italic && 1)};
868 }gxe;
869 $_;
870}
871
872
3c014959 873##############################################################################
874# *roff-specific parsing and magic
875##############################################################################
9741dab0 876
877# Called instead of parse_text, calls parse_text with the right flags.
878sub parse {
879 my $self = shift;
880 $self->parse_text ({ -expand_seq => 'sequence',
881 -expand_ptree => 'collapse' }, @_);
882}
5cdeb5a2 883
9741dab0 884# Takes a parse tree and a flag saying whether or not to treat it as literal
3c014959 885# text (not call guesswork on it), and returns the concatenation of all of the
886# text strings in that parse tree. If the literal flag isn't true,
9741dab0 887# guesswork() will be called on all plain scalars in the parse tree.
3c014959 888# Otherwise, just escape backslashes in the normal case. If collapse is being
889# called on a C<> sequence, literal is set to 2, and we do some additional
890# cleanup. Assumes that everything in the parse tree is either a scalar or a
891# reference to a scalar.
9741dab0 892sub collapse {
893 my ($self, $ptree, $literal) = @_;
894 if ($literal) {
895 return join ('', map {
896 if (ref $_) {
897 $$_;
898 } else {
bf202ccd 899 s/\\/\\e/g if $literal > 1;
a3e04946 900 s/-/\\-/g if $literal > 1;
901 s/__/_\\|_/g if $literal > 1;
9741dab0 902 $_;
903 }
904 } $ptree->children);
905 } else {
906 return join ('', map {
907 ref ($_) ? $$_ : $self->guesswork ($_)
908 } $ptree->children);
909 }
910}
911
912# Takes a text block to perform guesswork on; this is guaranteed not to
3c014959 913# contain any interior sequences. Returns the text block with remapping done.
9741dab0 914sub guesswork {
915 my $self = shift;
916 local $_ = shift;
917
918 # rofficate backslashes.
919 s/\\/\\e/g;
920
921 # Ensure double underbars have a tiny space between them.
922 s/__/_\\|_/g;
923
bf202ccd 924 # Leave hyphens only if they're part of regular words and there is only
925 # one dash at a time. Leave a dash after the first character as a regular
926 # non-breaking dash, but don't let it mark the rest of the word invalid
927 # for hyphenation.
928 s/-/\\-/g;
929 s{
930 ( (?:\G|^|\s) [a-zA-Z] ) ( \\- )?
931 ( (?: [a-zA-Z]+ \\-)+ )
932 ( [a-zA-Z]+ ) (?=\s|\Z)
933 \b
934 } {
935 my ($prefix, $hyphen, $main, $suffix) = ($1, $2, $3, $4);
936 $hyphen ||= '';
937 $main =~ s/\\-/-/g;
938 $prefix . $hyphen . $main . $suffix;
939 }egx;
940
941 # Translate -- into a real em dash if it's used like one.
942 s{ (\s) \\-\\- (\s) } { $1 . '\*(--' . $2 }egx;
943 s{ (\b[a-zA-Z]+) \\-\\- (\s|\Z|[a-zA-Z]+\b) } { $1 . '\*(--' . $2 }egx;
944
3c014959 945 # Make all caps a little smaller. Be careful here, since we don't want to
946 # make @ARGV into small caps, nor do we want to fix the MIME in
9741dab0 947 # MIME-Version, since it looks weird with the full-height V.
948 s{
949 ( ^ | [\s\(\"\'\`\[\{<>] )
bf202ccd 950 ( [A-Z] [A-Z] (?: [/A-Z+:\d_\$&] | \\- )* )
951 (?= [\s>\}\]\(\)\'\".?!,;] | \\*\(-- | $ )
c9abbd5d 952 } { $1 . '\s-1' . $2 . '\s0' }egx;
9741dab0 953
9741dab0 954 # Italize functions in the form func().
955 s{
ee89c1da 956 ( \b | \\s-1 )
9741dab0 957 (
ee89c1da 958 [A-Za-z_] ([:\w]|\\s-?[01])+ \(\)
9741dab0 959 )
ee89c1da 960 } { $1 . '\f(IS' . $2 . '\f(IE' }egx;
9741dab0 961
962 # func(n) is a reference to a manual page. Make it \fIfunc\fR\|(n).
963 s{
ee89c1da 964 ( \b | \\s-1 )
bf202ccd 965 ( [A-Za-z_] (?:[.:\w]|\\-|\\s-?[01])+ )
9741dab0 966 (
ee89c1da 967 \( \d [a-z]* \)
9741dab0 968 )
ee89c1da 969 } { $1 . '\f(IS' . $2 . '\f(IE\|' . $3 }egx;
9741dab0 970
971 # Convert simple Perl variable references to a fixed-width font.
972 s{
973 ( \s+ )
974 ( [\$\@%] [\w:]+ )
975 (?! \( )
976 } { $1 . '\f(FS' . $2 . '\f(FE'}egx;
977
9741dab0 978 # Fix up double quotes.
979 s{ \" ([^\"]+) \" } { '\*(L"' . $1 . '\*(R"' }egx;
980
981 # Make C++ into \*(C+, which is a squinched version.
982 s{ \b C\+\+ } {\\*\(C+}gx;
983
984 # All done.
985 $_;
986}
987
3c014959 988# Handles C<> text, deciding whether to put \*C` around it or not. This is a
989# whole bunch of messy heuristics to try to avoid overquoting, originally from
990# Barrie Slaymaker. This largely duplicates similar code in Pod::Text.
991sub quote_literal {
992 my $self = shift;
993 local $_ = shift;
994
995 # A regex that matches the portion of a variable reference that's the
996 # array or hash index, separated out just because we want to use it in
997 # several places in the following regex.
998 my $index = '(?: \[.*\] | \{.*\} )?';
999
1000 # Check for things that we don't want to quote, and if we find any of
1001 # them, return the string with just a font change and no quoting.
1002 m{
1003 ^\s*
1004 (?:
1005 ( [\'\`\"] ) .* \1 # already quoted
1006 | \` .* \' # `quoted'
1007 | \$+ [\#^]? \S $index # special ($^Foo, $")
1008 | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func
1009 | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
1010 | [+-]? [\d.]+ (?: [eE] [+-]? \d+ )? # a number
1011 | 0x [a-fA-F\d]+ # a hex constant
1012 )
1013 \s*\z
1014 }xo && return '\f(FS' . $_ . '\f(FE';
1015
1016 # If we didn't return, go ahead and quote the text.
1017 return '\f(FS\*(C`' . $_ . "\\*(C'\\f(FE";
1018}
1019
9741dab0 1020
3c014959 1021##############################################################################
9741dab0 1022# Output formatting
3c014959 1023##############################################################################
9741dab0 1024
1025# Make vertical whitespace.
1026sub makespace {
1027 my $self = shift;
5cdeb5a2 1028 $self->output (".PD\n") if ($$self{ITEMS} > 1);
1029 $$self{ITEMS} = 0;
1030 $self->output ($$self{INDENT} > 0 ? ".Sp\n" : ".PP\n")
1031 if $$self{NEEDSPACE};
9741dab0 1032}
1033
3c014959 1034# Output any pending index entries, and optionally an index entry given as an
1035# argument. Support multiple index entries in X<> separated by slashes, and
1036# strip special escapes from index entries.
9741dab0 1037sub outindex {
1038 my ($self, $section, $index) = @_;
1039 my @entries = map { split m%\s*/\s*% } @{ $$self{INDEX} };
1040 return unless ($section || @entries);
1041 $$self{INDEX} = [];
1042 my $output;
1043 if (@entries) {
59548eca 1044 $output = '.IX Xref "'
9741dab0 1045 . join (' ', map { s/\"/\"\"/; $_ } @entries)
1046 . '"' . "\n";
1047 }
1048 if ($section) {
1049 $index =~ s/\"/\"\"/;
1050 $index =~ s/\\-/-/g;
1051 $index =~ s/\\(?:s-?\d|.\(..|.)//g;
1052 $output .= ".IX $section " . '"' . $index . '"' . "\n";
1053 }
1054 $self->output ($output);
1055}
1056
1057# Output text to the output device.
1058sub output { print { $_[0]->output_handle } $_[1] }
1059
50a3fd2a 1060# Given a command and a single argument that may or may not contain double
1061# quotes, handle double-quote formatting for it. If there are no double
1062# quotes, just return the command followed by the argument in double quotes.
1063# If there are double quotes, use an if statement to test for nroff, and for
1064# nroff output the command followed by the argument in double quotes with
1065# embedded double quotes doubled. For other formatters, remap paired double
73849855 1066# quotes to LQUOTE and RQUOTE.
50a3fd2a 1067sub switchquotes {
1068 my $self = shift;
1069 my $command = shift;
1070 local $_ = shift;
1071 my $extra = shift;
1072 s/\\\*\([LR]\"/\"/g;
1073
1074 # We also have to deal with \*C` and \*C', which are used to add the
1075 # quotes around C<> text, since they may expand to " and if they do this
3c014959 1076 # confuses the .SH macros and the like no end. Expand them ourselves. If
1077 # $extra is set, we're dealing with =item, which in most nroff macro sets
1078 # requires an extra level of quoting of double quotes because it passes
1079 # the argument off to .TP.
50a3fd2a 1080 my $c_is_quote = ($$self{LQUOTE} =~ /\"/) || ($$self{RQUOTE} =~ /\"/);
3c014959 1081 if (/\"/ || /\\f\(CW/) {
50a3fd2a 1082 s/\"/\"\"/g;
3c014959 1083 my $nroff = $_;
50a3fd2a 1084 my $troff = $_;
1085 $troff =~ s/\"\"([^\"]*)\"\"/\`\`$1\'\'/g;
3c014959 1086 if ($c_is_quote && /\\\*\(C[\'\`]/) {
1087 $nroff =~ s/\\\*\(C\`/$$self{LQUOTE}/g;
1088 $nroff =~ s/\\\*\(C\'/$$self{RQUOTE}/g;
1089 $troff =~ s/\\\*\(C[\'\`]//g;
1090 }
1091 $nroff = qq("$nroff") . ($extra ? " $extra" : '');
50a3fd2a 1092 $troff = qq("$troff") . ($extra ? " $extra" : '');
3c014959 1093
1094 # Work around the Solaris nroff bug where \f(CW\fP leaves the font set
1095 # to Roman rather than the actual previous font when used in headings.
1096 # troff output may still be broken, but at least we can fix nroff by
1097 # just stripping out the font changes since fixed-width fonts don't
1098 # mean anything for nroff. While we're at it, also remove the font
1099 # changes for nroff in =item tags, since they're unnecessary.
1100 $nroff =~ s/\\f\(CW(.*)\\f[PR]/$1/g;
1101
59548eca 1102 # Now finally output the command. Only bother with .ie if the nroff
3c014959 1103 # and troff output isn't the same.
1104 if ($nroff ne $troff) {
59548eca 1105 return ".ie n $command $nroff\n.el $command $troff\n";
3c014959 1106 } else {
1107 return "$command $nroff\n";
1108 }
50a3fd2a 1109 } else {
1110 $_ = qq("$_") . ($extra ? " $extra" : '');
1111 return "$command $_\n";
1112 }
1113}
1114
9741dab0 1115__END__
1116
bf202ccd 1117.\" These are some extra bits of roff that I don't want to lose track of but
1118.\" that have been removed from the preamble to make it a bit shorter since
1119.\" they're not currently being used. They're accents and special characters
1120.\" we don't currently have escapes for.
9741dab0 1121.if n \{\
1122. ds ? ?
1123. ds ! !
1124. ds q
1125.\}
1126.if t \{\
1127. ds ? \s-2c\h'-\w'c'u*7/10'\u\h'\*(#H'\zi\d\s+2\h'\w'c'u*8/10'
1128. ds ! \s-2\(or\s+2\h'-\w'\(or'u'\v'-.8m'.\v'.8m'
1129. 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'
1130.\}
1131.ds v \\k:\h'-(\\n(.wu*9/10-\*(#H)'\v'-\*(#V'\*(#[\s-4v\s0\v'\*(#V'\h'|\\n:u'\*(#]
1132.ds _ \\k:\h'-(\\n(.wu*9/10-\*(#H+(\*(#F*2/3))'\v'-.4m'\z\(hy\v'.4m'\h'|\\n:u'
1133.ds . \\k:\h'-(\\n(.wu*8/10)'\v'\*(#V*4/10'\z.\v'-\*(#V*4/10'\h'|\\n:u'
1134.ds 3 \*(#[\v'.2m'\s-2\&3\s0\v'-.2m'\*(#]
1135.ds oe o\h'-(\w'o'u*4/10)'e
1136.ds Oe O\h'-(\w'O'u*4/10)'E
1137.if \n(.H>23 .if \n(.V>19 \
1138\{\
1139. ds v \h'-1'\o'\(aa\(ga'
1140. ds _ \h'-1'^
1141. ds . \h'-1'.
1142. ds 3 3
1143. ds oe oe
1144. ds Oe OE
1145.\}
1146
3c014959 1147##############################################################################
9741dab0 1148# Documentation
3c014959 1149##############################################################################
9741dab0 1150
1151=head1 NAME
1152
1153Pod::Man - Convert POD data to formatted *roff input
1154
1155=head1 SYNOPSIS
1156
1157 use Pod::Man;
1158 my $parser = Pod::Man->new (release => $VERSION, section => 8);
1159
1160 # Read POD from STDIN and write to STDOUT.
1161 $parser->parse_from_filehandle;
1162
1163 # Read POD from file.pod and write to file.1.
1164 $parser->parse_from_file ('file.pod', 'file.1');
1165
1166=head1 DESCRIPTION
1167
1168Pod::Man is a module to convert documentation in the POD format (the
1169preferred language for documenting Perl) into *roff input using the man
1170macro set. The resulting *roff code is suitable for display on a terminal
bf202ccd 1171using L<nroff(1)>, normally via L<man(1)>, or printing using L<troff(1)>.
1172It is conventionally invoked using the driver script B<pod2man>, but it can
1173also be used directly.
9741dab0 1174
1175As a derived class from Pod::Parser, Pod::Man supports the same methods and
1176interfaces. See L<Pod::Parser> for all the details; briefly, one creates a
1177new parser with C<Pod::Man-E<gt>new()> and then calls either
1178parse_from_filehandle() or parse_from_file().
1179
1180new() can take options, in the form of key/value pairs that control the
1181behavior of the parser. See below for details.
1182
1183If no options are given, Pod::Man uses the name of the input file with any
1184trailing C<.pod>, C<.pm>, or C<.pl> stripped as the man page title, to
1185section 1 unless the file ended in C<.pm> in which case it defaults to
1186section 3, to a centered title of "User Contributed Perl Documentation", to
1187a centered footer of the Perl version it is run with, and to a left-hand
1188footer of the modification date of its input (or the current date if given
1189STDIN for input).
1190
1191Pod::Man assumes that your *roff formatters have a fixed-width font named
1192CW. If yours is called something else (like CR), use the C<fixed> option to
1193specify it. This generally only matters for troff output for printing.
1194Similarly, you can set the fonts used for bold, italic, and bold italic
1195fixed-width output.
1196
1197Besides the obvious pod conversions, Pod::Man also takes care of formatting
bf202ccd 1198func(), func(3), and simple variable references like $foo or @bar so you
9741dab0 1199don't have to use code escapes for them; complex expressions like
1200C<$fred{'stuff'}> will still need to be escaped, though. It also translates
1201dashes that aren't used as hyphens into en dashes, makes long dashes--like
1202this--into proper em dashes, fixes "paired quotes," makes C++ and PI look
1203right, puts a little space between double underbars, makes ALLCAPS a teeny
bf202ccd 1204bit smaller in B<troff>, and escapes stuff that *roff treats as special so
9741dab0 1205that you don't have to.
1206
1207The recognized options to new() are as follows. All options take a single
1208argument.
1209
1210=over 4
1211
1212=item center
1213
1214Sets the centered page header to use instead of "User Contributed Perl
1215Documentation".
1216
1217=item date
1218
1219Sets the left-hand footer. By default, the modification date of the input
1220file will be used, or the current date if stat() can't find that file (the
1221case if the input is from STDIN), and the date will be formatted as
1222YYYY-MM-DD.
1223
1224=item fixed
1225
1226The fixed-width font to use for vertabim text and code. Defaults to CW.
bf202ccd 1227Some systems may want CR instead. Only matters for B<troff> output.
9741dab0 1228
1229=item fixedbold
1230
1231Bold version of the fixed-width font. Defaults to CB. Only matters for
bf202ccd 1232B<troff> output.
9741dab0 1233
1234=item fixeditalic
1235
1236Italic version of the fixed-width font (actually, something of a misnomer,
1237since most fixed-width fonts only have an oblique version, not an italic
bf202ccd 1238version). Defaults to CI. Only matters for B<troff> output.
9741dab0 1239
1240=item fixedbolditalic
1241
1242Bold italic (probably actually oblique) version of the fixed-width font.
1243Pod::Man doesn't assume you have this, and defaults to CB. Some systems
bf202ccd 1244(such as Solaris) have this font available as CX. Only matters for B<troff>
9741dab0 1245output.
1246
bf202ccd 1247=item name
1248
1249Set the name of the manual page. Without this option, the manual name is
1250set to the uppercased base name of the file being converted unless the
1251manual section is 3, in which case the path is parsed to see if it is a Perl
1252module path. If it is, a path like C<.../lib/Pod/Man.pm> is converted into
1253a name like C<Pod::Man>. This option, if given, overrides any automatic
1254determination of the name.
1255
ab1f1d91 1256=item quotes
1257
1258Sets the quote marks used to surround CE<lt>> text. If the value is a
1259single character, it is used as both the left and right quote; if it is two
1260characters, the first character is used as the left quote and the second as
1261the right quoted; and if it is four characters, the first two are used as
1262the left quote and the second two as the right quote.
1263
1264This may also be set to the special value C<none>, in which case no quote
1265marks are added around CE<lt>> text (but the font is still changed for troff
1266output).
1267
9741dab0 1268=item release
1269
1270Set the centered footer. By default, this is the version of Perl you run
bf202ccd 1271Pod::Man under. Note that some system an macro sets assume that the
9741dab0 1272centered footer will be a modification date and will prepend something like
1273"Last modified: "; if this is the case, you may want to set C<release> to
1274the last modified date and C<date> to the version number.
1275
1276=item section
1277
1278Set the section for the C<.TH> macro. The standard section numbering
1279convention is to use 1 for user commands, 2 for system calls, 3 for
1280functions, 4 for devices, 5 for file formats, 6 for games, 7 for
1281miscellaneous information, and 8 for administrator commands. There is a lot
1282of variation here, however; some systems (like Solaris) use 4 for file
1283formats, 5 for miscellaneous information, and 7 for devices. Still others
1284use 1m instead of 8, or some mix of both. About the only section numbers
1285that are reliably consistent are 1, 2, and 3.
1286
1287By default, section 1 will be used unless the file ends in .pm in which case
1288section 3 will be selected.
1289
1290=back
1291
1292The standard Pod::Parser method parse_from_filehandle() takes up to two
1293arguments, the first being the file handle to read POD from and the second
1294being the file handle to write the formatted output to. The first defaults
1295to STDIN if not given, and the second defaults to STDOUT. The method
1296parse_from_file() is almost identical, except that its two arguments are the
1297input and output disk files instead. See L<Pod::Parser> for the specific
1298details.
1299
1300=head1 DIAGNOSTICS
1301
1302=over 4
1303
ab1f1d91 1304=item roff font should be 1 or 2 chars, not "%s"
9741dab0 1305
1306(F) You specified a *roff font (using C<fixed>, C<fixedbold>, etc.) that
1307wasn't either one or two characters. Pod::Man doesn't support *roff fonts
1308longer than two characters, although some *roff extensions do (the canonical
bf202ccd 1309versions of B<nroff> and B<troff> don't either).
9741dab0 1310
1311=item Invalid link %s
1312
1313(W) The POD source contained a C<LE<lt>E<gt>> sequence that Pod::Man was
1314unable to parse. You should never see this error message; it probably
1315indicates a bug in Pod::Man.
1316
ab1f1d91 1317=item Invalid quote specification "%s"
1318
1319(F) The quote specification given (the quotes option to the constructor) was
1320invalid. A quote specification must be one, two, or four characters long.
1321
1322=item %s:%d: Unknown command paragraph "%s".
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
aa212ad6 1327=item %s:%d: Unknown escape EE<lt>%sE<gt>
9741dab0 1328
1329(W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Man didn't
1330know about. C<EE<lt>%sE<gt>> was printed verbatim in the output.
1331
aa212ad6 1332=item %s:%d: Unknown sequence %s
9741dab0 1333
1334(W) The POD source contained a non-standard interior sequence (something of
1335the form C<XE<lt>E<gt>>) that Pod::Man didn't know about. It was ignored.
1336
aa212ad6 1337=item %s:%d: Unmatched =back
9741dab0 1338
1339(W) Pod::Man encountered a C<=back> command that didn't correspond to an
1340C<=over> command.
1341
1342=back
1343
1344=head1 BUGS
1345
1346The lint-like features and strict POD format checking done by B<pod2man> are
1347not yet implemented and should be, along with the corresponding C<lax>
1348option.
1349
1350The NAME section should be recognized specially and index entries emitted
1351for everything in that section. This would have to be deferred until the
1352next section, since extraneous things in NAME tends to confuse various man
1353page processors.
1354
bf202ccd 1355The handling of hyphens and em dashes is somewhat fragile, and one may get
1356the wrong one under some circumstances. This should only matter for
1357B<troff> output.
9741dab0 1358
1359When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
1360necessarily get it right.
1361
1362Pod::Man doesn't handle font names longer than two characters. Neither do
bf202ccd 1363most B<troff> implementations, but GNU troff does as an extension. It would
9741dab0 1364be nice to support as an option for those who want to use it.
1365
1366The preamble added to each output file is rather verbose, and most of it is
1367only necessary in the presence of EE<lt>E<gt> escapes for non-ASCII
1368characters. It would ideally be nice if all of those definitions were only
1369output if needed, perhaps on the fly as the characters are used.
1370
9741dab0 1371Pod::Man is excessively slow.
1372
9741dab0 1373=head1 SEE ALSO
1374
bf202ccd 1375L<Pod::Parser>, L<perlpod(1)>, L<pod2man(1)>, L<nroff(1)>, L<troff(1)>,
1376L<man(1)>, L<man(7)>
9741dab0 1377
1378Ossanna, Joseph F., and Brian W. Kernighan. "Troff User's Manual,"
1379Computing Science Technical Report No. 54, AT&T Bell Laboratories. This is
bf202ccd 1380the best documentation of standard B<nroff> and B<troff>. At the time of
1381this writing, it's available at
1382L<http://www.cs.bell-labs.com/cm/cs/cstr.html>.
9741dab0 1383
bf202ccd 1384The man page documenting the man macro set may be L<man(5)> instead of
1385L<man(7)> on your system. Also, please see L<pod2man(1)> for extensive
1386documentation on writing manual pages if you've not done it before and
1387aren't familiar with the conventions.
9741dab0 1388
1389=head1 AUTHOR
1390
bf202ccd 1391Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
1392B<pod2man> by Tom Christiansen <tchrist@mox.perl.com>.
9741dab0 1393
3c014959 1394=head1 COPYRIGHT AND LICENSE
1395
1396Copyright 1999, 2000, 2001 by Russ Allbery <rra@stanford.edu>.
1397
1398This program is free software; you may redistribute it and/or modify it
1399under the same terms as Perl itself.
1400
9741dab0 1401=cut