7 Pod::Text - convert POD data to formatted ASCII text
13 pod2text("perlfunc.pod");
21 Pod::Text is a module that can convert documentation in the POD format (such
22 as can be found throughout the Perl distribution) into formatted ASCII.
23 Termcap is optionally supported for boldface/underline, and can enabled via
24 C<$Pod::Text::termcap=1>. If termcap has not been enabled, then backspaces
25 will be used to simulate bold and underlined text.
27 A separate F<pod2text> program is included that is primarily a wrapper for
30 The single function C<pod2text()> can take 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);
62 local($file,*OUTPUT) = @_;
63 *OUTPUT = *STDOUT if @_<2;
65 if($termcap and !$setuptermcap) {
68 my($term) = Tgetent Term::Cap { TERM => undef, OSPEED => 9600 };
69 $UNDL = $term->{'_us'};
70 $INV = $term->{'_mr'};
71 $BOLD = $term->{'_md'};
72 $NORM = $term->{'_me'};
75 $SCREEN = ($_[0] =~ /^-(\d+)/ && (shift, $1))
77 || ($ENV{TERMCAP} =~ /co#(\d+)/)[0]
78 || (`stty -a 2>/dev/null` =~ /(\d+) columns/)[0]
87 $indent = $DEF_INDENT;
91 open(IN, $file) || die "Couldn't open $file: $!";
93 POD_DIRECTIVE: while (<IN>) {
99 if (/^=end\s+$begun/) {
102 elsif ($begun eq "text") {
107 1 while s{^(.*?)(\t+)(.*)$}{
109 . (' ' x (length($2) * 8 - length($1) % 8))
112 # Translate verbatim paragraph
119 if (/^=for\s+(\S+)\s*/s) {
127 elsif (/^=begin\s+(\S+)\s*/s) {
135 sub prepare_for_output {
140 # need to hide E<> first; they're processed in clear_noremap
141 s/(E<[^<>]+>)/noremap($1)/ge;
143 while ($maxnest-- && /[A-Z]</) {
147 s/C<(.*?)>/noremap("E<lchevron>${1}E<rchevron>")/sge;
149 # s/[IF]<(.*?)>/italic($1)/ge;
151 # s/[CB]<(.*?)>/bold($1)/ge;
153 # LREF: a manpage(3f)
154 s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the $1$2 manpage:g;
155 # LREF: an =item on another manpage
165 } {the "$2" entry in the $1 manpage}gx;
167 # LREF: an =item on this manpage
179 } { internal_lrefs($1) }gex;
181 # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
182 # the "func" can disambiguate
192 $1 # if no $1, assume it means on this page.
193 ? "the section on \"$2\" in the $1 manpage"
194 : "the section on \"$2\""
198 s/[A-Z]<(.*?)>/$1/sg;
206 # $needspace = 0; # Assume this.
208 ($Cmd, $_) = split(' ', $_, 2);
213 elsif ($Cmd eq 'head1') {
216 # print OUTPUT uc($_);
218 elsif ($Cmd eq 'head2') {
221 #print ' ' x $DEF_INDENT, $_;
223 s/(\w)/\xA7 $1/ if $FANCY;
224 print OUTPUT ' ' x ($DEF_INDENT/2), $_, "\n";
226 elsif ($Cmd eq 'over') {
227 push(@indent,$indent);
228 $indent += ($_ + 0) || $DEF_INDENT;
230 elsif ($Cmd eq 'back') {
231 $indent = pop(@indent);
232 warn "Unmatched =back\n" unless defined $indent;
235 elsif ($Cmd eq 'item') {
237 # s/\A(\s*)\*/$1\xb7/ if $FANCY;
238 # s/^(\s*\*\s+)/$1 /;
240 if (length() + 3 < $indent) {
243 if (/^=/) { # tricked!
244 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
249 IP_output($paratag, $_);
251 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
257 warn "Unrecognized directive: $Cmd\n";
271 #########################################################################
282 return $line if $use_format;
284 $line = "$BOLD$line$NORM";
286 $line =~ s/(.)/$1\b$1/g;
288 # $line = "$BOLD$line$NORM" if $ansify;
294 return $line if $use_format;
296 $line = "$UNDL$line$NORM";
298 $line =~ s/(.)/$1\b_/g;
300 # $line = "$UNDL$line$NORM" if $ansify;
304 # Fill a paragraph including underlined and overstricken chars.
305 # It's not perfect for words longer than the margin, and it's probably
306 # slow, but it works.
310 my $indent_space = " " x $indent;
311 my $marg = $SCREEN-$indent;
312 my $line = $indent_space;
315 my $word_length = length;
316 $word_length -= 2 while /\010/g; # Subtract backspaces
318 if ($line_length + $word_length > $marg) {
319 $par .= $line . "\n";
320 $line= $indent_space . $_;
321 $line_length = $word_length;
328 $line_length += $word_length;
332 $par .= "$line\n" if $line;
338 local($tag, $_) = @_;
339 local($tag_indent) = $indent[$#index - 1] || $DEF_INDENT;
340 $tag_cols = $SCREEN - $tag_indent;
341 $cols = $SCREEN - $indent;
345 $str = "format OUTPUT = \n"
346 . (" " x ($tag_indent))
347 . '@' . ('<' x ($indent - $tag_indent - 1))
348 . "^" . ("<" x ($cols - 1)) . "\n"
351 . (" " x ($indent-2))
352 . "^" . ("<" x ($cols - 5)) . "\n"
354 #warn $str; warn "tag is $tag, _ is $_";
360 local($_, $reformat) = @_;
362 $cols = $SCREEN - $indent;
365 $str = "format OUTPUT = \n~~"
366 . (" " x ($indent-2))
367 . "^" . ("<" x ($cols - 5)) . "\n"
372 s/^/' ' x $indent/gem;
379 local($thing_to_hide) = shift;
380 $thing_to_hide =~ tr/\000-\177/\200-\377/;
381 return $thing_to_hide;
385 die "unmatched init" if $mapready++;
386 #mask off high bit characters in input stream
387 s/([\200-\377])/"E<".ord($1).">"/ge;
391 my $ready_to_print = $_[0];
392 die "unmatched clear" unless $mapready--;
393 tr/\200-\377/\000-\177/;
394 # now for the E<>s, which have been hidden until now
395 # otherwise the interative \w<> processing would have
396 # been hosed by the E<gt>
409 defined $HTML_Escapes{$3}
410 ? do { $HTML_Escapes{$3} }
412 warn "Unknown escape: $& in $_";
416 }egx if $ready_to_print;
422 my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
425 for ($i = 0; $i <= $#items; $i++) {
426 $retstr .= "C<$items[$i]>";
427 $retstr .= ", " if @items > 2 && $i != $#items;
428 $retstr .= " and " if $i+2 == @items;
431 $retstr .= " entr" . ( @items > 1 ? "ies" : "y" )
432 . " elsewhere in this document ";
441 'amp' => '&', # ampersand
442 'lt' => '<', # left chevron, less-than
443 'gt' => '>', # right chevron, greater-than
444 'quot' => '"', # double quote
446 "Aacute" => "\xC1", # capital A, acute accent
447 "aacute" => "\xE1", # small a, acute accent
448 "Acirc" => "\xC2", # capital A, circumflex accent
449 "acirc" => "\xE2", # small a, circumflex accent
450 "AElig" => "\xC6", # capital AE diphthong (ligature)
451 "aelig" => "\xE6", # small ae diphthong (ligature)
452 "Agrave" => "\xC0", # capital A, grave accent
453 "agrave" => "\xE0", # small a, grave accent
454 "Aring" => "\xC5", # capital A, ring
455 "aring" => "\xE5", # small a, ring
456 "Atilde" => "\xC3", # capital A, tilde
457 "atilde" => "\xE3", # small a, tilde
458 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
459 "auml" => "\xE4", # small a, dieresis or umlaut mark
460 "Ccedil" => "\xC7", # capital C, cedilla
461 "ccedil" => "\xE7", # small c, cedilla
462 "Eacute" => "\xC9", # capital E, acute accent
463 "eacute" => "\xE9", # small e, acute accent
464 "Ecirc" => "\xCA", # capital E, circumflex accent
465 "ecirc" => "\xEA", # small e, circumflex accent
466 "Egrave" => "\xC8", # capital E, grave accent
467 "egrave" => "\xE8", # small e, grave accent
468 "ETH" => "\xD0", # capital Eth, Icelandic
469 "eth" => "\xF0", # small eth, Icelandic
470 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
471 "euml" => "\xEB", # small e, dieresis or umlaut mark
472 "Iacute" => "\xCD", # capital I, acute accent
473 "iacute" => "\xED", # small i, acute accent
474 "Icirc" => "\xCE", # capital I, circumflex accent
475 "icirc" => "\xEE", # small i, circumflex accent
476 "Igrave" => "\xCD", # capital I, grave accent
477 "igrave" => "\xED", # small i, grave accent
478 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
479 "iuml" => "\xEF", # small i, dieresis or umlaut mark
480 "Ntilde" => "\xD1", # capital N, tilde
481 "ntilde" => "\xF1", # small n, tilde
482 "Oacute" => "\xD3", # capital O, acute accent
483 "oacute" => "\xF3", # small o, acute accent
484 "Ocirc" => "\xD4", # capital O, circumflex accent
485 "ocirc" => "\xF4", # small o, circumflex accent
486 "Ograve" => "\xD2", # capital O, grave accent
487 "ograve" => "\xF2", # small o, grave accent
488 "Oslash" => "\xD8", # capital O, slash
489 "oslash" => "\xF8", # small o, slash
490 "Otilde" => "\xD5", # capital O, tilde
491 "otilde" => "\xF5", # small o, tilde
492 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
493 "ouml" => "\xF6", # small o, dieresis or umlaut mark
494 "szlig" => "\xDF", # small sharp s, German (sz ligature)
495 "THORN" => "\xDE", # capital THORN, Icelandic
496 "thorn" => "\xFE", # small thorn, Icelandic
497 "Uacute" => "\xDA", # capital U, acute accent
498 "uacute" => "\xFA", # small u, acute accent
499 "Ucirc" => "\xDB", # capital U, circumflex accent
500 "ucirc" => "\xFB", # small u, circumflex accent
501 "Ugrave" => "\xD9", # capital U, grave accent
502 "ugrave" => "\xF9", # small u, grave accent
503 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
504 "uuml" => "\xFC", # small u, dieresis or umlaut mark
505 "Yacute" => "\xDD", # capital Y, acute accent
506 "yacute" => "\xFD", # small y, acute accent
507 "yuml" => "\xFF", # small y, dieresis or umlaut mark
509 "lchevron" => "\xAB", # left chevron (double less than)
510 "rchevron" => "\xBB", # right chevron (double greater than)