Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text -- Convert POD data to formatted ASCII text. |
50a3fd2a |
2 | # $Id: Text.pm,v 2.7 2000/11/19 04:47:50 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. |
50a3fd2a |
40 | $VERSION = 2.07; |
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 |
ab1f1d91 |
56 | 'sol' => '/', # solidus (forward slash) |
be3174d2 |
57 | 'verbar' => '|', # vertical bar |
5cdeb5a2 |
58 | |
6055f9d4 |
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 |
5cdeb5a2 |
85 | "Iacute" => "\xCC", # capital I, acute accent |
86 | "iacute" => "\xEC", # small i, acute accent |
6055f9d4 |
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 |
5cdeb5a2 |
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 | |
ab1f1d91 |
175 | # Figure out what quotes we'll be using for C<> text. |
50a3fd2a |
176 | $$self{quotes} ||= '"'; |
ab1f1d91 |
177 | if ($$self{quotes} eq 'none') { |
178 | $$self{LQUOTE} = $$self{RQUOTE} = ''; |
179 | } elsif (length ($$self{quotes}) == 1) { |
180 | $$self{LQUOTE} = $$self{RQUOTE} = $$self{quotes}; |
181 | } elsif ($$self{quotes} =~ /^(.)(.)$/ |
182 | || $$self{quotes} =~ /^(..)(..)$/) { |
183 | $$self{LQUOTE} = $1; |
184 | $$self{RQUOTE} = $2; |
185 | } else { |
186 | croak qq(Invalid quote specification "$$self{quotes}"); |
187 | } |
188 | |
6055f9d4 |
189 | $$self{INDENTS} = []; # Stack of indentations. |
190 | $$self{MARGIN} = $$self{indent}; # Current left margin in spaces. |
69e00e79 |
191 | |
6055f9d4 |
192 | $self->SUPER::initialize; |
193 | } |
69e00e79 |
194 | |
69e00e79 |
195 | |
6055f9d4 |
196 | ############################################################################ |
197 | # Core overrides |
198 | ############################################################################ |
199 | |
200 | # Called for each command paragraph. Gets the command, the associated |
201 | # paragraph, the line number, and a Pod::Paragraph object. Just dispatches |
202 | # the command to a method named the same as the command. =cut is handled |
203 | # internally by Pod::Parser. |
204 | sub command { |
205 | my $self = shift; |
206 | my $command = shift; |
207 | return if $command eq 'pod'; |
208 | return if ($$self{EXCLUDE} && $command ne 'end'); |
209 | $self->item ("\n") if defined $$self{ITEM}; |
ab1f1d91 |
210 | if ($self->can ('cmd_' . $command)) { |
211 | $command = 'cmd_' . $command; |
212 | $self->$command (@_); |
213 | } else { |
214 | my ($text, $line, $paragraph) = @_; |
5cdeb5a2 |
215 | my $file; |
216 | ($file, $line) = $paragraph->file_line; |
ab1f1d91 |
217 | $text =~ s/\n+\z//; |
218 | $text = " $text" if ($text =~ /^\S/); |
219 | warn qq($file:$line: Unknown command paragraph "=$command$text"\n); |
220 | return; |
221 | } |
6055f9d4 |
222 | } |
69e00e79 |
223 | |
6055f9d4 |
224 | # Called for a verbatim paragraph. Gets the paragraph, the line number, and |
225 | # a Pod::Paragraph object. Just output it verbatim, but with tabs converted |
226 | # to spaces. |
227 | sub verbatim { |
228 | my $self = shift; |
229 | return if $$self{EXCLUDE}; |
230 | $self->item if defined $$self{ITEM}; |
231 | local $_ = shift; |
232 | return if /^\s*$/; |
233 | s/^(\s*\S+)/(' ' x $$self{MARGIN}) . $1/gme; |
234 | $self->output ($_); |
235 | } |
69e00e79 |
236 | |
6055f9d4 |
237 | # Called for a regular text block. Gets the paragraph, the line number, and |
238 | # a Pod::Paragraph object. Perform interpolation and output the results. |
239 | sub textblock { |
27f805f4 |
240 | my $self = shift; |
6055f9d4 |
241 | return if $$self{EXCLUDE}; |
27f805f4 |
242 | $self->output ($_[0]), return if $$self{VERBATIM}; |
243 | local $_ = shift; |
244 | my $line = shift; |
6055f9d4 |
245 | |
246 | # Perform a little magic to collapse multiple L<> references. This is |
27f805f4 |
247 | # here mostly for backwards-compatibility. We'll just rewrite the whole |
248 | # thing into actual text at this part, bypassing the whole internal |
249 | # sequence parsing thing. |
6055f9d4 |
250 | s{ |
251 | ( |
252 | L< # A link of the form L</something>. |
253 | / |
254 | ( |
255 | [:\w]+ # The item has to be a simple word... |
256 | (\(\))? # ...or simple function. |
257 | ) |
258 | > |
259 | ( |
260 | ,?\s+(and\s+)? # Allow lots of them, conjuncted. |
5cdeb5a2 |
261 | L< |
6055f9d4 |
262 | / |
263 | ( |
264 | [:\w]+ |
265 | (\(\))? |
266 | ) |
267 | > |
268 | )+ |
269 | ) |
270 | } { |
271 | local $_ = $1; |
272 | s%L</([^>]+)>%$1%g; |
273 | my @items = split /(?:,?\s+(?:and\s+)?)/; |
274 | my $string = "the "; |
275 | my $i; |
276 | for ($i = 0; $i < @items; $i++) { |
277 | $string .= $items[$i]; |
278 | $string .= ", " if @items > 2 && $i != $#items; |
279 | $string .= " and " if ($i == $#items - 1); |
280 | } |
281 | $string .= " entries elsewhere in this document"; |
282 | $string; |
283 | }gex; |
284 | |
285 | # Now actually interpolate and output the paragraph. |
286 | $_ = $self->interpolate ($_, $line); |
287 | s/\s+$/\n/; |
288 | if (defined $$self{ITEM}) { |
289 | $self->item ($_ . "\n"); |
290 | } else { |
291 | $self->output ($self->reformat ($_ . "\n")); |
292 | } |
293 | } |
69e00e79 |
294 | |
6055f9d4 |
295 | # Called for an interior sequence. Gets the command, argument, and a |
296 | # Pod::InteriorSequence object and is expected to return the resulting text. |
297 | # Calls code, bold, italic, file, and link to handle those types of |
298 | # sequences, and handles S<>, E<>, X<>, and Z<> directly. |
299 | sub interior_sequence { |
300 | my $self = shift; |
301 | my $command = shift; |
302 | local $_ = shift; |
303 | return '' if ($command eq 'X' || $command eq 'Z'); |
69e00e79 |
304 | |
6055f9d4 |
305 | # Expand escapes into the actual character now, carping if invalid. |
306 | if ($command eq 'E') { |
2e20e14f |
307 | if (/^\d+$/) { |
308 | return chr; |
309 | } else { |
310 | return $ESCAPES{$_} if defined $ESCAPES{$_}; |
311 | carp "Unknown escape: E<$_>"; |
312 | return "E<$_>"; |
313 | } |
6055f9d4 |
314 | } |
69e00e79 |
315 | |
6055f9d4 |
316 | # For all the other sequences, empty content produces no output. |
27f805f4 |
317 | return if $_ eq ''; |
69e00e79 |
318 | |
6055f9d4 |
319 | # For S<>, compress all internal whitespace and then map spaces to \01. |
320 | # When we output the text, we'll map this back. |
321 | if ($command eq 'S') { |
322 | s/\s{2,}/ /g; |
323 | tr/ /\01/; |
324 | return $_; |
325 | } |
69e00e79 |
326 | |
6055f9d4 |
327 | # Anything else needs to get dispatched to another method. |
328 | if ($command eq 'B') { return $self->seq_b ($_) } |
329 | elsif ($command eq 'C') { return $self->seq_c ($_) } |
330 | elsif ($command eq 'F') { return $self->seq_f ($_) } |
331 | elsif ($command eq 'I') { return $self->seq_i ($_) } |
332 | elsif ($command eq 'L') { return $self->seq_l ($_) } |
333 | else { carp "Unknown sequence $command<$_>" } |
334 | } |
f02a87df |
335 | |
6055f9d4 |
336 | # Called for each paragraph that's actually part of the POD. We take |
337 | # advantage of this opportunity to untabify the input. |
338 | sub preprocess_paragraph { |
339 | my $self = shift; |
340 | local $_ = shift; |
341 | 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me; |
342 | $_; |
343 | } |
3ec07288 |
344 | |
69e00e79 |
345 | |
6055f9d4 |
346 | ############################################################################ |
347 | # Command paragraphs |
348 | ############################################################################ |
f2506fb2 |
349 | |
6055f9d4 |
350 | # All command paragraphs take the paragraph and the line number. |
69e00e79 |
351 | |
6055f9d4 |
352 | # First level heading. |
353 | sub cmd_head1 { |
354 | my $self = shift; |
355 | local $_ = shift; |
356 | s/\s+$//; |
27f805f4 |
357 | $_ = $self->interpolate ($_, shift); |
6055f9d4 |
358 | if ($$self{alt}) { |
359 | $self->output ("\n==== $_ ====\n\n"); |
360 | } else { |
361 | $_ .= "\n" if $$self{loose}; |
362 | $self->output ($_ . "\n"); |
363 | } |
364 | } |
69e00e79 |
365 | |
6055f9d4 |
366 | # Second level heading. |
367 | sub cmd_head2 { |
368 | my $self = shift; |
369 | local $_ = shift; |
370 | s/\s+$//; |
27f805f4 |
371 | $_ = $self->interpolate ($_, shift); |
6055f9d4 |
372 | if ($$self{alt}) { |
373 | $self->output ("\n== $_ ==\n\n"); |
374 | } else { |
375 | $self->output (' ' x ($$self{indent} / 2) . $_ . "\n\n"); |
376 | } |
377 | } |
69e00e79 |
378 | |
50a3fd2a |
379 | # Third level heading. |
380 | sub cmd_head3 { |
381 | my $self = shift; |
382 | local $_ = shift; |
383 | s/\s+$//; |
384 | $_ = $self->interpolate ($_, shift); |
385 | if ($$self{alt}) { |
386 | $self->output ("\n= $_ =\n\n"); |
387 | } else { |
388 | $self->output (' ' x ($$self{indent} * 2 / 3 + 0.5) . $_ . "\n\n"); |
389 | } |
390 | } |
391 | |
392 | # Third level heading. |
393 | sub cmd_head4 { |
394 | my $self = shift; |
395 | local $_ = shift; |
396 | s/\s+$//; |
397 | $_ = $self->interpolate ($_, shift); |
398 | if ($$self{alt}) { |
399 | $self->output ("\n- $_ -\n\n"); |
400 | } else { |
401 | $self->output (' ' x ($$self{indent} * 3 / 4 + 0.5) . $_ . "\n\n"); |
402 | } |
403 | } |
404 | |
6055f9d4 |
405 | # Start a list. |
406 | sub cmd_over { |
407 | my $self = shift; |
408 | local $_ = shift; |
409 | unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} } |
410 | push (@{ $$self{INDENTS} }, $$self{MARGIN}); |
411 | $$self{MARGIN} += ($_ + 0); |
412 | } |
69e00e79 |
413 | |
6055f9d4 |
414 | # End a list. |
415 | sub cmd_back { |
416 | my $self = shift; |
417 | $$self{MARGIN} = pop @{ $$self{INDENTS} }; |
418 | unless (defined $$self{MARGIN}) { |
419 | carp "Unmatched =back"; |
420 | $$self{MARGIN} = $$self{indent}; |
421 | } |
69e00e79 |
422 | } |
423 | |
6055f9d4 |
424 | # An individual list item. |
425 | sub cmd_item { |
426 | my $self = shift; |
427 | if (defined $$self{ITEM}) { $self->item } |
428 | local $_ = shift; |
429 | s/\s+$//; |
430 | $$self{ITEM} = $self->interpolate ($_); |
431 | } |
69e00e79 |
432 | |
27f805f4 |
433 | # Begin a block for a particular translator. Setting VERBATIM triggers |
434 | # special handling in textblock(). |
6055f9d4 |
435 | sub cmd_begin { |
436 | my $self = shift; |
437 | local $_ = shift; |
438 | my ($kind) = /^(\S+)/ or return; |
27f805f4 |
439 | if ($kind eq 'text') { |
440 | $$self{VERBATIM} = 1; |
441 | } else { |
442 | $$self{EXCLUDE} = 1; |
443 | } |
6055f9d4 |
444 | } |
f2506fb2 |
445 | |
6055f9d4 |
446 | # End a block for a particular translator. We assume that all =begin/=end |
27f805f4 |
447 | # pairs are properly closed. |
6055f9d4 |
448 | sub cmd_end { |
449 | my $self = shift; |
27f805f4 |
450 | $$self{EXCLUDE} = 0; |
451 | $$self{VERBATIM} = 0; |
5cdeb5a2 |
452 | } |
6055f9d4 |
453 | |
454 | # One paragraph for a particular translator. Ignore it unless it's intended |
27f805f4 |
455 | # for text, in which case we treat it as a verbatim text block. |
6055f9d4 |
456 | sub cmd_for { |
457 | my $self = shift; |
458 | local $_ = shift; |
459 | my $line = shift; |
27f805f4 |
460 | return unless s/^text\b[ \t]*\n?//; |
461 | $self->verbatim ($_, $line); |
6055f9d4 |
462 | } |
f2506fb2 |
463 | |
69e00e79 |
464 | |
6055f9d4 |
465 | ############################################################################ |
466 | # Interior sequences |
467 | ############################################################################ |
69e00e79 |
468 | |
6055f9d4 |
469 | # The simple formatting ones. These are here mostly so that subclasses can |
470 | # override them and do more complicated things. |
27f805f4 |
471 | sub seq_b { return $_[0]{alt} ? "``$_[1]''" : $_[1] } |
27f805f4 |
472 | sub seq_f { return $_[0]{alt} ? "\"$_[1]\"" : $_[1] } |
6055f9d4 |
473 | sub seq_i { return '*' . $_[1] . '*' } |
ab1f1d91 |
474 | sub seq_c { |
475 | return $_[0]{alt} ? "``$_[1]''" : "$_[0]{LQUOTE}$_[1]$_[0]{RQUOTE}" |
476 | } |
69e00e79 |
477 | |
6055f9d4 |
478 | # The complicated one. Handle links. Since this is plain text, we can't |
479 | # actually make any real links, so this is all to figure out what text we |
480 | # print out. |
481 | sub seq_l { |
482 | my $self = shift; |
483 | local $_ = shift; |
69e00e79 |
484 | |
6055f9d4 |
485 | # Smash whitespace in case we were split across multiple lines. |
486 | s/\s+/ /g; |
69e00e79 |
487 | |
6055f9d4 |
488 | # If we were given any explicit text, just output it. |
489 | if (/^([^|]+)\|/) { return $1 } |
490 | |
491 | # Okay, leading and trailing whitespace isn't important; get rid of it. |
492 | s/^\s+//; |
493 | s/\s+$//; |
6055f9d4 |
494 | |
495 | # Default to using the whole content of the link entry as a section |
496 | # name. Note that L<manpage/> forces a manpage interpretation, as does |
497 | # something looking like L<manpage(section)>. The latter is an |
498 | # enhancement over the original Pod::Text. |
499 | my ($manpage, $section) = ('', $_); |
500 | if (/^"\s*(.*?)\s*"$/) { |
501 | $section = '"' . $1 . '"'; |
502 | } elsif (m/^[-:.\w]+(?:\(\S+\))?$/) { |
503 | ($manpage, $section) = ($_, ''); |
504 | } elsif (m%/%) { |
505 | ($manpage, $section) = split (/\s*\/\s*/, $_, 2); |
8c634b6e |
506 | } |
507 | |
6055f9d4 |
508 | # Now build the actual output text. |
509 | my $text = ''; |
510 | if (!length $section) { |
511 | $text = "the $manpage manpage" if length $manpage; |
512 | } elsif ($section =~ /^[:\w]+(?:\(\))?/) { |
513 | $text .= 'the ' . $section . ' entry'; |
514 | $text .= (length $manpage) ? " in the $manpage manpage" |
515 | : " elsewhere in this document"; |
516 | } else { |
517 | $section =~ s/^\"\s*//; |
518 | $section =~ s/\s*\"$//; |
519 | $text .= 'the section on "' . $section . '"'; |
520 | $text .= " in the $manpage manpage" if length $manpage; |
69e00e79 |
521 | } |
6055f9d4 |
522 | $text; |
69e00e79 |
523 | } |
524 | |
6055f9d4 |
525 | |
526 | ############################################################################ |
527 | # List handling |
528 | ############################################################################ |
529 | |
530 | # This method is called whenever an =item command is complete (in other |
531 | # words, we've seen its associated paragraph or know for certain that it |
532 | # doesn't have one). It gets the paragraph associated with the item as an |
533 | # argument. If that argument is empty, just output the item tag; if it |
534 | # contains a newline, output the item tag followed by the newline. |
535 | # Otherwise, see if there's enough room for us to output the item tag in the |
536 | # margin of the text or if we have to put it on a separate line. |
537 | sub item { |
538 | my $self = shift; |
539 | local $_ = shift; |
540 | my $tag = $$self{ITEM}; |
541 | unless (defined $tag) { |
542 | carp "item called without tag"; |
543 | return; |
69e00e79 |
544 | } |
6055f9d4 |
545 | undef $$self{ITEM}; |
546 | my $indent = $$self{INDENTS}[-1]; |
547 | unless (defined $indent) { $indent = $$self{indent} } |
548 | my $space = ' ' x $indent; |
549 | $space =~ s/^ /:/ if $$self{alt}; |
550 | if (!$_ || /^\s+$/ || ($$self{MARGIN} - $indent < length ($tag) + 1)) { |
27f805f4 |
551 | my $margin = $$self{MARGIN}; |
552 | $$self{MARGIN} = $indent; |
553 | my $output = $self->reformat ($tag); |
554 | $output =~ s/\n*$/\n/; |
555 | $self->output ($output); |
556 | $$self{MARGIN} = $margin; |
6055f9d4 |
557 | $self->output ($self->reformat ($_)) if /\S/; |
558 | } else { |
559 | $_ = $self->reformat ($_); |
560 | s/^ /:/ if ($$self{alt} && $indent > 0); |
561 | my $tagspace = ' ' x length $tag; |
562 | s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item"; |
563 | $self->output ($_); |
69e00e79 |
564 | } |
565 | } |
566 | |
69e00e79 |
567 | |
6055f9d4 |
568 | ############################################################################ |
569 | # Output formatting |
570 | ############################################################################ |
69e00e79 |
571 | |
6055f9d4 |
572 | # Wrap a line, indenting by the current left margin. We can't use |
573 | # Text::Wrap because it plays games with tabs. We can't use formline, even |
574 | # though we'd really like to, because it screws up non-printing characters. |
575 | # So we have to do the wrapping ourselves. |
576 | sub wrap { |
577 | my $self = shift; |
578 | local $_ = shift; |
579 | my $output = ''; |
580 | my $spaces = ' ' x $$self{MARGIN}; |
581 | my $width = $$self{width} - $$self{MARGIN}; |
582 | while (length > $width) { |
583 | if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) { |
584 | $output .= $spaces . $1 . "\n"; |
585 | } else { |
586 | last; |
587 | } |
69e00e79 |
588 | } |
6055f9d4 |
589 | $output .= $spaces . $_; |
590 | $output =~ s/\s+$/\n\n/; |
591 | $output; |
69e00e79 |
592 | } |
593 | |
6055f9d4 |
594 | # Reformat a paragraph of text for the current margin. Takes the text to |
595 | # reformat and returns the formatted text. |
596 | sub reformat { |
597 | my $self = shift; |
598 | local $_ = shift; |
69e00e79 |
599 | |
6055f9d4 |
600 | # If we're trying to preserve two spaces after sentences, do some |
601 | # munging to support that. Otherwise, smash all repeated whitespace. |
602 | if ($$self{sentence}) { |
603 | s/ +$//mg; |
604 | s/\.\n/. \n/g; |
605 | s/\n/ /g; |
606 | s/ +/ /g; |
69e00e79 |
607 | } else { |
6055f9d4 |
608 | s/\s+/ /g; |
69e00e79 |
609 | } |
6055f9d4 |
610 | $self->wrap ($_); |
69e00e79 |
611 | } |
612 | |
6055f9d4 |
613 | # Output text to the output device. |
614 | sub output { $_[1] =~ tr/\01/ /; print { $_[0]->output_handle } $_[1] } |
69e00e79 |
615 | |
69e00e79 |
616 | |
6055f9d4 |
617 | ############################################################################ |
27f805f4 |
618 | # Backwards compatibility |
619 | ############################################################################ |
620 | |
621 | # The old Pod::Text module did everything in a pod2text() function. This |
622 | # tries to provide the same interface for legacy applications. |
623 | sub pod2text { |
624 | my @args; |
625 | |
626 | # This is really ugly; I hate doing option parsing in the middle of a |
627 | # module. But the old Pod::Text module supported passing flags to its |
628 | # entry function, so handle -a and -<number>. |
629 | while ($_[0] =~ /^-/) { |
630 | my $flag = shift; |
631 | if ($flag eq '-a') { push (@args, alt => 1) } |
632 | elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) } |
633 | else { |
634 | unshift (@_, $flag); |
635 | last; |
636 | } |
637 | } |
638 | |
639 | # Now that we know what arguments we're using, create the parser. |
640 | my $parser = Pod::Text->new (@args); |
641 | |
642 | # If two arguments were given, the second argument is going to be a file |
643 | # handle. That means we want to call parse_from_filehandle(), which |
644 | # means we need to turn the first argument into a file handle. Magic |
645 | # open will handle the <&STDIN case automagically. |
646 | if (defined $_[1]) { |
ab1f1d91 |
647 | my @fhs = @_; |
27f805f4 |
648 | local *IN; |
ab1f1d91 |
649 | unless (open (IN, $fhs[0])) { |
650 | croak ("Can't open $fhs[0] for reading: $!\n"); |
27f805f4 |
651 | return; |
652 | } |
ab1f1d91 |
653 | $fhs[0] = \*IN; |
654 | return $parser->parse_from_filehandle (@fhs); |
27f805f4 |
655 | } else { |
656 | return $parser->parse_from_file (@_); |
657 | } |
658 | } |
659 | |
660 | |
661 | ############################################################################ |
6055f9d4 |
662 | # Module return value and documentation |
663 | ############################################################################ |
69e00e79 |
664 | |
6055f9d4 |
665 | 1; |
666 | __END__ |
69e00e79 |
667 | |
6055f9d4 |
668 | =head1 NAME |
69e00e79 |
669 | |
6055f9d4 |
670 | Pod::Text - Convert POD data to formatted ASCII text |
69e00e79 |
671 | |
6055f9d4 |
672 | =head1 SYNOPSIS |
69e00e79 |
673 | |
6055f9d4 |
674 | use Pod::Text; |
675 | my $parser = Pod::Text->new (sentence => 0, width => 78); |
69e00e79 |
676 | |
6055f9d4 |
677 | # Read POD from STDIN and write to STDOUT. |
678 | $parser->parse_from_filehandle; |
69e00e79 |
679 | |
6055f9d4 |
680 | # Read POD from file.pod and write to file.txt. |
681 | $parser->parse_from_file ('file.pod', 'file.txt'); |
69e00e79 |
682 | |
6055f9d4 |
683 | =head1 DESCRIPTION |
5491a304 |
684 | |
27f805f4 |
685 | Pod::Text is a module that can convert documentation in the POD format (the |
686 | preferred language for documenting Perl) into formatted ASCII. It uses no |
687 | special formatting controls or codes whatsoever, and its output is therefore |
688 | suitable for nearly any device. |
69e00e79 |
689 | |
27f805f4 |
690 | As a derived class from Pod::Parser, Pod::Text supports the same methods and |
691 | interfaces. See L<Pod::Parser> for all the details; briefly, one creates a |
692 | new parser with C<Pod::Text-E<gt>new()> and then calls either |
693 | parse_from_filehandle() or parse_from_file(). |
6055f9d4 |
694 | |
27f805f4 |
695 | new() can take options, in the form of key/value pairs, that control the |
6055f9d4 |
696 | behavior of the parser. The currently recognized options are: |
697 | |
698 | =over 4 |
699 | |
700 | =item alt |
701 | |
702 | If set to a true value, selects an alternate output format that, among other |
703 | things, uses a different heading style and marks C<=item> entries with a |
704 | colon in the left margin. Defaults to false. |
705 | |
706 | =item indent |
707 | |
708 | The number of spaces to indent regular text, and the default indentation for |
709 | C<=over> blocks. Defaults to 4. |
710 | |
711 | =item loose |
712 | |
713 | If set to a true value, a blank line is printed after a C<=head1> heading. |
714 | If set to false (the default), no blank line is printed after C<=head1>, |
715 | although one is still printed after C<=head2>. This is the default because |
716 | it's the expected formatting for manual pages; if you're formatting |
717 | arbitrary text documents, setting this to true may result in more pleasing |
718 | output. |
719 | |
ab1f1d91 |
720 | =item quotes |
721 | |
722 | Sets the quote marks used to surround CE<lt>> text. If the value is a |
723 | single character, it is used as both the left and right quote; if it is two |
724 | characters, the first character is used as the left quote and the second as |
725 | the right quoted; and if it is four characters, the first two are used as |
726 | the left quote and the second two as the right quote. |
727 | |
728 | This may also be set to the special value C<none>, in which case no quote |
729 | marks are added around CE<lt>> text. |
730 | |
6055f9d4 |
731 | =item sentence |
732 | |
27f805f4 |
733 | If set to a true value, Pod::Text will assume that each sentence ends in two |
734 | spaces, and will try to preserve that spacing. If set to false, all |
6055f9d4 |
735 | consecutive whitespace in non-verbatim paragraphs is compressed into a |
736 | single space. Defaults to true. |
737 | |
738 | =item width |
739 | |
740 | The column at which to wrap text on the right-hand side. Defaults to 76. |
741 | |
742 | =back |
743 | |
27f805f4 |
744 | The standard Pod::Parser method parse_from_filehandle() takes up to two |
6055f9d4 |
745 | arguments, the first being the file handle to read POD from and the second |
746 | being the file handle to write the formatted output to. The first defaults |
747 | to STDIN if not given, and the second defaults to STDOUT. The method |
27f805f4 |
748 | parse_from_file() is almost identical, except that its two arguments are the |
749 | input and output disk files instead. See L<Pod::Parser> for the specific |
750 | details. |
6055f9d4 |
751 | |
752 | =head1 DIAGNOSTICS |
753 | |
754 | =over 4 |
755 | |
27f805f4 |
756 | =item Bizarre space in item |
757 | |
758 | (W) Something has gone wrong in internal C<=item> processing. This message |
759 | indicates a bug in Pod::Text; you should never see it. |
760 | |
761 | =item Can't open %s for reading: %s |
762 | |
763 | (F) Pod::Text was invoked via the compatibility mode pod2text() interface |
764 | and the input file it was given could not be opened. |
765 | |
ab1f1d91 |
766 | =item Invalid quote specification "%s" |
767 | |
768 | (F) The quote specification given (the quotes option to the constructor) was |
769 | invalid. A quote specification must be one, two, or four characters long. |
770 | |
771 | =item %s:%d: Unknown command paragraph "%s". |
772 | |
773 | (W) The POD source contained a non-standard command paragraph (something of |
774 | the form C<=command args>) that Pod::Man didn't know about. It was ignored. |
775 | |
6055f9d4 |
776 | =item Unknown escape: %s |
777 | |
27f805f4 |
778 | (W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Text didn't |
779 | know about. |
6055f9d4 |
780 | |
781 | =item Unknown sequence: %s |
782 | |
27f805f4 |
783 | (W) The POD source contained a non-standard internal sequence (something of |
784 | the form C<XE<lt>E<gt>>) that Pod::Text didn't know about. |
6055f9d4 |
785 | |
786 | =item Unmatched =back |
787 | |
27f805f4 |
788 | (W) Pod::Text encountered a C<=back> command that didn't correspond to an |
6055f9d4 |
789 | C<=over> command. |
790 | |
791 | =back |
792 | |
27f805f4 |
793 | =head1 RESTRICTIONS |
794 | |
795 | Embedded Ctrl-As (octal 001) in the input will be mapped to spaces on |
796 | output, due to an internal implementation detail. |
797 | |
6055f9d4 |
798 | =head1 NOTES |
799 | |
27f805f4 |
800 | This is a replacement for an earlier Pod::Text module written by Tom |
801 | Christiansen. It has a revamped interface, since it now uses Pod::Parser, |
802 | but an interface roughly compatible with the old Pod::Text::pod2text() |
803 | function is still available. Please change to the new calling convention, |
804 | though. |
6055f9d4 |
805 | |
806 | The original Pod::Text contained code to do formatting via termcap |
807 | sequences, although it wasn't turned on by default and it was problematic to |
27f805f4 |
808 | get it to work at all. This rewrite doesn't even try to do that, but a |
809 | subclass of it does. Look for L<Pod::Text::Termcap|Pod::Text::Termcap>. |
6055f9d4 |
810 | |
811 | =head1 SEE ALSO |
812 | |
27f805f4 |
813 | L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>, |
814 | pod2text(1) |
6055f9d4 |
815 | |
816 | =head1 AUTHOR |
817 | |
818 | Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the |
819 | original Pod::Text by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt> and |
820 | its conversion to Pod::Parser by Brad Appleton |
821 | E<lt>bradapp@enteract.comE<gt>. |
822 | |
823 | =cut |