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