Fix fd leak in IO::Pipe
[p5sagit/p5-mst-13.2.git] / lib / Pod / Text.pm
CommitLineData
69e00e79 1package Pod::Text;
2
69e00e79 3=head1 NAME
4
5Pod::Text - convert POD data to formatted ASCII text
6
7=head1 SYNOPSIS
8
9 use Pod::Text;
10
11 pod2text("perlfunc.pod");
12
13Also:
14
15 pod2text < input.pod
16
17=head1 DESCRIPTION
18
19Pod::Text is a module that can convert documentation in the POD format (such
20as can be found throughout the Perl distribution) into formatted ASCII.
21Termcap is optionally supported for boldface/underline, and can enabled via
22C<$Pod::Text::termcap=1>. If termcap has not been enabled, then backspaces
23will be used to simulate bold and underlined text.
24
25A separate F<pod2text> program is included that is primarily a wrapper for
26Pod::Text.
27
28The single function C<pod2text()> can take one or two arguments. The first
1fef88e7 29should be the name of a file to read the pod from, or "E<lt>&STDIN" to read from
69e00e79 30STDIN. A second argument, if provided, should be a filehandle glob where
31output should be sent.
32
33=head1 AUTHOR
34
1fef88e7 35Tom Christiansen E<lt>F<tchrist@mox.perl.com>E<gt>
69e00e79 36
37=head1 TODO
38
39Cleanup work. The input and output locations need to be more flexible,
40termcap shouldn't be a global variable, and the terminal speed needs to
41be properly calculated.
42
43=cut
44
45use Term::Cap;
46require Exporter;
47@ISA = Exporter;
48@EXPORT = qw(pod2text);
49
f02a87df 50use vars qw($VERSION);
51$VERSION = "1.0201";
52
69e00e79 53$termcap=0;
54
55#$use_format=1;
56
57$UNDL = "\x1b[4m";
58$INV = "\x1b[7m";
59$BOLD = "\x1b[1m";
60$NORM = "\x1b[0m";
61
62sub pod2text {
63local($file,*OUTPUT) = @_;
64*OUTPUT = *STDOUT if @_<2;
65
66if($termcap and !$setuptermcap) {
67 $setuptermcap=1;
68
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'};
74}
75
76$SCREEN = ($_[0] =~ /^-(\d+)/ && (shift, $1))
69e00e79 77 || $ENV{COLUMNS}
36477c24 78 || ($ENV{TERMCAP} =~ /co#(\d+)/)[0]
69e00e79 79 || (`stty -a 2>/dev/null` =~ /(\d+) columns/)[0]
80 || 72;
81
82$/ = "";
83
84$FANCY = 0;
85
86$cutting = 1;
87$DEF_INDENT = 4;
88$indent = $DEF_INDENT;
89$needspace = 0;
8c634b6e 90$begun = "";
69e00e79 91
2e917f57 92open(IN, $file) || die "Couldn't open $file: $!";
69e00e79 93
94POD_DIRECTIVE: while (<IN>) {
95 if ($cutting) {
96 next unless /^=/;
97 $cutting = 0;
98 }
8c634b6e 99 if ($begun) {
100 if (/^=end\s+$begun/) {
101 $begun = "";
102 }
103 elsif ($begun eq "text") {
104 print STDOUT $_;
105 }
106 next;
107 }
69e00e79 108 1 while s{^(.*?)(\t+)(.*)$}{
109 $1
110 . (' ' x (length($2) * 8 - length($1) % 8))
111 . $3
112 }me;
113 # Translate verbatim paragraph
114 if (/^\s/) {
115 $needspace = 1;
116 output($_);
117 next;
118 }
119
f02a87df 120 if (/^=for\s+(\S+)\s*(.*)/s) {
8c634b6e 121 if ($1 eq "text") {
f02a87df 122 print STDOUT $2,"";
8c634b6e 123 } else {
124 # ignore unknown for
125 }
126 next;
127 }
f02a87df 128 elsif (/^=begin\s+(\S+)\s*(.*)/s) {
8c634b6e 129 $begun = $1;
130 if ($1 eq "text") {
f02a87df 131 print STDOUT $2."";
8c634b6e 132 }
133 next;
134 }
135
69e00e79 136sub prepare_for_output {
137
138 s/\s*$/\n/;
139 &init_noremap;
140
141 # need to hide E<> first; they're processed in clear_noremap
142 s/(E<[^<>]+>)/noremap($1)/ge;
143 $maxnest = 10;
144 while ($maxnest-- && /[A-Z]</) {
145 unless ($FANCY) {
55497cff 146 s/C<(.*?)>/`$1'/sg;
69e00e79 147 } else {
55497cff 148 s/C<(.*?)>/noremap("E<lchevron>${1}E<rchevron>")/sge;
69e00e79 149 }
150 # s/[IF]<(.*?)>/italic($1)/ge;
55497cff 151 s/I<(.*?)>/*$1*/sg;
69e00e79 152 # s/[CB]<(.*?)>/bold($1)/ge;
55497cff 153 s/X<.*?>//sg;
69e00e79 154 # LREF: a manpage(3f)
155 s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the $1$2 manpage:g;
156 # LREF: an =item on another manpage
157 s{
158 L<
159 ([^/]+)
160 /
161 (
162 [:\w]+
163 (\(\))?
164 )
165 >
166 } {the "$2" entry in the $1 manpage}gx;
167
168 # LREF: an =item on this manpage
169 s{
170 ((?:
171 L<
172 /
173 (
174 [:\w]+
175 (\(\))?
176 )
177 >
178 (,?\s+(and\s+)?)?
179 )+)
180 } { internal_lrefs($1) }gex;
181
182 # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
183 # the "func" can disambiguate
184 s{
185 L<
186 (?:
187 ([a-zA-Z]\S+?) /
188 )?
189 "?(.*?)"?
190 >
191 }{
192 do {
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\""
196 }
55497cff 197 }sgex;
69e00e79 198
55497cff 199 s/[A-Z]<(.*?)>/$1/sg;
69e00e79 200 }
201 clear_noremap(1);
202}
203
204 &prepare_for_output;
205
206 if (s/^=//) {
207 # $needspace = 0; # Assume this.
208 # s/\n/ /g;
209 ($Cmd, $_) = split(' ', $_, 2);
210 # clear_noremap(1);
211 if ($Cmd eq 'cut') {
212 $cutting = 1;
213 }
214 elsif ($Cmd eq 'head1') {
215 makespace();
216 print OUTPUT;
217 # print OUTPUT uc($_);
218 }
219 elsif ($Cmd eq 'head2') {
220 makespace();
221 # s/(\w+)/\u\L$1/g;
222 #print ' ' x $DEF_INDENT, $_;
223 # print "\xA7";
224 s/(\w)/\xA7 $1/ if $FANCY;
225 print OUTPUT ' ' x ($DEF_INDENT/2), $_, "\n";
226 }
227 elsif ($Cmd eq 'over') {
228 push(@indent,$indent);
229 $indent += ($_ + 0) || $DEF_INDENT;
230 }
231 elsif ($Cmd eq 'back') {
232 $indent = pop(@indent);
233 warn "Unmatched =back\n" unless defined $indent;
234 $needspace = 1;
235 }
236 elsif ($Cmd eq 'item') {
237 makespace();
238 # s/\A(\s*)\*/$1\xb7/ if $FANCY;
239 # s/^(\s*\*\s+)/$1 /;
240 {
241 if (length() + 3 < $indent) {
242 my $paratag = $_;
2e917f57 243 $_ = <IN>;
69e00e79 244 if (/^=/) { # tricked!
245 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
246 output($paratag);
247 redo POD_DIRECTIVE;
248 }
249 &prepare_for_output;
250 IP_output($paratag, $_);
251 } else {
252 local($indent) = $indent[$#index - 1] || $DEF_INDENT;
253 output($_);
254 }
255 }
256 }
257 else {
258 warn "Unrecognized directive: $Cmd\n";
259 }
260 }
261 else {
262 # clear_noremap(1);
263 makespace();
264 output($_, 1);
265 }
266}
267
268close(IN);
269
270}
271
272#########################################################################
273
274sub makespace {
275 if ($needspace) {
276 print OUTPUT "\n";
277 $needspace = 0;
278 }
279}
280
281sub bold {
282 my $line = shift;
283 return $line if $use_format;
284 if($termcap) {
285 $line = "$BOLD$line$NORM";
286 } else {
287 $line =~ s/(.)/$1\b$1/g;
288 }
289# $line = "$BOLD$line$NORM" if $ansify;
290 return $line;
291}
292
293sub italic {
294 my $line = shift;
295 return $line if $use_format;
296 if($termcap) {
297 $line = "$UNDL$line$NORM";
298 } else {
299 $line =~ s/(.)/$1\b_/g;
300 }
301# $line = "$UNDL$line$NORM" if $ansify;
302 return $line;
303}
304
305# Fill a paragraph including underlined and overstricken chars.
306# It's not perfect for words longer than the margin, and it's probably
307# slow, but it works.
308sub fill {
309 local $_ = shift;
310 my $par = "";
311 my $indent_space = " " x $indent;
312 my $marg = $SCREEN-$indent;
313 my $line = $indent_space;
314 my $line_length;
315 foreach (split) {
316 my $word_length = length;
317 $word_length -= 2 while /\010/g; # Subtract backspaces
318
319 if ($line_length + $word_length > $marg) {
320 $par .= $line . "\n";
321 $line= $indent_space . $_;
322 $line_length = $word_length;
323 }
324 else {
325 if ($line_length) {
326 $line_length++;
327 $line .= " ";
328 }
329 $line_length += $word_length;
330 $line .= $_;
331 }
332 }
333 $par .= "$line\n" if $line;
334 $par .= "\n";
335 return $par;
336}
337
338sub IP_output {
339 local($tag, $_) = @_;
340 local($tag_indent) = $indent[$#index - 1] || $DEF_INDENT;
341 $tag_cols = $SCREEN - $tag_indent;
342 $cols = $SCREEN - $indent;
343 $tag =~ s/\s*$//;
344 s/\s+/ /g;
345 s/^ //;
346 $str = "format OUTPUT = \n"
347 . (" " x ($tag_indent))
348 . '@' . ('<' x ($indent - $tag_indent - 1))
349 . "^" . ("<" x ($cols - 1)) . "\n"
350 . '$tag, $_'
351 . "\n~~"
352 . (" " x ($indent-2))
353 . "^" . ("<" x ($cols - 5)) . "\n"
354 . '$_' . "\n\n.\n1";
355 #warn $str; warn "tag is $tag, _ is $_";
356 eval $str || die;
357 write OUTPUT;
358}
359
360sub output {
361 local($_, $reformat) = @_;
362 if ($reformat) {
363 $cols = $SCREEN - $indent;
364 s/\s+/ /g;
365 s/^ //;
366 $str = "format OUTPUT = \n~~"
367 . (" " x ($indent-2))
368 . "^" . ("<" x ($cols - 5)) . "\n"
369 . '$_' . "\n\n.\n1";
370 eval $str || die;
371 write OUTPUT;
372 } else {
373 s/^/' ' x $indent/gem;
374 s/^\s+\n$/\n/gm;
375 print OUTPUT;
376 }
377}
378
379sub noremap {
380 local($thing_to_hide) = shift;
381 $thing_to_hide =~ tr/\000-\177/\200-\377/;
382 return $thing_to_hide;
383}
384
385sub init_noremap {
386 die "unmatched init" if $mapready++;
26fb054b 387 #mask off high bit characters in input stream
388 s/([\200-\377])/"E<".ord($1).">"/ge;
69e00e79 389}
390
391sub clear_noremap {
392 my $ready_to_print = $_[0];
393 die "unmatched clear" unless $mapready--;
394 tr/\200-\377/\000-\177/;
395 # now for the E<>s, which have been hidden until now
396 # otherwise the interative \w<> processing would have
397 # been hosed by the E<gt>
398 s {
26fb054b 399 E<
400 (
401 ( \d+ )
402 | ( [A-Za-z]+ )
403 )
69e00e79 404 >
405 } {
406 do {
26fb054b 407 defined $2
408 ? chr($2)
409 :
410 defined $HTML_Escapes{$3}
411 ? do { $HTML_Escapes{$3} }
69e00e79 412 : do {
f02a87df 413 warn "Unknown escape: E<$1> in $_";
69e00e79 414 "E<$1>";
415 }
416 }
417 }egx if $ready_to_print;
418}
419
420sub internal_lrefs {
421 local($_) = shift;
422 s{L</([^>]+)>}{$1}g;
423 my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
424 my $retstr = "the ";
425 my $i;
426 for ($i = 0; $i <= $#items; $i++) {
427 $retstr .= "C<$items[$i]>";
428 $retstr .= ", " if @items > 2 && $i != $#items;
429 $retstr .= " and " if $i+2 == @items;
430 }
431
432 $retstr .= " entr" . ( @items > 1 ? "ies" : "y" )
433 . " elsewhere in this document ";
434
435 return $retstr;
436
437}
438
439BEGIN {
440
441%HTML_Escapes = (
442 'amp' => '&', # ampersand
443 'lt' => '<', # left chevron, less-than
444 'gt' => '>', # right chevron, greater-than
445 'quot' => '"', # double quote
446
447 "Aacute" => "\xC1", # capital A, acute accent
448 "aacute" => "\xE1", # small a, acute accent
449 "Acirc" => "\xC2", # capital A, circumflex accent
450 "acirc" => "\xE2", # small a, circumflex accent
451 "AElig" => "\xC6", # capital AE diphthong (ligature)
452 "aelig" => "\xE6", # small ae diphthong (ligature)
453 "Agrave" => "\xC0", # capital A, grave accent
454 "agrave" => "\xE0", # small a, grave accent
455 "Aring" => "\xC5", # capital A, ring
456 "aring" => "\xE5", # small a, ring
457 "Atilde" => "\xC3", # capital A, tilde
458 "atilde" => "\xE3", # small a, tilde
459 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
460 "auml" => "\xE4", # small a, dieresis or umlaut mark
461 "Ccedil" => "\xC7", # capital C, cedilla
462 "ccedil" => "\xE7", # small c, cedilla
463 "Eacute" => "\xC9", # capital E, acute accent
464 "eacute" => "\xE9", # small e, acute accent
465 "Ecirc" => "\xCA", # capital E, circumflex accent
466 "ecirc" => "\xEA", # small e, circumflex accent
467 "Egrave" => "\xC8", # capital E, grave accent
468 "egrave" => "\xE8", # small e, grave accent
469 "ETH" => "\xD0", # capital Eth, Icelandic
470 "eth" => "\xF0", # small eth, Icelandic
471 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
472 "euml" => "\xEB", # small e, dieresis or umlaut mark
473 "Iacute" => "\xCD", # capital I, acute accent
474 "iacute" => "\xED", # small i, acute accent
475 "Icirc" => "\xCE", # capital I, circumflex accent
476 "icirc" => "\xEE", # small i, circumflex accent
477 "Igrave" => "\xCD", # capital I, grave accent
478 "igrave" => "\xED", # small i, grave accent
479 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
480 "iuml" => "\xEF", # small i, dieresis or umlaut mark
481 "Ntilde" => "\xD1", # capital N, tilde
482 "ntilde" => "\xF1", # small n, tilde
483 "Oacute" => "\xD3", # capital O, acute accent
484 "oacute" => "\xF3", # small o, acute accent
485 "Ocirc" => "\xD4", # capital O, circumflex accent
486 "ocirc" => "\xF4", # small o, circumflex accent
487 "Ograve" => "\xD2", # capital O, grave accent
488 "ograve" => "\xF2", # small o, grave accent
489 "Oslash" => "\xD8", # capital O, slash
490 "oslash" => "\xF8", # small o, slash
491 "Otilde" => "\xD5", # capital O, tilde
492 "otilde" => "\xF5", # small o, tilde
493 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
494 "ouml" => "\xF6", # small o, dieresis or umlaut mark
495 "szlig" => "\xDF", # small sharp s, German (sz ligature)
496 "THORN" => "\xDE", # capital THORN, Icelandic
497 "thorn" => "\xFE", # small thorn, Icelandic
498 "Uacute" => "\xDA", # capital U, acute accent
499 "uacute" => "\xFA", # small u, acute accent
500 "Ucirc" => "\xDB", # capital U, circumflex accent
501 "ucirc" => "\xFB", # small u, circumflex accent
502 "Ugrave" => "\xD9", # capital U, grave accent
503 "ugrave" => "\xF9", # small u, grave accent
504 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
505 "uuml" => "\xFC", # small u, dieresis or umlaut mark
506 "Yacute" => "\xDD", # capital Y, acute accent
507 "yacute" => "\xFD", # small y, acute accent
508 "yuml" => "\xFF", # small y, dieresis or umlaut mark
509
510 "lchevron" => "\xAB", # left chevron (double less than)
511 "rchevron" => "\xBB", # right chevron (double greater than)
512);
513}
514
5151;