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
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 'pod') {
216 elsif ($Cmd eq 'head1') {
219 # print OUTPUT uc($_);
221 elsif ($Cmd eq 'head2') {
224 #print ' ' x $DEF_INDENT, $_;
226 s/(\w)/\xA7 $1/ if $FANCY;
227 print OUTPUT ' ' x ($DEF_INDENT/2), $_, "\n";
229 elsif ($Cmd eq 'over') {
230 push(@indent,$indent);
231 $indent += ($_ + 0) || $DEF_INDENT;
233 elsif ($Cmd eq 'back') {
234 $indent = pop(@indent);
235 warn "Unmatched =back\n" unless defined $indent;
238 elsif ($Cmd eq 'item') {
240 # s/\A(\s*)\*/$1\xb7/ if $FANCY;
241 # s/^(\s*\*\s+)/$1 /;
243 if (length() + 3 < $indent) {
246 if (/^=/) { # tricked!
247 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
252 IP_output($paratag, $_);
254 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
260 warn "Unrecognized directive: $Cmd\n";
274 #########################################################################
285 return $line if $use_format;
287 $line = "$BOLD$line$NORM";
289 $line =~ s/(.)/$1\b$1/g;
291 # $line = "$BOLD$line$NORM" if $ansify;
297 return $line if $use_format;
299 $line = "$UNDL$line$NORM";
301 $line =~ s/(.)/$1\b_/g;
303 # $line = "$UNDL$line$NORM" if $ansify;
307 # Fill a paragraph including underlined and overstricken chars.
308 # It's not perfect for words longer than the margin, and it's probably
309 # slow, but it works.
313 my $indent_space = " " x $indent;
314 my $marg = $SCREEN-$indent;
315 my $line = $indent_space;
318 my $word_length = length;
319 $word_length -= 2 while /\010/g; # Subtract backspaces
321 if ($line_length + $word_length > $marg) {
322 $par .= $line . "\n";
323 $line= $indent_space . $_;
324 $line_length = $word_length;
331 $line_length += $word_length;
335 $par .= "$line\n" if $line;
341 local($tag, $_) = @_;
342 local($tag_indent) = $indent[$#index - 1] || $DEF_INDENT;
343 $tag_cols = $SCREEN - $tag_indent;
344 $cols = $SCREEN - $indent;
348 $str = "format OUTPUT = \n"
349 . (" " x ($tag_indent))
350 . '@' . ('<' x ($indent - $tag_indent - 1))
351 . "^" . ("<" x ($cols - 1)) . "\n"
354 . (" " x ($indent-2))
355 . "^" . ("<" x ($cols - 5)) . "\n"
357 #warn $str; warn "tag is $tag, _ is $_";
363 local($_, $reformat) = @_;
365 $cols = $SCREEN - $indent;
368 $str = "format OUTPUT = \n~~"
369 . (" " x ($indent-2))
370 . "^" . ("<" x ($cols - 5)) . "\n"
375 s/^/' ' x $indent/gem;
382 local($thing_to_hide) = shift;
383 $thing_to_hide =~ tr/\000-\177/\200-\377/;
384 return $thing_to_hide;
388 die "unmatched init" if $mapready++;
389 #mask off high bit characters in input stream
390 s/([\200-\377])/"E<".ord($1).">"/ge;
394 my $ready_to_print = $_[0];
395 die "unmatched clear" unless $mapready--;
396 tr/\200-\377/\000-\177/;
397 # now for the E<>s, which have been hidden until now
398 # otherwise the interative \w<> processing would have
399 # been hosed by the E<gt>
412 defined $HTML_Escapes{$3}
413 ? do { $HTML_Escapes{$3} }
415 warn "Unknown escape: E<$1> in $_";
419 }egx if $ready_to_print;
425 my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
428 for ($i = 0; $i <= $#items; $i++) {
429 $retstr .= "C<$items[$i]>";
430 $retstr .= ", " if @items > 2 && $i != $#items;
431 $retstr .= " and " if $i+2 == @items;
434 $retstr .= " entr" . ( @items > 1 ? "ies" : "y" )
435 . " elsewhere in this document ";
444 'amp' => '&', # ampersand
445 'lt' => '<', # left chevron, less-than
446 'gt' => '>', # right chevron, greater-than
447 'quot' => '"', # double quote
449 "Aacute" => "\xC1", # capital A, acute accent
450 "aacute" => "\xE1", # small a, acute accent
451 "Acirc" => "\xC2", # capital A, circumflex accent
452 "acirc" => "\xE2", # small a, circumflex accent
453 "AElig" => "\xC6", # capital AE diphthong (ligature)
454 "aelig" => "\xE6", # small ae diphthong (ligature)
455 "Agrave" => "\xC0", # capital A, grave accent
456 "agrave" => "\xE0", # small a, grave accent
457 "Aring" => "\xC5", # capital A, ring
458 "aring" => "\xE5", # small a, ring
459 "Atilde" => "\xC3", # capital A, tilde
460 "atilde" => "\xE3", # small a, tilde
461 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
462 "auml" => "\xE4", # small a, dieresis or umlaut mark
463 "Ccedil" => "\xC7", # capital C, cedilla
464 "ccedil" => "\xE7", # small c, cedilla
465 "Eacute" => "\xC9", # capital E, acute accent
466 "eacute" => "\xE9", # small e, acute accent
467 "Ecirc" => "\xCA", # capital E, circumflex accent
468 "ecirc" => "\xEA", # small e, circumflex accent
469 "Egrave" => "\xC8", # capital E, grave accent
470 "egrave" => "\xE8", # small e, grave accent
471 "ETH" => "\xD0", # capital Eth, Icelandic
472 "eth" => "\xF0", # small eth, Icelandic
473 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
474 "euml" => "\xEB", # small e, dieresis or umlaut mark
475 "Iacute" => "\xCD", # capital I, acute accent
476 "iacute" => "\xED", # small i, acute accent
477 "Icirc" => "\xCE", # capital I, circumflex accent
478 "icirc" => "\xEE", # small i, circumflex accent
479 "Igrave" => "\xCD", # capital I, grave accent
480 "igrave" => "\xED", # small i, grave accent
481 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
482 "iuml" => "\xEF", # small i, dieresis or umlaut mark
483 "Ntilde" => "\xD1", # capital N, tilde
484 "ntilde" => "\xF1", # small n, tilde
485 "Oacute" => "\xD3", # capital O, acute accent
486 "oacute" => "\xF3", # small o, acute accent
487 "Ocirc" => "\xD4", # capital O, circumflex accent
488 "ocirc" => "\xF4", # small o, circumflex accent
489 "Ograve" => "\xD2", # capital O, grave accent
490 "ograve" => "\xF2", # small o, grave accent
491 "Oslash" => "\xD8", # capital O, slash
492 "oslash" => "\xF8", # small o, slash
493 "Otilde" => "\xD5", # capital O, tilde
494 "otilde" => "\xF5", # small o, tilde
495 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
496 "ouml" => "\xF6", # small o, dieresis or umlaut mark
497 "szlig" => "\xDF", # small sharp s, German (sz ligature)
498 "THORN" => "\xDE", # capital THORN, Icelandic
499 "thorn" => "\xFE", # small thorn, Icelandic
500 "Uacute" => "\xDA", # capital U, acute accent
501 "uacute" => "\xFA", # small u, acute accent
502 "Ucirc" => "\xDB", # capital U, circumflex accent
503 "ucirc" => "\xFB", # small u, circumflex accent
504 "Ugrave" => "\xD9", # capital U, grave accent
505 "ugrave" => "\xF9", # small u, grave accent
506 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
507 "uuml" => "\xFC", # small u, dieresis or umlaut mark
508 "Yacute" => "\xDD", # capital Y, acute accent
509 "yacute" => "\xFD", # small y, acute accent
510 "yuml" => "\xFF", # small y, dieresis or umlaut mark
512 "lchevron" => "\xAB", # left chevron (double less than)
513 "rchevron" => "\xBB", # right chevron (double greater than)