Commit | Line | Data |
6055f9d4 |
1 | # Pod::Text -- Convert POD data to formatted ASCII text. |
b7ae008f |
2 | # $Id: Text.pm,v 3.1 2005/03/19 19:40:01 eagle Exp $ |
6055f9d4 |
3 | # |
b7ae008f |
4 | # Copyright 1999, 2000, 2001, 2002, 2004 by Russ Allbery <rra@stanford.edu> |
6055f9d4 |
5 | # |
3c014959 |
6 | # This program is free software; you may redistribute it and/or modify it |
6055f9d4 |
7 | # under the same terms as Perl itself. |
8 | # |
5ec554fb |
9 | # This module converts POD to formatted text. It replaces the old Pod::Text |
10 | # module that came with versions of Perl prior to 5.6.0 and attempts to match |
11 | # its output except for some specific circumstances where other decisions |
12 | # seemed to produce better output. It uses Pod::Parser and is designed to be |
13 | # very easy to subclass. |
3c014959 |
14 | # |
15 | # Perl core hackers, please note that this module is also separately |
16 | # maintained outside of the Perl core as part of the podlators. Please send |
17 | # me any patches at the address above in addition to sending them to the |
18 | # standard Perl mailing lists. |
6055f9d4 |
19 | |
3c014959 |
20 | ############################################################################## |
6055f9d4 |
21 | # Modules and declarations |
3c014959 |
22 | ############################################################################## |
69e00e79 |
23 | |
6055f9d4 |
24 | package Pod::Text; |
69e00e79 |
25 | |
6055f9d4 |
26 | require 5.004; |
27 | |
6055f9d4 |
28 | use strict; |
2e20e14f |
29 | use vars qw(@ISA @EXPORT %ESCAPES $VERSION); |
6055f9d4 |
30 | |
b7ae008f |
31 | use Carp qw(carp croak); |
32 | use Exporter (); |
33 | use Pod::Simple (); |
34 | |
35 | @ISA = qw(Pod::Simple Exporter); |
6055f9d4 |
36 | |
2e20e14f |
37 | # We have to export pod2text for backward compatibility. |
38 | @EXPORT = qw(pod2text); |
39 | |
3c014959 |
40 | # Don't use the CVS revision as the version, since this module is also in Perl |
41 | # core and too many things could munge CVS magic revision strings. This |
42 | # number should ideally be the same as the CVS revision in podlators, however. |
b7ae008f |
43 | $VERSION = 3.01; |
69e00e79 |
44 | |
3c014959 |
45 | ############################################################################## |
6055f9d4 |
46 | # Initialization |
3c014959 |
47 | ############################################################################## |
69e00e79 |
48 | |
b7ae008f |
49 | # This function handles code blocks. It's registered as a callback to |
50 | # Pod::Simple and therefore doesn't work as a regular method call, but all it |
51 | # does is call output_code with the line. |
52 | sub handle_code { |
53 | my ($line, $number, $parser) = @_; |
54 | $parser->output_code ($line . "\n"); |
55 | } |
56 | |
57 | # Initialize the object and set various Pod::Simple options that we need. |
58 | # Here, we also process any additional options passed to the constructor or |
59 | # set up defaults if none were given. Note that all internal object keys are |
60 | # in all-caps, reserving all lower-case object keys for Pod::Simple and user |
61 | # arguments. |
62 | sub new { |
63 | my $class = shift; |
64 | my $self = $class->SUPER::new; |
65 | |
66 | # Tell Pod::Simple to handle S<> by automatically inserting . |
67 | $self->nbsp_for_S (1); |
68 | |
69 | # Tell Pod::Simple to keep whitespace whenever possible. |
70 | if ($self->can ('preserve_whitespace')) { |
71 | $self->preserve_whitespace (1); |
72 | } else { |
73 | $self->fullstop_space_harden (1); |
74 | } |
69e00e79 |
75 | |
b7ae008f |
76 | # The =for and =begin targets that we accept. |
77 | $self->accept_targets (qw/text TEXT/); |
78 | |
79 | # Ensure that contiguous blocks of code are merged together. Otherwise, |
80 | # some of the guesswork heuristics don't work right. |
81 | $self->merge_text (1); |
82 | |
83 | # Pod::Simple doesn't do anything useful with our arguments, but we want |
84 | # to put them in our object as hash keys and values. This could cause |
85 | # problems if we ever clash with Pod::Simple's own internal class |
86 | # variables. |
87 | my %opts = @_; |
88 | my @opts = map { ("opt_$_", $opts{$_}) } keys %opts; |
89 | %$self = (%$self, @opts); |
90 | |
91 | # Initialize various things from our parameters. |
92 | $$self{opt_alt} = 0 unless defined $$self{opt_alt}; |
93 | $$self{opt_indent} = 4 unless defined $$self{opt_indent}; |
94 | $$self{opt_margin} = 0 unless defined $$self{opt_margin}; |
95 | $$self{opt_loose} = 0 unless defined $$self{opt_loose}; |
96 | $$self{opt_sentence} = 0 unless defined $$self{opt_sentence}; |
97 | $$self{opt_width} = 76 unless defined $$self{opt_width}; |
69e00e79 |
98 | |
ab1f1d91 |
99 | # Figure out what quotes we'll be using for C<> text. |
b7ae008f |
100 | $$self{opt_quotes} ||= '"'; |
101 | if ($$self{opt_quotes} eq 'none') { |
ab1f1d91 |
102 | $$self{LQUOTE} = $$self{RQUOTE} = ''; |
b7ae008f |
103 | } elsif (length ($$self{opt_quotes}) == 1) { |
104 | $$self{LQUOTE} = $$self{RQUOTE} = $$self{opt_quotes}; |
105 | } elsif ($$self{opt_quotes} =~ /^(.)(.)$/ |
106 | || $$self{opt_quotes} =~ /^(..)(..)$/) { |
ab1f1d91 |
107 | $$self{LQUOTE} = $1; |
108 | $$self{RQUOTE} = $2; |
109 | } else { |
b7ae008f |
110 | croak qq(Invalid quote specification "$$self{opt_quotes}"); |
ab1f1d91 |
111 | } |
112 | |
b7ae008f |
113 | # If requested, do something with the non-POD text. |
114 | $self->code_handler (\&handle_code) if $$self{opt_code}; |
11f72409 |
115 | |
b7ae008f |
116 | # Return the created object. |
117 | return $self; |
118 | } |
69e00e79 |
119 | |
b7ae008f |
120 | ############################################################################## |
121 | # Core parsing |
122 | ############################################################################## |
59548eca |
123 | |
b7ae008f |
124 | # This is the glue that connects the code below with Pod::Simple itself. The |
125 | # goal is to convert the event stream coming from the POD parser into method |
126 | # calls to handlers once the complete content of a tag has been seen. Each |
127 | # paragraph or POD command will have textual content associated with it, and |
128 | # as soon as all of a paragraph or POD command has been seen, that content |
129 | # will be passed in to the corresponding method for handling that type of |
130 | # object. The exceptions are handlers for lists, which have opening tag |
131 | # handlers and closing tag handlers that will be called right away. |
132 | # |
133 | # The internal hash key PENDING is used to store the contents of a tag until |
134 | # all of it has been seen. It holds a stack of open tags, each one |
135 | # represented by a tuple of the attributes hash for the tag and the contents |
136 | # of the tag. |
137 | |
138 | # Add a block of text to the contents of the current node, formatting it |
139 | # according to the current formatting instructions as we do. |
140 | sub _handle_text { |
141 | my ($self, $text) = @_; |
142 | my $tag = $$self{PENDING}[-1]; |
143 | $$tag[1] .= $text; |
6055f9d4 |
144 | } |
69e00e79 |
145 | |
b7ae008f |
146 | # Given an element name, get the corresponding method name. |
147 | sub method_for_element { |
148 | my ($self, $element) = @_; |
149 | $element =~ tr/-/_/; |
150 | $element =~ tr/A-Z/a-z/; |
151 | $element =~ tr/_a-z0-9//cd; |
152 | return $element; |
153 | } |
69e00e79 |
154 | |
b7ae008f |
155 | # Handle the start of a new element. If cmd_element is defined, assume that |
156 | # we need to collect the entire tree for this element before passing it to the |
157 | # element method, and create a new tree into which we'll collect blocks of |
158 | # text and nested elements. Otherwise, if start_element is defined, call it. |
159 | sub _handle_element_start { |
160 | my ($self, $element, $attrs) = @_; |
161 | my $method = $self->method_for_element ($element); |
162 | |
163 | # If we have a command handler, we need to accumulate the contents of the |
164 | # tag before calling it. |
165 | if ($self->can ("cmd_$method")) { |
166 | push (@{ $$self{PENDING} }, [ $attrs, '' ]); |
167 | } elsif ($self->can ("start_$method")) { |
168 | my $method = 'start_' . $method; |
169 | $self->$method ($attrs, ''); |
170 | } |
171 | } |
6055f9d4 |
172 | |
b7ae008f |
173 | # Handle the end of an element. If we had a cmd_ method for this element, |
174 | # this is where we pass along the text that we've accumulated. Otherwise, if |
175 | # we have an end_ method for the element, call that. |
176 | sub _handle_element_end { |
177 | my ($self, $element) = @_; |
178 | my $method = $self->method_for_element ($element); |
179 | |
180 | # If we have a command handler, pull off the pending text and pass it to |
181 | # the handler along with the saved attribute hash. |
182 | if ($self->can ("cmd_$method")) { |
183 | my $tag = pop @{ $$self{PENDING} }; |
184 | my $method = 'cmd_' . $method; |
185 | my $text = $self->$method (@$tag); |
186 | if (defined $text) { |
187 | if (@{ $$self{PENDING} } > 1) { |
188 | $$self{PENDING}[-1][1] .= $text; |
189 | } else { |
190 | $self->output ($text); |
191 | } |
192 | } |
193 | } elsif ($self->can ("end_$method")) { |
194 | my $method = 'end_' . $method; |
195 | $self->$method; |
ab1f1d91 |
196 | } |
6055f9d4 |
197 | } |
69e00e79 |
198 | |
b7ae008f |
199 | ############################################################################## |
200 | # Output formatting |
201 | ############################################################################## |
202 | |
203 | # Wrap a line, indenting by the current left margin. We can't use Text::Wrap |
204 | # because it plays games with tabs. We can't use formline, even though we'd |
205 | # really like to, because it screws up non-printing characters. So we have to |
206 | # do the wrapping ourselves. |
207 | sub wrap { |
6055f9d4 |
208 | my $self = shift; |
6055f9d4 |
209 | local $_ = shift; |
b7ae008f |
210 | my $output = ''; |
211 | my $spaces = ' ' x $$self{MARGIN}; |
212 | my $width = $$self{opt_width} - $$self{MARGIN}; |
213 | while (length > $width) { |
214 | if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) { |
215 | $output .= $spaces . $1 . "\n"; |
216 | } else { |
217 | last; |
218 | } |
219 | } |
220 | $output .= $spaces . $_; |
221 | $output =~ s/\s+$/\n\n/; |
222 | return $output; |
6055f9d4 |
223 | } |
69e00e79 |
224 | |
b7ae008f |
225 | # Reformat a paragraph of text for the current margin. Takes the text to |
226 | # reformat and returns the formatted text. |
227 | sub reformat { |
27f805f4 |
228 | my $self = shift; |
27f805f4 |
229 | local $_ = shift; |
6055f9d4 |
230 | |
b7ae008f |
231 | # If we're trying to preserve two spaces after sentences, do some munging |
232 | # to support that. Otherwise, smash all repeated whitespace. |
233 | if ($$self{opt_sentence}) { |
234 | s/ +$//mg; |
235 | s/\.\n/. \n/g; |
236 | s/\n/ /g; |
237 | s/ +/ /g; |
6055f9d4 |
238 | } else { |
b7ae008f |
239 | s/\s+/ /g; |
6055f9d4 |
240 | } |
b7ae008f |
241 | return $self->wrap ($_); |
6055f9d4 |
242 | } |
69e00e79 |
243 | |
b7ae008f |
244 | # Output text to the output device. |
245 | sub output { |
246 | my ($self, $text) = @_; |
247 | $text =~ tr/\240\255/ /d; |
248 | print { $$self{output_fh} } $text; |
249 | } |
bf202ccd |
250 | |
b7ae008f |
251 | # Output a block of code (something that isn't part of the POD text). Called |
252 | # by preprocess_paragraph only if we were given the code option. Exists here |
253 | # only so that it can be overridden by subclasses. |
254 | sub output_code { $_[0]->output ($_[1]) } |
69e00e79 |
255 | |
b7ae008f |
256 | ############################################################################## |
257 | # Document initialization |
258 | ############################################################################## |
259 | |
260 | # Set up various things that have to be initialized on a per-document basis. |
261 | sub start_document { |
262 | my $self = shift; |
263 | my $margin = $$self{opt_indent} + $$self{opt_margin}; |
264 | |
265 | # Initialize a few per-document variables. |
266 | $$self{INDENTS} = []; # Stack of indentations. |
267 | $$self{MARGIN} = $margin; # Default left margin. |
268 | $$self{PENDING} = [[]]; # Pending output. |
269 | |
270 | return ''; |
271 | } |
272 | |
273 | ############################################################################## |
274 | # Text blocks |
275 | ############################################################################## |
276 | |
277 | # This method is called whenever an =item command is complete (in other words, |
278 | # we've seen its associated paragraph or know for certain that it doesn't have |
279 | # one). It gets the paragraph associated with the item as an argument. If |
280 | # that argument is empty, just output the item tag; if it contains a newline, |
281 | # output the item tag followed by the newline. Otherwise, see if there's |
282 | # enough room for us to output the item tag in the margin of the text or if we |
283 | # have to put it on a separate line. |
284 | sub item { |
285 | my ($self, $text) = @_; |
286 | my $tag = $$self{ITEM}; |
287 | unless (defined $tag) { |
288 | carp "Item called without tag"; |
289 | return; |
6055f9d4 |
290 | } |
b7ae008f |
291 | undef $$self{ITEM}; |
69e00e79 |
292 | |
b7ae008f |
293 | # Calculate the indentation and margin. $fits is set to true if the tag |
294 | # will fit into the margin of the paragraph given our indentation level. |
295 | my $indent = $$self{INDENTS}[-1]; |
296 | $indent = $$self{opt_indent} unless defined $indent; |
297 | my $margin = ' ' x $$self{opt_margin}; |
298 | my $fits = ($$self{MARGIN} - $indent >= length ($tag) + 1); |
69e00e79 |
299 | |
b7ae008f |
300 | # If the tag doesn't fit, or if we have no associated text, print out the |
301 | # tag separately. Otherwise, put the tag in the margin of the paragraph. |
302 | if (!$text || $text =~ /^\s+$/ || !$fits) { |
303 | my $realindent = $$self{MARGIN}; |
304 | $$self{MARGIN} = $indent; |
305 | my $output = $self->reformat ($tag); |
306 | $output =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0); |
307 | $output =~ s/\n*$/\n/; |
308 | |
309 | # If the text is just whitespace, we have an empty item paragraph; |
310 | # this can result from =over/=item/=back without any intermixed |
311 | # paragraphs. Insert some whitespace to keep the =item from merging |
312 | # into the next paragraph. |
313 | $output .= "\n" if $text && $text =~ /^\s*$/; |
314 | |
315 | $self->output ($output); |
316 | $$self{MARGIN} = $realindent; |
317 | $self->output ($self->reformat ($text)) if ($text && $text =~ /\S/); |
318 | } else { |
319 | my $space = ' ' x $indent; |
320 | $space =~ s/^$margin /$margin:/ if $$self{opt_alt}; |
321 | $text = $self->reformat ($text); |
322 | $text =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0); |
323 | my $tagspace = ' ' x length $tag; |
324 | $text =~ s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item"; |
325 | $self->output ($text); |
6055f9d4 |
326 | } |
b7ae008f |
327 | } |
69e00e79 |
328 | |
b7ae008f |
329 | # Handle a basic block of text. The only tricky thing here is that if there |
330 | # is a pending item tag, we need to format this as an item paragraph. |
331 | sub cmd_para { |
332 | my ($self, $attrs, $text) = @_; |
333 | $text =~ s/\s+$/\n/; |
334 | if (defined $$self{ITEM}) { |
335 | $self->item ($text . "\n"); |
336 | } else { |
337 | $self->output ($self->reformat ($text . "\n")); |
59548eca |
338 | } |
b7ae008f |
339 | return ''; |
6055f9d4 |
340 | } |
f02a87df |
341 | |
b7ae008f |
342 | # Handle a verbatim paragraph. Just print it out, but indent it according to |
343 | # our margin. |
344 | sub cmd_verbatim { |
345 | my ($self, $attrs, $text) = @_; |
346 | $self->item if defined $$self{ITEM}; |
347 | return if $text =~ /^\s*$/; |
348 | $text =~ s/^(\n*)(\s*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme; |
349 | $text =~ s/\s*$/\n\n/; |
350 | $self->output ($text); |
351 | return ''; |
6055f9d4 |
352 | } |
3ec07288 |
353 | |
b7ae008f |
354 | # Handle literal text (produced by =for and similar constructs). Just output |
355 | # it with the minimum of changes. |
356 | sub cmd_data { |
357 | my ($self, $attrs, $text) = @_; |
358 | $text =~ s/^\n+//; |
359 | $text =~ s/\n{0,2}$/\n/; |
360 | $self->output ($text); |
361 | return ''; |
362 | } |
69e00e79 |
363 | |
3c014959 |
364 | ############################################################################## |
b7ae008f |
365 | # Headings |
3c014959 |
366 | ############################################################################## |
f2506fb2 |
367 | |
b7ae008f |
368 | # The common code for handling all headers. Takes the header text, the |
369 | # indentation, and the surrounding marker for the alt formatting method. |
370 | sub heading { |
371 | my ($self, $text, $indent, $marker) = @_; |
372 | $self->item ("\n\n") if defined $$self{ITEM}; |
373 | $text =~ s/\s+$//; |
374 | if ($$self{opt_alt}) { |
375 | my $closemark = reverse (split (//, $marker)); |
376 | my $margin = ' ' x $$self{opt_margin}; |
377 | $self->output ("\n" . "$margin$marker $text $closemark" . "\n\n"); |
378 | } else { |
379 | $text .= "\n" if $$self{opt_loose}; |
380 | my $margin = ' ' x ($$self{opt_margin} + $indent); |
381 | $self->output ($margin . $text . "\n"); |
382 | } |
383 | return ''; |
384 | } |
69e00e79 |
385 | |
6055f9d4 |
386 | # First level heading. |
387 | sub cmd_head1 { |
b7ae008f |
388 | my ($self, $attrs, $text) = @_; |
389 | $self->heading ($text, 0, '===='); |
6055f9d4 |
390 | } |
69e00e79 |
391 | |
6055f9d4 |
392 | # Second level heading. |
393 | sub cmd_head2 { |
b7ae008f |
394 | my ($self, $attrs, $text) = @_; |
395 | $self->heading ($text, $$self{opt_indent} / 2, '== '); |
6055f9d4 |
396 | } |
69e00e79 |
397 | |
50a3fd2a |
398 | # Third level heading. |
399 | sub cmd_head3 { |
b7ae008f |
400 | my ($self, $attrs, $text) = @_; |
401 | $self->heading ($text, $$self{opt_indent} * 2 / 3 + 0.5, '= '); |
50a3fd2a |
402 | } |
403 | |
b7ae008f |
404 | # Fourth level heading. |
50a3fd2a |
405 | sub cmd_head4 { |
b7ae008f |
406 | my ($self, $attrs, $text) = @_; |
407 | $self->heading ($text, $$self{opt_indent} * 3 / 4 + 0.5, '- '); |
50a3fd2a |
408 | } |
409 | |
b7ae008f |
410 | ############################################################################## |
411 | # List handling |
412 | ############################################################################## |
413 | |
414 | # Handle the beginning of an =over block. Takes the type of the block as the |
415 | # first argument, and then the attr hash. This is called by the handlers for |
416 | # the four different types of lists (bullet, number, text, and block). |
417 | sub over_common_start { |
418 | my ($self, $attrs) = @_; |
b616daaf |
419 | $self->item ("\n\n") if defined $$self{ITEM}; |
b7ae008f |
420 | |
421 | # Find the indentation level. |
422 | my $indent = $$attrs{indent}; |
423 | unless (defined ($indent) && $indent =~ /^\s*[-+]?\d{1,4}\s*$/) { |
424 | $indent = $$self{opt_indent}; |
425 | } |
426 | |
427 | # Add this to our stack of indents and increase our current margin. |
6055f9d4 |
428 | push (@{ $$self{INDENTS} }, $$self{MARGIN}); |
b7ae008f |
429 | $$self{MARGIN} += ($indent + 0); |
430 | return ''; |
6055f9d4 |
431 | } |
69e00e79 |
432 | |
b7ae008f |
433 | # End an =over block. Takes no options other than the class pointer. Output |
434 | # any pending items and then pop one level of indentation. |
435 | sub over_common_end { |
436 | my ($self) = @_; |
b616daaf |
437 | $self->item ("\n\n") if defined $$self{ITEM}; |
6055f9d4 |
438 | $$self{MARGIN} = pop @{ $$self{INDENTS} }; |
b7ae008f |
439 | return ''; |
69e00e79 |
440 | } |
441 | |
b7ae008f |
442 | # Dispatch the start and end calls as appropriate. |
443 | sub start_over_bullet { $_[0]->over_common_start ($_[1]) } |
444 | sub start_over_number { $_[0]->over_common_start ($_[1]) } |
445 | sub start_over_text { $_[0]->over_common_start ($_[1]) } |
446 | sub start_over_block { $_[0]->over_common_start ($_[1]) } |
447 | sub end_over_bullet { $_[0]->over_common_end } |
448 | sub end_over_number { $_[0]->over_common_end } |
449 | sub end_over_text { $_[0]->over_common_end } |
450 | sub end_over_block { $_[0]->over_common_end } |
451 | |
452 | # The common handler for all item commands. Takes the type of the item, the |
453 | # attributes, and then the text of the item. |
454 | sub item_common { |
455 | my ($self, $type, $attrs, $text) = @_; |
456 | $self->item if defined $$self{ITEM}; |
69e00e79 |
457 | |
b7ae008f |
458 | # Clean up the text. We want to end up with two variables, one ($text) |
459 | # which contains any body text after taking out the item portion, and |
460 | # another ($item) which contains the actual item text. Note the use of |
461 | # the internal Pod::Simple attribute here; that's a potential land mine. |
462 | $text =~ s/\s+$//; |
463 | my ($item, $index); |
464 | if ($type eq 'bullet') { |
465 | $item = '*'; |
466 | } elsif ($type eq 'number') { |
467 | $item = $$attrs{'~orig_content'}; |
27f805f4 |
468 | } else { |
b7ae008f |
469 | $item = $text; |
470 | $item =~ s/\s*\n\s*/ /g; |
471 | $text = ''; |
27f805f4 |
472 | } |
b7ae008f |
473 | $$self{ITEM} = $item; |
6055f9d4 |
474 | |
b7ae008f |
475 | # If body text for this item was included, go ahead and output that now. |
476 | if ($text) { |
477 | $text =~ s/\s*$/\n/; |
478 | $self->item ($text); |
479 | } |
480 | return ''; |
6055f9d4 |
481 | } |
f2506fb2 |
482 | |
b7ae008f |
483 | # Dispatch the item commands to the appropriate place. |
484 | sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) } |
485 | sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) } |
486 | sub cmd_item_text { my $self = shift; $self->item_common ('text', @_) } |
487 | sub cmd_item_block { my $self = shift; $self->item_common ('block', @_) } |
69e00e79 |
488 | |
3c014959 |
489 | ############################################################################## |
5ec554fb |
490 | # Formatting codes |
3c014959 |
491 | ############################################################################## |
69e00e79 |
492 | |
b7ae008f |
493 | # The simple ones. |
494 | sub cmd_b { return $_[0]{alt} ? "``$_[2]''" : $_[2] } |
495 | sub cmd_f { return $_[0]{alt} ? "\"$_[2]\"" : $_[2] } |
496 | sub cmd_i { return '*' . $_[2] . '*' } |
497 | sub cmd_x { return '' } |
3c014959 |
498 | |
499 | # Apply a whole bunch of messy heuristics to not quote things that don't |
500 | # benefit from being quoted. These originally come from Barrie Slaymaker and |
501 | # largely duplicate code in Pod::Man. |
b7ae008f |
502 | sub cmd_c { |
503 | my ($self, $attrs, $text) = @_; |
3c014959 |
504 | |
505 | # A regex that matches the portion of a variable reference that's the |
506 | # array or hash index, separated out just because we want to use it in |
507 | # several places in the following regex. |
508 | my $index = '(?: \[.*\] | \{.*\} )?'; |
509 | |
510 | # Check for things that we don't want to quote, and if we find any of |
511 | # them, return the string with just a font change and no quoting. |
b7ae008f |
512 | $text =~ m{ |
3c014959 |
513 | ^\s* |
514 | (?: |
515 | ( [\'\`\"] ) .* \1 # already quoted |
516 | | \` .* \' # `quoted' |
517 | | \$+ [\#^]? \S $index # special ($^Foo, $") |
518 | | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func |
519 | | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call |
f011ec7d |
520 | | [+-]? ( \d[\d.]* | \.\d+ ) (?: [eE][+-]?\d+ )? # a number |
3c014959 |
521 | | 0x [a-fA-F\d]+ # a hex constant |
522 | ) |
523 | \s*\z |
b7ae008f |
524 | }xo && return $text; |
3c014959 |
525 | |
526 | # If we didn't return, go ahead and quote the text. |
b7ae008f |
527 | return $$self{opt_alt} |
528 | ? "``$text''" |
529 | : "$$self{LQUOTE}$text$$self{RQUOTE}"; |
69e00e79 |
530 | } |
531 | |
b7ae008f |
532 | # Links reduce to the text that we're given, wrapped in angle brackets if it's |
533 | # a URL. |
534 | sub cmd_l { |
535 | my ($self, $attrs, $text) = @_; |
536 | return $$attrs{type} eq 'url' ? "<$text>" : $text; |
b616daaf |
537 | } |
538 | |
3c014959 |
539 | ############################################################################## |
27f805f4 |
540 | # Backwards compatibility |
3c014959 |
541 | ############################################################################## |
27f805f4 |
542 | |
543 | # The old Pod::Text module did everything in a pod2text() function. This |
544 | # tries to provide the same interface for legacy applications. |
545 | sub pod2text { |
546 | my @args; |
547 | |
548 | # This is really ugly; I hate doing option parsing in the middle of a |
549 | # module. But the old Pod::Text module supported passing flags to its |
550 | # entry function, so handle -a and -<number>. |
551 | while ($_[0] =~ /^-/) { |
552 | my $flag = shift; |
553 | if ($flag eq '-a') { push (@args, alt => 1) } |
554 | elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) } |
555 | else { |
556 | unshift (@_, $flag); |
557 | last; |
558 | } |
559 | } |
560 | |
561 | # Now that we know what arguments we're using, create the parser. |
562 | my $parser = Pod::Text->new (@args); |
563 | |
564 | # If two arguments were given, the second argument is going to be a file |
3c014959 |
565 | # handle. That means we want to call parse_from_filehandle(), which means |
566 | # we need to turn the first argument into a file handle. Magic open will |
567 | # handle the <&STDIN case automagically. |
27f805f4 |
568 | if (defined $_[1]) { |
ab1f1d91 |
569 | my @fhs = @_; |
27f805f4 |
570 | local *IN; |
ab1f1d91 |
571 | unless (open (IN, $fhs[0])) { |
572 | croak ("Can't open $fhs[0] for reading: $!\n"); |
27f805f4 |
573 | return; |
574 | } |
ab1f1d91 |
575 | $fhs[0] = \*IN; |
b7ae008f |
576 | return $parser->parse_file (@fhs); |
27f805f4 |
577 | } else { |
b7ae008f |
578 | return $parser->parse_file (@_); |
27f805f4 |
579 | } |
580 | } |
581 | |
3c014959 |
582 | ############################################################################## |
6055f9d4 |
583 | # Module return value and documentation |
3c014959 |
584 | ############################################################################## |
69e00e79 |
585 | |
6055f9d4 |
586 | 1; |
587 | __END__ |
69e00e79 |
588 | |
6055f9d4 |
589 | =head1 NAME |
69e00e79 |
590 | |
6055f9d4 |
591 | Pod::Text - Convert POD data to formatted ASCII text |
69e00e79 |
592 | |
6055f9d4 |
593 | =head1 SYNOPSIS |
69e00e79 |
594 | |
6055f9d4 |
595 | use Pod::Text; |
596 | my $parser = Pod::Text->new (sentence => 0, width => 78); |
69e00e79 |
597 | |
6055f9d4 |
598 | # Read POD from STDIN and write to STDOUT. |
599 | $parser->parse_from_filehandle; |
69e00e79 |
600 | |
6055f9d4 |
601 | # Read POD from file.pod and write to file.txt. |
602 | $parser->parse_from_file ('file.pod', 'file.txt'); |
69e00e79 |
603 | |
6055f9d4 |
604 | =head1 DESCRIPTION |
5491a304 |
605 | |
27f805f4 |
606 | Pod::Text is a module that can convert documentation in the POD format (the |
607 | preferred language for documenting Perl) into formatted ASCII. It uses no |
608 | special formatting controls or codes whatsoever, and its output is therefore |
609 | suitable for nearly any device. |
69e00e79 |
610 | |
b7ae008f |
611 | As a derived class from Pod::Simple, Pod::Text supports the same methods and |
612 | interfaces. See L<Pod::Simple> for all the details; briefly, one creates a |
613 | new parser with C<< Pod::Text->new() >> and then normally calls parse_file(). |
6055f9d4 |
614 | |
27f805f4 |
615 | new() can take options, in the form of key/value pairs, that control the |
6055f9d4 |
616 | behavior of the parser. The currently recognized options are: |
617 | |
618 | =over 4 |
619 | |
620 | =item alt |
621 | |
622 | If set to a true value, selects an alternate output format that, among other |
623 | things, uses a different heading style and marks C<=item> entries with a |
624 | colon in the left margin. Defaults to false. |
625 | |
59548eca |
626 | =item code |
627 | |
628 | If set to a true value, the non-POD parts of the input file will be included |
629 | in the output. Useful for viewing code documented with POD blocks with the |
630 | POD rendered and the code left intact. |
631 | |
6055f9d4 |
632 | =item indent |
633 | |
634 | The number of spaces to indent regular text, and the default indentation for |
635 | C<=over> blocks. Defaults to 4. |
636 | |
637 | =item loose |
638 | |
639 | If set to a true value, a blank line is printed after a C<=head1> heading. |
640 | If set to false (the default), no blank line is printed after C<=head1>, |
641 | although one is still printed after C<=head2>. This is the default because |
642 | it's the expected formatting for manual pages; if you're formatting |
643 | arbitrary text documents, setting this to true may result in more pleasing |
644 | output. |
645 | |
11f72409 |
646 | =item margin |
647 | |
648 | The width of the left margin in spaces. Defaults to 0. This is the margin |
649 | for all text, including headings, not the amount by which regular text is |
650 | indented; for the latter, see the I<indent> option. To set the right |
651 | margin, see the I<width> option. |
652 | |
ab1f1d91 |
653 | =item quotes |
654 | |
655 | Sets the quote marks used to surround CE<lt>> text. If the value is a |
656 | single character, it is used as both the left and right quote; if it is two |
657 | characters, the first character is used as the left quote and the second as |
658 | the right quoted; and if it is four characters, the first two are used as |
659 | the left quote and the second two as the right quote. |
660 | |
661 | This may also be set to the special value C<none>, in which case no quote |
662 | marks are added around CE<lt>> text. |
663 | |
6055f9d4 |
664 | =item sentence |
665 | |
27f805f4 |
666 | If set to a true value, Pod::Text will assume that each sentence ends in two |
667 | spaces, and will try to preserve that spacing. If set to false, all |
6055f9d4 |
668 | consecutive whitespace in non-verbatim paragraphs is compressed into a |
669 | single space. Defaults to true. |
670 | |
671 | =item width |
672 | |
673 | The column at which to wrap text on the right-hand side. Defaults to 76. |
674 | |
675 | =back |
676 | |
b7ae008f |
677 | The standard Pod::Simple method parse_file() takes one argument, the file or |
678 | file handle to read from, and writes output to standard output unless that |
679 | has been changed with the output_fh() method. See L<Pod::Simple> for the |
680 | specific details and for other alternative interfaces. |
6055f9d4 |
681 | |
682 | =head1 DIAGNOSTICS |
683 | |
684 | =over 4 |
685 | |
27f805f4 |
686 | =item Bizarre space in item |
687 | |
59548eca |
688 | =item Item called without tag |
689 | |
690 | (W) Something has gone wrong in internal C<=item> processing. These |
691 | messages indicate a bug in Pod::Text; you should never see them. |
27f805f4 |
692 | |
693 | =item Can't open %s for reading: %s |
694 | |
695 | (F) Pod::Text was invoked via the compatibility mode pod2text() interface |
696 | and the input file it was given could not be opened. |
697 | |
ab1f1d91 |
698 | =item Invalid quote specification "%s" |
699 | |
700 | (F) The quote specification given (the quotes option to the constructor) was |
701 | invalid. A quote specification must be one, two, or four characters long. |
702 | |
6055f9d4 |
703 | =back |
704 | |
705 | =head1 NOTES |
706 | |
27f805f4 |
707 | This is a replacement for an earlier Pod::Text module written by Tom |
b7ae008f |
708 | Christiansen. It has a revamped interface, since it now uses Pod::Simple, |
27f805f4 |
709 | but an interface roughly compatible with the old Pod::Text::pod2text() |
710 | function is still available. Please change to the new calling convention, |
711 | though. |
6055f9d4 |
712 | |
713 | The original Pod::Text contained code to do formatting via termcap |
714 | sequences, although it wasn't turned on by default and it was problematic to |
27f805f4 |
715 | get it to work at all. This rewrite doesn't even try to do that, but a |
bf202ccd |
716 | subclass of it does. Look for L<Pod::Text::Termcap>. |
6055f9d4 |
717 | |
718 | =head1 SEE ALSO |
719 | |
b7ae008f |
720 | L<Pod::Simple>, L<Pod::Text::Termcap>, L<pod2text(1)> |
6055f9d4 |
721 | |
fd20da51 |
722 | The current version of this module is always available from its web site at |
723 | L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the |
724 | Perl core distribution as of 5.6.0. |
725 | |
6055f9d4 |
726 | =head1 AUTHOR |
727 | |
bf202ccd |
728 | Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original |
729 | Pod::Text by Tom Christiansen <tchrist@mox.perl.com> and its conversion to |
b7ae008f |
730 | Pod::Parser by Brad Appleton <bradapp@enteract.com>. Sean Burke's initial |
731 | conversion of Pod::Man to use Pod::Simple provided much-needed guidance on |
732 | how to use Pod::Simple. |
6055f9d4 |
733 | |
3c014959 |
734 | =head1 COPYRIGHT AND LICENSE |
735 | |
b7ae008f |
736 | Copyright 1999, 2000, 2001, 2002, 2004 by Russ Allbery <rra@stanford.edu>. |
3c014959 |
737 | |
738 | This program is free software; you may redistribute it and/or modify it |
739 | under the same terms as Perl itself. |
740 | |
6055f9d4 |
741 | =cut |