Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text -- Convert POD data to formatted ASCII text. |
2e20e14f |
2 | # $Id: Text.pm,v 2.3 1999/10/07 09:41:57 eagle Exp $ |
6055f9d4 |
3 | # |
4 | # Copyright 1999 by Russ Allbery <rra@stanford.edu> |
5 | # |
6 | # This program is free software; you can redistribute it and/or modify it |
7 | # under the same terms as Perl itself. |
8 | # |
27f805f4 |
9 | # This module is intended to be a replacement for Pod::Text, and attempts to |
10 | # match its output except for some specific circumstances where other |
11 | # decisions seemed to produce better output. It uses Pod::Parser and is |
12 | # designed to be very easy to subclass. |
6055f9d4 |
13 | |
14 | ############################################################################ |
15 | # Modules and declarations |
16 | ############################################################################ |
69e00e79 |
17 | |
6055f9d4 |
18 | package Pod::Text; |
69e00e79 |
19 | |
6055f9d4 |
20 | require 5.004; |
21 | |
27f805f4 |
22 | use Carp qw(carp croak); |
2e20e14f |
23 | use Exporter (); |
27f805f4 |
24 | use Pod::Select (); |
6055f9d4 |
25 | |
26 | use strict; |
2e20e14f |
27 | use vars qw(@ISA @EXPORT %ESCAPES $VERSION); |
6055f9d4 |
28 | |
27f805f4 |
29 | # We inherit from Pod::Select instead of Pod::Parser so that we can be used |
30 | # by Pod::Usage. |
2e20e14f |
31 | @ISA = qw(Pod::Select Exporter); |
6055f9d4 |
32 | |
2e20e14f |
33 | # We have to export pod2text for backward compatibility. |
34 | @EXPORT = qw(pod2text); |
35 | |
36 | ($VERSION = (split (' ', q$Revision: 2.3 $ ))[1]) =~ s/\.(\d)$/.0$1/; |
6055f9d4 |
37 | |
38 | |
39 | ############################################################################ |
40 | # Table of supported E<> escapes |
41 | ############################################################################ |
42 | |
9741dab0 |
43 | # This table is taken near verbatim from Pod::PlainText in Pod::Parser, |
27f805f4 |
44 | # which got it near verbatim from the original Pod::Text. It is therefore |
45 | # credited to Tom Christiansen, and I'm glad I didn't have to write it. :) |
6055f9d4 |
46 | %ESCAPES = ( |
47 | 'amp' => '&', # ampersand |
48 | 'lt' => '<', # left chevron, less-than |
49 | 'gt' => '>', # right chevron, greater-than |
50 | 'quot' => '"', # double quote |
51 | |
52 | "Aacute" => "\xC1", # capital A, acute accent |
53 | "aacute" => "\xE1", # small a, acute accent |
54 | "Acirc" => "\xC2", # capital A, circumflex accent |
55 | "acirc" => "\xE2", # small a, circumflex accent |
56 | "AElig" => "\xC6", # capital AE diphthong (ligature) |
57 | "aelig" => "\xE6", # small ae diphthong (ligature) |
58 | "Agrave" => "\xC0", # capital A, grave accent |
59 | "agrave" => "\xE0", # small a, grave accent |
60 | "Aring" => "\xC5", # capital A, ring |
61 | "aring" => "\xE5", # small a, ring |
62 | "Atilde" => "\xC3", # capital A, tilde |
63 | "atilde" => "\xE3", # small a, tilde |
64 | "Auml" => "\xC4", # capital A, dieresis or umlaut mark |
65 | "auml" => "\xE4", # small a, dieresis or umlaut mark |
66 | "Ccedil" => "\xC7", # capital C, cedilla |
67 | "ccedil" => "\xE7", # small c, cedilla |
68 | "Eacute" => "\xC9", # capital E, acute accent |
69 | "eacute" => "\xE9", # small e, acute accent |
70 | "Ecirc" => "\xCA", # capital E, circumflex accent |
71 | "ecirc" => "\xEA", # small e, circumflex accent |
72 | "Egrave" => "\xC8", # capital E, grave accent |
73 | "egrave" => "\xE8", # small e, grave accent |
74 | "ETH" => "\xD0", # capital Eth, Icelandic |
75 | "eth" => "\xF0", # small eth, Icelandic |
76 | "Euml" => "\xCB", # capital E, dieresis or umlaut mark |
77 | "euml" => "\xEB", # small e, dieresis or umlaut mark |
78 | "Iacute" => "\xCD", # capital I, acute accent |
79 | "iacute" => "\xED", # small i, acute accent |
80 | "Icirc" => "\xCE", # capital I, circumflex accent |
81 | "icirc" => "\xEE", # small i, circumflex accent |
82 | "Igrave" => "\xCD", # capital I, grave accent |
83 | "igrave" => "\xED", # small i, grave accent |
84 | "Iuml" => "\xCF", # capital I, dieresis or umlaut mark |
85 | "iuml" => "\xEF", # small i, dieresis or umlaut mark |
86 | "Ntilde" => "\xD1", # capital N, tilde |
87 | "ntilde" => "\xF1", # small n, tilde |
88 | "Oacute" => "\xD3", # capital O, acute accent |
89 | "oacute" => "\xF3", # small o, acute accent |
90 | "Ocirc" => "\xD4", # capital O, circumflex accent |
91 | "ocirc" => "\xF4", # small o, circumflex accent |
92 | "Ograve" => "\xD2", # capital O, grave accent |
93 | "ograve" => "\xF2", # small o, grave accent |
94 | "Oslash" => "\xD8", # capital O, slash |
95 | "oslash" => "\xF8", # small o, slash |
96 | "Otilde" => "\xD5", # capital O, tilde |
97 | "otilde" => "\xF5", # small o, tilde |
98 | "Ouml" => "\xD6", # capital O, dieresis or umlaut mark |
99 | "ouml" => "\xF6", # small o, dieresis or umlaut mark |
100 | "szlig" => "\xDF", # small sharp s, German (sz ligature) |
101 | "THORN" => "\xDE", # capital THORN, Icelandic |
102 | "thorn" => "\xFE", # small thorn, Icelandic |
103 | "Uacute" => "\xDA", # capital U, acute accent |
104 | "uacute" => "\xFA", # small u, acute accent |
105 | "Ucirc" => "\xDB", # capital U, circumflex accent |
106 | "ucirc" => "\xFB", # small u, circumflex accent |
107 | "Ugrave" => "\xD9", # capital U, grave accent |
108 | "ugrave" => "\xF9", # small u, grave accent |
109 | "Uuml" => "\xDC", # capital U, dieresis or umlaut mark |
110 | "uuml" => "\xFC", # small u, dieresis or umlaut mark |
111 | "Yacute" => "\xDD", # capital Y, acute accent |
112 | "yacute" => "\xFD", # small y, acute accent |
113 | "yuml" => "\xFF", # small y, dieresis or umlaut mark |
114 | |
115 | "lchevron" => "\xAB", # left chevron (double less than) |
116 | "rchevron" => "\xBB", # right chevron (double greater than) |
117 | ); |
69e00e79 |
118 | |
69e00e79 |
119 | |
6055f9d4 |
120 | ############################################################################ |
121 | # Initialization |
122 | ############################################################################ |
69e00e79 |
123 | |
6055f9d4 |
124 | # Initialize the object. Must be sure to call our parent initializer. |
125 | sub initialize { |
126 | my $self = shift; |
69e00e79 |
127 | |
6055f9d4 |
128 | $$self{alt} = 0 unless defined $$self{alt}; |
129 | $$self{indent} = 4 unless defined $$self{indent}; |
130 | $$self{loose} = 0 unless defined $$self{loose}; |
131 | $$self{sentence} = 0 unless defined $$self{sentence}; |
132 | $$self{width} = 76 unless defined $$self{width}; |
69e00e79 |
133 | |
6055f9d4 |
134 | $$self{INDENTS} = []; # Stack of indentations. |
135 | $$self{MARGIN} = $$self{indent}; # Current left margin in spaces. |
69e00e79 |
136 | |
6055f9d4 |
137 | $self->SUPER::initialize; |
138 | } |
69e00e79 |
139 | |
69e00e79 |
140 | |
6055f9d4 |
141 | ############################################################################ |
142 | # Core overrides |
143 | ############################################################################ |
144 | |
145 | # Called for each command paragraph. Gets the command, the associated |
146 | # paragraph, the line number, and a Pod::Paragraph object. Just dispatches |
147 | # the command to a method named the same as the command. =cut is handled |
148 | # internally by Pod::Parser. |
149 | sub command { |
150 | my $self = shift; |
151 | my $command = shift; |
152 | return if $command eq 'pod'; |
153 | return if ($$self{EXCLUDE} && $command ne 'end'); |
154 | $self->item ("\n") if defined $$self{ITEM}; |
155 | $command = 'cmd_' . $command; |
156 | $self->$command (@_); |
157 | } |
69e00e79 |
158 | |
6055f9d4 |
159 | # Called for a verbatim paragraph. Gets the paragraph, the line number, and |
160 | # a Pod::Paragraph object. Just output it verbatim, but with tabs converted |
161 | # to spaces. |
162 | sub verbatim { |
163 | my $self = shift; |
164 | return if $$self{EXCLUDE}; |
165 | $self->item if defined $$self{ITEM}; |
166 | local $_ = shift; |
167 | return if /^\s*$/; |
168 | s/^(\s*\S+)/(' ' x $$self{MARGIN}) . $1/gme; |
169 | $self->output ($_); |
170 | } |
69e00e79 |
171 | |
6055f9d4 |
172 | # Called for a regular text block. Gets the paragraph, the line number, and |
173 | # a Pod::Paragraph object. Perform interpolation and output the results. |
174 | sub textblock { |
27f805f4 |
175 | my $self = shift; |
6055f9d4 |
176 | return if $$self{EXCLUDE}; |
27f805f4 |
177 | $self->output ($_[0]), return if $$self{VERBATIM}; |
178 | local $_ = shift; |
179 | my $line = shift; |
6055f9d4 |
180 | |
181 | # Perform a little magic to collapse multiple L<> references. This is |
27f805f4 |
182 | # here mostly for backwards-compatibility. We'll just rewrite the whole |
183 | # thing into actual text at this part, bypassing the whole internal |
184 | # sequence parsing thing. |
6055f9d4 |
185 | s{ |
186 | ( |
187 | L< # A link of the form L</something>. |
188 | / |
189 | ( |
190 | [:\w]+ # The item has to be a simple word... |
191 | (\(\))? # ...or simple function. |
192 | ) |
193 | > |
194 | ( |
195 | ,?\s+(and\s+)? # Allow lots of them, conjuncted. |
196 | L< |
197 | / |
198 | ( |
199 | [:\w]+ |
200 | (\(\))? |
201 | ) |
202 | > |
203 | )+ |
204 | ) |
205 | } { |
206 | local $_ = $1; |
207 | s%L</([^>]+)>%$1%g; |
208 | my @items = split /(?:,?\s+(?:and\s+)?)/; |
209 | my $string = "the "; |
210 | my $i; |
211 | for ($i = 0; $i < @items; $i++) { |
212 | $string .= $items[$i]; |
213 | $string .= ", " if @items > 2 && $i != $#items; |
214 | $string .= " and " if ($i == $#items - 1); |
215 | } |
216 | $string .= " entries elsewhere in this document"; |
217 | $string; |
218 | }gex; |
219 | |
220 | # Now actually interpolate and output the paragraph. |
221 | $_ = $self->interpolate ($_, $line); |
222 | s/\s+$/\n/; |
223 | if (defined $$self{ITEM}) { |
224 | $self->item ($_ . "\n"); |
225 | } else { |
226 | $self->output ($self->reformat ($_ . "\n")); |
227 | } |
228 | } |
69e00e79 |
229 | |
6055f9d4 |
230 | # Called for an interior sequence. Gets the command, argument, and a |
231 | # Pod::InteriorSequence object and is expected to return the resulting text. |
232 | # Calls code, bold, italic, file, and link to handle those types of |
233 | # sequences, and handles S<>, E<>, X<>, and Z<> directly. |
234 | sub interior_sequence { |
235 | my $self = shift; |
236 | my $command = shift; |
237 | local $_ = shift; |
238 | return '' if ($command eq 'X' || $command eq 'Z'); |
69e00e79 |
239 | |
6055f9d4 |
240 | # Expand escapes into the actual character now, carping if invalid. |
241 | if ($command eq 'E') { |
2e20e14f |
242 | if (/^\d+$/) { |
243 | return chr; |
244 | } else { |
245 | return $ESCAPES{$_} if defined $ESCAPES{$_}; |
246 | carp "Unknown escape: E<$_>"; |
247 | return "E<$_>"; |
248 | } |
6055f9d4 |
249 | } |
69e00e79 |
250 | |
6055f9d4 |
251 | # For all the other sequences, empty content produces no output. |
27f805f4 |
252 | return if $_ eq ''; |
69e00e79 |
253 | |
6055f9d4 |
254 | # For S<>, compress all internal whitespace and then map spaces to \01. |
255 | # When we output the text, we'll map this back. |
256 | if ($command eq 'S') { |
257 | s/\s{2,}/ /g; |
258 | tr/ /\01/; |
259 | return $_; |
260 | } |
69e00e79 |
261 | |
6055f9d4 |
262 | # Anything else needs to get dispatched to another method. |
263 | if ($command eq 'B') { return $self->seq_b ($_) } |
264 | elsif ($command eq 'C') { return $self->seq_c ($_) } |
265 | elsif ($command eq 'F') { return $self->seq_f ($_) } |
266 | elsif ($command eq 'I') { return $self->seq_i ($_) } |
267 | elsif ($command eq 'L') { return $self->seq_l ($_) } |
268 | else { carp "Unknown sequence $command<$_>" } |
269 | } |
f02a87df |
270 | |
6055f9d4 |
271 | # Called for each paragraph that's actually part of the POD. We take |
272 | # advantage of this opportunity to untabify the input. |
273 | sub preprocess_paragraph { |
274 | my $self = shift; |
275 | local $_ = shift; |
276 | 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me; |
277 | $_; |
278 | } |
3ec07288 |
279 | |
69e00e79 |
280 | |
6055f9d4 |
281 | ############################################################################ |
282 | # Command paragraphs |
283 | ############################################################################ |
f2506fb2 |
284 | |
6055f9d4 |
285 | # All command paragraphs take the paragraph and the line number. |
69e00e79 |
286 | |
6055f9d4 |
287 | # First level heading. |
288 | sub cmd_head1 { |
289 | my $self = shift; |
290 | local $_ = shift; |
291 | s/\s+$//; |
27f805f4 |
292 | $_ = $self->interpolate ($_, shift); |
6055f9d4 |
293 | if ($$self{alt}) { |
294 | $self->output ("\n==== $_ ====\n\n"); |
295 | } else { |
296 | $_ .= "\n" if $$self{loose}; |
297 | $self->output ($_ . "\n"); |
298 | } |
299 | } |
69e00e79 |
300 | |
6055f9d4 |
301 | # Second level heading. |
302 | sub cmd_head2 { |
303 | my $self = shift; |
304 | local $_ = shift; |
305 | s/\s+$//; |
27f805f4 |
306 | $_ = $self->interpolate ($_, shift); |
6055f9d4 |
307 | if ($$self{alt}) { |
308 | $self->output ("\n== $_ ==\n\n"); |
309 | } else { |
310 | $self->output (' ' x ($$self{indent} / 2) . $_ . "\n\n"); |
311 | } |
312 | } |
69e00e79 |
313 | |
6055f9d4 |
314 | # Start a list. |
315 | sub cmd_over { |
316 | my $self = shift; |
317 | local $_ = shift; |
318 | unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} } |
319 | push (@{ $$self{INDENTS} }, $$self{MARGIN}); |
320 | $$self{MARGIN} += ($_ + 0); |
321 | } |
69e00e79 |
322 | |
6055f9d4 |
323 | # End a list. |
324 | sub cmd_back { |
325 | my $self = shift; |
326 | $$self{MARGIN} = pop @{ $$self{INDENTS} }; |
327 | unless (defined $$self{MARGIN}) { |
328 | carp "Unmatched =back"; |
329 | $$self{MARGIN} = $$self{indent}; |
330 | } |
69e00e79 |
331 | } |
332 | |
6055f9d4 |
333 | # An individual list item. |
334 | sub cmd_item { |
335 | my $self = shift; |
336 | if (defined $$self{ITEM}) { $self->item } |
337 | local $_ = shift; |
338 | s/\s+$//; |
339 | $$self{ITEM} = $self->interpolate ($_); |
340 | } |
69e00e79 |
341 | |
27f805f4 |
342 | # Begin a block for a particular translator. Setting VERBATIM triggers |
343 | # special handling in textblock(). |
6055f9d4 |
344 | sub cmd_begin { |
345 | my $self = shift; |
346 | local $_ = shift; |
347 | my ($kind) = /^(\S+)/ or return; |
27f805f4 |
348 | if ($kind eq 'text') { |
349 | $$self{VERBATIM} = 1; |
350 | } else { |
351 | $$self{EXCLUDE} = 1; |
352 | } |
6055f9d4 |
353 | } |
f2506fb2 |
354 | |
6055f9d4 |
355 | # End a block for a particular translator. We assume that all =begin/=end |
27f805f4 |
356 | # pairs are properly closed. |
6055f9d4 |
357 | sub cmd_end { |
358 | my $self = shift; |
27f805f4 |
359 | $$self{EXCLUDE} = 0; |
360 | $$self{VERBATIM} = 0; |
6055f9d4 |
361 | } |
362 | |
363 | # One paragraph for a particular translator. Ignore it unless it's intended |
27f805f4 |
364 | # for text, in which case we treat it as a verbatim text block. |
6055f9d4 |
365 | sub cmd_for { |
366 | my $self = shift; |
367 | local $_ = shift; |
368 | my $line = shift; |
27f805f4 |
369 | return unless s/^text\b[ \t]*\n?//; |
370 | $self->verbatim ($_, $line); |
6055f9d4 |
371 | } |
f2506fb2 |
372 | |
69e00e79 |
373 | |
6055f9d4 |
374 | ############################################################################ |
375 | # Interior sequences |
376 | ############################################################################ |
69e00e79 |
377 | |
6055f9d4 |
378 | # The simple formatting ones. These are here mostly so that subclasses can |
379 | # override them and do more complicated things. |
27f805f4 |
380 | sub seq_b { return $_[0]{alt} ? "``$_[1]''" : $_[1] } |
381 | sub seq_c { return $_[0]{alt} ? "``$_[1]''" : "`$_[1]'" } |
382 | sub seq_f { return $_[0]{alt} ? "\"$_[1]\"" : $_[1] } |
6055f9d4 |
383 | sub seq_i { return '*' . $_[1] . '*' } |
69e00e79 |
384 | |
6055f9d4 |
385 | # The complicated one. Handle links. Since this is plain text, we can't |
386 | # actually make any real links, so this is all to figure out what text we |
387 | # print out. |
388 | sub seq_l { |
389 | my $self = shift; |
390 | local $_ = shift; |
69e00e79 |
391 | |
6055f9d4 |
392 | # Smash whitespace in case we were split across multiple lines. |
393 | s/\s+/ /g; |
69e00e79 |
394 | |
6055f9d4 |
395 | # If we were given any explicit text, just output it. |
396 | if (/^([^|]+)\|/) { return $1 } |
397 | |
398 | # Okay, leading and trailing whitespace isn't important; get rid of it. |
399 | s/^\s+//; |
400 | s/\s+$//; |
6055f9d4 |
401 | |
402 | # Default to using the whole content of the link entry as a section |
403 | # name. Note that L<manpage/> forces a manpage interpretation, as does |
404 | # something looking like L<manpage(section)>. The latter is an |
405 | # enhancement over the original Pod::Text. |
406 | my ($manpage, $section) = ('', $_); |
407 | if (/^"\s*(.*?)\s*"$/) { |
408 | $section = '"' . $1 . '"'; |
409 | } elsif (m/^[-:.\w]+(?:\(\S+\))?$/) { |
410 | ($manpage, $section) = ($_, ''); |
411 | } elsif (m%/%) { |
412 | ($manpage, $section) = split (/\s*\/\s*/, $_, 2); |
8c634b6e |
413 | } |
414 | |
6055f9d4 |
415 | # Now build the actual output text. |
416 | my $text = ''; |
417 | if (!length $section) { |
418 | $text = "the $manpage manpage" if length $manpage; |
419 | } elsif ($section =~ /^[:\w]+(?:\(\))?/) { |
420 | $text .= 'the ' . $section . ' entry'; |
421 | $text .= (length $manpage) ? " in the $manpage manpage" |
422 | : " elsewhere in this document"; |
423 | } else { |
424 | $section =~ s/^\"\s*//; |
425 | $section =~ s/\s*\"$//; |
426 | $text .= 'the section on "' . $section . '"'; |
427 | $text .= " in the $manpage manpage" if length $manpage; |
69e00e79 |
428 | } |
6055f9d4 |
429 | $text; |
69e00e79 |
430 | } |
431 | |
6055f9d4 |
432 | |
433 | ############################################################################ |
434 | # List handling |
435 | ############################################################################ |
436 | |
437 | # This method is called whenever an =item command is complete (in other |
438 | # words, we've seen its associated paragraph or know for certain that it |
439 | # doesn't have one). It gets the paragraph associated with the item as an |
440 | # argument. If that argument is empty, just output the item tag; if it |
441 | # contains a newline, output the item tag followed by the newline. |
442 | # Otherwise, see if there's enough room for us to output the item tag in the |
443 | # margin of the text or if we have to put it on a separate line. |
444 | sub item { |
445 | my $self = shift; |
446 | local $_ = shift; |
447 | my $tag = $$self{ITEM}; |
448 | unless (defined $tag) { |
449 | carp "item called without tag"; |
450 | return; |
69e00e79 |
451 | } |
6055f9d4 |
452 | undef $$self{ITEM}; |
453 | my $indent = $$self{INDENTS}[-1]; |
454 | unless (defined $indent) { $indent = $$self{indent} } |
455 | my $space = ' ' x $indent; |
456 | $space =~ s/^ /:/ if $$self{alt}; |
457 | if (!$_ || /^\s+$/ || ($$self{MARGIN} - $indent < length ($tag) + 1)) { |
27f805f4 |
458 | my $margin = $$self{MARGIN}; |
459 | $$self{MARGIN} = $indent; |
460 | my $output = $self->reformat ($tag); |
461 | $output =~ s/\n*$/\n/; |
462 | $self->output ($output); |
463 | $$self{MARGIN} = $margin; |
6055f9d4 |
464 | $self->output ($self->reformat ($_)) if /\S/; |
465 | } else { |
466 | $_ = $self->reformat ($_); |
467 | s/^ /:/ if ($$self{alt} && $indent > 0); |
468 | my $tagspace = ' ' x length $tag; |
469 | s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item"; |
470 | $self->output ($_); |
69e00e79 |
471 | } |
472 | } |
473 | |
69e00e79 |
474 | |
6055f9d4 |
475 | ############################################################################ |
476 | # Output formatting |
477 | ############################################################################ |
69e00e79 |
478 | |
6055f9d4 |
479 | # Wrap a line, indenting by the current left margin. We can't use |
480 | # Text::Wrap because it plays games with tabs. We can't use formline, even |
481 | # though we'd really like to, because it screws up non-printing characters. |
482 | # So we have to do the wrapping ourselves. |
483 | sub wrap { |
484 | my $self = shift; |
485 | local $_ = shift; |
486 | my $output = ''; |
487 | my $spaces = ' ' x $$self{MARGIN}; |
488 | my $width = $$self{width} - $$self{MARGIN}; |
489 | while (length > $width) { |
490 | if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) { |
491 | $output .= $spaces . $1 . "\n"; |
492 | } else { |
493 | last; |
494 | } |
69e00e79 |
495 | } |
6055f9d4 |
496 | $output .= $spaces . $_; |
497 | $output =~ s/\s+$/\n\n/; |
498 | $output; |
69e00e79 |
499 | } |
500 | |
6055f9d4 |
501 | # Reformat a paragraph of text for the current margin. Takes the text to |
502 | # reformat and returns the formatted text. |
503 | sub reformat { |
504 | my $self = shift; |
505 | local $_ = shift; |
69e00e79 |
506 | |
6055f9d4 |
507 | # If we're trying to preserve two spaces after sentences, do some |
508 | # munging to support that. Otherwise, smash all repeated whitespace. |
509 | if ($$self{sentence}) { |
510 | s/ +$//mg; |
511 | s/\.\n/. \n/g; |
512 | s/\n/ /g; |
513 | s/ +/ /g; |
69e00e79 |
514 | } else { |
6055f9d4 |
515 | s/\s+/ /g; |
69e00e79 |
516 | } |
6055f9d4 |
517 | $self->wrap ($_); |
69e00e79 |
518 | } |
519 | |
6055f9d4 |
520 | # Output text to the output device. |
521 | sub output { $_[1] =~ tr/\01/ /; print { $_[0]->output_handle } $_[1] } |
69e00e79 |
522 | |
69e00e79 |
523 | |
6055f9d4 |
524 | ############################################################################ |
27f805f4 |
525 | # Backwards compatibility |
526 | ############################################################################ |
527 | |
528 | # The old Pod::Text module did everything in a pod2text() function. This |
529 | # tries to provide the same interface for legacy applications. |
530 | sub pod2text { |
531 | my @args; |
532 | |
533 | # This is really ugly; I hate doing option parsing in the middle of a |
534 | # module. But the old Pod::Text module supported passing flags to its |
535 | # entry function, so handle -a and -<number>. |
536 | while ($_[0] =~ /^-/) { |
537 | my $flag = shift; |
538 | if ($flag eq '-a') { push (@args, alt => 1) } |
539 | elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) } |
540 | else { |
541 | unshift (@_, $flag); |
542 | last; |
543 | } |
544 | } |
545 | |
546 | # Now that we know what arguments we're using, create the parser. |
547 | my $parser = Pod::Text->new (@args); |
548 | |
549 | # If two arguments were given, the second argument is going to be a file |
550 | # handle. That means we want to call parse_from_filehandle(), which |
551 | # means we need to turn the first argument into a file handle. Magic |
552 | # open will handle the <&STDIN case automagically. |
553 | if (defined $_[1]) { |
554 | local *IN; |
555 | unless (open (IN, $_[0])) { |
556 | croak ("Can't open $_[0] for reading: $!\n"); |
557 | return; |
558 | } |
559 | $_[0] = \*IN; |
560 | return $parser->parse_from_filehandle (@_); |
561 | } else { |
562 | return $parser->parse_from_file (@_); |
563 | } |
564 | } |
565 | |
566 | |
567 | ############################################################################ |
6055f9d4 |
568 | # Module return value and documentation |
569 | ############################################################################ |
69e00e79 |
570 | |
6055f9d4 |
571 | 1; |
572 | __END__ |
69e00e79 |
573 | |
6055f9d4 |
574 | =head1 NAME |
69e00e79 |
575 | |
6055f9d4 |
576 | Pod::Text - Convert POD data to formatted ASCII text |
69e00e79 |
577 | |
6055f9d4 |
578 | =head1 SYNOPSIS |
69e00e79 |
579 | |
6055f9d4 |
580 | use Pod::Text; |
581 | my $parser = Pod::Text->new (sentence => 0, width => 78); |
69e00e79 |
582 | |
6055f9d4 |
583 | # Read POD from STDIN and write to STDOUT. |
584 | $parser->parse_from_filehandle; |
69e00e79 |
585 | |
6055f9d4 |
586 | # Read POD from file.pod and write to file.txt. |
587 | $parser->parse_from_file ('file.pod', 'file.txt'); |
69e00e79 |
588 | |
6055f9d4 |
589 | =head1 DESCRIPTION |
5491a304 |
590 | |
27f805f4 |
591 | Pod::Text is a module that can convert documentation in the POD format (the |
592 | preferred language for documenting Perl) into formatted ASCII. It uses no |
593 | special formatting controls or codes whatsoever, and its output is therefore |
594 | suitable for nearly any device. |
69e00e79 |
595 | |
27f805f4 |
596 | As a derived class from Pod::Parser, Pod::Text supports the same methods and |
597 | interfaces. See L<Pod::Parser> for all the details; briefly, one creates a |
598 | new parser with C<Pod::Text-E<gt>new()> and then calls either |
599 | parse_from_filehandle() or parse_from_file(). |
6055f9d4 |
600 | |
27f805f4 |
601 | new() can take options, in the form of key/value pairs, that control the |
6055f9d4 |
602 | behavior of the parser. The currently recognized options are: |
603 | |
604 | =over 4 |
605 | |
606 | =item alt |
607 | |
608 | If set to a true value, selects an alternate output format that, among other |
609 | things, uses a different heading style and marks C<=item> entries with a |
610 | colon in the left margin. Defaults to false. |
611 | |
612 | =item indent |
613 | |
614 | The number of spaces to indent regular text, and the default indentation for |
615 | C<=over> blocks. Defaults to 4. |
616 | |
617 | =item loose |
618 | |
619 | If set to a true value, a blank line is printed after a C<=head1> heading. |
620 | If set to false (the default), no blank line is printed after C<=head1>, |
621 | although one is still printed after C<=head2>. This is the default because |
622 | it's the expected formatting for manual pages; if you're formatting |
623 | arbitrary text documents, setting this to true may result in more pleasing |
624 | output. |
625 | |
626 | =item sentence |
627 | |
27f805f4 |
628 | If set to a true value, Pod::Text will assume that each sentence ends in two |
629 | spaces, and will try to preserve that spacing. If set to false, all |
6055f9d4 |
630 | consecutive whitespace in non-verbatim paragraphs is compressed into a |
631 | single space. Defaults to true. |
632 | |
633 | =item width |
634 | |
635 | The column at which to wrap text on the right-hand side. Defaults to 76. |
636 | |
637 | =back |
638 | |
27f805f4 |
639 | The standard Pod::Parser method parse_from_filehandle() takes up to two |
6055f9d4 |
640 | arguments, the first being the file handle to read POD from and the second |
641 | being the file handle to write the formatted output to. The first defaults |
642 | to STDIN if not given, and the second defaults to STDOUT. The method |
27f805f4 |
643 | parse_from_file() is almost identical, except that its two arguments are the |
644 | input and output disk files instead. See L<Pod::Parser> for the specific |
645 | details. |
6055f9d4 |
646 | |
647 | =head1 DIAGNOSTICS |
648 | |
649 | =over 4 |
650 | |
27f805f4 |
651 | =item Bizarre space in item |
652 | |
653 | (W) Something has gone wrong in internal C<=item> processing. This message |
654 | indicates a bug in Pod::Text; you should never see it. |
655 | |
656 | =item Can't open %s for reading: %s |
657 | |
658 | (F) Pod::Text was invoked via the compatibility mode pod2text() interface |
659 | and the input file it was given could not be opened. |
660 | |
6055f9d4 |
661 | =item Unknown escape: %s |
662 | |
27f805f4 |
663 | (W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Text didn't |
664 | know about. |
6055f9d4 |
665 | |
666 | =item Unknown sequence: %s |
667 | |
27f805f4 |
668 | (W) The POD source contained a non-standard internal sequence (something of |
669 | the form C<XE<lt>E<gt>>) that Pod::Text didn't know about. |
6055f9d4 |
670 | |
671 | =item Unmatched =back |
672 | |
27f805f4 |
673 | (W) Pod::Text encountered a C<=back> command that didn't correspond to an |
6055f9d4 |
674 | C<=over> command. |
675 | |
676 | =back |
677 | |
27f805f4 |
678 | =head1 RESTRICTIONS |
679 | |
680 | Embedded Ctrl-As (octal 001) in the input will be mapped to spaces on |
681 | output, due to an internal implementation detail. |
682 | |
6055f9d4 |
683 | =head1 NOTES |
684 | |
27f805f4 |
685 | This is a replacement for an earlier Pod::Text module written by Tom |
686 | Christiansen. It has a revamped interface, since it now uses Pod::Parser, |
687 | but an interface roughly compatible with the old Pod::Text::pod2text() |
688 | function is still available. Please change to the new calling convention, |
689 | though. |
6055f9d4 |
690 | |
691 | The original Pod::Text contained code to do formatting via termcap |
692 | sequences, although it wasn't turned on by default and it was problematic to |
27f805f4 |
693 | get it to work at all. This rewrite doesn't even try to do that, but a |
694 | subclass of it does. Look for L<Pod::Text::Termcap|Pod::Text::Termcap>. |
6055f9d4 |
695 | |
696 | =head1 SEE ALSO |
697 | |
27f805f4 |
698 | L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>, |
699 | pod2text(1) |
6055f9d4 |
700 | |
701 | =head1 AUTHOR |
702 | |
703 | Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the |
704 | original Pod::Text by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt> and |
705 | its conversion to Pod::Parser by Brad Appleton |
706 | E<lt>bradapp@enteract.comE<gt>. |
707 | |
708 | =cut |