xref: /openbsd-src/gnu/usr.bin/perl/cpan/podlators/scripts/pod2man.PL (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1#!perl
2
3use Config;
4use File::Basename qw(&basename &dirname);
5use Cwd;
6
7# List explicitly here the variables you want Configure to
8# generate.  Metaconfig only looks for shell variables, so you
9# have to mention them as if they were shell variables, not
10# %Config entries.  Thus you write
11#  $startperl
12# to ensure Configure will look for $Config{startperl}.
13
14# This forces PL files to create target in same directory as PL file.
15# This is so that make depend always knows where to find PL derivatives.
16$origdir = cwd;
17chdir dirname($0);
18$file = basename($0, '.PL');
19$file .= '.com' if $^O eq 'VMS';
20
21open OUT,">$file" or die "Can't create $file: $!";
22
23print "Extracting $file (with variable substitutions)\n";
24
25# In this section, perl variables will be expanded during extraction.
26# You can use $Config{...} to use Configure variables.
27
28print OUT <<"!GROK!THIS!";
29$Config{startperl}
30    eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
31        if \$running_under_some_shell;
32!GROK!THIS!
33
34# In the following, perl variables are not expanded during extraction.
35
36print OUT <<'!NO!SUBS!';
37
38# pod2man -- Convert POD data to formatted *roff input.
39#
40# Copyright 1999, 2000, 2001, 2004, 2006, 2008, 2010, 2012, 2013
41#     Russ Allbery <rra@stanford.edu>
42#
43# This program is free software; you may redistribute it and/or modify it
44# under the same terms as Perl itself.
45
46require 5.004;
47
48use Getopt::Long qw(GetOptions);
49use Pod::Man ();
50use Pod::Usage qw(pod2usage);
51
52use strict;
53
54# Clean up $0 for error reporting.
55$0 =~ s%.*/%%;
56
57# Insert -- into @ARGV before any single dash argument to hide it from
58# Getopt::Long; we want to interpret it as meaning stdin.
59my $stdin;
60@ARGV = map { $_ eq '-' && !$stdin++ ? ('--', $_) : $_ } @ARGV;
61
62# Parse our options, trying to retain backward compatibility with pod2man but
63# allowing short forms as well.  --lax is currently ignored.
64my %options;
65$options{utf8} = 1;
66Getopt::Long::config ('bundling_override');
67GetOptions (\%options, 'center|c=s', 'date|d=s', 'errors=s', 'fixed=s',
68            'fixedbold=s', 'fixeditalic=s', 'fixedbolditalic=s', 'help|h',
69            'lax|l', 'name|n=s', 'nourls', 'official|o', 'quotes|q=s',
70            'release|r:s', 'section|s=s', 'stderr', 'verbose|v', 'utf8|u!')
71    or exit 1;
72pod2usage (0) if $options{help};
73
74# Official sets --center, but don't override things explicitly set.
75if ($options{official} && !defined $options{center}) {
76    $options{center} = 'Perl Programmers Reference Guide';
77}
78
79# Verbose is only our flag, not a Pod::Man flag.
80my $verbose = $options{verbose};
81delete $options{verbose};
82
83# This isn't a valid Pod::Man option and is only accepted for backward
84# compatibility.
85delete $options{lax};
86
87# If neither stderr nor errors is set, default to errors = die.
88if (!defined $options{stderr} && !defined $options{errors}) {
89    $options{errors} = 'die';
90}
91
92# Initialize and run the formatter, pulling a pair of input and output off at
93# a time.  For each file, we check whether the document was completely empty
94# and, if so, will remove the created file and exit with a non-zero exit
95# status.
96my $parser = Pod::Man->new (%options);
97my $status = 0;
98my @files;
99do {
100    @files = splice (@ARGV, 0, 2);
101    print "  $files[1]\n" if $verbose;
102    $parser->parse_from_file (@files);
103    if ($parser->{CONTENTLESS}) {
104        $status = 1;
105        warn "$0: unable to format $files[0]\n";
106        if (defined ($files[1]) and $files[1] ne '-') {
107            unlink $files[1] unless (-s $files[1]);
108        }
109    }
110} while (@ARGV);
111exit $status;
112
113__END__
114
115=for stopwords
116en em --stderr stderr --utf8 --no-utf8 UTF-8
117overdo markup MT-LEVEL Allbery Solaris
118URL troff troff-specific formatters uppercased Christiansen --nourls
119
120=head1 NAME
121
122pod2man - Convert POD data to formatted *roff input
123
124=head1 SYNOPSIS
125
126pod2man [B<--center>=I<string>] [B<--date>=I<string>] [B<--errors>=I<style>]
127    [B<--fixed>=I<font>] [B<--fixedbold>=I<font>] [B<--fixeditalic>=I<font>]
128    [B<--fixedbolditalic>=I<font>] [B<--name>=I<name>] [B<--nourls>]
129    [B<--official>] [B<--quotes>=I<quotes>] [B<--release>[=I<version>]]
130    [B<--section>=I<manext>] [B<--stderr>] [B<--no-utf8>] [B<--verbose>]
131    [I<input> [I<output>] ...]
132
133pod2man B<--help>
134
135=head1 DESCRIPTION
136
137B<pod2man> is a front-end for Pod::Man, using it to generate *roff input
138from POD source.  The resulting *roff code is suitable for display on a
139terminal using nroff(1), normally via man(1), or printing using troff(1).
140
141I<input> is the file to read for POD source (the POD can be embedded in
142code).  If I<input> isn't given, it defaults to C<STDIN>.  I<output>, if
143given, is the file to which to write the formatted output.  If I<output>
144isn't given, the formatted output is written to C<STDOUT>.  Several POD
145files can be processed in the same B<pod2man> invocation (saving module
146load and compile times) by providing multiple pairs of I<input> and
147I<output> files on the command line.
148
149B<--section>, B<--release>, B<--center>, B<--date>, and B<--official> can
150be used to set the headers and footers to use; if not given, Pod::Man will
151assume various defaults.  See below or L<Pod::Man> for details.
152
153B<pod2man> assumes that your *roff formatters have a fixed-width font
154named C<CW>.  If yours is called something else (like C<CR>), use
155B<--fixed> to specify it.  This generally only matters for troff output
156for printing.  Similarly, you can set the fonts used for bold, italic, and
157bold italic fixed-width output.
158
159Besides the obvious pod conversions, Pod::Man, and therefore pod2man also
160takes care of formatting func(), func(n), and simple variable references
161like $foo or @bar so you don't have to use code escapes for them; complex
162expressions like C<$fred{'stuff'}> will still need to be escaped, though.
163It also translates dashes that aren't used as hyphens into en dashes, makes
164long dashes--like this--into proper em dashes, fixes "paired quotes," and
165takes care of several other troff-specific tweaks.  See L<Pod::Man> for
166complete information.
167
168=head1 OPTIONS
169
170=over 4
171
172=item B<-c> I<string>, B<--center>=I<string>
173
174Sets the centered page header to I<string>.  The default is "User
175Contributed Perl Documentation", but also see B<--official> below.
176
177=item B<-d> I<string>, B<--date>=I<string>
178
179Set the left-hand footer string to this value.  By default, the modification
180date of the input file will be used, or the current date if input comes from
181C<STDIN>.
182
183=item B<-errors>=I<style>
184
185Set the error handling style.  C<die> says to throw an exception on any
186POD formatting error.  C<stderr> says to report errors on standard error,
187but not to throw an exception.  C<pod> says to include a POD ERRORS
188section in the resulting documentation summarizing the errors.  C<none>
189ignores POD errors entirely, as much as possible.
190
191The default is C<die>.
192
193=item B<--fixed>=I<font>
194
195The fixed-width font to use for verbatim text and code.  Defaults to
196C<CW>.  Some systems may want C<CR> instead.  Only matters for troff(1)
197output.
198
199=item B<--fixedbold>=I<font>
200
201Bold version of the fixed-width font.  Defaults to C<CB>.  Only matters
202for troff(1) output.
203
204=item B<--fixeditalic>=I<font>
205
206Italic version of the fixed-width font (actually, something of a misnomer,
207since most fixed-width fonts only have an oblique version, not an italic
208version).  Defaults to C<CI>.  Only matters for troff(1) output.
209
210=item B<--fixedbolditalic>=I<font>
211
212Bold italic (probably actually oblique) version of the fixed-width font.
213Pod::Man doesn't assume you have this, and defaults to C<CB>.  Some
214systems (such as Solaris) have this font available as C<CX>.  Only matters
215for troff(1) output.
216
217=item B<-h>, B<--help>
218
219Print out usage information.
220
221=item B<-l>, B<--lax>
222
223No longer used.  B<pod2man> used to check its input for validity as a
224manual page, but this should now be done by L<podchecker(1)> instead.
225Accepted for backward compatibility; this option no longer does anything.
226
227=item B<-n> I<name>, B<--name>=I<name>
228
229Set the name of the manual page to I<name>.  Without this option, the manual
230name is set to the uppercased base name of the file being converted unless
231the manual section is 3, in which case the path is parsed to see if it is a
232Perl module path.  If it is, a path like C<.../lib/Pod/Man.pm> is converted
233into a name like C<Pod::Man>.  This option, if given, overrides any
234automatic determination of the name.
235
236Note that this option is probably not useful when converting multiple POD
237files at once.  The convention for Unix man pages for commands is for the
238man page title to be in all-uppercase even if the command isn't.
239
240=item B<--nourls>
241
242Normally, LZ<><> formatting codes with a URL but anchor text are formatted
243to show both the anchor text and the URL.  In other words:
244
245    L<foo|http://example.com/>
246
247is formatted as:
248
249    foo <http://example.com/>
250
251This flag, if given, suppresses the URL when anchor text is given, so this
252example would be formatted as just C<foo>.  This can produce less
253cluttered output in cases where the URLs are not particularly important.
254
255=item B<-o>, B<--official>
256
257Set the default header to indicate that this page is part of the standard
258Perl release, if B<--center> is not also given.
259
260=item B<-q> I<quotes>, B<--quotes>=I<quotes>
261
262Sets the quote marks used to surround CE<lt>> text to I<quotes>.  If
263I<quotes> is a single character, it is used as both the left and right
264quote; if I<quotes> is two characters, the first character is used as the
265left quote and the second as the right quoted; and if I<quotes> is four
266characters, the first two are used as the left quote and the second two as
267the right quote.
268
269I<quotes> may also be set to the special value C<none>, in which case no
270quote marks are added around CE<lt>> text (but the font is still changed for
271troff output).
272
273=item B<-r>, B<--release>
274
275Set the centered footer.  By default, this is the version of Perl you run
276B<pod2man> under.  Note that some system an macro sets assume that the
277centered footer will be a modification date and will prepend something like
278"Last modified: "; if this is the case, you may want to set B<--release> to
279the last modified date and B<--date> to the version number.
280
281=item B<-s>, B<--section>
282
283Set the section for the C<.TH> macro.  The standard section numbering
284convention is to use 1 for user commands, 2 for system calls, 3 for
285functions, 4 for devices, 5 for file formats, 6 for games, 7 for
286miscellaneous information, and 8 for administrator commands.  There is a lot
287of variation here, however; some systems (like Solaris) use 4 for file
288formats, 5 for miscellaneous information, and 7 for devices.  Still others
289use 1m instead of 8, or some mix of both.  About the only section numbers
290that are reliably consistent are 1, 2, and 3.
291
292By default, section 1 will be used unless the file ends in C<.pm>, in
293which case section 3 will be selected.
294
295=item B<--stderr>
296
297By default, B<pod2man> dies if any errors are detected in the POD input.
298If B<--stderr> is given and no B<--errors> flag is present, errors are
299sent to standard error, but B<pod2man> does not abort.  This is equivalent
300to C<--errors=stderr> and is supported for backward compatibility.
301
302=item B<-u>, B<--utf8>
303
304This option allows B<pod2man> to output literal UTF-8 characters.
305On OpenBSD, it is enabled by default and can be disabled with
306B<--no-utf8>, in which case non-ASCII characters are converted
307either to *roff escape sequences or to C<X>.
308
309Be aware that, when using this option, the input encoding of your POD
310source must be properly declared unless it is US-ASCII or Latin-1.  POD
311input without an C<=encoding> command will be assumed to be in Latin-1,
312and if it's actually in UTF-8, the output will be double-encoded.  See
313L<perlpod(1)> for more information on the C<=encoding> command.
314
315=item B<-v>, B<--verbose>
316
317Print out the name of each output file as it is being generated.
318
319=back
320
321=head1 EXIT STATUS
322
323As long as all documents processed result in some output, even if that
324output includes errata (a C<POD ERRORS> section generated with
325C<--errors=pod>), B<pod2man> will exit with status 0.  If any of the
326documents being processed do not result in an output document, B<pod2man>
327will exit with status 1.  If there are syntax errors in a POD document
328being processed and the error handling style is set to the default of
329C<die>, B<pod2man> will abort immediately with exit status 255.
330
331=head1 DIAGNOSTICS
332
333If B<pod2man> fails with errors, see L<Pod::Man> and L<Pod::Simple> for
334information about what those errors might mean.
335
336=head1 EXAMPLES
337
338    pod2man program > program.1
339    pod2man SomeModule.pm /usr/perl/man/man3/SomeModule.3
340    pod2man --section=7 note.pod > note.7
341
342If you would like to print out a lot of man page continuously, you probably
343want to set the C and D registers to set contiguous page numbering and
344even/odd paging, at least on some versions of man(7).
345
346    troff -man -rC1 -rD1 perl.1 perldata.1 perlsyn.1 ...
347
348To get index entries on C<STDERR>, turn on the F register, as in:
349
350    troff -man -rF1 perl.1
351
352The indexing merely outputs messages via C<.tm> for each major page,
353section, subsection, item, and any C<XE<lt>E<gt>> directives.  See
354L<Pod::Man> for more details.
355
356=head1 BUGS
357
358Lots of this documentation is duplicated from L<Pod::Man>.
359
360=head1 SEE ALSO
361
362L<Pod::Man>, L<Pod::Simple>, L<man(1)>, L<nroff(1)>, L<perlpod(1)>,
363L<podchecker(1)>, L<perlpodstyle(1)>, L<troff(1)>, L<man(7)>
364
365The man page documenting the an macro set may be L<man(5)> instead of
366L<man(7)> on your system.
367
368The current version of this script is always available from its web site at
369L<http://www.eyrie.org/~eagle/software/podlators/>.  It is also part of the
370Perl core distribution as of 5.6.0.
371
372=head1 AUTHOR
373
374Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
375B<pod2man> by Larry Wall and Tom Christiansen.
376
377=head1 COPYRIGHT AND LICENSE
378
379Copyright 1999, 2000, 2001, 2004, 2006, 2008, 2010, 2012, 2013 Russ
380Allbery <rra@stanford.edu>.
381
382This program is free software; you may redistribute it and/or modify it
383under the same terms as Perl itself.
384
385=cut
386!NO!SUBS!
387
388close OUT or die "Can't close $file: $!";
389chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
390exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
391chdir $origdir;
392