← Index
NYTProf Performance Profile   « line view »
For /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/bin/perlcritic
  Run on Sat Mar 19 22:12:22 2016
Reported on Sat Mar 19 22:14:10 2016

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