xref: /openbsd-src/gnu/usr.bin/perl/cpan/podlators/scripts/pod2text.PL (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
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# pod2text -- Convert POD data to formatted ASCII text.
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#
46# The driver script for Pod::Text, Pod::Text::Termcap, and Pod::Text::Color,
47# invoked by perldoc -t among other things.
48
49require 5.004;
50
51use Getopt::Long qw(GetOptions);
52use Pod::Text ();
53use Pod::Usage qw(pod2usage);
54
55use strict;
56
57# Clean up $0 for error reporting.
58$0 =~ s%.*/%%;
59
60# Take an initial pass through our options, looking for one of the form
61# -<number>.  We turn that into -w <number> for compatibility with the
62# original pod2text script.
63for (my $i = 0; $i < @ARGV; $i++) {
64    last if $ARGV[$i] =~ /^--$/;
65    if ($ARGV[$i] =~ /^-(\d+)$/) {
66        splice (@ARGV, $i++, 1, '-w', $1);
67    }
68}
69
70# Insert -- into @ARGV before any single dash argument to hide it from
71# Getopt::Long; we want to interpret it as meaning stdin (which Pod::Simple
72# does correctly).
73my $stdin;
74@ARGV = map { $_ eq '-' && !$stdin++ ? ('--', $_) : $_ } @ARGV;
75
76# Parse our options.  Use the same names as Pod::Text for simplicity, and
77# default to sentence boundaries turned off for compatibility.
78my %options;
79$options{sentence} = 0;
80Getopt::Long::config ('bundling');
81GetOptions (\%options, 'alt|a', 'code', 'color|c', 'errors=s', 'help|h',
82            'indent|i=i', 'loose|l', 'margin|left-margin|m=i', 'nourls',
83            'overstrike|o', 'quotes|q=s', 'sentence|s', 'stderr', 'termcap|t',
84            'utf8|u', 'width|w=i')
85    or exit 1;
86pod2usage (1) if $options{help};
87
88# Figure out what formatter we're going to use.  -c overrides -t.
89my $formatter = 'Pod::Text';
90if ($options{color}) {
91    $formatter = 'Pod::Text::Color';
92    eval { require Term::ANSIColor };
93    if ($@) { die "-c (--color) requires Term::ANSIColor be installed\n" }
94    require Pod::Text::Color;
95} elsif ($options{termcap}) {
96    $formatter = 'Pod::Text::Termcap';
97    require Pod::Text::Termcap;
98} elsif ($options{overstrike}) {
99    $formatter = 'Pod::Text::Overstrike';
100    require Pod::Text::Overstrike;
101}
102delete @options{'color', 'termcap', 'overstrike'};
103
104# If neither stderr nor errors is set, default to errors = die.
105if (!defined $options{stderr} && !defined $options{errors}) {
106    $options{errors} = 'die';
107}
108
109# Initialize and run the formatter.
110my $parser = $formatter->new (%options);
111my $status = 0;
112do {
113    my ($input, $output) = splice (@ARGV, 0, 2);
114    $parser->parse_from_file ($input, $output);
115    if ($parser->{CONTENTLESS}) {
116        $status = 1;
117        warn "$0: unable to format $input\n";
118        if (defined ($output) and $output ne '-') {
119            unlink $output unless (-s $output);
120        }
121    }
122} while (@ARGV);
123exit $status;
124
125__END__
126
127=for stopwords
128-aclostu --alt --stderr Allbery --overstrike overstrike --termcap --utf8
129UTF-8 subclasses --nourls
130
131=head1 NAME
132
133pod2text - Convert POD data to formatted ASCII text
134
135=head1 SYNOPSIS
136
137pod2text [B<-aclostu>] [B<--code>] [B<--errors>=I<style>] [B<-i> I<indent>]
138    S<[B<-q> I<quotes>]> [B<--nourls>] [B<--stderr>] S<[B<-w> I<width>]>
139    [I<input> [I<output> ...]]
140
141pod2text B<-h>
142
143=head1 DESCRIPTION
144
145B<pod2text> is a front-end for Pod::Text and its subclasses.  It uses them
146to generate formatted ASCII text from POD source.  It can optionally use
147either termcap sequences or ANSI color escape sequences to format the text.
148
149I<input> is the file to read for POD source (the POD can be embedded in
150code).  If I<input> isn't given, it defaults to C<STDIN>.  I<output>, if
151given, is the file to which to write the formatted output.  If I<output>
152isn't given, the formatted output is written to C<STDOUT>.  Several POD
153files can be processed in the same B<pod2text> invocation (saving module
154load and compile times) by providing multiple pairs of I<input> and
155I<output> files on the command line.
156
157=head1 OPTIONS
158
159=over 4
160
161=item B<-a>, B<--alt>
162
163Use an alternate output format that, among other things, uses a different
164heading style and marks C<=item> entries with a colon in the left margin.
165
166=item B<--code>
167
168Include any non-POD text from the input file in the output as well.  Useful
169for viewing code documented with POD blocks with the POD rendered and the
170code left intact.
171
172=item B<-c>, B<--color>
173
174Format the output with ANSI color escape sequences.  Using this option
175requires that Term::ANSIColor be installed on your system.
176
177=item B<-i> I<indent>, B<--indent=>I<indent>
178
179Set the number of spaces to indent regular text, and the default indentation
180for C<=over> blocks.  Defaults to 4 spaces if this option isn't given.
181
182=item B<-errors>=I<style>
183
184Set the error handling style.  C<die> says to throw an exception on any
185POD formatting error.  C<stderr> says to report errors on standard error,
186but not to throw an exception.  C<pod> says to include a POD ERRORS
187section in the resulting documentation summarizing the errors.  C<none>
188ignores POD errors entirely, as much as possible.
189
190The default is C<die>.
191
192=item B<-h>, B<--help>
193
194Print out usage information and exit.
195
196=item B<-l>, B<--loose>
197
198Print a blank line after a C<=head1> heading.  Normally, no blank line is
199printed after C<=head1>, although one is still printed after C<=head2>,
200because this is the expected formatting for manual pages; if you're
201formatting arbitrary text documents, using this option is recommended.
202
203=item B<-m> I<width>, B<--left-margin>=I<width>, B<--margin>=I<width>
204
205The width of the left margin in spaces.  Defaults to 0.  This is the margin
206for all text, including headings, not the amount by which regular text is
207indented; for the latter, see B<-i> option.
208
209=item B<--nourls>
210
211Normally, LZ<><> formatting codes with a URL but anchor text are formatted
212to show both the anchor text and the URL.  In other words:
213
214    L<foo|http://example.com/>
215
216is formatted as:
217
218    foo <http://example.com/>
219
220This flag, if given, suppresses the URL when anchor text is given, so this
221example would be formatted as just C<foo>.  This can produce less
222cluttered output in cases where the URLs are not particularly important.
223
224=item B<-o>, B<--overstrike>
225
226Format the output with overstrike printing.  Bold text is rendered as
227character, backspace, character.  Italics and file names are rendered as
228underscore, backspace, character.  Many pagers, such as B<less>, know how
229to convert this to bold or underlined text.
230
231=item B<-q> I<quotes>, B<--quotes>=I<quotes>
232
233Sets the quote marks used to surround CE<lt>> text to I<quotes>.  If
234I<quotes> is a single character, it is used as both the left and right
235quote; if I<quotes> is two characters, the first character is used as the
236left quote and the second as the right quoted; and if I<quotes> is four
237characters, the first two are used as the left quote and the second two as
238the right quote.
239
240I<quotes> may also be set to the special value C<none>, in which case no
241quote marks are added around CE<lt>> text.
242
243=item B<-s>, B<--sentence>
244
245Assume each sentence ends with two spaces and try to preserve that spacing.
246Without this option, all consecutive whitespace in non-verbatim paragraphs
247is compressed into a single space.
248
249=item B<--stderr>
250
251By default, B<pod2text> dies if any errors are detected in the POD input.
252If B<--stderr> is given and no B<--errors> flag is present, errors are
253sent to standard error, but B<pod2text> does not abort.  This is
254equivalent to C<--errors=stderr> and is supported for backward
255compatibility.
256
257=item B<-t>, B<--termcap>
258
259Try to determine the width of the screen and the bold and underline
260sequences for the terminal from termcap, and use that information in
261formatting the output.  Output will be wrapped at two columns less than the
262width of your terminal device.  Using this option requires that your system
263have a termcap file somewhere where Term::Cap can find it and requires that
264your system support termios.  With this option, the output of B<pod2text>
265will contain terminal control sequences for your current terminal type.
266
267=item B<-u>, B<--utf8>
268
269By default, B<pod2text> tries to use the same output encoding as its input
270encoding (to be backward-compatible with older versions).  This option
271says to instead force the output encoding to UTF-8.
272
273Be aware that, when using this option, the input encoding of your POD
274source must be properly declared unless it is US-ASCII or Latin-1.  POD
275input without an C<=encoding> command will be assumed to be in Latin-1,
276and if it's actually in UTF-8, the output will be double-encoded.  See
277L<perlpod(1)> for more information on the C<=encoding> command.
278
279=item B<-w>, B<--width=>I<width>, B<->I<width>
280
281The column at which to wrap text on the right-hand side.  Defaults to 76,
282unless B<-t> is given, in which case it's two columns less than the width of
283your terminal device.
284
285=back
286
287=head1 EXIT STATUS
288
289As long as all documents processed result in some output, even if that
290output includes errata (a C<POD ERRORS> section generated with
291C<--errors=pod>), B<pod2text> will exit with status 0.  If any of the
292documents being processed do not result in an output document, B<pod2text>
293will exit with status 1.  If there are syntax errors in a POD document
294being processed and the error handling style is set to the default of
295C<die>, B<pod2text> will abort immediately with exit status 255.
296
297=head1 DIAGNOSTICS
298
299If B<pod2text> fails with errors, see L<Pod::Text> and L<Pod::Simple> for
300information about what those errors might mean.  Internally, it can also
301produce the following diagnostics:
302
303=over 4
304
305=item -c (--color) requires Term::ANSIColor be installed
306
307(F) B<-c> or B<--color> were given, but Term::ANSIColor could not be
308loaded.
309
310=item Unknown option: %s
311
312(F) An unknown command line option was given.
313
314=back
315
316In addition, other L<Getopt::Long> error messages may result from invalid
317command-line options.
318
319=head1 ENVIRONMENT
320
321=over 4
322
323=item COLUMNS
324
325If B<-t> is given, B<pod2text> will take the current width of your screen
326from this environment variable, if available.  It overrides terminal width
327information in TERMCAP.
328
329=item TERMCAP
330
331If B<-t> is given, B<pod2text> will use the contents of this environment
332variable if available to determine the correct formatting sequences for your
333current terminal device.
334
335=back
336
337=head1 SEE ALSO
338
339L<Pod::Text>, L<Pod::Text::Color>, L<Pod::Text::Overstrike>,
340L<Pod::Text::Termcap>, L<Pod::Simple>, L<perlpod(1)>
341
342The current version of this script is always available from its web site at
343L<http://www.eyrie.org/~eagle/software/podlators/>.  It is also part of the
344Perl core distribution as of 5.6.0.
345
346=head1 AUTHOR
347
348Russ Allbery <rra@stanford.edu>.
349
350=head1 COPYRIGHT AND LICENSE
351
352Copyright 1999, 2000, 2001, 2004, 2006, 2008, 2010, 2012, 2013 Russ
353Allbery <rra@stanford.edu>.
354
355This program is free software; you may redistribute it and/or modify it
356under the same terms as Perl itself.
357
358=cut
359!NO!SUBS!
360
361close OUT or die "Can't close $file: $!";
362chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
363exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
364chdir $origdir;
365