Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Pod / Text.pm
CommitLineData
3fea05b9 1# Pod::Text -- Convert POD data to formatted ASCII text.
2#
3# Copyright 1999, 2000, 2001, 2002, 2004, 2006, 2008
4# Russ Allbery <rra@stanford.edu>
5#
6# This program is free software; you may redistribute it and/or modify it
7# under the same terms as Perl itself.
8#
9# This module converts POD to formatted text. It replaces the old Pod::Text
10# module that came with versions of Perl prior to 5.6.0 and attempts to match
11# its output except for some specific circumstances where other decisions
12# seemed to produce better output. It uses Pod::Parser and is designed to be
13# very easy to subclass.
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.
19
20##############################################################################
21# Modules and declarations
22##############################################################################
23
24package Pod::Text;
25
26require 5.004;
27
28use strict;
29use vars qw(@ISA @EXPORT %ESCAPES $VERSION);
30
31use Carp qw(carp croak);
32use Exporter ();
33use Pod::Simple ();
34
35@ISA = qw(Pod::Simple Exporter);
36
37# We have to export pod2text for backward compatibility.
38@EXPORT = qw(pod2text);
39
40$VERSION = '3.13';
41
42##############################################################################
43# Initialization
44##############################################################################
45
46# This function handles code blocks. It's registered as a callback to
47# Pod::Simple and therefore doesn't work as a regular method call, but all it
48# does is call output_code with the line.
49sub handle_code {
50 my ($line, $number, $parser) = @_;
51 $parser->output_code ($line . "\n");
52}
53
54# Initialize the object and set various Pod::Simple options that we need.
55# Here, we also process any additional options passed to the constructor or
56# set up defaults if none were given. Note that all internal object keys are
57# in all-caps, reserving all lower-case object keys for Pod::Simple and user
58# arguments.
59sub new {
60 my $class = shift;
61 my $self = $class->SUPER::new;
62
63 # Tell Pod::Simple to handle S<> by automatically inserting &nbsp;.
64 $self->nbsp_for_S (1);
65
66 # Tell Pod::Simple to keep whitespace whenever possible.
67 if ($self->can ('preserve_whitespace')) {
68 $self->preserve_whitespace (1);
69 } else {
70 $self->fullstop_space_harden (1);
71 }
72
73 # The =for and =begin targets that we accept.
74 $self->accept_targets (qw/text TEXT/);
75
76 # Ensure that contiguous blocks of code are merged together. Otherwise,
77 # some of the guesswork heuristics don't work right.
78 $self->merge_text (1);
79
80 # Pod::Simple doesn't do anything useful with our arguments, but we want
81 # to put them in our object as hash keys and values. This could cause
82 # problems if we ever clash with Pod::Simple's own internal class
83 # variables.
84 my %opts = @_;
85 my @opts = map { ("opt_$_", $opts{$_}) } keys %opts;
86 %$self = (%$self, @opts);
87
88 # Send errors to stderr if requested.
89 if ($$self{opt_stderr}) {
90 $self->no_errata_section (1);
91 $self->complain_stderr (1);
92 delete $$self{opt_stderr};
93 }
94
95 # Initialize various things from our parameters.
96 $$self{opt_alt} = 0 unless defined $$self{opt_alt};
97 $$self{opt_indent} = 4 unless defined $$self{opt_indent};
98 $$self{opt_margin} = 0 unless defined $$self{opt_margin};
99 $$self{opt_loose} = 0 unless defined $$self{opt_loose};
100 $$self{opt_sentence} = 0 unless defined $$self{opt_sentence};
101 $$self{opt_width} = 76 unless defined $$self{opt_width};
102
103 # Figure out what quotes we'll be using for C<> text.
104 $$self{opt_quotes} ||= '"';
105 if ($$self{opt_quotes} eq 'none') {
106 $$self{LQUOTE} = $$self{RQUOTE} = '';
107 } elsif (length ($$self{opt_quotes}) == 1) {
108 $$self{LQUOTE} = $$self{RQUOTE} = $$self{opt_quotes};
109 } elsif ($$self{opt_quotes} =~ /^(.)(.)$/
110 || $$self{opt_quotes} =~ /^(..)(..)$/) {
111 $$self{LQUOTE} = $1;
112 $$self{RQUOTE} = $2;
113 } else {
114 croak qq(Invalid quote specification "$$self{opt_quotes}");
115 }
116
117 # If requested, do something with the non-POD text.
118 $self->code_handler (\&handle_code) if $$self{opt_code};
119
120 # Return the created object.
121 return $self;
122}
123
124##############################################################################
125# Core parsing
126##############################################################################
127
128# This is the glue that connects the code below with Pod::Simple itself. The
129# goal is to convert the event stream coming from the POD parser into method
130# calls to handlers once the complete content of a tag has been seen. Each
131# paragraph or POD command will have textual content associated with it, and
132# as soon as all of a paragraph or POD command has been seen, that content
133# will be passed in to the corresponding method for handling that type of
134# object. The exceptions are handlers for lists, which have opening tag
135# handlers and closing tag handlers that will be called right away.
136#
137# The internal hash key PENDING is used to store the contents of a tag until
138# all of it has been seen. It holds a stack of open tags, each one
139# represented by a tuple of the attributes hash for the tag and the contents
140# of the tag.
141
142# Add a block of text to the contents of the current node, formatting it
143# according to the current formatting instructions as we do.
144sub _handle_text {
145 my ($self, $text) = @_;
146 my $tag = $$self{PENDING}[-1];
147 $$tag[1] .= $text;
148}
149
150# Given an element name, get the corresponding method name.
151sub method_for_element {
152 my ($self, $element) = @_;
153 $element =~ tr/-/_/;
154 $element =~ tr/A-Z/a-z/;
155 $element =~ tr/_a-z0-9//cd;
156 return $element;
157}
158
159# Handle the start of a new element. If cmd_element is defined, assume that
160# we need to collect the entire tree for this element before passing it to the
161# element method, and create a new tree into which we'll collect blocks of
162# text and nested elements. Otherwise, if start_element is defined, call it.
163sub _handle_element_start {
164 my ($self, $element, $attrs) = @_;
165 my $method = $self->method_for_element ($element);
166
167 # If we have a command handler, we need to accumulate the contents of the
168 # tag before calling it.
169 if ($self->can ("cmd_$method")) {
170 push (@{ $$self{PENDING} }, [ $attrs, '' ]);
171 } elsif ($self->can ("start_$method")) {
172 my $method = 'start_' . $method;
173 $self->$method ($attrs, '');
174 }
175}
176
177# Handle the end of an element. If we had a cmd_ method for this element,
178# this is where we pass along the text that we've accumulated. Otherwise, if
179# we have an end_ method for the element, call that.
180sub _handle_element_end {
181 my ($self, $element) = @_;
182 my $method = $self->method_for_element ($element);
183
184 # If we have a command handler, pull off the pending text and pass it to
185 # the handler along with the saved attribute hash.
186 if ($self->can ("cmd_$method")) {
187 my $tag = pop @{ $$self{PENDING} };
188 my $method = 'cmd_' . $method;
189 my $text = $self->$method (@$tag);
190 if (defined $text) {
191 if (@{ $$self{PENDING} } > 1) {
192 $$self{PENDING}[-1][1] .= $text;
193 } else {
194 $self->output ($text);
195 }
196 }
197 } elsif ($self->can ("end_$method")) {
198 my $method = 'end_' . $method;
199 $self->$method ();
200 }
201}
202
203##############################################################################
204# Output formatting
205##############################################################################
206
207# Wrap a line, indenting by the current left margin. We can't use Text::Wrap
208# because it plays games with tabs. We can't use formline, even though we'd
209# really like to, because it screws up non-printing characters. So we have to
210# do the wrapping ourselves.
211sub wrap {
212 my $self = shift;
213 local $_ = shift;
214 my $output = '';
215 my $spaces = ' ' x $$self{MARGIN};
216 my $width = $$self{opt_width} - $$self{MARGIN};
217 while (length > $width) {
218 if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) {
219 $output .= $spaces . $1 . "\n";
220 } else {
221 last;
222 }
223 }
224 $output .= $spaces . $_;
225 $output =~ s/\s+$/\n\n/;
226 return $output;
227}
228
229# Reformat a paragraph of text for the current margin. Takes the text to
230# reformat and returns the formatted text.
231sub reformat {
232 my $self = shift;
233 local $_ = shift;
234
235 # If we're trying to preserve two spaces after sentences, do some munging
236 # to support that. Otherwise, smash all repeated whitespace.
237 if ($$self{opt_sentence}) {
238 s/ +$//mg;
239 s/\.\n/. \n/g;
240 s/\n/ /g;
241 s/ +/ /g;
242 } else {
243 s/\s+/ /g;
244 }
245 return $self->wrap ($_);
246}
247
248# Output text to the output device. Replace non-breaking spaces with spaces
249# and soft hyphens with nothing, and then try to fix the output encoding if
250# necessary to match the input encoding unless UTF-8 output is forced. This
251# preserves the traditional pass-through behavior of Pod::Text.
252sub output {
253 my ($self, $text) = @_;
254 $text =~ tr/\240\255/ /d;
255 unless ($$self{opt_utf8} || $$self{CHECKED_ENCODING}) {
256 my $encoding = $$self{encoding} || '';
257 if ($encoding) {
258 eval { binmode ($$self{output_fh}, ":encoding($encoding)") };
259 }
260 $$self{CHECKED_ENCODING} = 1;
261 }
262 print { $$self{output_fh} } $text;
263}
264
265# Output a block of code (something that isn't part of the POD text). Called
266# by preprocess_paragraph only if we were given the code option. Exists here
267# only so that it can be overridden by subclasses.
268sub output_code { $_[0]->output ($_[1]) }
269
270##############################################################################
271# Document initialization
272##############################################################################
273
274# Set up various things that have to be initialized on a per-document basis.
275sub start_document {
276 my $self = shift;
277 my $margin = $$self{opt_indent} + $$self{opt_margin};
278
279 # Initialize a few per-document variables.
280 $$self{INDENTS} = []; # Stack of indentations.
281 $$self{MARGIN} = $margin; # Default left margin.
282 $$self{PENDING} = [[]]; # Pending output.
283
284 # We have to redo encoding handling for each document.
285 delete $$self{CHECKED_ENCODING};
286
287 # If we were given the utf8 option, set an output encoding on our file
288 # handle. Wrap in an eval in case we're using a version of Perl too old
289 # to understand this.
290 #
291 # This is evil because it changes the global state of a file handle that
292 # we may not own. However, we can't just blindly encode all output, since
293 # there may be a pre-applied output encoding (such as from PERL_UNICODE)
294 # and then we would double-encode. This seems to be the least bad
295 # approach.
296 if ($$self{opt_utf8}) {
297 eval { binmode ($$self{output_fh}, ':encoding(UTF-8)') };
298 }
299
300 return '';
301}
302
303##############################################################################
304# Text blocks
305##############################################################################
306
307# This method is called whenever an =item command is complete (in other words,
308# we've seen its associated paragraph or know for certain that it doesn't have
309# one). It gets the paragraph associated with the item as an argument. If
310# that argument is empty, just output the item tag; if it contains a newline,
311# output the item tag followed by the newline. Otherwise, see if there's
312# enough room for us to output the item tag in the margin of the text or if we
313# have to put it on a separate line.
314sub item {
315 my ($self, $text) = @_;
316 my $tag = $$self{ITEM};
317 unless (defined $tag) {
318 carp "Item called without tag";
319 return;
320 }
321 undef $$self{ITEM};
322
323 # Calculate the indentation and margin. $fits is set to true if the tag
324 # will fit into the margin of the paragraph given our indentation level.
325 my $indent = $$self{INDENTS}[-1];
326 $indent = $$self{opt_indent} unless defined $indent;
327 my $margin = ' ' x $$self{opt_margin};
328 my $fits = ($$self{MARGIN} - $indent >= length ($tag) + 1);
329
330 # If the tag doesn't fit, or if we have no associated text, print out the
331 # tag separately. Otherwise, put the tag in the margin of the paragraph.
332 if (!$text || $text =~ /^\s+$/ || !$fits) {
333 my $realindent = $$self{MARGIN};
334 $$self{MARGIN} = $indent;
335 my $output = $self->reformat ($tag);
336 $output =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
337 $output =~ s/\n*$/\n/;
338
339 # If the text is just whitespace, we have an empty item paragraph;
340 # this can result from =over/=item/=back without any intermixed
341 # paragraphs. Insert some whitespace to keep the =item from merging
342 # into the next paragraph.
343 $output .= "\n" if $text && $text =~ /^\s*$/;
344
345 $self->output ($output);
346 $$self{MARGIN} = $realindent;
347 $self->output ($self->reformat ($text)) if ($text && $text =~ /\S/);
348 } else {
349 my $space = ' ' x $indent;
350 $space =~ s/^$margin /$margin:/ if $$self{opt_alt};
351 $text = $self->reformat ($text);
352 $text =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
353 my $tagspace = ' ' x length $tag;
354 $text =~ s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item";
355 $self->output ($text);
356 }
357}
358
359# Handle a basic block of text. The only tricky thing here is that if there
360# is a pending item tag, we need to format this as an item paragraph.
361sub cmd_para {
362 my ($self, $attrs, $text) = @_;
363 $text =~ s/\s+$/\n/;
364 if (defined $$self{ITEM}) {
365 $self->item ($text . "\n");
366 } else {
367 $self->output ($self->reformat ($text . "\n"));
368 }
369 return '';
370}
371
372# Handle a verbatim paragraph. Just print it out, but indent it according to
373# our margin.
374sub cmd_verbatim {
375 my ($self, $attrs, $text) = @_;
376 $self->item if defined $$self{ITEM};
377 return if $text =~ /^\s*$/;
378 $text =~ s/^(\n*)([ \t]*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme;
379 $text =~ s/\s*$/\n\n/;
380 $self->output ($text);
381 return '';
382}
383
384# Handle literal text (produced by =for and similar constructs). Just output
385# it with the minimum of changes.
386sub cmd_data {
387 my ($self, $attrs, $text) = @_;
388 $text =~ s/^\n+//;
389 $text =~ s/\n{0,2}$/\n/;
390 $self->output ($text);
391 return '';
392}
393
394##############################################################################
395# Headings
396##############################################################################
397
398# The common code for handling all headers. Takes the header text, the
399# indentation, and the surrounding marker for the alt formatting method.
400sub heading {
401 my ($self, $text, $indent, $marker) = @_;
402 $self->item ("\n\n") if defined $$self{ITEM};
403 $text =~ s/\s+$//;
404 if ($$self{opt_alt}) {
405 my $closemark = reverse (split (//, $marker));
406 my $margin = ' ' x $$self{opt_margin};
407 $self->output ("\n" . "$margin$marker $text $closemark" . "\n\n");
408 } else {
409 $text .= "\n" if $$self{opt_loose};
410 my $margin = ' ' x ($$self{opt_margin} + $indent);
411 $self->output ($margin . $text . "\n");
412 }
413 return '';
414}
415
416# First level heading.
417sub cmd_head1 {
418 my ($self, $attrs, $text) = @_;
419 $self->heading ($text, 0, '====');
420}
421
422# Second level heading.
423sub cmd_head2 {
424 my ($self, $attrs, $text) = @_;
425 $self->heading ($text, $$self{opt_indent} / 2, '== ');
426}
427
428# Third level heading.
429sub cmd_head3 {
430 my ($self, $attrs, $text) = @_;
431 $self->heading ($text, $$self{opt_indent} * 2 / 3 + 0.5, '= ');
432}
433
434# Fourth level heading.
435sub cmd_head4 {
436 my ($self, $attrs, $text) = @_;
437 $self->heading ($text, $$self{opt_indent} * 3 / 4 + 0.5, '- ');
438}
439
440##############################################################################
441# List handling
442##############################################################################
443
444# Handle the beginning of an =over block. Takes the type of the block as the
445# first argument, and then the attr hash. This is called by the handlers for
446# the four different types of lists (bullet, number, text, and block).
447sub over_common_start {
448 my ($self, $attrs) = @_;
449 $self->item ("\n\n") if defined $$self{ITEM};
450
451 # Find the indentation level.
452 my $indent = $$attrs{indent};
453 unless (defined ($indent) && $indent =~ /^\s*[-+]?\d{1,4}\s*$/) {
454 $indent = $$self{opt_indent};
455 }
456
457 # Add this to our stack of indents and increase our current margin.
458 push (@{ $$self{INDENTS} }, $$self{MARGIN});
459 $$self{MARGIN} += ($indent + 0);
460 return '';
461}
462
463# End an =over block. Takes no options other than the class pointer. Output
464# any pending items and then pop one level of indentation.
465sub over_common_end {
466 my ($self) = @_;
467 $self->item ("\n\n") if defined $$self{ITEM};
468 $$self{MARGIN} = pop @{ $$self{INDENTS} };
469 return '';
470}
471
472# Dispatch the start and end calls as appropriate.
473sub start_over_bullet { $_[0]->over_common_start ($_[1]) }
474sub start_over_number { $_[0]->over_common_start ($_[1]) }
475sub start_over_text { $_[0]->over_common_start ($_[1]) }
476sub start_over_block { $_[0]->over_common_start ($_[1]) }
477sub end_over_bullet { $_[0]->over_common_end }
478sub end_over_number { $_[0]->over_common_end }
479sub end_over_text { $_[0]->over_common_end }
480sub end_over_block { $_[0]->over_common_end }
481
482# The common handler for all item commands. Takes the type of the item, the
483# attributes, and then the text of the item.
484sub item_common {
485 my ($self, $type, $attrs, $text) = @_;
486 $self->item if defined $$self{ITEM};
487
488 # Clean up the text. We want to end up with two variables, one ($text)
489 # which contains any body text after taking out the item portion, and
490 # another ($item) which contains the actual item text. Note the use of
491 # the internal Pod::Simple attribute here; that's a potential land mine.
492 $text =~ s/\s+$//;
493 my ($item, $index);
494 if ($type eq 'bullet') {
495 $item = '*';
496 } elsif ($type eq 'number') {
497 $item = $$attrs{'~orig_content'};
498 } else {
499 $item = $text;
500 $item =~ s/\s*\n\s*/ /g;
501 $text = '';
502 }
503 $$self{ITEM} = $item;
504
505 # If body text for this item was included, go ahead and output that now.
506 if ($text) {
507 $text =~ s/\s*$/\n/;
508 $self->item ($text);
509 }
510 return '';
511}
512
513# Dispatch the item commands to the appropriate place.
514sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) }
515sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) }
516sub cmd_item_text { my $self = shift; $self->item_common ('text', @_) }
517sub cmd_item_block { my $self = shift; $self->item_common ('block', @_) }
518
519##############################################################################
520# Formatting codes
521##############################################################################
522
523# The simple ones.
524sub cmd_b { return $_[0]{alt} ? "``$_[2]''" : $_[2] }
525sub cmd_f { return $_[0]{alt} ? "\"$_[2]\"" : $_[2] }
526sub cmd_i { return '*' . $_[2] . '*' }
527sub cmd_x { return '' }
528
529# Apply a whole bunch of messy heuristics to not quote things that don't
530# benefit from being quoted. These originally come from Barrie Slaymaker and
531# largely duplicate code in Pod::Man.
532sub cmd_c {
533 my ($self, $attrs, $text) = @_;
534
535 # A regex that matches the portion of a variable reference that's the
536 # array or hash index, separated out just because we want to use it in
537 # several places in the following regex.
538 my $index = '(?: \[.*\] | \{.*\} )?';
539
540 # Check for things that we don't want to quote, and if we find any of
541 # them, return the string with just a font change and no quoting.
542 $text =~ m{
543 ^\s*
544 (?:
545 ( [\'\`\"] ) .* \1 # already quoted
546 | \` .* \' # `quoted'
547 | \$+ [\#^]? \S $index # special ($^Foo, $")
548 | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func
549 | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
550 | [+-]? ( \d[\d.]* | \.\d+ ) (?: [eE][+-]?\d+ )? # a number
551 | 0x [a-fA-F\d]+ # a hex constant
552 )
553 \s*\z
554 }xo && return $text;
555
556 # If we didn't return, go ahead and quote the text.
557 return $$self{opt_alt}
558 ? "``$text''"
559 : "$$self{LQUOTE}$text$$self{RQUOTE}";
560}
561
562# Links reduce to the text that we're given, wrapped in angle brackets if it's
563# a URL.
564sub cmd_l {
565 my ($self, $attrs, $text) = @_;
566 return $$attrs{type} eq 'url' ? "<$text>" : $text;
567}
568
569##############################################################################
570# Backwards compatibility
571##############################################################################
572
573# The old Pod::Text module did everything in a pod2text() function. This
574# tries to provide the same interface for legacy applications.
575sub pod2text {
576 my @args;
577
578 # This is really ugly; I hate doing option parsing in the middle of a
579 # module. But the old Pod::Text module supported passing flags to its
580 # entry function, so handle -a and -<number>.
581 while ($_[0] =~ /^-/) {
582 my $flag = shift;
583 if ($flag eq '-a') { push (@args, alt => 1) }
584 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
585 else {
586 unshift (@_, $flag);
587 last;
588 }
589 }
590
591 # Now that we know what arguments we're using, create the parser.
592 my $parser = Pod::Text->new (@args);
593
594 # If two arguments were given, the second argument is going to be a file
595 # handle. That means we want to call parse_from_filehandle(), which means
596 # we need to turn the first argument into a file handle. Magic open will
597 # handle the <&STDIN case automagically.
598 if (defined $_[1]) {
599 my @fhs = @_;
600 local *IN;
601 unless (open (IN, $fhs[0])) {
602 croak ("Can't open $fhs[0] for reading: $!\n");
603 return;
604 }
605 $fhs[0] = \*IN;
606 $parser->output_fh ($fhs[1]);
607 my $retval = $parser->parse_file ($fhs[0]);
608 my $fh = $parser->output_fh ();
609 close $fh;
610 return $retval;
611 } else {
612 $parser->output_fh (\*STDOUT);
613 return $parser->parse_file (@_);
614 }
615}
616
617# Reset the underlying Pod::Simple object between calls to parse_from_file so
618# that the same object can be reused to convert multiple pages.
619sub parse_from_file {
620 my $self = shift;
621 $self->reinit;
622
623 # Fake the old cutting option to Pod::Parser. This fiddings with internal
624 # Pod::Simple state and is quite ugly; we need a better approach.
625 if (ref ($_[0]) eq 'HASH') {
626 my $opts = shift @_;
627 if (defined ($$opts{-cutting}) && !$$opts{-cutting}) {
628 $$self{in_pod} = 1;
629 $$self{last_was_blank} = 1;
630 }
631 }
632
633 # Do the work.
634 my $retval = $self->Pod::Simple::parse_from_file (@_);
635
636 # Flush output, since Pod::Simple doesn't do this. Ideally we should also
637 # close the file descriptor if we had to open one, but we can't easily
638 # figure this out.
639 my $fh = $self->output_fh ();
640 my $oldfh = select $fh;
641 my $oldflush = $|;
642 $| = 1;
643 print $fh '';
644 $| = $oldflush;
645 select $oldfh;
646 return $retval;
647}
648
649# Pod::Simple failed to provide this backward compatibility function, so
650# implement it ourselves. File handles are one of the inputs that
651# parse_from_file supports.
652sub parse_from_filehandle {
653 my $self = shift;
654 $self->parse_from_file (@_);
655}
656
657##############################################################################
658# Module return value and documentation
659##############################################################################
660
6611;
662__END__
663
664=head1 NAME
665
666Pod::Text - Convert POD data to formatted ASCII text
667
668=for stopwords
669alt stderr Allbery Sean Burke's Christiansen UTF-8 pre-Unicode utf8
670
671=head1 SYNOPSIS
672
673 use Pod::Text;
674 my $parser = Pod::Text->new (sentence => 0, width => 78);
675
676 # Read POD from STDIN and write to STDOUT.
677 $parser->parse_from_filehandle;
678
679 # Read POD from file.pod and write to file.txt.
680 $parser->parse_from_file ('file.pod', 'file.txt');
681
682=head1 DESCRIPTION
683
684Pod::Text is a module that can convert documentation in the POD format (the
685preferred language for documenting Perl) into formatted ASCII. It uses no
686special formatting controls or codes whatsoever, and its output is therefore
687suitable for nearly any device.
688
689As a derived class from Pod::Simple, Pod::Text supports the same methods and
690interfaces. See L<Pod::Simple> for all the details; briefly, one creates a
691new parser with C<< Pod::Text->new() >> and then normally calls parse_file().
692
693new() can take options, in the form of key/value pairs, that control the
694behavior of the parser. The currently recognized options are:
695
696=over 4
697
698=item alt
699
700If set to a true value, selects an alternate output format that, among other
701things, uses a different heading style and marks C<=item> entries with a
702colon in the left margin. Defaults to false.
703
704=item code
705
706If set to a true value, the non-POD parts of the input file will be included
707in the output. Useful for viewing code documented with POD blocks with the
708POD rendered and the code left intact.
709
710=item indent
711
712The number of spaces to indent regular text, and the default indentation for
713C<=over> blocks. Defaults to 4.
714
715=item loose
716
717If set to a true value, a blank line is printed after a C<=head1> heading.
718If set to false (the default), no blank line is printed after C<=head1>,
719although one is still printed after C<=head2>. This is the default because
720it's the expected formatting for manual pages; if you're formatting
721arbitrary text documents, setting this to true may result in more pleasing
722output.
723
724=item margin
725
726The width of the left margin in spaces. Defaults to 0. This is the margin
727for all text, including headings, not the amount by which regular text is
728indented; for the latter, see the I<indent> option. To set the right
729margin, see the I<width> option.
730
731=item quotes
732
733Sets the quote marks used to surround CE<lt>> text. If the value is a
734single character, it is used as both the left and right quote; if it is two
735characters, the first character is used as the left quote and the second as
736the right quoted; and if it is four characters, the first two are used as
737the left quote and the second two as the right quote.
738
739This may also be set to the special value C<none>, in which case no quote
740marks are added around CE<lt>> text.
741
742=item sentence
743
744If set to a true value, Pod::Text will assume that each sentence ends in two
745spaces, and will try to preserve that spacing. If set to false, all
746consecutive whitespace in non-verbatim paragraphs is compressed into a
747single space. Defaults to true.
748
749=item stderr
750
751Send error messages about invalid POD to standard error instead of
752appending a POD ERRORS section to the generated output.
753
754=item utf8
755
756By default, Pod::Text uses the same output encoding as the input encoding
757of the POD source (provided that Perl was built with PerlIO; otherwise, it
758doesn't encode its output). If this option is given, the output encoding
759is forced to UTF-8.
760
761Be aware that, when using this option, the input encoding of your POD
762source must be properly declared unless it is US-ASCII or Latin-1. POD
763input without an C<=encoding> command will be assumed to be in Latin-1,
764and if it's actually in UTF-8, the output will be double-encoded. See
765L<perlpod(1)> for more information on the C<=encoding> command.
766
767=item width
768
769The column at which to wrap text on the right-hand side. Defaults to 76.
770
771=back
772
773The standard Pod::Simple method parse_file() takes one argument, the file or
774file handle to read from, and writes output to standard output unless that
775has been changed with the output_fh() method. See L<Pod::Simple> for the
776specific details and for other alternative interfaces.
777
778=head1 DIAGNOSTICS
779
780=over 4
781
782=item Bizarre space in item
783
784=item Item called without tag
785
786(W) Something has gone wrong in internal C<=item> processing. These
787messages indicate a bug in Pod::Text; you should never see them.
788
789=item Can't open %s for reading: %s
790
791(F) Pod::Text was invoked via the compatibility mode pod2text() interface
792and the input file it was given could not be opened.
793
794=item Invalid quote specification "%s"
795
796(F) The quote specification given (the quotes option to the constructor) was
797invalid. A quote specification must be one, two, or four characters long.
798
799=back
800
801=head1 BUGS
802
803Encoding handling assumes that PerlIO is available and does not work
804properly if it isn't. The C<utf8> option is therefore not supported
805unless Perl is built with PerlIO support.
806
807=head1 CAVEATS
808
809If Pod::Text is given the C<utf8> option, the encoding of its output file
810handle will be forced to UTF-8 if possible, overriding any existing
811encoding. This will be done even if the file handle is not created by
812Pod::Text and was passed in from outside. This maintains consistency
813regardless of PERL_UNICODE and other settings.
814
815If the C<utf8> option is not given, the encoding of its output file handle
816will be forced to the detected encoding of the input POD, which preserves
817whatever the input text is. This ensures backward compatibility with
818earlier, pre-Unicode versions of this module, without large numbers of
819Perl warnings.
820
821This is not ideal, but it seems to be the best compromise. If it doesn't
822work for you, please let me know the details of how it broke.
823
824=head1 NOTES
825
826This is a replacement for an earlier Pod::Text module written by Tom
827Christiansen. It has a revamped interface, since it now uses Pod::Simple,
828but an interface roughly compatible with the old Pod::Text::pod2text()
829function is still available. Please change to the new calling convention,
830though.
831
832The original Pod::Text contained code to do formatting via termcap
833sequences, although it wasn't turned on by default and it was problematic to
834get it to work at all. This rewrite doesn't even try to do that, but a
835subclass of it does. Look for L<Pod::Text::Termcap>.
836
837=head1 SEE ALSO
838
839L<Pod::Simple>, L<Pod::Text::Termcap>, L<perlpod(1)>, L<pod2text(1)>
840
841The current version of this module is always available from its web site at
842L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
843Perl core distribution as of 5.6.0.
844
845=head1 AUTHOR
846
847Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
848Pod::Text by Tom Christiansen <tchrist@mox.perl.com> and its conversion to
849Pod::Parser by Brad Appleton <bradapp@enteract.com>. Sean Burke's initial
850conversion of Pod::Man to use Pod::Simple provided much-needed guidance on
851how to use Pod::Simple.
852
853=head1 COPYRIGHT AND LICENSE
854
855Copyright 1999, 2000, 2001, 2002, 2004, 2006, 2008 Russ Allbery
856<rra@stanford.edu>.
857
858This program is free software; you may redistribute it and/or modify it
859under the same terms as Perl itself.
860
861=cut