File Coverage

blib/lib/Pod/Text.pm
Criterion Covered Total %
statement 307 339 90.5
branch 106 140 75.7
condition 37 49 75.5
subroutine 57 58 98.2
pod 6 46 13.0
total 513 632 81.1


line stmt bran cond sub pod time code
1             # Pod::Text -- Convert POD data to formatted text.
2             #
3             # This module converts POD to formatted text. It replaces the old Pod::Text
4             # module that came with versions of Perl prior to 5.6.0 and attempts to match
5             # its output except for some specific circumstances where other decisions
6             # seemed to produce better output. It uses Pod::Parser and is designed to be
7             # very easy to subclass.
8             #
9             # Perl core hackers, please note that this module is also separately
10             # maintained outside of the Perl core as part of the podlators. Please send
11             # me any patches at the address above in addition to sending them to the
12             # standard Perl mailing lists.
13             #
14             # Copyright 1999, 2000, 2001, 2002, 2004, 2006, 2008, 2009, 2012, 2013, 2014,
15             # 2015 Russ Allbery
16             #
17             # This program is free software; you may redistribute it and/or modify it
18             # under the same terms as Perl itself.
19              
20             ##############################################################################
21             # Modules and declarations
22             ##############################################################################
23              
24             package Pod::Text;
25              
26 12     12   114990 use 5.006;
  12         37  
27 12     12   43 use strict;
  12         14  
  12         260  
28 12     12   39 use warnings;
  12         19  
  12         331  
29              
30 12     12   36 use vars qw(@ISA @EXPORT %ESCAPES $VERSION);
  12         11  
  12         672  
31              
32 12     12   38 use Carp qw(carp croak);
  12         12  
  12         711  
33 12     12   4842 use Encode qw(encode);
  12         75664  
  12         981  
34 12     12   60 use Exporter ();
  12         17  
  12         895  
35 12     12   5638 use Pod::Simple ();
  12         203890  
  12         36291  
