5 Pod::Text - convert POD data to formatted ASCII text
11 pod2text("perlfunc.pod");
15 pod2text [B<-a>] [B<->I<width>] < input.pod
19 Pod::Text is a module that can convert documentation in the POD format (such
20 as can be found throughout the Perl distribution) into formatted ASCII.
21 Termcap is optionally supported for boldface/underline, and can enabled via
22 C<$Pod::Text::termcap=1>. If termcap has not been enabled, then backspaces
23 will be used to simulate bold and underlined text.
25 A separate F<pod2text> program is included that is primarily a wrapper for
28 The single function C<pod2text()> can take the optional options B<-a>
29 for an alternative output format, then a B<->I<width> option with the
30 max terminal width, followed by one or two arguments. The first
31 should be the name of a file to read the pod from, or "E<lt>&STDIN" to read from
32 STDIN. A second argument, if provided, should be a filehandle glob where
33 output should be sent.
37 Tom Christiansen E<lt>F<tchrist@mox.perl.com>E<gt>
41 Cleanup work. The input and output locations need to be more flexible,
42 termcap shouldn't be a global variable, and the terminal speed needs to
43 be properly calculated.
50 @EXPORT = qw(pod2text);
52 use vars qw($VERSION);
67 shift if $opt_alt_format = ($_[0] eq '-a');
69 if($termcap and !$setuptermcap) {
72 my($term) = Tgetent Term::Cap { TERM => undef, OSPEED => 9600 };
73 $UNDL = $term->{'_us'};
74 $INV = $term->{'_mr'};
75 $BOLD = $term->{'_md'};
76 $NORM = $term->{'_me'};
79 $SCREEN = ($_[0] =~ /^-(\d+)/ && (shift, $1))
81 || ($ENV{TERMCAP} =~ /co#(\d+)/)[0]
82 || (`stty -a 2>/dev/null` =~ /(\d+) columns/)[0]
85 @_ = ("<&STDIN") unless @_;
86 local($file,*OUTPUT) = @_;
87 *OUTPUT = *STDOUT if @_<2;
90 $: = " \n" if $opt_alt_format; # Do not break ``-L/lib/'' into ``- L/lib/''.
98 $indent = $DEF_INDENT;
102 open(IN, $file) || die "Couldn't open $file: $!";
104 POD_DIRECTIVE: while (<IN>) {
110 if (/^=end\s+$begun/) {
113 elsif ($begun eq "text") {
118 1 while s{^(.*?)(\t+)(.*)$}{
120 . (' ' x (length($2) * 8 - length($1) % 8))
123 # Translate verbatim paragraph
129 if (/^=for\s+(\S+)\s*(.*)/s) {
137 elsif (/^=begin\s+(\S+)\s*(.*)/s) {
145 sub prepare_for_output {
150 # need to hide E<> first; they're processed in clear_noremap
151 s/(E<[^<>]+>)/noremap($1)/ge;
153 while ($maxnest-- && /[A-Z]</) {
155 if ($opt_alt_format) {
156 s/[BC]<(.*?)>/``$1''/sg;
162 s/C<(.*?)>/noremap("E<lchevron>${1}E<rchevron>")/sge;
164 # s/[IF]<(.*?)>/italic($1)/ge;
166 # s/[CB]<(.*?)>/bold($1)/ge;
168 # LREF: a manpage(3f)
169 s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the $1$2 manpage:g;
170 # LREF: an =item on another manpage
180 } {the "$2" entry in the $1 manpage}gx;
182 # LREF: an =item on this manpage
194 } { internal_lrefs($1) }gex;
196 # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
197 # the "func" can disambiguate
207 $1 # if no $1, assume it means on this page.
208 ? "the section on \"$2\" in the $1 manpage"
209 : "the section on \"$2\""
213 s/[A-Z]<(.*?)>/$1/sg;
221 # $needspace = 0; # Assume this.
223 ($Cmd, $_) = split(' ', $_, 2);
228 elsif ($Cmd eq 'pod') {
231 elsif ($Cmd eq 'head1') {
233 if ($opt_alt_format) {
235 s/^(.+?)[ \t]*$/==== $1 ====/;
238 # print OUTPUT uc($_);
239 $needspace = $opt_alt_format;
241 elsif ($Cmd eq 'head2') {
244 #print ' ' x $DEF_INDENT, $_;
246 s/(\w)/\xA7 $1/ if $FANCY;
247 if ($opt_alt_format) {
248 s/^(.+?)[ \t]*$/== $1 ==/;
249 print OUTPUT "\n", $_;
251 print OUTPUT ' ' x ($DEF_INDENT/2), $_, "\n";
253 $needspace = $opt_alt_format;
255 elsif ($Cmd eq 'over') {
256 push(@indent,$indent);
257 $indent += ($_ + 0) || $DEF_INDENT;
259 elsif ($Cmd eq 'back') {
260 $indent = pop(@indent);
261 warn "Unmatched =back\n" unless defined $indent;
263 elsif ($Cmd eq 'item') {
265 # s/\A(\s*)\*/$1\xb7/ if $FANCY;
266 # s/^(\s*\*\s+)/$1 /;
268 if (length() + 3 < $indent) {
271 if (/^=/) { # tricked!
272 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
277 IP_output($paratag, $_);
279 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
285 warn "Unrecognized directive: $Cmd\n";
299 #########################################################################
310 return $line if $use_format;
312 $line = "$BOLD$line$NORM";
314 $line =~ s/(.)/$1\b$1/g;
316 # $line = "$BOLD$line$NORM" if $ansify;
322 return $line if $use_format;
324 $line = "$UNDL$line$NORM";
326 $line =~ s/(.)/$1\b_/g;
328 # $line = "$UNDL$line$NORM" if $ansify;
332 # Fill a paragraph including underlined and overstricken chars.
333 # It's not perfect for words longer than the margin, and it's probably
334 # slow, but it works.
338 my $indent_space = " " x $indent;
339 my $marg = $SCREEN-$indent;
340 my $line = $indent_space;
343 my $word_length = length;
344 $word_length -= 2 while /\010/g; # Subtract backspaces
346 if ($line_length + $word_length > $marg) {
347 $par .= $line . "\n";
348 $line= $indent_space . $_;
349 $line_length = $word_length;
356 $line_length += $word_length;
360 $par .= "$line\n" if $line;
366 local($tag, $_) = @_;
367 local($tag_indent) = $indent[$#index - 1] || $DEF_INDENT;
368 $tag_cols = $SCREEN - $tag_indent;
369 $cols = $SCREEN - $indent;
373 $str = "format OUTPUT = \n"
374 . (($opt_alt_format && $tag_indent > 1)
375 ? ":" . " " x ($tag_indent - 1)
376 : " " x ($tag_indent))
377 . '@' . ('<' x ($indent - $tag_indent - 1))
378 . "^" . ("<" x ($cols - 1)) . "\n"
381 . (" " x ($indent-2))
382 . "^" . ("<" x ($cols - 5)) . "\n"
384 #warn $str; warn "tag is $tag, _ is $_";
390 local($_, $reformat) = @_;
392 $cols = $SCREEN - $indent;
395 $str = "format OUTPUT = \n~~"
396 . (" " x ($indent-2))
397 . "^" . ("<" x ($cols - 5)) . "\n"
402 s/^/' ' x $indent/gem;
404 s/^ /: /s if defined($reformat) && $opt_alt_format;
410 local($thing_to_hide) = shift;
411 $thing_to_hide =~ tr/\000-\177/\200-\377/;
412 return $thing_to_hide;
416 die "unmatched init" if $mapready++;
417 #mask off high bit characters in input stream
418 s/([\200-\377])/"E<".ord($1).">"/ge;
422 my $ready_to_print = $_[0];
423 die "unmatched clear" unless $mapready--;
424 tr/\200-\377/\000-\177/;
425 # now for the E<>s, which have been hidden until now
426 # otherwise the interative \w<> processing would have
427 # been hosed by the E<gt>
440 defined $HTML_Escapes{$3}
441 ? do { $HTML_Escapes{$3} }
443 warn "Unknown escape: E<$1> in $_";
447 }egx if $ready_to_print;
453 my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
456 for ($i = 0; $i <= $#items; $i++) {
457 $retstr .= "C<$items[$i]>";
458 $retstr .= ", " if @items > 2 && $i != $#items;
459 $retstr .= " and " if $i+2 == @items;
462 $retstr .= " entr" . ( @items > 1 ? "ies" : "y" )
463 . " elsewhere in this document ";
472 'amp' => '&', # ampersand
473 'lt' => '<', # left chevron, less-than
474 'gt' => '>', # right chevron, greater-than
475 'quot' => '"', # double quote
477 "Aacute" => "\xC1", # capital A, acute accent
478 "aacute" => "\xE1", # small a, acute accent
479 "Acirc" => "\xC2", # capital A, circumflex accent
480 "acirc" => "\xE2", # small a, circumflex accent
481 "AElig" => "\xC6", # capital AE diphthong (ligature)
482 "aelig" => "\xE6", # small ae diphthong (ligature)
483 "Agrave" => "\xC0", # capital A, grave accent
484 "agrave" => "\xE0", # small a, grave accent
485 "Aring" => "\xC5", # capital A, ring
486 "aring" => "\xE5", # small a, ring
487 "Atilde" => "\xC3", # capital A, tilde
488 "atilde" => "\xE3", # small a, tilde
489 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
490 "auml" => "\xE4", # small a, dieresis or umlaut mark
491 "Ccedil" => "\xC7", # capital C, cedilla
492 "ccedil" => "\xE7", # small c, cedilla
493 "Eacute" => "\xC9", # capital E, acute accent
494 "eacute" => "\xE9", # small e, acute accent
495 "Ecirc" => "\xCA", # capital E, circumflex accent
496 "ecirc" => "\xEA", # small e, circumflex accent
497 "Egrave" => "\xC8", # capital E, grave accent
498 "egrave" => "\xE8", # small e, grave accent
499 "ETH" => "\xD0", # capital Eth, Icelandic
500 "eth" => "\xF0", # small eth, Icelandic
501 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
502 "euml" => "\xEB", # small e, dieresis or umlaut mark
503 "Iacute" => "\xCD", # capital I, acute accent
504 "iacute" => "\xED", # small i, acute accent
505 "Icirc" => "\xCE", # capital I, circumflex accent
506 "icirc" => "\xEE", # small i, circumflex accent
507 "Igrave" => "\xCD", # capital I, grave accent
508 "igrave" => "\xED", # small i, grave accent
509 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
510 "iuml" => "\xEF", # small i, dieresis or umlaut mark
511 "Ntilde" => "\xD1", # capital N, tilde
512 "ntilde" => "\xF1", # small n, tilde
513 "Oacute" => "\xD3", # capital O, acute accent
514 "oacute" => "\xF3", # small o, acute accent
515 "Ocirc" => "\xD4", # capital O, circumflex accent
516 "ocirc" => "\xF4", # small o, circumflex accent
517 "Ograve" => "\xD2", # capital O, grave accent
518 "ograve" => "\xF2", # small o, grave accent
519 "Oslash" => "\xD8", # capital O, slash
520 "oslash" => "\xF8", # small o, slash
521 "Otilde" => "\xD5", # capital O, tilde
522 "otilde" => "\xF5", # small o, tilde
523 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
524 "ouml" => "\xF6", # small o, dieresis or umlaut mark
525 "szlig" => "\xDF", # small sharp s, German (sz ligature)
526 "THORN" => "\xDE", # capital THORN, Icelandic
527 "thorn" => "\xFE", # small thorn, Icelandic
528 "Uacute" => "\xDA", # capital U, acute accent
529 "uacute" => "\xFA", # small u, acute accent
530 "Ucirc" => "\xDB", # capital U, circumflex accent
531 "ucirc" => "\xFB", # small u, circumflex accent
532 "Ugrave" => "\xD9", # capital U, grave accent
533 "ugrave" => "\xF9", # small u, grave accent
534 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
535 "uuml" => "\xFC", # small u, dieresis or umlaut mark
536 "Yacute" => "\xDD", # capital Y, acute accent
537 "yacute" => "\xFD", # small y, acute accent
538 "yuml" => "\xFF", # small y, dieresis or umlaut mark
540 "lchevron" => "\xAB", # left chevron (double less than)
541 "rchevron" => "\xBB", # right chevron (double greater than)