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