36              
37             @ISA = qw(Pod::Simple Exporter);
38              
39             # We have to export pod2text for backward compatibility.
40             @EXPORT = qw(pod2text);
41              
42             $VERSION = '4.08';
43              
44             ##############################################################################
45             # Initialization
46             ##############################################################################
47              
48             # This function handles code blocks. It's registered as a callback to
49             # Pod::Simple and therefore doesn't work as a regular method call, but all it
50             # does is call output_code with the line.
51             sub handle_code {
52 8     8 0 191 my ($line, $number, $parser) = @_;
53 8         25 $parser->output_code ($line . "\n");
54             }
55              
56             # Initialize the object and set various Pod::Simple options that we need.
57             # Here, we also process any additional options passed to the constructor or
58             # set up defaults if none were given. Note that all internal object keys are
59             # in all-caps, reserving all lower-case object keys for Pod::Simple and user
60             # arguments.
61             sub new {
62 29     29 1 22676 my $class = shift;
63 29         172 my $self = $class->SUPER::new;
64              
65             # Tell Pod::Simple to handle S<> by automatically inserting  .
66 29         698 $self->nbsp_for_S (1);
67              
68             # Tell Pod::Simple to keep whitespace whenever possible.
69 29 50       448 if ($self->can ('preserve_whitespace')) {
70 29         83 $self->preserve_whitespace (1);
71             } else {
72 0         0 $self->fullstop_space_harden (1);
73             }
74              
75             # The =for and =begin targets that we accept.
76 29         210 $self->accept_targets (qw/text TEXT/);
77              
78             # Ensure that contiguous blocks of code are merged together. Otherwise,
79             # some of the guesswork heuristics don't work right.
80 29         581 $self->merge_text (1);
81              
82             # Pod::Simple doesn't do anything useful with our arguments, but we want
83             # to put them in our object as hash keys and values. This could cause
84             # problems if we ever clash with Pod::Simple's own internal class
85             # variables.
86 29         147 my %opts = @_;
87 29         64 my @opts = map { ("opt_$_", $opts{$_}) } keys %opts;
  16         57  
88 29         172 %$self = (%$self, @opts);
89              
90             # Send errors to stderr if requested.
91 29 100 66     126 if ($$self{opt_stderr} and not $$self{opt_errors}) {
92 1         3 $$self{opt_errors} = 'stderr';
93             }
94 29         38 delete $$self{opt_stderr};
95              
96             # Validate the errors parameter and act on it.
97 29 100       127 if (not defined $$self{opt_errors}) {
98 24         63 $$self{opt_errors} = 'pod';
99             }
100 29 100 100     216 if ($$self{opt_errors} eq 'stderr' || $$self{opt_errors} eq 'die') {
    100          
    50          
101 3         11 $self->no_errata_section (1);
102 3         15 $self->complain_stderr (1);
103 3 100       14 if ($$self{opt_errors} eq 'die') {
104 1         2 $$self{complain_die} = 1;
105             }
106             } elsif ($$self{opt_errors} eq 'pod') {
107 25         102 $self->no_errata_section (0);
108 25         179 $self->complain_stderr (0);
109             } elsif ($$self{opt_errors} eq 'none') {
110 1         6 $self->no_whining (1);
111             } else {
112 0         0 croak (qq(Invalid errors setting: "$$self{errors}"));
113             }
114 29         110 delete $$self{errors};
115              
116             # Initialize various things from our parameters.
117 29 100       131 $$self{opt_alt} = 0 unless defined $$self{opt_alt};
118 29 50       80 $$self{opt_indent} = 4 unless defined $$self{opt_indent};
119 29 100       80 $$self{opt_margin} = 0 unless defined $$self{opt_margin};
120 29 50       71 $$self{opt_loose} = 0 unless defined $$self{opt_loose};
121 29 100       69 $$self{opt_sentence} = 0 unless defined $$self{opt_sentence};
122 29 50       87 $$self{opt_width} = 76 unless defined $$self{opt_width};
123              
124             # Figure out what quotes we'll be using for C<> text.
125 29   100     128 $$self{opt_quotes} ||= '"';
126 29 50       99 if ($$self{opt_quotes} eq 'none') {
    100          
    50          
127 0         0 $$self{LQUOTE} = $$self{RQUOTE} = '';
128             } elsif (length ($$self{opt_quotes}) == 1) {
129 28         62 $$self{LQUOTE} = $$self{RQUOTE} = $$self{opt_quotes};
130             } elsif (length ($$self{opt_quotes}) % 2 == 0) {
131 1         3 my $length = length ($$self{opt_quotes}) / 2;
132 1         2 $$self{LQUOTE} = substr ($$self{opt_quotes}, 0, $length);
133 1         2 $$self{RQUOTE} = substr ($$self{opt_quotes}, $length);
134             } else {
135 0         0 croak qq(Invalid quote specification "$$self{opt_quotes}");
136             }
137              
138             # If requested, do something with the non-POD text.
139 29 100       70 $self->code_handler (\&handle_code) if $$self{opt_code};
140              
141             # Return the created object.
142 29         84 return $self;
143             }
144              
145             ##############################################################################
146             # Core parsing
147             ##############################################################################
148              
149             # This is the glue that connects the code below with Pod::Simple itself. The
150             # goal is to convert the event stream coming from the POD parser into method
151             # calls to handlers once the complete content of a tag has been seen. Each
152             # paragraph or POD command will have textual content associated with it, and
153             # as soon as all of a paragraph or POD command has been seen, that content
154             # will be passed in to the corresponding method for handling that type of
155             # object. The exceptions are handlers for lists, which have opening tag
156             # handlers and closing tag handlers that will be called right away.
157             #
158             # The internal hash key PENDING is used to store the contents of a tag until
159             # all of it has been seen. It holds a stack of open tags, each one
160             # represented by a tuple of the attributes hash for the tag and the contents
161             # of the tag.
162              
163             # Add a block of text to the contents of the current node, formatting it
164             # according to the current formatting instructions as we do.
165             sub _handle_text {
166 1075     1075   5087 my ($self, $text) = @_;
167 1075         985 my $tag = $$self{PENDING}[-1];
168 1075         1817 $$tag[1] .= $text;
169             }
170              
171             # Given an element name, get the corresponding method name.
172             sub method_for_element {
173 2204     2204 0 1581 my ($self, $element) = @_;
174 2204         1777 $element =~ tr/-/_/;
175 2204         1608 $element =~ tr/A-Z/a-z/;
176 2204         1562 $element =~ tr/_a-z0-9//cd;
177 2204         2627 return $element;
178             }
179              
180             # Handle the start of a new element. If cmd_element is defined, assume that
181             # we need to collect the entire tree for this element before passing it to the
182             # element method, and create a new tree into which we'll collect blocks of
183             # text and nested elements. Otherwise, if start_element is defined, call it.
184             sub _handle_element_start {
185 1102     1102   145565 my ($self, $element, $attrs) = @_;
186 1102         1354 my $method = $self->method_for_element ($element);
187              
188             # If we have a command handler, we need to accumulate the contents of the
189             # tag before calling it.
190 1102 100       3674 if ($self->can ("cmd_$method")) {
    100          
191 994         757 push (@{ $$self{PENDING} }, [ $attrs, '' ]);
  994         2151  
192             } elsif ($self->can ("start_$method")) {
193 106         119 my $method = 'start_' . $method;
194 106         213 $self->$method ($attrs, '');
195             }
196             }
197              
198             # Handle the end of an element. If we had a cmd_ method for this element,
199             # this is where we pass along the text that we've accumulated. Otherwise, if
200             # we have an end_ method for the element, call that.
201             sub _handle_element_end {
202 1102     1102   6989 my ($self, $element) = @_;
203 1102         1168 my $method = $self->method_for_element ($element);
204              
205             # If we have a command handler, pull off the pending text and pass it to
206             # the handler along with the saved attribute hash.
207 1102 100       2701 if ($self->can ("cmd_$method")) {
    100          
208 994         713 my $tag = pop @{ $$self{PENDING} };
  994         1028  
209 994         892 my $method = 'cmd_' . $method;
210 994         1623 my $text = $self->$method (@$tag);
211 994 50       2073 if (defined $text) {
212 994 100       612 if (@{ $$self{PENDING} } > 1) {
  994         1520  
213 344         733 $$self{PENDING}[-1][1] .= $text;
214             } else {
215 650         785 $self->output ($text);
216             }
217             }
218             } elsif ($self->can ("end_$method")) {
219 106         117 my $method = 'end_' . $method;
220 106         200 $self->$method ();
221             }
222             }
223              
224             ##############################################################################
225             # Output formatting
226             ##############################################################################
227              
228             # Wrap a line, indenting by the current left margin. We can't use Text::Wrap
229             # because it plays games with tabs. We can't use formline, even though we'd
230             # really like to, because it screws up non-printing characters. So we have to
231             # do the wrapping ourselves.
232             sub wrap {
233 162     162 0 134 my $self = shift;
234 162         139 local $_ = shift;
235 162         119 my $output = '';
236 162         193 my $spaces = ' ' x $$self{MARGIN};
237 162         169 my $width = $$self{opt_width} - $$self{MARGIN};
238 162         312 while (length > $width) {
239 40 50 33     404 if (s/^([^\n]{0,$width})\s+// || s/^([^\n]{$width})//) {
240 40         141 $output .= $spaces . $1 . "\n";
241             } else {
242 0         0 last;
243             }
244             }
245 162         225 $output .= $spaces . $_;
246 162         542 $output =~ s/\s+$/\n\n/;
247 162         359 return $output;
248             }
249              
250             # Reformat a paragraph of text for the current margin. Takes the text to
251             # reformat and returns the formatted text.
252             sub reformat {
253 465     465 0 365 my $self = shift;
254 465         438 local $_ = shift;
255              
256             # If we're trying to preserve two spaces after sentences, do some munging
257             # to support that. Otherwise, smash all repeated whitespace.
258 465 100       561 if ($$self{opt_sentence}) {
259 1         5 s/ +$//mg;
260 1         3 s/\.\n/. \n/g;
261 1         3 s/\n/ /g;
262 1         2 s/ +/ /g;
263             } else {
264 464         2215 s/\s+/ /g;
265             }
266 465         956 return $self->wrap ($_);
267             }
268              
269             # Output text to the output device. Replace non-breaking spaces with spaces
270             # and soft hyphens with nothing, and then try to fix the output encoding if
271             # necessary to match the input encoding unless UTF-8 output is forced. This
272             # preserves the traditional pass-through behavior of Pod::Text.
273             sub output {
274 1243     1243 0 1466 my ($self, @text) = @_;
275 1243         1396 my $text = join ('', @text);
276 1243         1023 $text =~ tr/\240\255/ /d;
277 1243 100       1820 unless ($$self{opt_utf8}) {
278 1223   100     2922 my $encoding = $$self{encoding} || '';
279 1223 100 100     2078 if ($encoding && $encoding ne $$self{ENCODING}) {
280 5         6 $$self{ENCODING} = $encoding;
281 5     1   6 eval { binmode ($$self{output_fh}, ":encoding($encoding)") };
  5         62  
  1         5  
  1         1  
  1         5  
282             }
283             }
284 1243 50       2205 if ($$self{ENCODE}) {
285 0         0 print { $$self{output_fh} } encode ('UTF-8', $text);
  0         0  
286             } else {
287 1243         896 print { $$self{output_fh} } $text;
  1243         2897  
288             }
289             }
290              
291             # Output a block of code (something that isn't part of the POD text). Called
292             # by preprocess_paragraph only if we were given the code option. Exists here
293             # only so that it can be overridden by subclasses.
294 8     8 0 17 sub output_code { $_[0]->output ($_[1]) }
295              
296             ##############################################################################
297             # Document initialization
298             ##############################################################################
299              
300             # Set up various things that have to be initialized on a per-document basis.
301             sub start_document {
302 40     40 0 51 my ($self, $attrs) = @_;
303 40 100 66     125 if ($$attrs{contentless} && !$$self{ALWAYS_EMIT_SOMETHING}) {
304 1         4 $$self{CONTENTLESS} = 1;
305             } else {
306 39         43 delete $$self{CONTENTLESS};
307             }
308 40         64 my $margin = $$self{opt_indent} + $$self{opt_margin};
309              
310             # Initialize a few per-document variables.
311 40         60 $$self{INDENTS} = []; # Stack of indentations.
312 40         67 $$self{MARGIN} = $margin; # Default left margin.
313 40         112 $$self{PENDING} = [[]]; # Pending output.
314              
315             # We have to redo encoding handling for each document.
316 40         67 $$self{ENCODING} = '';
317              
318             # When UTF-8 output is set, check whether our output file handle already
319             # has a PerlIO encoding layer set. If it does not, we'll need to encode
320             # our output before printing it (handled in the output() sub). Wrap the
321             # check in an eval to handle versions of Perl without PerlIO.
322 40         53 $$self{ENCODE} = 0;
323 40 100       89 if ($$self{opt_utf8}) {
324 2         2 $$self{ENCODE} = 1;
325 2         3 eval {
326 2         3 my @options = (output => 1, details => 1);
327 2         11 my $flag = (PerlIO::get_layers ($$self{output_fh}, @options))[-1];
328 2 50       17 if ($flag & PerlIO::F_UTF8 ()) {
329 2         2 $$self{ENCODE} = 0;
330 2         3 $$self{ENCODING} = 'UTF-8';
331             }
332             };
333             }
334              
335 40         86 return '';
336             }
337              
338             # Handle the end of the document. The only thing we do is handle dying on POD
339             # errors, since Pod::Parser currently doesn't.
340             sub end_document {
341 40     40 0 45 my ($self) = @_;
342 40 100 66     174 if ($$self{complain_die} && $self->errors_seen) {
343 1         209 croak ("POD document had syntax errors");
344             }
345             }
346              
347             ##############################################################################
348             # Text blocks
349             ##############################################################################
350              
351             # Intended for subclasses to override, this method returns text with any
352             # non-printing formatting codes stripped out so that length() correctly
353             # returns the length of the text. For basic Pod::Text, it does nothing.
354             sub strip_format {
355 49     49 0 44 my ($self, $string) = @_;
356 49         70 return $string;
357             }
358              
359             # This method is called whenever an =item command is complete (in other words,
360             # we've seen its associated paragraph or know for certain that it doesn't have
361             # one). It gets the paragraph associated with the item as an argument. If
362             # that argument is empty, just output the item tag; if it contains a newline,
363             # output the item tag followed by the newline. Otherwise, see if there's
364             # enough room for us to output the item tag in the margin of the text or if we
365             # have to put it on a separate line.
366             sub item {
367 136     136 1 137 my ($self, $text) = @_;
368 136         130 my $tag = $$self{ITEM};
369 136 50       232 unless (defined $tag) {
370 0         0 carp "Item called without tag";
371 0         0 return;
372             }
373 136         164 undef $$self{ITEM};
374              
375             # Calculate the indentation and margin. $fits is set to true if the tag
376             # will fit into the margin of the paragraph given our indentation level.
377 136         140 my $indent = $$self{INDENTS}[-1];
378 136 50       185 $indent = $$self{opt_indent} unless defined $indent;
379 136         205 my $margin = ' ' x $$self{opt_margin};
380 136         273 my $tag_length = length ($self->strip_format ($tag));
381 136         241 my $fits = ($$self{MARGIN} - $indent >= $tag_length + 1);
382              
383             # If the tag doesn't fit, or if we have no associated text, print out the
384             # tag separately. Otherwise, put the tag in the margin of the paragraph.
385 136 100 100     823 if (!$text || $text =~ /^\s+$/ || !$fits) {
      100        
386 39         45 my $realindent = $$self{MARGIN};
387 39         39 $$self{MARGIN} = $indent;
388 39         93 my $output = $self->reformat ($tag);
389 39 100 66     140 $output =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
390 39         154 $output =~ s/\n*$/\n/;
391              
392             # If the text is just whitespace, we have an empty item paragraph;
393             # this can result from =over/=item/=back without any intermixed
394             # paragraphs. Insert some whitespace to keep the =item from merging
395             # into the next paragraph.
396 39 100 100     171 $output .= "\n" if $text && $text =~ /^\s*$/;
397              
398 39         80 $self->output ($output);
399 39         151 $$self{MARGIN} = $realindent;
400 39 100 100     182 $self->output ($self->reformat ($text)) if ($text && $text =~ /\S/);
401             } else {
402 97         123 my $space = ' ' x $indent;
403 97 100       189 $space =~ s/^$margin /$margin:/ if $$self{opt_alt};
404 97         147 $text = $self->reformat ($text);
405 97 100 66     239 $text =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
406 97         99 my $tagspace = ' ' x $tag_length;
407 97 50       963 $text =~ s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item";
408 97         164 $self->output ($text);
409             }
410             }
411              
412             # Handle a basic block of text. The only tricky thing here is that if there
413             # is a pending item tag, we need to format this as an item paragraph.
414             sub cmd_para {
415 394     394 0 379 my ($self, $attrs, $text) = @_;
416 394         1058 $text =~ s/\s+$/\n/;
417 394 100       564 if (defined $$self{ITEM}) {
418 87         208 $self->item ($text . "\n");
419             } else {
420 307         633 $self->output ($self->reformat ($text . "\n"));
421             }
422 394         2100 return '';
423             }
424              
425             # Handle a verbatim paragraph. Just print it out, but indent it according to
426             # our margin.
427             sub cmd_verbatim {
428 22     22 0 30 my ($self, $attrs, $text) = @_;
429 22 50       52 $self->item if defined $$self{ITEM};
430 22 50       68 return if $text =~ /^\s*$/;
431 22         72 $text =~ s/^(\n*)([ \t]*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme;
  111         324  
432 22         449 $text =~ s/\s*$/\n\n/;
433 22         41 $self->output ($text);
434 22         130 return '';
435             }
436              
437             # Handle literal text (produced by =for and similar constructs). Just output
438             # it with the minimum of changes.
439             sub cmd_data {
440 2     2 0 3 my ($self, $attrs, $text) = @_;
441 2         4 $text =~ s/^\n+//;
442 2         5 $text =~ s/\n{0,2}$/\n/;
443 2         4 $self->output ($text);
444 2         1 return '';
445             }
446              
447             ##############################################################################
448             # Headings
449             ##############################################################################
450              
451             # The common code for handling all headers. Takes the header text, the
452             # indentation, and the surrounding marker for the alt formatting method.
453             sub heading {
454 79     79 0 99 my ($self, $text, $indent, $marker) = @_;
455 79 50       168 $self->item ("\n\n") if defined $$self{ITEM};
456 79         173 $text =~ s/\s+$//;
457 79 100       110 if ($$self{opt_alt}) {
458 1         10 my $closemark = reverse (split (//, $marker));
459 1         4 my $margin = ' ' x $$self{opt_margin};
460 1         6 $self->output ("\n" . "$margin$marker $text $closemark" . "\n\n");
461             } else {
462 78 50       139 $text .= "\n" if $$self{opt_loose};
463 78         139 my $margin = ' ' x ($$self{opt_margin} + $indent);
464 78         175 $self->output ($margin . $text . "\n");
465             }
466 79         340 return '';
467             }
468              
469             # First level heading.
470             sub cmd_head1 {
471 72     72 0 309 my ($self, $attrs, $text) = @_;
472 72         145 $self->heading ($text, 0, '====');
473             }
474              
475             # Second level heading.
476             sub cmd_head2 {
477 8     8 0 41 my ($self, $attrs, $text) = @_;
478 8         21 $self->heading ($text, $$self{opt_indent} / 2, '== ');
479             }
480              
481             # Third level heading.
482             sub cmd_head3 {
483 8     8 0 11 my ($self, $attrs, $text) = @_;
484 8         23 $self->heading ($text, $$self{opt_indent} * 2 / 3 + 0.5, '= ');
485             }
486              
487             # Fourth level heading.
488             sub cmd_head4 {
489 8     8 0 10 my ($self, $attrs, $text) = @_;
490 8         23 $self->heading ($text, $$self{opt_indent} * 3 / 4 + 0.5, '- ');
491             }
492              
493             ##############################################################################
494             # List handling
495             ##############################################################################
496              
497             # Handle the beginning of an =over block. Takes the type of the block as the
498             # first argument, and then the attr hash. This is called by the handlers for
499             # the four different types of lists (bullet, number, text, and block).
500             sub over_common_start {
501 66     66 0 71 my ($self, $attrs) = @_;
502 66 50       130 $self->item ("\n\n") if defined $$self{ITEM};
503              
504             # Find the indentation level.
505 66         73 my $indent = $$attrs{indent};
506 66 50 33     359 unless (defined ($indent) && $indent =~ /^\s*[-+]?\d{1,4}\s*$/) {
507 0         0 $indent = $$self{opt_indent};
508             }
509              
510             # Add this to our stack of indents and increase our current margin.
511 66         63 push (@{ $$self{INDENTS} }, $$self{MARGIN});
  66         123  
512 66         115 $$self{MARGIN} += ($indent + 0);
513 66         140 return '';
514             }
515              
516             # End an =over block. Takes no options other than the class pointer. Output
517             # any pending items and then pop one level of indentation.
518             sub over_common_end {
519 66     66 0 72 my ($self) = @_;
520 66 100       137 $self->item ("\n\n") if defined $$self{ITEM};
521 66         51 $$self{MARGIN} = pop @{ $$self{INDENTS} };
  66         120  
522 66         246 return '';
523             }
524              
525             # Dispatch the start and end calls as appropriate.
526 8     8 0 16 sub start_over_bullet { $_[0]->over_common_start ($_[1]) }
527 8     8 0 12 sub start_over_number { $_[0]->over_common_start ($_[1]) }
528 38     38 0 116 sub start_over_text { $_[0]->over_common_start ($_[1]) }
529 12     12 0 22 sub start_over_block { $_[0]->over_common_start ($_[1]) }
530 8     8 0 16 sub end_over_bullet { $_[0]->over_common_end }
531 8     8 0 16 sub end_over_number { $_[0]->over_common_end }
532 38     38 0 103 sub end_over_text { $_[0]->over_common_end }
533 12     12 0 19 sub end_over_block { $_[0]->over_common_end }
534              
535             # The common handler for all item commands. Takes the type of the item, the
536             # attributes, and then the text of the item.
537             sub item_common {
538 136     136 0 144 my ($self, $type, $attrs, $text) = @_;
539 136 100       271 $self->item if defined $$self{ITEM};
540              
541             # Clean up the text. We want to end up with two variables, one ($text)
542             # which contains any body text after taking out the item portion, and
543             # another ($item) which contains the actual item text. Note the use of
544             # the internal Pod::Simple attribute here; that's a potential land mine.
545 136         277 $text =~ s/\s+$//;
546 136         97 my ($item, $index);
547 136 100       274 if ($type eq 'bullet') {
    100          
548 16         18 $item = '*';
549             } elsif ($type eq 'number') {
550 16         20 $item = $$attrs{'~orig_content'};
551             } else {
552 104         86 $item = $text;
553 104         111 $item =~ s/\s*\n\s*/ /g;
554 104         104 $text = '';
555             }
556 136         145 $$self{ITEM} = $item;
557              
558             # If body text for this item was included, go ahead and output that now.
559 136 100       190 if ($text) {
560 32         162 $text =~ s/\s*$/\n/;
561 32         56 $self->item ($text);
562             }
563 136         355 return '';
564             }
565              
566             # Dispatch the item commands to the appropriate place.
567 16     16 0 17 sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) }
  16         24  
568 16     16 0 16 sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) }
  16         22  
