5 Pod::Text - convert POD data to formatted ASCII text
11 pod2text("perlfunc.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 one or two arguments. The first
29 should be the name of a file to read the pod from, or "E<lt>&STDIN" to read from
30 STDIN. A second argument, if provided, should be a filehandle glob where
31 output should be sent.
35 Tom Christiansen E<lt>F<tchrist@mox.perl.com>E<gt>
39 Cleanup work. The input and output locations need to be more flexible,
40 termcap shouldn't be a global variable, and the terminal speed needs to
41 be properly calculated.
48 @EXPORT = qw(pod2text);
50 use vars qw($VERSION);
63 local($file,*OUTPUT) = @_;
64 *OUTPUT = *STDOUT if @_<2;
66 if($termcap and !$setuptermcap) {
69 my($term) = Tgetent Term::Cap { TERM => undef, OSPEED => 9600 };
70 $UNDL = $term->{'_us'};
71 $INV = $term->{'_mr'};
72 $BOLD = $term->{'_md'};
73 $NORM = $term->{'_me'};
76 $SCREEN = ($_[0] =~ /^-(\d+)/ && (shift, $1))
78 || ($ENV{TERMCAP} =~ /co#(\d+)/)[0]
79 || (`stty -a 2>/dev/null` =~ /(\d+) columns/)[0]
88 $indent = $DEF_INDENT;
92 open(IN, $file) || die "Couldn't open $file: $!";
94 POD_DIRECTIVE: while (<IN>) {
100 if (/^=end\s+$begun/) {
103 elsif ($begun eq "text") {
108 1 while s{^(.*?)(\t+)(.*)$}{
110 . (' ' x (length($2) * 8 - length($1) % 8))
113 # Translate verbatim paragraph
120 if (/^=for\s+(\S+)\s*(.*)/s) {
128 elsif (/^=begin\s+(\S+)\s*(.*)/s) {
136 sub prepare_for_output {
141 # need to hide E<> first; they're processed in clear_noremap
142 s/(E<[^<>]+>)/noremap($1)/ge;
144 while ($maxnest-- && /[A-Z]</) {
148 s/C<(.*?)>/noremap("E<lchevron>${1}E<rchevron>")/sge;
150 # s/[IF]<(.*?)>/italic($1)/ge;
152 # s/[CB]<(.*?)>/bold($1)/ge;
154 # LREF: a manpage(3f)
155 s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the $1$2 manpage:g;
156 # LREF: an =item on another manpage
166 } {the "$2" entry in the $1 manpage}gx;
168 # LREF: an =item on this manpage
180 } { internal_lrefs($1) }gex;
182 # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
183 # the "func" can disambiguate
193 $1 # if no $1, assume it means on this page.
194 ? "the section on \"$2\" in the $1 manpage"
195 : "the section on \"$2\""
199 s/[A-Z]<(.*?)>/$1/sg;
207 # $needspace = 0; # Assume this.
209 ($Cmd, $_) = split(' ', $_, 2);
214 elsif ($Cmd eq 'pod') {
217 elsif ($Cmd eq 'head1') {
220 # print OUTPUT uc($_);
222 elsif ($Cmd eq 'head2') {
225 #print ' ' x $DEF_INDENT, $_;
227 s/(\w)/\xA7 $1/ if $FANCY;
228 print OUTPUT ' ' x ($DEF_INDENT/2), $_, "\n";
230 elsif ($Cmd eq 'over') {
231 push(@indent,$indent);
232 $indent += ($_ + 0) || $DEF_INDENT;
234 elsif ($Cmd eq 'back') {
235 $indent = pop(@indent);
236 warn "Unmatched =back\n" unless defined $indent;
239 elsif ($Cmd eq 'item') {
241 # s/\A(\s*)\*/$1\xb7/ if $FANCY;
242 # s/^(\s*\*\s+)/$1 /;
244 if (length() + 3 < $indent) {
247 if (/^=/) { # tricked!
248 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
253 IP_output($paratag, $_);
255 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
261 warn "Unrecognized directive: $Cmd\n";
275 #########################################################################
286 return $line if $use_format;
288 $line = "$BOLD$line$NORM";
290 $line =~ s/(.)/$1\b$1/g;
292 # $line = "$BOLD$line$NORM" if $ansify;
298 return $line if $use_format;
300 $line = "$UNDL$line$NORM";
302 $line =~ s/(.)/$1\b_/g;
304 # $line = "$UNDL$line$NORM" if $ansify;
308 # Fill a paragraph including underlined and overstricken chars.
309 # It's not perfect for words longer than the margin, and it's probably
310 # slow, but it works.
314 my $indent_space = " " x $indent;
315 my $marg = $SCREEN-$indent;
316 my $line = $indent_space;
319 my $word_length = length;
320 $word_length -= 2 while /\010/g; # Subtract backspaces
322 if ($line_length + $word_length > $marg) {
323 $par .= $line . "\n";
324 $line= $indent_space . $_;
325 $line_length = $word_length;
332 $line_length += $word_length;
336 $par .= "$line\n" if $line;
342 local($tag, $_) = @_;
343 local($tag_indent) = $indent[$#index - 1] || $DEF_INDENT;
344 $tag_cols = $SCREEN - $tag_indent;
345 $cols = $SCREEN - $indent;
349 $str = "format OUTPUT = \n"
350 . (" " x ($tag_indent))
351 . '@' . ('<' x ($indent - $tag_indent - 1))
352 . "^" . ("<" x ($cols - 1)) . "\n"
355 . (" " x ($indent-2))
356 . "^" . ("<" x ($cols - 5)) . "\n"
358 #warn $str; warn "tag is $tag, _ is $_";
364 local($_, $reformat) = @_;
366 $cols = $SCREEN - $indent;
369 $str = "format OUTPUT = \n~~"
370 . (" " x ($indent-2))
371 . "^" . ("<" x ($cols - 5)) . "\n"
376 s/^/' ' x $indent/gem;
383 local($thing_to_hide) = shift;
384 $thing_to_hide =~ tr/\000-\177/\200-\377/;
385 return $thing_to_hide;
389 die "unmatched init" if $mapready++;
390 #mask off high bit characters in input stream
391 s/([\200-\377])/"E<".ord($1).">"/ge;
395 my $ready_to_print = $_[0];
396 die "unmatched clear" unless $mapready--;
397 tr/\200-\377/\000-\177/;
398 # now for the E<>s, which have been hidden until now
399 # otherwise the interative \w<> processing would have
400 # been hosed by the E<gt>
413 defined $HTML_Escapes{$3}
414 ? do { $HTML_Escapes{$3} }
416 warn "Unknown escape: E<$1> in $_";
420 }egx if $ready_to_print;
426 my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
429 for ($i = 0; $i <= $#items; $i++) {
430 $retstr .= "C<$items[$i]>";
431 $retstr .= ", " if @items > 2 && $i != $#items;
432 $retstr .= " and " if $i+2 == @items;
435 $retstr .= " entr" . ( @items > 1 ? "ies" : "y" )
436 . " elsewhere in this document ";
445 'amp' => '&', # ampersand
446 'lt' => '<', # left chevron, less-than
447 'gt' => '>', # right chevron, greater-than
448 'quot' => '"', # double quote
450 "Aacute" => "\xC1", # capital A, acute accent
451 "aacute" => "\xE1", # small a, acute accent
452 "Acirc" => "\xC2", # capital A, circumflex accent
453 "acirc" => "\xE2", # small a, circumflex accent
454 "AElig" => "\xC6", # capital AE diphthong (ligature)
455 "aelig" => "\xE6", # small ae diphthong (ligature)
456 "Agrave" => "\xC0", # capital A, grave accent
457 "agrave" => "\xE0", # small a, grave accent
458 "Aring" => "\xC5", # capital A, ring
459 "aring" => "\xE5", # small a, ring
460 "Atilde" => "\xC3", # capital A, tilde
461 "atilde" => "\xE3", # small a, tilde
462 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
463 "auml" => "\xE4", # small a, dieresis or umlaut mark
464 "Ccedil" => "\xC7", # capital C, cedilla
465 "ccedil" => "\xE7", # small c, cedilla
466 "Eacute" => "\xC9", # capital E, acute accent
467 "eacute" => "\xE9", # small e, acute accent
468 "Ecirc" => "\xCA", # capital E, circumflex accent
469 "ecirc" => "\xEA", # small e, circumflex accent
470 "Egrave" => "\xC8", # capital E, grave accent
471 "egrave" => "\xE8", # small e, grave accent
472 "ETH" => "\xD0", # capital Eth, Icelandic
473 "eth" => "\xF0", # small eth, Icelandic
474 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
475 "euml" => "\xEB", # small e, dieresis or umlaut mark
476 "Iacute" => "\xCD", # capital I, acute accent
477 "iacute" => "\xED", # small i, acute accent
478 "Icirc" => "\xCE", # capital I, circumflex accent
479 "icirc" => "\xEE", # small i, circumflex accent
480 "Igrave" => "\xCD", # capital I, grave accent
481 "igrave" => "\xED", # small i, grave accent
482 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
483 "iuml" => "\xEF", # small i, dieresis or umlaut mark
484 "Ntilde" => "\xD1", # capital N, tilde
485 "ntilde" => "\xF1", # small n, tilde
486 "Oacute" => "\xD3", # capital O, acute accent
487 "oacute" => "\xF3", # small o, acute accent
488 "Ocirc" => "\xD4", # capital O, circumflex accent
489 "ocirc" => "\xF4", # small o, circumflex accent
490 "Ograve" => "\xD2", # capital O, grave accent
491 "ograve" => "\xF2", # small o, grave accent
492 "Oslash" => "\xD8", # capital O, slash
493 "oslash" => "\xF8", # small o, slash
494 "Otilde" => "\xD5", # capital O, tilde
495 "otilde" => "\xF5", # small o, tilde
496 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
497 "ouml" => "\xF6", # small o, dieresis or umlaut mark
498 "szlig" => "\xDF", # small sharp s, German (sz ligature)
499 "THORN" => "\xDE", # capital THORN, Icelandic
500 "thorn" => "\xFE", # small thorn, Icelandic
501 "Uacute" => "\xDA", # capital U, acute accent
502 "uacute" => "\xFA", # small u, acute accent
503 "Ucirc" => "\xDB", # capital U, circumflex accent
504 "ucirc" => "\xFB", # small u, circumflex accent
505 "Ugrave" => "\xD9", # capital U, grave accent
506 "ugrave" => "\xF9", # small u, grave accent
507 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
508 "uuml" => "\xFC", # small u, dieresis or umlaut mark
509 "Yacute" => "\xDD", # capital Y, acute accent
510 "yacute" => "\xFD", # small y, acute accent
511 "yuml" => "\xFF", # small y, dieresis or umlaut mark
513 "lchevron" => "\xAB", # left chevron (double less than)
514 "rchevron" => "\xBB", # right chevron (double greater than)