Change the plan - 2.14 gets the copy permission behaviour change,
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text.pm
CommitLineData
6055f9d4 1# Pod::Text -- Convert POD data to formatted ASCII text.
6055f9d4 2#
0e4e3f6e 3# Copyright 1999, 2000, 2001, 2002, 2004, 2006, 2008
4# Russ Allbery <rra@stanford.edu>
6055f9d4 5#
3c014959 6# This program is free software; you may redistribute it and/or modify it
6055f9d4 7# under the same terms as Perl itself.
8#
5ec554fb 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.
3c014959 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.
6055f9d4 19
3c014959 20##############################################################################
6055f9d4 21# Modules and declarations
3c014959 22##############################################################################
69e00e79 23
6055f9d4 24package Pod::Text;
69e00e79 25
6055f9d4 26require 5.004;
27
6055f9d4 28use strict;
2e20e14f 29use vars qw(@ISA @EXPORT %ESCAPES $VERSION);
6055f9d4 30
b7ae008f 31use Carp qw(carp croak);
32use Exporter ();
33use Pod::Simple ();
34
35@ISA = qw(Pod::Simple Exporter);
6055f9d4 36
2e20e14f 37# We have to export pod2text for backward compatibility.
38@EXPORT = qw(pod2text);
39
2504ae52 40$VERSION = 3.11;
69e00e79 41
3c014959 42##############################################################################
6055f9d4 43# Initialization
3c014959 44##############################################################################
69e00e79 45
b7ae008f 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 }
69e00e79 72
b7ae008f 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
bc9c7511 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
b7ae008f 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};
69e00e79 102
ab1f1d91 103 # Figure out what quotes we'll be using for C<> text.
b7ae008f 104 $$self{opt_quotes} ||= '"';
105 if ($$self{opt_quotes} eq 'none') {
ab1f1d91 106 $$self{LQUOTE} = $$self{RQUOTE} = '';
b7ae008f 107 } elsif (length ($$self{opt_quotes}) == 1) {
108 $$self{LQUOTE} = $$self{RQUOTE} = $$self{opt_quotes};
109 } elsif ($$self{opt_quotes} =~ /^(.)(.)$/
110 || $$self{opt_quotes} =~ /^(..)(..)$/) {
ab1f1d91 111 $$self{LQUOTE} = $1;
112 $$self{RQUOTE} = $2;
113 } else {
b7ae008f 114 croak qq(Invalid quote specification "$$self{opt_quotes}");
ab1f1d91 115 }
116
b7ae008f 117 # If requested, do something with the non-POD text.
118 $self->code_handler (\&handle_code) if $$self{opt_code};
11f72409 119
b7ae008f 120 # Return the created object.
121 return $self;
122}
69e00e79 123
b7ae008f 124##############################################################################
125# Core parsing
126##############################################################################
59548eca 127
b7ae008f 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;
6055f9d4 148}
69e00e79 149
b7ae008f 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}
69e00e79 158
b7ae008f 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}
6055f9d4 176
b7ae008f 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;
8f202758 199 $self->$method ();
ab1f1d91 200 }
6055f9d4 201}
69e00e79 202
b7ae008f 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 {
6055f9d4 212 my $self = shift;
6055f9d4 213 local $_ = shift;
b7ae008f 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;
6055f9d4 227}
69e00e79 228
b7ae008f 229# Reformat a paragraph of text for the current margin. Takes the text to
230# reformat and returns the formatted text.
231sub reformat {
27f805f4 232 my $self = shift;
27f805f4 233 local $_ = shift;
6055f9d4 234
b7ae008f 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;
6055f9d4 242 } else {
b7ae008f 243 s/\s+/ /g;
6055f9d4 244 }
b7ae008f 245 return $self->wrap ($_);
6055f9d4 246}
69e00e79 247
bc9c7511 248# Output text to the output device. Replace non-breaking spaces with spaces
249# and soft hyphens with nothing.
b7ae008f 250sub output {
251 my ($self, $text) = @_;
252 $text =~ tr/\240\255/ /d;
253 print { $$self{output_fh} } $text;
254}
bf202ccd 255
b7ae008f 256# Output a block of code (something that isn't part of the POD text). Called
257# by preprocess_paragraph only if we were given the code option. Exists here
258# only so that it can be overridden by subclasses.
259sub output_code { $_[0]->output ($_[1]) }
69e00e79 260
b7ae008f 261##############################################################################
262# Document initialization
263##############################################################################
264
265# Set up various things that have to be initialized on a per-document basis.
266sub start_document {
267 my $self = shift;
268 my $margin = $$self{opt_indent} + $$self{opt_margin};
269
270 # Initialize a few per-document variables.
271 $$self{INDENTS} = []; # Stack of indentations.
272 $$self{MARGIN} = $margin; # Default left margin.
273 $$self{PENDING} = [[]]; # Pending output.
274
275 return '';
276}
277
278##############################################################################
279# Text blocks
280##############################################################################
281
282# This method is called whenever an =item command is complete (in other words,
283# we've seen its associated paragraph or know for certain that it doesn't have
284# one). It gets the paragraph associated with the item as an argument. If
285# that argument is empty, just output the item tag; if it contains a newline,
286# output the item tag followed by the newline. Otherwise, see if there's
287# enough room for us to output the item tag in the margin of the text or if we
288# have to put it on a separate line.
289sub item {
290 my ($self, $text) = @_;
291 my $tag = $$self{ITEM};
292 unless (defined $tag) {
293 carp "Item called without tag";
294 return;
6055f9d4 295 }
b7ae008f 296 undef $$self{ITEM};
69e00e79 297
b7ae008f 298 # Calculate the indentation and margin. $fits is set to true if the tag
299 # will fit into the margin of the paragraph given our indentation level.
300 my $indent = $$self{INDENTS}[-1];
301 $indent = $$self{opt_indent} unless defined $indent;
302 my $margin = ' ' x $$self{opt_margin};
303 my $fits = ($$self{MARGIN} - $indent >= length ($tag) + 1);
69e00e79 304
b7ae008f 305 # If the tag doesn't fit, or if we have no associated text, print out the
306 # tag separately. Otherwise, put the tag in the margin of the paragraph.
307 if (!$text || $text =~ /^\s+$/ || !$fits) {
308 my $realindent = $$self{MARGIN};
309 $$self{MARGIN} = $indent;
310 my $output = $self->reformat ($tag);
311 $output =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
312 $output =~ s/\n*$/\n/;
313
314 # If the text is just whitespace, we have an empty item paragraph;
315 # this can result from =over/=item/=back without any intermixed
316 # paragraphs. Insert some whitespace to keep the =item from merging
317 # into the next paragraph.
318 $output .= "\n" if $text && $text =~ /^\s*$/;
319
320 $self->output ($output);
321 $$self{MARGIN} = $realindent;
322 $self->output ($self->reformat ($text)) if ($text && $text =~ /\S/);
323 } else {
324 my $space = ' ' x $indent;
325 $space =~ s/^$margin /$margin:/ if $$self{opt_alt};
326 $text = $self->reformat ($text);
327 $text =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
328 my $tagspace = ' ' x length $tag;
329 $text =~ s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item";
330 $self->output ($text);
6055f9d4 331 }
b7ae008f 332}
69e00e79 333
b7ae008f 334# Handle a basic block of text. The only tricky thing here is that if there
335# is a pending item tag, we need to format this as an item paragraph.
336sub cmd_para {
337 my ($self, $attrs, $text) = @_;
338 $text =~ s/\s+$/\n/;
339 if (defined $$self{ITEM}) {
340 $self->item ($text . "\n");
341 } else {
342 $self->output ($self->reformat ($text . "\n"));
59548eca 343 }
b7ae008f 344 return '';
6055f9d4 345}
f02a87df 346
b7ae008f 347# Handle a verbatim paragraph. Just print it out, but indent it according to
348# our margin.
349sub cmd_verbatim {
350 my ($self, $attrs, $text) = @_;
351 $self->item if defined $$self{ITEM};
352 return if $text =~ /^\s*$/;
353 $text =~ s/^(\n*)(\s*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme;
354 $text =~ s/\s*$/\n\n/;
355 $self->output ($text);
356 return '';
6055f9d4 357}
3ec07288 358
b7ae008f 359# Handle literal text (produced by =for and similar constructs). Just output
360# it with the minimum of changes.
361sub cmd_data {
362 my ($self, $attrs, $text) = @_;
363 $text =~ s/^\n+//;
364 $text =~ s/\n{0,2}$/\n/;
365 $self->output ($text);
366 return '';
367}
69e00e79 368
3c014959 369##############################################################################
b7ae008f 370# Headings
3c014959 371##############################################################################
f2506fb2 372
b7ae008f 373# The common code for handling all headers. Takes the header text, the
374# indentation, and the surrounding marker for the alt formatting method.
375sub heading {
376 my ($self, $text, $indent, $marker) = @_;
377 $self->item ("\n\n") if defined $$self{ITEM};
378 $text =~ s/\s+$//;
379 if ($$self{opt_alt}) {
380 my $closemark = reverse (split (//, $marker));
381 my $margin = ' ' x $$self{opt_margin};
382 $self->output ("\n" . "$margin$marker $text $closemark" . "\n\n");
383 } else {
384 $text .= "\n" if $$self{opt_loose};
385 my $margin = ' ' x ($$self{opt_margin} + $indent);
386 $self->output ($margin . $text . "\n");
387 }
388 return '';
389}
69e00e79 390
6055f9d4 391# First level heading.
392sub cmd_head1 {
b7ae008f 393 my ($self, $attrs, $text) = @_;
394 $self->heading ($text, 0, '====');
6055f9d4 395}
69e00e79 396
6055f9d4 397# Second level heading.
398sub cmd_head2 {
b7ae008f 399 my ($self, $attrs, $text) = @_;
400 $self->heading ($text, $$self{opt_indent} / 2, '== ');
6055f9d4 401}
69e00e79 402
50a3fd2a 403# Third level heading.
404sub cmd_head3 {
b7ae008f 405 my ($self, $attrs, $text) = @_;
406 $self->heading ($text, $$self{opt_indent} * 2 / 3 + 0.5, '= ');
50a3fd2a 407}
408
b7ae008f 409# Fourth level heading.
50a3fd2a 410sub cmd_head4 {
b7ae008f 411 my ($self, $attrs, $text) = @_;
412 $self->heading ($text, $$self{opt_indent} * 3 / 4 + 0.5, '- ');
50a3fd2a 413}
414
b7ae008f 415##############################################################################
416# List handling
417##############################################################################
418
419# Handle the beginning of an =over block. Takes the type of the block as the
420# first argument, and then the attr hash. This is called by the handlers for
421# the four different types of lists (bullet, number, text, and block).
422sub over_common_start {
423 my ($self, $attrs) = @_;
b616daaf 424 $self->item ("\n\n") if defined $$self{ITEM};
b7ae008f 425
426 # Find the indentation level.
427 my $indent = $$attrs{indent};
428 unless (defined ($indent) && $indent =~ /^\s*[-+]?\d{1,4}\s*$/) {
429 $indent = $$self{opt_indent};
430 }
431
432 # Add this to our stack of indents and increase our current margin.
6055f9d4 433 push (@{ $$self{INDENTS} }, $$self{MARGIN});
b7ae008f 434 $$self{MARGIN} += ($indent + 0);
435 return '';
6055f9d4 436}
69e00e79 437
b7ae008f 438# End an =over block. Takes no options other than the class pointer. Output
439# any pending items and then pop one level of indentation.
440sub over_common_end {
441 my ($self) = @_;
b616daaf 442 $self->item ("\n\n") if defined $$self{ITEM};
6055f9d4 443 $$self{MARGIN} = pop @{ $$self{INDENTS} };
b7ae008f 444 return '';
69e00e79 445}
446
b7ae008f 447# Dispatch the start and end calls as appropriate.
448sub start_over_bullet { $_[0]->over_common_start ($_[1]) }
449sub start_over_number { $_[0]->over_common_start ($_[1]) }
450sub start_over_text { $_[0]->over_common_start ($_[1]) }
451sub start_over_block { $_[0]->over_common_start ($_[1]) }
452sub end_over_bullet { $_[0]->over_common_end }
453sub end_over_number { $_[0]->over_common_end }
454sub end_over_text { $_[0]->over_common_end }
455sub end_over_block { $_[0]->over_common_end }
456
457# The common handler for all item commands. Takes the type of the item, the
458# attributes, and then the text of the item.
459sub item_common {
460 my ($self, $type, $attrs, $text) = @_;
461 $self->item if defined $$self{ITEM};
69e00e79 462
b7ae008f 463 # Clean up the text. We want to end up with two variables, one ($text)
464 # which contains any body text after taking out the item portion, and
465 # another ($item) which contains the actual item text. Note the use of
466 # the internal Pod::Simple attribute here; that's a potential land mine.
467 $text =~ s/\s+$//;
468 my ($item, $index);
469 if ($type eq 'bullet') {
470 $item = '*';
471 } elsif ($type eq 'number') {
472 $item = $$attrs{'~orig_content'};
27f805f4 473 } else {
b7ae008f 474 $item = $text;
475 $item =~ s/\s*\n\s*/ /g;
476 $text = '';
27f805f4 477 }
b7ae008f 478 $$self{ITEM} = $item;
6055f9d4 479
b7ae008f 480 # If body text for this item was included, go ahead and output that now.
481 if ($text) {
482 $text =~ s/\s*$/\n/;
483 $self->item ($text);
484 }
485 return '';
6055f9d4 486}
f2506fb2 487
b7ae008f 488# Dispatch the item commands to the appropriate place.
489sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) }
490sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) }
491sub cmd_item_text { my $self = shift; $self->item_common ('text', @_) }
492sub cmd_item_block { my $self = shift; $self->item_common ('block', @_) }
69e00e79 493
3c014959 494##############################################################################
5ec554fb 495# Formatting codes
3c014959 496##############################################################################
69e00e79 497
b7ae008f 498# The simple ones.
499sub cmd_b { return $_[0]{alt} ? "``$_[2]''" : $_[2] }
500sub cmd_f { return $_[0]{alt} ? "\"$_[2]\"" : $_[2] }
501sub cmd_i { return '*' . $_[2] . '*' }
502sub cmd_x { return '' }
3c014959 503
504# Apply a whole bunch of messy heuristics to not quote things that don't
505# benefit from being quoted. These originally come from Barrie Slaymaker and
506# largely duplicate code in Pod::Man.
b7ae008f 507sub cmd_c {
508 my ($self, $attrs, $text) = @_;
3c014959 509
510 # A regex that matches the portion of a variable reference that's the
511 # array or hash index, separated out just because we want to use it in
512 # several places in the following regex.
513 my $index = '(?: \[.*\] | \{.*\} )?';
514
515 # Check for things that we don't want to quote, and if we find any of
516 # them, return the string with just a font change and no quoting.
b7ae008f 517 $text =~ m{
3c014959 518 ^\s*
519 (?:
520 ( [\'\`\"] ) .* \1 # already quoted
521 | \` .* \' # `quoted'
522 | \$+ [\#^]? \S $index # special ($^Foo, $")
523 | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func
524 | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
f011ec7d 525 | [+-]? ( \d[\d.]* | \.\d+ ) (?: [eE][+-]?\d+ )? # a number
3c014959 526 | 0x [a-fA-F\d]+ # a hex constant
527 )
528 \s*\z
b7ae008f 529 }xo && return $text;
3c014959 530
531 # If we didn't return, go ahead and quote the text.
b7ae008f 532 return $$self{opt_alt}
533 ? "``$text''"
534 : "$$self{LQUOTE}$text$$self{RQUOTE}";
69e00e79 535}
536
b7ae008f 537# Links reduce to the text that we're given, wrapped in angle brackets if it's
538# a URL.
539sub cmd_l {
540 my ($self, $attrs, $text) = @_;
541 return $$attrs{type} eq 'url' ? "<$text>" : $text;
b616daaf 542}
543
3c014959 544##############################################################################
27f805f4 545# Backwards compatibility
3c014959 546##############################################################################
27f805f4 547
548# The old Pod::Text module did everything in a pod2text() function. This
549# tries to provide the same interface for legacy applications.
550sub pod2text {
551 my @args;
552
553 # This is really ugly; I hate doing option parsing in the middle of a
554 # module. But the old Pod::Text module supported passing flags to its
555 # entry function, so handle -a and -<number>.
556 while ($_[0] =~ /^-/) {
557 my $flag = shift;
558 if ($flag eq '-a') { push (@args, alt => 1) }
559 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
560 else {
561 unshift (@_, $flag);
562 last;
563 }
564 }
565
566 # Now that we know what arguments we're using, create the parser.
567 my $parser = Pod::Text->new (@args);
568
569 # If two arguments were given, the second argument is going to be a file
3c014959 570 # handle. That means we want to call parse_from_filehandle(), which means
571 # we need to turn the first argument into a file handle. Magic open will
572 # handle the <&STDIN case automagically.
27f805f4 573 if (defined $_[1]) {
ab1f1d91 574 my @fhs = @_;
27f805f4 575 local *IN;
ab1f1d91 576 unless (open (IN, $fhs[0])) {
577 croak ("Can't open $fhs[0] for reading: $!\n");
27f805f4 578 return;
579 }
ab1f1d91 580 $fhs[0] = \*IN;
8f202758 581 $parser->output_fh ($fhs[1]);
582 my $retval = $parser->parse_file ($fhs[0]);
583 my $fh = $parser->output_fh ();
584 close $fh;
585 return $retval;
27f805f4 586 } else {
b7ae008f 587 return $parser->parse_file (@_);
27f805f4 588 }
589}
590
8f202758 591# Reset the underlying Pod::Simple object between calls to parse_from_file so
592# that the same object can be reused to convert multiple pages.
593sub parse_from_file {
594 my $self = shift;
595 $self->reinit;
42ae9e1d 596
597 # Fake the old cutting option to Pod::Parser. This fiddings with internal
598 # Pod::Simple state and is quite ugly; we need a better approach.
599 if (ref ($_[0]) eq 'HASH') {
600 my $opts = shift @_;
601 if (defined ($$opts{-cutting}) && !$$opts{-cutting}) {
602 $$self{in_pod} = 1;
603 $$self{last_was_blank} = 1;
604 }
605 }
606
607 # Do the work.
8f202758 608 my $retval = $self->Pod::Simple::parse_from_file (@_);
42ae9e1d 609
610 # Flush output, since Pod::Simple doesn't do this. Ideally we should also
611 # close the file descriptor if we had to open one, but we can't easily
612 # figure this out.
8f202758 613 my $fh = $self->output_fh ();
614 my $oldfh = select $fh;
615 my $oldflush = $|;
616 $| = 1;
617 print $fh '';
618 $| = $oldflush;
619 select $oldfh;
620 return $retval;
621}
622
fcf69717 623# Pod::Simple failed to provide this backward compatibility function, so
624# implement it ourselves. File handles are one of the inputs that
625# parse_from_file supports.
626sub parse_from_filehandle {
627 my $self = shift;
628 $self->parse_from_file (@_);
629}
630
3c014959 631##############################################################################
6055f9d4 632# Module return value and documentation
3c014959 633##############################################################################
69e00e79 634
6055f9d4 6351;
636__END__
69e00e79 637
6055f9d4 638=head1 NAME
69e00e79 639
6055f9d4 640Pod::Text - Convert POD data to formatted ASCII text
69e00e79 641
0e4e3f6e 642=for stopwords
2504ae52 643alt stderr Allbery Sean Burke's Christiansen
0e4e3f6e 644
6055f9d4 645=head1 SYNOPSIS
69e00e79 646
6055f9d4 647 use Pod::Text;
648 my $parser = Pod::Text->new (sentence => 0, width => 78);
69e00e79 649
6055f9d4 650 # Read POD from STDIN and write to STDOUT.
651 $parser->parse_from_filehandle;
69e00e79 652
6055f9d4 653 # Read POD from file.pod and write to file.txt.
654 $parser->parse_from_file ('file.pod', 'file.txt');
69e00e79 655
6055f9d4 656=head1 DESCRIPTION
5491a304 657
27f805f4 658Pod::Text is a module that can convert documentation in the POD format (the
659preferred language for documenting Perl) into formatted ASCII. It uses no
660special formatting controls or codes whatsoever, and its output is therefore
661suitable for nearly any device.
69e00e79 662
b7ae008f 663As a derived class from Pod::Simple, Pod::Text supports the same methods and
664interfaces. See L<Pod::Simple> for all the details; briefly, one creates a
665new parser with C<< Pod::Text->new() >> and then normally calls parse_file().
6055f9d4 666
27f805f4 667new() can take options, in the form of key/value pairs, that control the
6055f9d4 668behavior of the parser. The currently recognized options are:
669
670=over 4
671
672=item alt
673
674If set to a true value, selects an alternate output format that, among other
675things, uses a different heading style and marks C<=item> entries with a
676colon in the left margin. Defaults to false.
677
59548eca 678=item code
679
680If set to a true value, the non-POD parts of the input file will be included
681in the output. Useful for viewing code documented with POD blocks with the
682POD rendered and the code left intact.
683
6055f9d4 684=item indent
685
686The number of spaces to indent regular text, and the default indentation for
687C<=over> blocks. Defaults to 4.
688
689=item loose
690
691If set to a true value, a blank line is printed after a C<=head1> heading.
692If set to false (the default), no blank line is printed after C<=head1>,
693although one is still printed after C<=head2>. This is the default because
694it's the expected formatting for manual pages; if you're formatting
695arbitrary text documents, setting this to true may result in more pleasing
696output.
697
11f72409 698=item margin
699
700The width of the left margin in spaces. Defaults to 0. This is the margin
701for all text, including headings, not the amount by which regular text is
702indented; for the latter, see the I<indent> option. To set the right
703margin, see the I<width> option.
704
ab1f1d91 705=item quotes
706
707Sets the quote marks used to surround CE<lt>> text. If the value is a
708single character, it is used as both the left and right quote; if it is two
709characters, the first character is used as the left quote and the second as
710the right quoted; and if it is four characters, the first two are used as
711the left quote and the second two as the right quote.
712
713This may also be set to the special value C<none>, in which case no quote
714marks are added around CE<lt>> text.
715
6055f9d4 716=item sentence
717
27f805f4 718If set to a true value, Pod::Text will assume that each sentence ends in two
719spaces, and will try to preserve that spacing. If set to false, all
6055f9d4 720consecutive whitespace in non-verbatim paragraphs is compressed into a
721single space. Defaults to true.
722
bc9c7511 723=item stderr
724
725Send error messages about invalid POD to standard error instead of
726appending a POD ERRORS section to the generated output.
727
6055f9d4 728=item width
729
730The column at which to wrap text on the right-hand side. Defaults to 76.
731
732=back
733
b7ae008f 734The standard Pod::Simple method parse_file() takes one argument, the file or
735file handle to read from, and writes output to standard output unless that
736has been changed with the output_fh() method. See L<Pod::Simple> for the
737specific details and for other alternative interfaces.
6055f9d4 738
739=head1 DIAGNOSTICS
740
741=over 4
742
27f805f4 743=item Bizarre space in item
744
59548eca 745=item Item called without tag
746
747(W) Something has gone wrong in internal C<=item> processing. These
748messages indicate a bug in Pod::Text; you should never see them.
27f805f4 749
750=item Can't open %s for reading: %s
751
752(F) Pod::Text was invoked via the compatibility mode pod2text() interface
753and the input file it was given could not be opened.
754
ab1f1d91 755=item Invalid quote specification "%s"
756
757(F) The quote specification given (the quotes option to the constructor) was
758invalid. A quote specification must be one, two, or four characters long.
759
6055f9d4 760=back
761
762=head1 NOTES
763
27f805f4 764This is a replacement for an earlier Pod::Text module written by Tom
b7ae008f 765Christiansen. It has a revamped interface, since it now uses Pod::Simple,
27f805f4 766but an interface roughly compatible with the old Pod::Text::pod2text()
767function is still available. Please change to the new calling convention,
768though.
6055f9d4 769
770The original Pod::Text contained code to do formatting via termcap
771sequences, although it wasn't turned on by default and it was problematic to
27f805f4 772get it to work at all. This rewrite doesn't even try to do that, but a
bf202ccd 773subclass of it does. Look for L<Pod::Text::Termcap>.
6055f9d4 774
775=head1 SEE ALSO
776
b7ae008f 777L<Pod::Simple>, L<Pod::Text::Termcap>, L<pod2text(1)>
6055f9d4 778
fd20da51 779The current version of this module is always available from its web site at
780L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the
781Perl core distribution as of 5.6.0.
782
6055f9d4 783=head1 AUTHOR
784
bf202ccd 785Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
786Pod::Text by Tom Christiansen <tchrist@mox.perl.com> and its conversion to
b7ae008f 787Pod::Parser by Brad Appleton <bradapp@enteract.com>. Sean Burke's initial
788conversion of Pod::Man to use Pod::Simple provided much-needed guidance on
789how to use Pod::Simple.
6055f9d4 790
3c014959 791=head1 COPYRIGHT AND LICENSE
792
0e4e3f6e 793Copyright 1999, 2000, 2001, 2002, 2004, 2006, 2008 Russ Allbery
794<rra@stanford.edu>.
3c014959 795
796This program is free software; you may redistribute it and/or modify it
797under the same terms as Perl itself.
798
6055f9d4 799=cut