569 104     104 0 90 sub cmd_item_text { my $self = shift; $self->item_common ('text', @_) }
  104         182  
570 0     0 0 0 sub cmd_item_block { my $self = shift; $self->item_common ('block', @_) }
  0         0  
571              
572             ##############################################################################
573             # Formatting codes
574             ##############################################################################
575              
576             # The simple ones.
577 10 50   10 0 22 sub cmd_b { return $_[0]{alt} ? "``$_[2]''" : $_[2] }
578 6 50   6 0 14 sub cmd_f { return $_[0]{alt} ? "\"$_[2]\"" : $_[2] }
579 11     11 0 21 sub cmd_i { return '*' . $_[2] . '*' }
580 4     4 0 6 sub cmd_x { return '' }
581              
582             # Apply a whole bunch of messy heuristics to not quote things that don't
583             # benefit from being quoted. These originally come from Barrie Slaymaker and
584             # largely duplicate code in Pod::Man.
585             sub cmd_c {
586 113     113 0 113 my ($self, $attrs, $text) = @_;
587              
588             # A regex that matches the portion of a variable reference that's the
589             # array or hash index, separated out just because we want to use it in
590             # several places in the following regex.
591 113         81 my $index = '(?: \[.*\] | \{.*\} )?';
592              
593             # Check for things that we don't want to quote, and if we find any of
594             # them, return the string with just a font change and no quoting.
595 113 100       1966 $text =~ m{
596             ^\s*
597             (?:
598             ( [\'\`\"] ) .* \1 # already quoted
599             | \` .* \' # `quoted'
600             | \$+ [\#^]? \S $index # special ($^Foo, $")
601             | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func
602             | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
603             | [+-]? ( \d[\d.]* | \.\d+ ) (?: [eE][+-]?\d+ )? # a number
604             | 0x [a-fA-F\d]+ # a hex constant
605             )
606             \s*\z
607             }xo && return $text;
608              
609             # If we didn't return, go ahead and quote the text.
610             return $$self{opt_alt}
611 105 50       325 ? "``$text''"
612             : "$$self{LQUOTE}$text$$self{RQUOTE}";
613             }
614              
615             # Links reduce to the text that we're given, wrapped in angle brackets if it's
616             # a URL.
617             sub cmd_l {
618 110     110 0 116 my ($self, $attrs, $text) = @_;
619 110 100       176 if ($$attrs{type} eq 'url') {
620 6 100 66     32 if (not defined($$attrs{to}) or $$attrs{to} eq $text) {
    100          
621 3         37 return "<$text>";
622             } elsif ($$self{opt_nourls}) {
623 1         13 return $text;
624             } else {
625 2         23 return "$text <$$attrs{to}>";
626             }
627             } else {
628 104         125 return $text;
629             }
630             }
631              
632             ##############################################################################
633             # Backwards compatibility
634             ##############################################################################
635              
636             # The old Pod::Text module did everything in a pod2text() function. This
637             # tries to provide the same interface for legacy applications.
638             sub pod2text {
639 1     1 0 678 my @args;
640              
641             # This is really ugly; I hate doing option parsing in the middle of a
642             # module. But the old Pod::Text module supported passing flags to its
643             # entry function, so handle -a and -.
644 1         6 while ($_[0] =~ /^-/) {
645 0         0 my $flag = shift;
646 0 0       0 if ($flag eq '-a') { push (@args, alt => 1) }
  0 0       0  
647 0         0 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
648             else {
649 0         0 unshift (@_, $flag);
650 0         0 last;
651             }
652             }
653              
654             # Now that we know what arguments we're using, create the parser.
655 1         5 my $parser = Pod::Text->new (@args);
656              
657             # If two arguments were given, the second argument is going to be a file
658             # handle. That means we want to call parse_from_filehandle(), which means
659             # we need to turn the first argument into a file handle. Magic open will
660             # handle the <&STDIN case automagically.
661 1 50       3 if (defined $_[1]) {
662 0         0 my @fhs = @_;
663 0         0 local *IN;
664 0 0       0 unless (open (IN, $fhs[0])) {
665 0         0 croak ("Can't open $fhs[0] for reading: $!\n");
666 0         0 return;
667             }
668 0         0 $fhs[0] = \*IN;
669 0         0 $parser->output_fh ($fhs[1]);
670 0         0 my $retval = $parser->parse_file ($fhs[0]);
671 0         0 my $fh = $parser->output_fh ();
672 0         0 close $fh;
673 0         0 return $retval;
674             } else {
675 1         4 $parser->output_fh (\*STDOUT);
676 1         5 return $parser->parse_file (@_);
677             }
678             }
679              
680             # Reset the underlying Pod::Simple object between calls to parse_from_file so
681             # that the same object can be reused to convert multiple pages.
682             sub parse_from_file {
683 34     34 1 22855 my $self = shift;
684 34         188 $self->reinit;
685              
686             # Fake the old cutting option to Pod::Parser. This fiddings with internal
687             # Pod::Simple state and is quite ugly; we need a better approach.
688 34 100       636 if (ref ($_[0]) eq 'HASH') {
689 1         2 my $opts = shift @_;
690 1 50 33     8 if (defined ($$opts{-cutting}) && !$$opts{-cutting}) {
691 1         2 $$self{in_pod} = 1;
692 1         3 $$self{last_was_blank} = 1;
693             }
694             }
695              
696             # Do the work.
697 34         136 my $retval = $self->Pod::Simple::parse_from_file (@_);
698              
699             # Flush output, since Pod::Simple doesn't do this. Ideally we should also
700             # close the file descriptor if we had to open one, but we can't easily
701             # figure this out.
702 33         995 my $fh = $self->output_fh ();
703 33         238 my $oldfh = select $fh;
704 33         67 my $oldflush = $|;
705 33         1066 $| = 1;
706 33         64 print $fh '';
707 33         84 $| = $oldflush;
708 33         104 select $oldfh;
709 33         80 return $retval;
710             }
711              
712             # Pod::Simple failed to provide this backward compatibility function, so
713             # implement it ourselves. File handles are one of the inputs that
714             # parse_from_file supports.
715             sub parse_from_filehandle {
716 1     1 0 17 my $self = shift;
717 1         3 $self->parse_from_file (@_);
718             }
719              
720             # Pod::Simple's parse_file doesn't set output_fh. Wrap the call and do so
721             # ourself unless it was already set by the caller, since our documentation has
722             # always said that this should work.
723             sub parse_file {
724 39     39 1 2199 my ($self, $in) = @_;
725 39 50       104 unless (defined $$self{output_fh}) {
726 0         0 $self->output_fh (\*STDOUT);
727             }
728 39         118 return $self->SUPER::parse_file ($in);
729             }
730              
731             # Do the same for parse_lines, just to be polite. Pod::Simple's man page
732             # implies that the caller is responsible for setting this, but I don't see any
733             # reason not to set a default.
734             sub parse_lines {
735 126     126 1 18021 my ($self, @lines) = @_;
736 126 50       267 unless (defined $$self{output_fh}) {
737 0         0 $self->output_fh (\*STDOUT);
738             }
739 126         359 return $self->SUPER::parse_lines (@lines);
740             }
741              
742             # Likewise for parse_string_document.
743             sub parse_string_document {
744 2     2 1 3885 my ($self, $doc) = @_;
745 2 50       19 unless (defined $$self{output_fh}) {
746 0         0 $self->output_fh (\*STDOUT);
747             }
748 2         8 return $self->SUPER::parse_string_document ($doc);
749             }
750              
751             ##############################################################################
752             # Module return value and documentation
753             ##############################################################################
754              
755             1;
756             __END__