1 # Pod::Text -- Convert POD data to formatted ASCII text.
2 # $Id: Text.pm,v 2.3 1999/10/07 09:41:57 eagle Exp $
4 # Copyright 1999 by Russ Allbery <rra@stanford.edu>
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
9 # This module is intended to be a replacement for Pod::Text, and attempts to
10 # match its output except for some specific circumstances where other
11 # decisions seemed to produce better output. It uses Pod::Parser and is
12 # designed to be very easy to subclass.
14 ############################################################################
15 # Modules and declarations
16 ############################################################################
22 use Carp qw(carp croak);
27 use vars qw(@ISA @EXPORT %ESCAPES $VERSION);
29 # We inherit from Pod::Select instead of Pod::Parser so that we can be used
31 @ISA = qw(Pod::Select Exporter);
33 # We have to export pod2text for backward compatibility.
34 @EXPORT = qw(pod2text);
36 ($VERSION = (split (' ', q$Revision: 2.3 $ ))[1]) =~ s/\.(\d)$/.0$1/;
39 ############################################################################
40 # Table of supported E<> escapes
41 ############################################################################
43 # This table is taken near verbatim from Pod::PlainText in Pod::Parser,
44 # which got it near verbatim from the original Pod::Text. It is therefore
45 # credited to Tom Christiansen, and I'm glad I didn't have to write it. :)
47 'amp' => '&', # ampersand
48 'lt' => '<', # left chevron, less-than
49 'gt' => '>', # right chevron, greater-than
50 'quot' => '"', # double quote
52 "Aacute" => "\xC1", # capital A, acute accent
53 "aacute" => "\xE1", # small a, acute accent
54 "Acirc" => "\xC2", # capital A, circumflex accent
55 "acirc" => "\xE2", # small a, circumflex accent
56 "AElig" => "\xC6", # capital AE diphthong (ligature)
57 "aelig" => "\xE6", # small ae diphthong (ligature)
58 "Agrave" => "\xC0", # capital A, grave accent
59 "agrave" => "\xE0", # small a, grave accent
60 "Aring" => "\xC5", # capital A, ring
61 "aring" => "\xE5", # small a, ring
62 "Atilde" => "\xC3", # capital A, tilde
63 "atilde" => "\xE3", # small a, tilde
64 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
65 "auml" => "\xE4", # small a, dieresis or umlaut mark
66 "Ccedil" => "\xC7", # capital C, cedilla
67 "ccedil" => "\xE7", # small c, cedilla
68 "Eacute" => "\xC9", # capital E, acute accent
69 "eacute" => "\xE9", # small e, acute accent
70 "Ecirc" => "\xCA", # capital E, circumflex accent
71 "ecirc" => "\xEA", # small e, circumflex accent
72 "Egrave" => "\xC8", # capital E, grave accent
73 "egrave" => "\xE8", # small e, grave accent
74 "ETH" => "\xD0", # capital Eth, Icelandic
75 "eth" => "\xF0", # small eth, Icelandic
76 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
77 "euml" => "\xEB", # small e, dieresis or umlaut mark
78 "Iacute" => "\xCD", # capital I, acute accent
79 "iacute" => "\xED", # small i, acute accent
80 "Icirc" => "\xCE", # capital I, circumflex accent
81 "icirc" => "\xEE", # small i, circumflex accent
82 "Igrave" => "\xCD", # capital I, grave accent
83 "igrave" => "\xED", # small i, grave accent
84 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
85 "iuml" => "\xEF", # small i, dieresis or umlaut mark
86 "Ntilde" => "\xD1", # capital N, tilde
87 "ntilde" => "\xF1", # small n, tilde
88 "Oacute" => "\xD3", # capital O, acute accent
89 "oacute" => "\xF3", # small o, acute accent
90 "Ocirc" => "\xD4", # capital O, circumflex accent
91 "ocirc" => "\xF4", # small o, circumflex accent
92 "Ograve" => "\xD2", # capital O, grave accent
93 "ograve" => "\xF2", # small o, grave accent
94 "Oslash" => "\xD8", # capital O, slash
95 "oslash" => "\xF8", # small o, slash
96 "Otilde" => "\xD5", # capital O, tilde
97 "otilde" => "\xF5", # small o, tilde
98 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
99 "ouml" => "\xF6", # small o, dieresis or umlaut mark
100 "szlig" => "\xDF", # small sharp s, German (sz ligature)
101 "THORN" => "\xDE", # capital THORN, Icelandic
102 "thorn" => "\xFE", # small thorn, Icelandic
103 "Uacute" => "\xDA", # capital U, acute accent
104 "uacute" => "\xFA", # small u, acute accent
105 "Ucirc" => "\xDB", # capital U, circumflex accent
106 "ucirc" => "\xFB", # small u, circumflex accent
107 "Ugrave" => "\xD9", # capital U, grave accent
108 "ugrave" => "\xF9", # small u, grave accent
109 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
110 "uuml" => "\xFC", # small u, dieresis or umlaut mark
111 "Yacute" => "\xDD", # capital Y, acute accent
112 "yacute" => "\xFD", # small y, acute accent
113 "yuml" => "\xFF", # small y, dieresis or umlaut mark
115 "lchevron" => "\xAB", # left chevron (double less than)
116 "rchevron" => "\xBB", # right chevron (double greater than)
120 ############################################################################
122 ############################################################################
124 # Initialize the object. Must be sure to call our parent initializer.
128 $$self{alt} = 0 unless defined $$self{alt};
129 $$self{indent} = 4 unless defined $$self{indent};
130 $$self{loose} = 0 unless defined $$self{loose};
131 $$self{sentence} = 0 unless defined $$self{sentence};
132 $$self{width} = 76 unless defined $$self{width};
134 $$self{INDENTS} = []; # Stack of indentations.
135 $$self{MARGIN} = $$self{indent}; # Current left margin in spaces.
137 $self->SUPER::initialize;
141 ############################################################################
143 ############################################################################
145 # Called for each command paragraph. Gets the command, the associated
146 # paragraph, the line number, and a Pod::Paragraph object. Just dispatches
147 # the command to a method named the same as the command. =cut is handled
148 # internally by Pod::Parser.
152 return if $command eq 'pod';
153 return if ($$self{EXCLUDE} && $command ne 'end');
154 $self->item ("\n") if defined $$self{ITEM};
155 $command = 'cmd_' . $command;
156 $self->$command (@_);
159 # Called for a verbatim paragraph. Gets the paragraph, the line number, and
160 # a Pod::Paragraph object. Just output it verbatim, but with tabs converted
164 return if $$self{EXCLUDE};
165 $self->item if defined $$self{ITEM};
168 s/^(\s*\S+)/(' ' x $$self{MARGIN}) . $1/gme;
172 # Called for a regular text block. Gets the paragraph, the line number, and
173 # a Pod::Paragraph object. Perform interpolation and output the results.
176 return if $$self{EXCLUDE};
177 $self->output ($_[0]), return if $$self{VERBATIM};
181 # Perform a little magic to collapse multiple L<> references. This is
182 # here mostly for backwards-compatibility. We'll just rewrite the whole
183 # thing into actual text at this part, bypassing the whole internal
184 # sequence parsing thing.
187 L< # A link of the form L</something>.
190 [:\w]+ # The item has to be a simple word...
191 (\(\))? # ...or simple function.
195 ,?\s+(and\s+)? # Allow lots of them, conjuncted.
208 my @items = split /(?:,?\s+(?:and\s+)?)/;
211 for ($i = 0; $i < @items; $i++) {
212 $string .= $items[$i];
213 $string .= ", " if @items > 2 && $i != $#items;
214 $string .= " and " if ($i == $#items - 1);
216 $string .= " entries elsewhere in this document";
220 # Now actually interpolate and output the paragraph.
221 $_ = $self->interpolate ($_, $line);
223 if (defined $$self{ITEM}) {
224 $self->item ($_ . "\n");
226 $self->output ($self->reformat ($_ . "\n"));
230 # Called for an interior sequence. Gets the command, argument, and a
231 # Pod::InteriorSequence object and is expected to return the resulting text.
232 # Calls code, bold, italic, file, and link to handle those types of
233 # sequences, and handles S<>, E<>, X<>, and Z<> directly.
234 sub interior_sequence {
238 return '' if ($command eq 'X' || $command eq 'Z');
240 # Expand escapes into the actual character now, carping if invalid.
241 if ($command eq 'E') {
245 return $ESCAPES{$_} if defined $ESCAPES{$_};
246 carp "Unknown escape: E<$_>";
251 # For all the other sequences, empty content produces no output.
254 # For S<>, compress all internal whitespace and then map spaces to \01.
255 # When we output the text, we'll map this back.
256 if ($command eq 'S') {
262 # Anything else needs to get dispatched to another method.
263 if ($command eq 'B') { return $self->seq_b ($_) }
264 elsif ($command eq 'C') { return $self->seq_c ($_) }
265 elsif ($command eq 'F') { return $self->seq_f ($_) }
266 elsif ($command eq 'I') { return $self->seq_i ($_) }
267 elsif ($command eq 'L') { return $self->seq_l ($_) }
268 else { carp "Unknown sequence $command<$_>" }
271 # Called for each paragraph that's actually part of the POD. We take
272 # advantage of this opportunity to untabify the input.
273 sub preprocess_paragraph {
276 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
281 ############################################################################
283 ############################################################################
285 # All command paragraphs take the paragraph and the line number.
287 # First level heading.
292 $_ = $self->interpolate ($_, shift);
294 $self->output ("\n==== $_ ====\n\n");
296 $_ .= "\n" if $$self{loose};
297 $self->output ($_ . "\n");
301 # Second level heading.
306 $_ = $self->interpolate ($_, shift);
308 $self->output ("\n== $_ ==\n\n");
310 $self->output (' ' x ($$self{indent} / 2) . $_ . "\n\n");
318 unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
319 push (@{ $$self{INDENTS} }, $$self{MARGIN});
320 $$self{MARGIN} += ($_ + 0);
326 $$self{MARGIN} = pop @{ $$self{INDENTS} };
327 unless (defined $$self{MARGIN}) {
328 carp "Unmatched =back";
329 $$self{MARGIN} = $$self{indent};
333 # An individual list item.
336 if (defined $$self{ITEM}) { $self->item }
339 $$self{ITEM} = $self->interpolate ($_);
342 # Begin a block for a particular translator. Setting VERBATIM triggers
343 # special handling in textblock().
347 my ($kind) = /^(\S+)/ or return;
348 if ($kind eq 'text') {
349 $$self{VERBATIM} = 1;
355 # End a block for a particular translator. We assume that all =begin/=end
356 # pairs are properly closed.
360 $$self{VERBATIM} = 0;
363 # One paragraph for a particular translator. Ignore it unless it's intended
364 # for text, in which case we treat it as a verbatim text block.
369 return unless s/^text\b[ \t]*\n?//;
370 $self->verbatim ($_, $line);
374 ############################################################################
376 ############################################################################
378 # The simple formatting ones. These are here mostly so that subclasses can
379 # override them and do more complicated things.
380 sub seq_b { return $_[0]{alt} ? "``$_[1]''" : $_[1] }
381 sub seq_c { return $_[0]{alt} ? "``$_[1]''" : "`$_[1]'" }
382 sub seq_f { return $_[0]{alt} ? "\"$_[1]\"" : $_[1] }
383 sub seq_i { return '*' . $_[1] . '*' }
385 # The complicated one. Handle links. Since this is plain text, we can't
386 # actually make any real links, so this is all to figure out what text we
392 # Smash whitespace in case we were split across multiple lines.
395 # If we were given any explicit text, just output it.
396 if (/^([^|]+)\|/) { return $1 }
398 # Okay, leading and trailing whitespace isn't important; get rid of it.
402 # Default to using the whole content of the link entry as a section
403 # name. Note that L<manpage/> forces a manpage interpretation, as does
404 # something looking like L<manpage(section)>. The latter is an
405 # enhancement over the original Pod::Text.
406 my ($manpage, $section) = ('', $_);
407 if (/^"\s*(.*?)\s*"$/) {
408 $section = '"' . $1 . '"';
409 } elsif (m/^[-:.\w]+(?:\(\S+\))?$/) {
410 ($manpage, $section) = ($_, '');
412 ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
415 # Now build the actual output text.
417 if (!length $section) {
418 $text = "the $manpage manpage" if length $manpage;
419 } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
420 $text .= 'the ' . $section . ' entry';
421 $text .= (length $manpage) ? " in the $manpage manpage"
422 : " elsewhere in this document";
424 $section =~ s/^\"\s*//;
425 $section =~ s/\s*\"$//;
426 $text .= 'the section on "' . $section . '"';
427 $text .= " in the $manpage manpage" if length $manpage;
433 ############################################################################
435 ############################################################################
437 # This method is called whenever an =item command is complete (in other
438 # words, we've seen its associated paragraph or know for certain that it
439 # doesn't have one). It gets the paragraph associated with the item as an
440 # argument. If that argument is empty, just output the item tag; if it
441 # contains a newline, output the item tag followed by the newline.
442 # Otherwise, see if there's enough room for us to output the item tag in the
443 # margin of the text or if we have to put it on a separate line.
447 my $tag = $$self{ITEM};
448 unless (defined $tag) {
449 carp "item called without tag";
453 my $indent = $$self{INDENTS}[-1];
454 unless (defined $indent) { $indent = $$self{indent} }
455 my $space = ' ' x $indent;
456 $space =~ s/^ /:/ if $$self{alt};
457 if (!$_ || /^\s+$/ || ($$self{MARGIN} - $indent < length ($tag) + 1)) {
458 my $margin = $$self{MARGIN};
459 $$self{MARGIN} = $indent;
460 my $output = $self->reformat ($tag);
461 $output =~ s/\n*$/\n/;
462 $self->output ($output);
463 $$self{MARGIN} = $margin;
464 $self->output ($self->reformat ($_)) if /\S/;
466 $_ = $self->reformat ($_);
467 s/^ /:/ if ($$self{alt} && $indent > 0);
468 my $tagspace = ' ' x length $tag;
469 s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item";
475 ############################################################################
477 ############################################################################
479 # Wrap a line, indenting by the current left margin. We can't use
480 # Text::Wrap because it plays games with tabs. We can't use formline, even
481 # though we'd really like to, because it screws up non-printing characters.
482 # So we have to do the wrapping ourselves.
487 my $spaces = ' ' x $$self{MARGIN};
488 my $width = $$self{width} - $$self{MARGIN};
489 while (length > $width) {
490 if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) {
491 $output .= $spaces . $1 . "\n";
496 $output .= $spaces . $_;
497 $output =~ s/\s+$/\n\n/;
501 # Reformat a paragraph of text for the current margin. Takes the text to
502 # reformat and returns the formatted text.
507 # If we're trying to preserve two spaces after sentences, do some
508 # munging to support that. Otherwise, smash all repeated whitespace.
509 if ($$self{sentence}) {
520 # Output text to the output device.
521 sub output { $_[1] =~ tr/\01/ /; print { $_[0]->output_handle } $_[1] }
524 ############################################################################
525 # Backwards compatibility
526 ############################################################################
528 # The old Pod::Text module did everything in a pod2text() function. This
529 # tries to provide the same interface for legacy applications.
533 # This is really ugly; I hate doing option parsing in the middle of a
534 # module. But the old Pod::Text module supported passing flags to its
535 # entry function, so handle -a and -<number>.
536 while ($_[0] =~ /^-/) {
538 if ($flag eq '-a') { push (@args, alt => 1) }
539 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
546 # Now that we know what arguments we're using, create the parser.
547 my $parser = Pod::Text->new (@args);
549 # If two arguments were given, the second argument is going to be a file
550 # handle. That means we want to call parse_from_filehandle(), which
551 # means we need to turn the first argument into a file handle. Magic
552 # open will handle the <&STDIN case automagically.
555 unless (open (IN, $_[0])) {
556 croak ("Can't open $_[0] for reading: $!\n");
560 return $parser->parse_from_filehandle (@_);
562 return $parser->parse_from_file (@_);
567 ############################################################################
568 # Module return value and documentation
569 ############################################################################
576 Pod::Text - Convert POD data to formatted ASCII text
581 my $parser = Pod::Text->new (sentence => 0, width => 78);
583 # Read POD from STDIN and write to STDOUT.
584 $parser->parse_from_filehandle;
586 # Read POD from file.pod and write to file.txt.
587 $parser->parse_from_file ('file.pod', 'file.txt');
591 Pod::Text is a module that can convert documentation in the POD format (the
592 preferred language for documenting Perl) into formatted ASCII. It uses no
593 special formatting controls or codes whatsoever, and its output is therefore
594 suitable for nearly any device.
596 As a derived class from Pod::Parser, Pod::Text supports the same methods and
597 interfaces. See L<Pod::Parser> for all the details; briefly, one creates a
598 new parser with C<Pod::Text-E<gt>new()> and then calls either
599 parse_from_filehandle() or parse_from_file().
601 new() can take options, in the form of key/value pairs, that control the
602 behavior of the parser. The currently recognized options are:
608 If set to a true value, selects an alternate output format that, among other
609 things, uses a different heading style and marks C<=item> entries with a
610 colon in the left margin. Defaults to false.
614 The number of spaces to indent regular text, and the default indentation for
615 C<=over> blocks. Defaults to 4.
619 If set to a true value, a blank line is printed after a C<=head1> heading.
620 If set to false (the default), no blank line is printed after C<=head1>,
621 although one is still printed after C<=head2>. This is the default because
622 it's the expected formatting for manual pages; if you're formatting
623 arbitrary text documents, setting this to true may result in more pleasing
628 If set to a true value, Pod::Text will assume that each sentence ends in two
629 spaces, and will try to preserve that spacing. If set to false, all
630 consecutive whitespace in non-verbatim paragraphs is compressed into a
631 single space. Defaults to true.
635 The column at which to wrap text on the right-hand side. Defaults to 76.
639 The standard Pod::Parser method parse_from_filehandle() takes up to two
640 arguments, the first being the file handle to read POD from and the second
641 being the file handle to write the formatted output to. The first defaults
642 to STDIN if not given, and the second defaults to STDOUT. The method
643 parse_from_file() is almost identical, except that its two arguments are the
644 input and output disk files instead. See L<Pod::Parser> for the specific
651 =item Bizarre space in item
653 (W) Something has gone wrong in internal C<=item> processing. This message
654 indicates a bug in Pod::Text; you should never see it.
656 =item Can't open %s for reading: %s
658 (F) Pod::Text was invoked via the compatibility mode pod2text() interface
659 and the input file it was given could not be opened.
661 =item Unknown escape: %s
663 (W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Text didn't
666 =item Unknown sequence: %s
668 (W) The POD source contained a non-standard internal sequence (something of
669 the form C<XE<lt>E<gt>>) that Pod::Text didn't know about.
671 =item Unmatched =back
673 (W) Pod::Text encountered a C<=back> command that didn't correspond to an
680 Embedded Ctrl-As (octal 001) in the input will be mapped to spaces on
681 output, due to an internal implementation detail.
685 This is a replacement for an earlier Pod::Text module written by Tom
686 Christiansen. It has a revamped interface, since it now uses Pod::Parser,
687 but an interface roughly compatible with the old Pod::Text::pod2text()
688 function is still available. Please change to the new calling convention,
691 The original Pod::Text contained code to do formatting via termcap
692 sequences, although it wasn't turned on by default and it was problematic to
693 get it to work at all. This rewrite doesn't even try to do that, but a
694 subclass of it does. Look for L<Pod::Text::Termcap|Pod::Text::Termcap>.
698 L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>,
703 Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
704 original Pod::Text by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt> and
705 its conversion to Pod::Parser by Brad Appleton
706 E<lt>bradapp@enteract.comE<gt>.