Text.pm

filter: Statementstmt 91.3% 337 / 369 Branchbran 78.7% 115 / 146 Conditioncond 77.5% 62 / 80 MC/DCmcdc 62.5% 30 / 48 Subroutinesub 98.2% 57 / 58 Podpod 19.1% 9 / 47 totaltotal 81.5% 610 / 748 CC 103 SCAR 22.8
CC
103
Cov
86%
new
28.3
pod2text
21.7
item
16.5
CRAP 9.8
« Man.pm ↑ Summary Text/Termcap.pm »
1 # 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 # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
10  
11 ##############################################################################
12 # Modules and declarations
13 ##############################################################################
14  
15 package Pod::Text v6.1.1;
16  
17 11 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use parent qw(Pod::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 11 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # We have to export pod2text for backward compatibility.
26 11 our @EXPORT = qw(pod2text);
27  
28 # Ensure that $Pod::Simple::nbsp and $Pod::Simple::shy are available. Code
29 # taken from Pod::Simple 3.32, but was only added in 3.30.
30 11 my ($NBSP, $SHY);
31 11 ▶if ($Pod::Simple::VERSION ge 3.30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::Simple::VERSION ge 3.3) { } 11 0 11
32 11 $NBSP = $Pod::Simple::nbsp;
33 11 $SHY = $Pod::Simple::shy;
34 } else {
35 0 $NBSP = chr utf8::unicode_to_native(0xA0);
36 0 $SHY = chr utf8::unicode_to_native(0xAD);
37 }
38  
39 # Import the ASCII constant from Pod::Simple. This is true iff we're in an
40 # ASCII-based universe (including such things as ISO 8859-1 and UTF-8), and is
41 # generally only false for EBCDIC.
42 11 ▶BEGIN { *ASCII = \&Pod::Simple::ASCII }
Subroutine BEGIN: called CC=1 SCAR=0.0
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 8 ▶sub handle_code {
Subroutine handle_code: called CC=1 SCAR=0.0
Pod undocumented
52 8 my ($line, $number, $parser) = @_;
53 8 $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 77 ▶sub new {
Subroutine new: called CC=27 SCAR=33.4
Pod documented
62 77 my $class = shift;
63 77 my $self = $class->SUPER::new;
64  
65 # Tell Pod::Simple to keep whitespace whenever possible.
66 77 ▶ if ($self->can ('preserve_whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('preserve_whitespace')) { } 77 0 77
67 77 $self->preserve_whitespace (1);
68 } else {
69 0 $self->fullstop_space_harden (1);
70 }
71  
72 # The =for and =begin targets that we accept.
73 77 $self->accept_targets (qw/text TEXT/);
74  
75 # Ensure that contiguous blocks of code are merged together. Otherwise,
76 # some of the guesswork heuristics don't work right.
77 77 $self->merge_text (1);
78  
79 # Pod::Simple doesn't do anything useful with our arguments, but we want
80 # to put them in our object as hash keys and values. This could cause
81 # problems if we ever clash with Pod::Simple's own internal class
82 # variables.
83 77 my %opts = @_;
84 77 my @opts = map { ("opt_$_", $opts{$_}) } keys %opts;
85 77 %$self = (%$self, @opts);
86  
87 # Backwards-compatibility support for the stderr option.
88 77 ▶ if ($$self{opt_stderr} and not $$self{opt_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt_stderr"} and not $self->{"opt_errors"}) 1 76 77
Condition66%2/3
$self->{"opt_stderr"} and not $self->{"opt_errors"}
!ll&&!rl&&r
7601
MC/DC50%1/2
$self->{"opt_stderr"} and not $self->{"opt_errors"}
$self->{"opt_stderr"} not $self->{"opt_errors"}
Truth table66%2/3
A and not(B)
A= $self->{"opt_stderr"}B= not $self->{"opt_errors"}
ABresult
0X0
100
111
89 1 $$self{opt_errors} = 'stderr';
90 }
91 77 delete $$self{opt_stderr};
92  
93 # Backwards-compatibility support for the utf8 option.
94 77 ▶ if ($$self{opt_utf8} && !$$self{opt_encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt_utf8"} and not $self->{"opt_encoding"}) 2 75 77
Condition66%2/3
$self->{"opt_utf8"} and not $self->{"opt_encoding"}
!ll&&!rl&&r
7502
MC/DC50%1/2
$self->{"opt_utf8"} and not $self->{"opt_encoding"}
$self->{"opt_utf8"} not $self->{"opt_encoding"}
Truth table66%2/3
A and not(B)
A= $self->{"opt_utf8"}B= not $self->{"opt_encoding"}
ABresult
0X0
100
111
95 2 $$self{opt_encoding} = 'UTF-8';
96 }
97 77 delete $$self{opt_utf8};
98  
99 # Validate the errors parameter and act on it.
100 77 ▶ $$self{opt_errors} //= 'pod';
Condition100%2/2
$self->{"opt_errors"} //= "pod"
l!l
671
MC/DC100%1/1
$self->{"opt_errors"} //= "pod"
$self->{"opt_errors"}
Truth table100%2/2
A
A= $self->{"opt_errors"}
Aresult
11
00
101 77 ▶ if ($$self{opt_errors} eq 'stderr' || $$self{opt_errors} eq 'die') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'opt_errors'} eq 'stderr' or $self->{'opt_errors'} eq 'die') { } 4 73 77
elsif ($self->{'opt_errors'} eq 'pod') { } 72 1 73
elsif ($self->{'opt_errors'} eq 'none') { } 1 0 1
Condition100%3/3
$self->{"opt_errors"} eq "stderr" or $self->{"opt_errors"} eq "die"
l!l&&r!l&&!r
2273
MC/DC100%2/2
$self->{"opt_errors"} eq "stderr" or $self->{"opt_errors"} eq "die"
$self->{"opt_errors"} eq "stderr" $self->{"opt_errors"} eq "die"
Truth table100%3/3
A or B
A= $self->{"opt_errors"} eq "stderr"B= $self->{"opt_errors"} eq "die"
ABresult
1X1
011
000
102 4 $self->no_errata_section (1);
103 4 $self->complain_stderr (1);
104 4 ▶ if ($$self{opt_errors} eq 'die') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt_errors"} eq "die") 2 2 4
105 2 $$self{complain_die} = 1;
106 }
107 } elsif ($$self{opt_errors} eq 'pod') {
108 72 $self->no_errata_section (0);
109 72 $self->complain_stderr (0);
110 } elsif ($$self{opt_errors} eq 'none') {
111 1 $self->no_errata_section (1);
112 1 $self->no_whining (1);
113 } else {
114 0 croak (qq(Invalid errors setting: "$$self{errors}"));
115 }
116 77 delete $$self{errors};
117  
118 # Initialize various things from our parameters.
119 77 ▶ $$self{opt_alt} //= 0;
Condition100%2/2
$self->{"opt_alt"} //= 0
l!l
176
MC/DC100%1/1
$self->{"opt_alt"} //= 0
$self->{"opt_alt"}
Truth table100%2/2
A
A= $self->{"opt_alt"}
Aresult
11
00
120 77 ▶ $$self{opt_indent} //= 4;
Condition50%1/2
$self->{"opt_indent"} //= 4
l!l
077
MC/DC0%0/1
$self->{"opt_indent"} //= 4
$self->{"opt_indent"}
Truth table50%1/2
A
A= $self->{"opt_indent"}
Aresult
11
00
121 77 ▶ $$self{opt_margin} //= 0;
Condition100%2/2
$self->{"opt_margin"} //= 0
l!l
176
MC/DC100%1/1
$self->{"opt_margin"} //= 0
$self->{"opt_margin"}
Truth table100%2/2
A
A= $self->{"opt_margin"}
Aresult
11
00
122 77 ▶ $$self{opt_loose} //= 0;
Condition50%1/2
$self->{"opt_loose"} //= 0
l!l
077
MC/DC0%0/1
$self->{"opt_loose"} //= 0
$self->{"opt_loose"}
Truth table50%1/2
A
A= $self->{"opt_loose"}
Aresult
11
00
123 77 ▶ $$self{opt_sentence} //= 0;
Condition100%2/2
$self->{"opt_sentence"} //= 0
l!l
176
MC/DC100%1/1
$self->{"opt_sentence"} //= 0
$self->{"opt_sentence"}
Truth table100%2/2
A
A= $self->{"opt_sentence"}
Aresult
11
00
124 77 ▶ $$self{opt_width} //= 76;
Condition100%2/2
$self->{"opt_width"} //= 76
l!l
1364
MC/DC100%1/1
$self->{"opt_width"} //= 76
$self->{"opt_width"}
Truth table100%2/2
A
A= $self->{"opt_width"}
Aresult
11
00
125  
126 # Figure out what quotes we'll be using for C<> text.
127 77 ▶ $$self{opt_quotes} ||= '"';
Condition100%2/2
$self->{"opt_quotes"} ||= "\""
l!l
374
MC/DC100%1/1
$self->{"opt_quotes"} ||= "\""
$self->{"opt_quotes"}
Truth table100%2/2
A
A= $self->{"opt_quotes"}
Aresult
11
00
128 77 ▶ if ($$self{opt_quotes} eq 'none') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'opt_quotes'} eq 'none') { } 1 76 77
elsif (length $self->{'opt_quotes'} == 1) { } 74 2 76
elsif (length($self->{'opt_quotes'}) % 2 == 0) { } 2 0 2
129 1 $$self{LQUOTE} = $$self{RQUOTE} = '';
130 } elsif (length ($$self{opt_quotes}) == 1) {
131 74 $$self{LQUOTE} = $$self{RQUOTE} = $$self{opt_quotes};
132 } elsif (length ($$self{opt_quotes}) % 2 == 0) {
133 2 my $length = length ($$self{opt_quotes}) / 2;
134 2 $$self{LQUOTE} = substr ($$self{opt_quotes}, 0, $length);
135 2 $$self{RQUOTE} = substr ($$self{opt_quotes}, $length);
136 } else {
137 0 croak qq(Invalid quote specification "$$self{opt_quotes}");
138 }
139  
140 # Configure guesswork based on options.
141 77 ▶ my $guesswork = $self->{opt_guesswork} || q{};
Condition100%2/2
$self->{'opt_guesswork'} || ''
l!l
176
MC/DC100%1/1
$self->{'opt_guesswork'} || ''
$self->{'opt_guesswork'}
Truth table100%2/2
A
A= $self->{'opt_guesswork'}
Aresult
11
00
142 77 my %guesswork = map { $_ => 1 } split(m{,}xms, $guesswork);
143 77 ▶ if (!%guesswork || $guesswork{all}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not %guesswork or $guesswork{'all'}) { } 76 1 77
elsif ($guesswork{'none'}) { } 1 0 1
Condition66%2/3
not %guesswork or $guesswork{"all"}
l!l&&r!l&&!r
7601
MC/DC50%1/2
not %guesswork or $guesswork{"all"}
not %guesswork $guesswork{"all"}
Truth table66%2/3
not(A) or B
A= not %guessworkB= $guesswork{"all"}
ABresult
1X1
011
000
144 76 $$self{GUESSWORK} = {quoting => 1};
145 } elsif ($guesswork{none}) {
146 1 $$self{GUESSWORK} = {};
147 } else {
148 0 $$self{GUESSWORK} = {%guesswork};
149 }
150  
151 # If requested, do something with the non-POD text.
152 77 ▶ $self->code_handler (\&handle_code) if $$self{opt_code};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_code"} 1 76 77
153  
154 # Return the created object.
155 77 return $self;
156 }
157  
158 ##############################################################################
159 # Core parsing
160 ##############################################################################
161  
162 # This is the glue that connects the code below with Pod::Simple itself. The
163 # goal is to convert the event stream coming from the POD parser into method
164 # calls to handlers once the complete content of a tag has been seen. Each
165 # paragraph or POD command will have textual content associated with it, and
166 # as soon as all of a paragraph or POD command has been seen, that content
167 # will be passed in to the corresponding method for handling that type of
168 # object. The exceptions are handlers for lists, which have opening tag
169 # handlers and closing tag handlers that will be called right away.
170 #
171 # The internal hash key PENDING is used to store the contents of a tag until
172 # all of it has been seen. It holds a stack of open tags, each one
173 # represented by a tuple of the attributes hash for the tag and the contents
174 # of the tag.
175  
176 # Add a block of text to the contents of the current node, formatting it
177 # according to the current formatting instructions as we do.
178 1388 ▶sub _handle_text {
Subroutine _handle_text: called CC=1 SCAR=0.0
179 1388 my ($self, $text) = @_;
180 1388 my $tag = $$self{PENDING}[-1];
181 1388 $$tag[1] .= $text;
182 }
183  
184 # Given an element name, get the corresponding method name.
185 2776 ▶sub method_for_element {
Subroutine method_for_element: called CC=1 SCAR=0.0
Pod undocumented
186 2776 my ($self, $element) = @_;
187 2776 $element =~ tr/-/_/;
188 2776 $element =~ tr/A-Z/a-z/;
189 2776 $element =~ tr/_a-z0-9//cd;
190 2776 return $element;
191 }
192  
193 # Handle the start of a new element. If cmd_element is defined, assume that
194 # we need to collect the entire tree for this element before passing it to the
195 # element method, and create a new tree into which we'll collect blocks of
196 # text and nested elements. Otherwise, if start_element is defined, call it.
197 1388 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=3 SCAR=11.0
198 1388 my ($self, $element, $attrs) = @_;
199 1388 my $method = $self->method_for_element ($element);
200  
201 # If we have a command handler, we need to accumulate the contents of the
202 # tag before calling it.
203 1388 ▶ if ($self->can ("cmd_$method")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can("cmd_$method")) { } 1230 158 1388
elsif ($self->can("start_$method")) { } 156 2 158
204 1230 push (@{ $$self{PENDING} }, [ $attrs, '' ]);
205 } elsif ($self->can ("start_$method")) {
206 156 my $method = 'start_' . $method;
207 156 $self->$method ($attrs, '');
208 }
209 }
210  
211 # Handle the end of an element. If we had a cmd_ method for this element,
212 # this is where we pass along the text that we've accumulated. Otherwise, if
213 # we have an end_ method for the element, call that.
214 1388 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=5 SCAR=16.1
215 1388 my ($self, $element) = @_;
216 1388 my $method = $self->method_for_element ($element);
217  
218 # If we have a command handler, pull off the pending text and pass it to
219 # the handler along with the saved attribute hash.
220 1388 ▶ if ($self->can ("cmd_$method")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can("cmd_$method")) { } 1230 158 1388
elsif ($self->can("end_$method")) { } 156 2 158
221 1230 my $tag = pop @{ $$self{PENDING} };
222 1230 my $method = 'cmd_' . $method;
223 1230 my $text = $self->$method (@$tag);
224 1230 ▶ if (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 1212 18 1230
225 1212 ▶ if (@{ $$self{PENDING} } > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'PENDING'};} > 1) { } 457 755 1212
226 457 $$self{PENDING}[-1][1] .= $text;
227 } else {
228 755 $self->output ($text);
229 }
230 }
231 } elsif ($self->can ("end_$method")) {
232 156 my $method = 'end_' . $method;
233 156 $self->$method ();
234 }
235 }
236  
237 ##############################################################################
238 # Output formatting
239 ##############################################################################
240  
241 # Wrap a line, indenting by the current left margin. We can't use Text::Wrap
242 # because it plays games with tabs. We can't use formline, even though we'd
243 # really like to, because it screws up non-printing characters. So we have to
244 # do the wrapping ourselves.
245 208 ▶sub wrap {
Subroutine wrap: called CC=5 SCAR=16.3
Pod undocumented
246 208 my $self = shift;
247 208 local $_ = shift;
248 208 my $output = '';
249 208 my $spaces = ' ' x $$self{MARGIN};
250 208 my $width = $$self{opt_width} - $$self{MARGIN};
251  
252 # Pathological margins make wrapping impossible. In this case, complain
253 # to the user and reset the margin to 0.
254 208 ▶ if ($width <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width <= 0) 1 207 208
255 1 my $error = 'Margin is wider than the output width';
256 1 $self->whine($self->line_count(), $error);
257 1 $spaces = q{};
258 1 $width = $self->{opt_width};
259 }
260  
261 # Perform the wrapping.
262 208 ▶ while (length > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $width) 59 208 267
263 59 ▶ if (s/^([^\n]{0,$width})[ \t\n]+// || s/^([^\n]{$width})//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^([^\n]{0,$width})[ \t\n]+//u or s/^([^\n]{$width})//u) { } 59 0 59
Condition33%1/3
s/^([^\n]{0,$width})[ \t\n]+//u or s/^([^\n]{$width})//u
l!l&&r!l&&!r
5900
MC/DC0%0/2
s/^([^\n]{0,$width})[ \t\n]+//u or s/^([^\n]{$width})//u
s/^([^\n]{0,$width})[ \t\n]+//u s/^([^\n]{$width})//u
Truth table33%1/3
A or B
A= s/^([^\n]{0,$width})[ \t\n]+//uB= s/^([^\n]{$width})//u
ABresult
1X1
011
000
264 59 $output .= $spaces . $1 . "\n";
265 } else {
266 0 last;
267 }
268 }
269 208 $output .= $spaces . $_;
270 208 $output =~ s/\s+$/\n\n/;
271 208 return $output;
272 }
273  
274 # Reformat a paragraph of text for the current margin. Takes the text to
275 # reformat and returns the formatted text.
276 545 ▶sub reformat {
Subroutine reformat: called CC=2 SCAR=6.9
Pod undocumented
277 545 my $self = shift;
278 545 local $_ = shift;
279  
280 # If we're trying to preserve two spaces after sentences, do some munging
281 # to support that. Otherwise, smash all repeated whitespace. Be careful
282 # not to use \s here, which in Unicode input may match non-breaking spaces
283 # that we don't want to smash.
284 545 ▶ if ($$self{opt_sentence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opt_sentence'}) { } 1 544 545
285 1 s/ +$//mg;
286 1 s/\.\n/. \n/g;
287 1 s/\n/ /g;
288 1 s/ +/ /g;
289 } else {
290 544 s/[ \t\n]+/ /g;
291 }
292 545 return $self->wrap ($_);
293 }
294  
295 # Output text to the output device. Replace non-breaking spaces with spaces
296 # and soft hyphens with nothing, and then determine the output encoding.
297 1471 ▶sub output {
Subroutine output: called CC=11 SCAR=24.0
Pod documented
298 1471 my ($self, @text) = @_;
299 1471 my $text = join ('', @text);
300 1471 ▶ if ($NBSP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NBSP) 1471 0 1471
301 1471 $text =~ s/$NBSP/ /g;
302 }
303 1471 ▶ if ($SHY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SHY) 1471 0 1471
304 1471 $text =~ s/$SHY//g;
305 }
306  
307 # The logic used here is described in the POD documentation. Prefer the
308 # configured encoding, then the pass-through option of using the same
309 # encoding as the input, and then UTF-8, but commit to an encoding for the
310 # document.
311 #
312 # ENCODE says whether to encode or not and is turned off if there is a
313 # PerlIO encoding layer (in start_document). ENCODING is the encoding
314 # that we previously committed to and is cleared at the start of each
315 # document.
316 1471 ▶ if ($$self{ENCODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ENCODE'}) { } 1421 50 1471
317 1421 my $encoding = $$self{ENCODING};
318 1421 ▶ if (!$encoding) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoding) 1311 110 1421
319 1311 $encoding = $self->encoding();
320 1311 ▶ if (!$encoding && ASCII && $text =~ /[^\x00-\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $encoding and 1 and $text =~ /[^\x00-\x7F]/u) 2 1309 1311
Condition100%5/5
not $encoding and 1 and $text =~ /[^\x00-\x7F]/u
!ll&&!rl&&r
1112982
not $encoding and 1
!ll
111300
MC/DC100%2/2
not $encoding and 1 and $text =~ /[^\x00-\x7F]/u
not $encoding $text =~ /[^\x00-\x7F]/u
Truth table100%3/3
not(A) and B
A= not $encodingB= $text =~ /[^\x00-\x7F]/u
ABresult
0X0
100
111
321 2 $encoding = 'UTF-8';
322 }
323 1311 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 13 1298 1311
324 13 $$self{ENCODING} = $encoding;
325 }
326 }
327 1421 ▶ if ($encoding && $encoding ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding and $encoding ne 'none') { } 121 1300 1421
Condition100%3/3
$encoding and $encoding ne "none"
!ll&&!rl&&r
12982121
MC/DC100%2/2
$encoding and $encoding ne "none"
$encoding $encoding ne "none"
Truth table100%3/3
A and B
A= $encodingB= $encoding ne "none"
ABresult
0X0
100
111
328 2 ▶ my $check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
329 2 my ($char) = @_;
330 2 my $display = '"\x{' . hex($char) . '}"';
331 2 my $error = "$display does not map to $$self{ENCODING}";
332 2 $self->whine ($self->line_count(), $error);
333 2 return Encode::encode ($$self{ENCODING}, chr($char));
334 121 };
335 121 print { $$self{output_fh} } encode ($encoding, $text, $check);
336 } else {
337 1300 print { $$self{output_fh} } $text;
338 }
339 } else {
340 50 print { $$self{output_fh} } $text;
341 }
342 }
343  
344 # Output a block of code (something that isn't part of the POD text). Called
345 # by preprocess_paragraph only if we were given the code option. Exists here
346 # only so that it can be overridden by subclasses.
347 8 ▶sub output_code { $_[0]->output ($_[1]) }
Subroutine output_code: called CC=1 SCAR=0.0
Pod undocumented
348  
349 ##############################################################################
350 # Document initialization
351 ##############################################################################
352  
353 # Set up various things that have to be initialized on a per-document basis.
354 75 ▶sub start_document {
Subroutine start_document: called CC=5 SCAR=16.1
Pod undocumented
355 75 my ($self, $attrs) = @_;
356 75 ▶ if ($$attrs{contentless} && !$$self{ALWAYS_EMIT_SOMETHING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'contentless'} and not $self->{'ALWAYS_EMIT_SOMETHING'}) { } 1 74 75
Condition66%2/3
$attrs->{"contentless"} and not $self->{"ALWAYS_EMIT_SOMETHING"}
!ll&&!rl&&r
7401
MC/DC50%1/2
$attrs->{"contentless"} and not $self->{"ALWAYS_EMIT_SOMETHING"}
$attrs->{"contentless"} not $self->{"ALWAYS_EMIT_SOMETHING"}
Truth table66%2/3
A and not(B)
A= $attrs->{"contentless"}B= not $self->{"ALWAYS_EMIT_SOMETHING"}
ABresult
0X0
100
111
357 1 $$self{CONTENTLESS} = 1;
358 } else {
359 74 delete $$self{CONTENTLESS};
360 }
361 75 my $margin = $$self{opt_indent} + $$self{opt_margin};
362  
363 # Initialize a few per-document variables.
364 75 $$self{INDENTS} = []; # Stack of indentations.
365 75 $$self{MARGIN} = $margin; # Default left margin.
366 75 $$self{PENDING} = [[]]; # Pending output.
367  
368 # We have to redo encoding handling for each document. Check whether the
369 # output file handle already has a PerlIO encoding layer set and, if so,
370 # disable encoding.
371 75 $$self{ENCODE} = 1;
372 75 eval {
373 75 require PerlIO;
374 75 my @options = (output => 1, details => 1);
375 75 my $flag = (PerlIO::get_layers ($$self{output_fh}, @options))[-1];
376 75 ▶ if ($flag && ($flag & PerlIO::F_UTF8 ())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag and $flag & &PerlIO::F_UTF8()) 5 70 75
Condition100%3/3
$flag and $flag & &PerlIO::F_UTF8()
!ll&&!rl&&r
6375
MC/DC100%2/2
$flag and $flag & &PerlIO::F_UTF8()
$flag $flag & &PerlIO::F_UTF8()
Truth table100%3/3
A and B
A= $flagB= $flag & &PerlIO::F_UTF8()
ABresult
0X0
100
111
377 5 $$self{ENCODE} = 0;
378 }
379 };
380 75 $$self{ENCODING} = $$self{opt_encoding};
381  
382 75 return '';
383 }
384  
385 # Handle the end of the document. The only thing we do is handle dying on POD
386 # errors, since Pod::Parser currently doesn't.
387 75 ▶sub end_document {
Subroutine end_document: called CC=3 SCAR=11.0
Pod undocumented
388 75 my ($self) = @_;
389 75 ▶ if ($$self{complain_die} && $self->errors_seen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"complain_die"} and $self->errors_seen) 2 73 75
Condition66%2/3
$self->{"complain_die"} and $self->errors_seen
!ll&&!rl&&r
7302
MC/DC50%1/2
$self->{"complain_die"} and $self->errors_seen
$self->{"complain_die"} $self->errors_seen
Truth table66%2/3
A and B
A= $self->{"complain_die"}B= $self->errors_seen
ABresult
0X0
100
111
390 2 croak ("POD document had syntax errors");
391 }
392 }
393  
394 ##############################################################################
395 # Text blocks
396 ##############################################################################
397  
398 # Intended for subclasses to override, this method returns text with any
399 # non-printing formatting codes stripped out so that length() correctly
400 # returns the length of the text. For basic Pod::Text, it does nothing.
401 53 ▶sub strip_format {
Subroutine strip_format: called CC=1 SCAR=0.0
Pod undocumented
402 53 my ($self, $string) = @_;
403 53 return $string;
404 }
405  
406 # This method is called whenever an =item command is complete (in other words,
407 # we've seen its associated paragraph or know for certain that it doesn't have
408 # one). It gets the paragraph associated with the item as an argument. If
409 # that argument is empty, just output the item tag; if it contains a newline,
410 # output the item tag followed by the newline. Otherwise, see if there's
411 # enough room for us to output the item tag in the margin of the text or if we
412 # have to put it on a separate line.
413 149 ▶sub item {
Subroutine item: called CC=16 SCAR=28.0
Pod documented
414 149 my ($self, $text) = @_;
415 149 my $tag = $$self{ITEM};
416 149 ▶ unless (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 0 149 149
417 0 carp "Item called without tag";
418 0 return;
419 }
420 149 undef $$self{ITEM};
421  
422 # Calculate the indentation and margin. $fits is set to true if the tag
423 # will fit into the margin of the paragraph given our indentation level.
424 149 ▶ my $indent = $$self{INDENTS}[-1] // $$self{opt_indent};
Condition33%1/3
$self->{"INDENTS"}[-1] // $self->{"opt_indent"}
l!l&&r!l&&!r
14900
MC/DC0%0/2
$self->{"INDENTS"}[-1] // $self->{"opt_indent"}
$self->{"INDENTS"}[-1] $self->{"opt_indent"}
Truth table33%1/3
A // B
A= $self->{"INDENTS"}[-1]B= $self->{"opt_indent"}
ABresult
1X1
011
000
425 149 my $margin = ' ' x $$self{opt_margin};
426 149 my $tag_length = length ($self->strip_format ($tag));
427 149 my $fits = ($$self{MARGIN} - $indent >= $tag_length + 1);
428  
429 # If the tag doesn't fit, or if we have no associated text, print out the
430 # tag separately. Otherwise, put the tag in the margin of the paragraph.
431 149 ▶ if (!$text || $text =~ /^\s+$/ || !$fits) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $text or $text =~ /^\s+$/u or not $fits) { } 52 97 149
Condition100%6/6
not $text or $text =~ /^\s+$/u or not $fits
l!l&&r!l&&!r
232997
not $text or $text =~ /^\s+$/u
l!l&&r!l&&!r
158126
MC/DC100%3/3
not $text or $text =~ /^\s+$/u or not $fits
not $text $text =~ /^\s+$/u not $fits
Truth table100%4/4
not(A) or B or not(C)
A= not $textB= $text =~ /^\s+$/uC= not $fits
ABCresult
1XX1
01X1
0011
0000
432 52 my $realindent = $$self{MARGIN};
433 52 $$self{MARGIN} = $indent;
434 52 my $output = $self->reformat ($tag);
435 52 ▶ $output =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_alt"} and $indent > 0 2 50 52
Condition66%2/3
$self->{"opt_alt"} and $indent > 0
!ll&&!rl&&r
5002
MC/DC50%1/2
$self->{"opt_alt"} and $indent > 0
$self->{"opt_alt"} $indent > 0
Truth table66%2/3
A and B
A= $self->{"opt_alt"}B= $indent > 0
ABresult
0X0
100
111
436 52 $output =~ s/\n*$/\n/;
437  
438 # If the text is just whitespace, we have an empty item paragraph;
439 # this can result from =over/=item/=back without any intermixed
440 # paragraphs. Insert some whitespace to keep the =item from merging
441 # into the next paragraph.
442 52 ▶ $output .= "\n" if $text && $text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text and $text =~ /^\s*$/u 8 44 52
Condition100%3/3
$text and $text =~ /^\s*$/u
!ll&&!rl&&r
15298
MC/DC100%2/2
$text and $text =~ /^\s*$/u
$text $text =~ /^\s*$/u
Truth table100%3/3
A and B
A= $textB= $text =~ /^\s*$/u
ABresult
0X0
100
111
443  
444 52 $self->output ($output);
445 52 $$self{MARGIN} = $realindent;
446 52 ▶ $self->output ($self->reformat ($text)) if ($text && $text =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
if $text and $text =~ /\S/u 29 23 52
Condition100%3/3
$text and $text =~ /\S/u
!ll&&!rl&&r
15829
MC/DC100%2/2
$text and $text =~ /\S/u
$text $text =~ /\S/u
Truth table100%3/3
A and B
A= $textB= $text =~ /\S/u
ABresult
0X0
100
111
447 } else {
448 97 my $space = ' ' x $indent;
449 97 ▶ $space =~ s/^$margin /$margin:/ if $$self{opt_alt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_alt"} 2 95 97
450 97 $text = $self->reformat ($text);
451 97 ▶ $text =~ s/^$margin /$margin:/ if ($$self{opt_alt} && $indent > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_alt"} and $indent > 0 2 95 97
Condition66%2/3
$self->{"opt_alt"} and $indent > 0
!ll&&!rl&&r
9502
MC/DC50%1/2
$self->{"opt_alt"} and $indent > 0
$self->{"opt_alt"} $indent > 0
Truth table66%2/3
A and B
A= $self->{"opt_alt"}B= $indent > 0
ABresult
0X0
100
111
452 97 my $tagspace = ' ' x $tag_length;
453 97 ▶ $text =~ s/^($space)$tagspace/$1$tag/ or warn "Bizarre space in item";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/^($space)$tagspace/$1$tag/u or warn "Bizarre space in item" 97 0 97
454 97 $self->output ($text);
455 }
456 }
457  
458 # Handle a basic block of text. The only tricky thing here is that if there
459 # is a pending item tag, we need to format this as an item paragraph.
460 461 ▶sub cmd_para {
Subroutine cmd_para: called CC=2 SCAR=6.9
Pod undocumented
461 461 my ($self, $attrs, $text) = @_;
462 461 $text =~ s/\s+$/\n/;
463 461 ▶ if (defined $$self{ITEM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ITEM'}) { } 94 367 461
464 94 $self->item ($text . "\n");
465 } else {
466 367 $self->output ($self->reformat ($text . "\n"));
467 }
468 461 return '';
469 }
470  
471 # Handle a verbatim paragraph. Just print it out, but indent it according to
472 # our margin.
473 27 ▶sub cmd_verbatim {
Subroutine cmd_verbatim: called CC=3 SCAR=11.1
Pod undocumented
474 27 my ($self, $attrs, $text) = @_;
475 27 ▶ $self->item if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 0 27 27
476 27 ▶ return if $text =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\s*$/u 0 27 27
477 27 $text =~ s/^(\n*)([ \t]*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme;
478 27 $text =~ s/\s*$/\n\n/;
479 27 $self->output ($text);
480 27 return '';
481 }
482  
483 # Handle literal text (produced by =for and similar constructs). Just output
484 # it with the minimum of changes.
485 2 ▶sub cmd_data {
Subroutine cmd_data: called CC=1 SCAR=0.0
Pod undocumented
486 2 my ($self, $attrs, $text) = @_;
487 2 $text =~ s/^\n+//;
488 2 $text =~ s/\n{0,2}$/\n/;
489 2 $self->output ($text);
490 2 return '';
491 }
492  
493 ##############################################################################
494 # Headings
495 ##############################################################################
496  
497 # The common code for handling all headers. Takes the header text, the
498 # indentation, and the surrounding marker for the alt formatting method.
499 114 ▶sub heading {
Subroutine heading: called CC=4 SCAR=13.9
Pod undocumented
500 114 my ($self, $text, $indent, $marker) = @_;
501 114 ▶ $self->item ("\n\n") if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 0 114 114
502 114 $text =~ s/\s+$//;
503 114 ▶ if ($$self{opt_alt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'opt_alt'}) { } 1 113 114
504 1 my $closemark = reverse (split (//, $marker));
505 1 my $margin = ' ' x $$self{opt_margin};
506 1 $self->output ("\n" . "$margin$marker $text $closemark" . "\n\n");
507 } else {
508 113 ▶ $text .= "\n" if $$self{opt_loose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_loose"} 0 113 113
509 113 my $margin = ' ' x ($$self{opt_margin} + $indent);
510 113 $self->output ($margin . $text . "\n");
511 }
512 114 return '';
513 }
514  
515 # First level heading.
516 110 ▶sub cmd_head1 {
Subroutine cmd_head1: called CC=1 SCAR=0.0
Pod undocumented
517 110 my ($self, $attrs, $text) = @_;
518 110 $self->heading ($text, 0, '====');
519 }
520  
521 # Second level heading.
522 8 ▶sub cmd_head2 {
Subroutine cmd_head2: called CC=1 SCAR=0.0
Pod undocumented
523 8 my ($self, $attrs, $text) = @_;
524 8 $self->heading ($text, $$self{opt_indent} / 2, '== ');
525 }
526  
527 # Third level heading.
528 8 ▶sub cmd_head3 {
Subroutine cmd_head3: called CC=1 SCAR=0.0
Pod undocumented
529 8 my ($self, $attrs, $text) = @_;
530 8 $self->heading ($text, $$self{opt_indent} * 2 / 3 + 0.5, '= ');
531 }
532  
533 # Fourth level heading.
534 8 ▶sub cmd_head4 {
Subroutine cmd_head4: called CC=1 SCAR=0.0
Pod undocumented
535 8 my ($self, $attrs, $text) = @_;
536 8 $self->heading ($text, $$self{opt_indent} * 3 / 4 + 0.5, '- ');
537 }
538  
539 ##############################################################################
540 # List handling
541 ##############################################################################
542  
543 # Handle the beginning of an =over block. Takes the type of the block as the
544 # first argument, and then the attr hash. This is called by the handlers for
545 # the four different types of lists (bullet, number, text, and block).
546 81 ▶sub over_common_start {
Subroutine over_common_start: called CC=4 SCAR=15.0
Pod undocumented
547 81 my ($self, $attrs) = @_;
548 81 ▶ $self->item ("\n\n") if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 0 81 81
549  
550 # Find the indentation level.
551 81 my $indent = $$attrs{indent};
552 81 ▶ unless (defined ($indent) && $indent =~ /^\s*[-+]?\d{1,4}\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $indent and $indent =~ /^\s*[-+]?\d{1,4}\s*$/u) 0 81 81
Condition33%1/3
defined $indent and $indent =~ /^\s*[-+]?\d{1,4}\s*$/u
!ll&&!rl&&r
0081
MC/DC0%0/2
defined $indent and $indent =~ /^\s*[-+]?\d{1,4}\s*$/u
defined $indent $indent =~ /^\s*[-+]?\d{1,4}\s*$/u
Truth table33%1/3
A and B
A= defined $indentB= $indent =~ /^\s*[-+]?\d{1,4}\s*$/u
ABresult
0X0
100
111
553 0 $indent = $$self{opt_indent};
554 }
555  
556 # Add this to our stack of indents and increase our current margin.
557 81 push (@{ $$self{INDENTS} }, $$self{MARGIN});
558 81 $$self{MARGIN} += ($indent + 0);
559 81 return '';
560 }
561  
562 # End an =over block. Takes no options other than the class pointer. Output
563 # any pending items and then pop one level of indentation.
564 81 ▶sub over_common_end {
Subroutine over_common_end: called CC=2 SCAR=6.9
Pod undocumented
565 81 my ($self) = @_;
566 81 ▶ $self->item ("\n\n") if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 8 73 81
567 81 $$self{MARGIN} = pop @{ $$self{INDENTS} };
568 81 return '';
569 }
570  
571 # Dispatch the start and end calls as appropriate.
572 8 ▶sub start_over_bullet { $_[0]->over_common_start ($_[1]) }
Subroutine start_over_bullet: called CC=1 SCAR=0.0
Pod undocumented
573 8 ▶sub start_over_number { $_[0]->over_common_start ($_[1]) }
Subroutine start_over_number: called CC=1 SCAR=0.0
Pod undocumented
574 49 ▶sub start_over_text { $_[0]->over_common_start ($_[1]) }
Subroutine start_over_text: called CC=1 SCAR=0.0
Pod undocumented
575 16 ▶sub start_over_block { $_[0]->over_common_start ($_[1]) }
Subroutine start_over_block: called CC=1 SCAR=0.0
Pod undocumented
576 8 ▶sub end_over_bullet { $_[0]->over_common_end }
Subroutine end_over_bullet: called CC=1 SCAR=0.0
Pod undocumented
577 8 ▶sub end_over_number { $_[0]->over_common_end }
Subroutine end_over_number: called CC=1 SCAR=0.0
Pod undocumented
578 49 ▶sub end_over_text { $_[0]->over_common_end }
Subroutine end_over_text: called CC=1 SCAR=0.0
Pod undocumented
579 16 ▶sub end_over_block { $_[0]->over_common_end }
Subroutine end_over_block: called CC=1 SCAR=0.0
Pod undocumented
580  
581 # The common handler for all item commands. Takes the type of the item, the
582 # attributes, and then the text of the item.
583 149 ▶sub item_common {
Subroutine item_common: called CC=5 SCAR=16.1
Pod undocumented
584 149 my ($self, $type, $attrs, $text) = @_;
585 149 ▶ $self->item if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 15 134 149
586  
587 # Clean up the text. We want to end up with two variables, one ($text)
588 # which contains any body text after taking out the item portion, and
589 # another ($item) which contains the actual item text. Note the use of
590 # the internal Pod::Simple attribute here; that's a potential land mine.
591 149 $text =~ s/\s+$//;
592 149 my ($item, $index);
593 149 ▶ if ($type eq 'bullet') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bullet') { } 16 133 149
elsif ($type eq 'number') { } 16 117 133
594 16 $item = '*';
595 } elsif ($type eq 'number') {
596 16 $item = $$attrs{'~orig_content'};
597 } else {
598 117 $item = $text;
599 117 $item =~ s/\s*\n\s*/ /g;
600 117 $text = '';
601 }
602 149 $$self{ITEM} = $item;
603  
604 # If body text for this item was included, go ahead and output that now.
605 149 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 32 117 149
606 32 $text =~ s/\s*$/\n/;
607 32 $self->item ($text);
608 }
609 149 return '';
610 }
611  
612 # Dispatch the item commands to the appropriate place.
613 16 ▶sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) }
Subroutine cmd_item_bullet: called CC=1 SCAR=0.0
Pod undocumented
614 16 ▶sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) }
Subroutine cmd_item_number: called CC=1 SCAR=0.0
Pod undocumented
615 117 ▶sub cmd_item_text { my $self = shift; $self->item_common ('text', @_) }
Subroutine cmd_item_text: called CC=1 SCAR=0.0
Pod undocumented
616 0 ▶sub cmd_item_block { my $self = shift; $self->item_common ('block', @_) }
Subroutine cmd_item_block: not called CC=1 SCAR=6.9
Pod undocumented
617  
618 ##############################################################################
619 # Formatting codes
620 ##############################################################################
621  
622 # The simple ones.
623 12 ▶sub cmd_b { return $_[0]{alt} ? "``$_[2]''" : $_[2] }
Subroutine cmd_b: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'alt'} ? : 0 12 12
624 6 ▶sub cmd_f { return $_[0]{alt} ? "\"$_[2]\"" : $_[2] }
Subroutine cmd_f: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'alt'} ? : 0 6 6
625 13 ▶sub cmd_i { return '*' . $_[2] . '*' }
Subroutine cmd_i: called CC=1 SCAR=0.0
Pod undocumented
626 4 ▶sub cmd_x { return '' }
Subroutine cmd_x: called CC=1 SCAR=0.0
Pod undocumented
627  
628 # Convert all internal whitespace to $NBSP.
629 21 ▶sub cmd_s {
Subroutine cmd_s: called CC=1 SCAR=0.0
Pod undocumented
630 21 my ($self, $attrs, $text) = @_;
631 21 $text =~ s{ \s }{$NBSP}xmsg;
632 21 return $text;
633 }
634  
635 # Apply a whole bunch of messy heuristics to not quote things that don't
636 # benefit from being quoted. These originally come from Barrie Slaymaker and
637 # largely duplicate code in Pod::Man.
638 161 ▶sub cmd_c {
Subroutine cmd_c: called CC=4 SCAR=13.9
Pod undocumented
639 161 my ($self, $attrs, $text) = @_;
640  
641 # A regex that matches the portion of a variable reference that's the
642 # array or hash index, separated out just because we want to use it in
643 # several places in the following regex.
644 161 my $index = '(?: \[[^]]+\] | \{[^}]+\} )?';
645  
646 # Check for things that we don't want to quote, and if we find any of
647 # them, return the string with just a font change and no quoting.
648 #
649 # Traditionally, Pod::Text has not quoted Perl variables, functions,
650 # numbers, or hex constants, but this is not always desirable. Make this
651 # optional on the quoting guesswork flag.
652 161 my $extra = qr{(?!)}xms; # never matches
653 161 ▶ if ($$self{GUESSWORK}{quoting}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"GUESSWORK"}{"quoting"}) 140 21 161
654 140 $extra = qr{
655 \$+ [\#^]? \S $index # special ($^F, $")
656 | [\$\@%&*]+ \#? [:\'\w]+ $index # plain var or func
657 | [\$\@%&*]* [:\'\w]+
658 (?: -> )? \(\s*[^\s,\)]*\s*\) # 0/1-arg func call
659 | [+-]? ( \d[\d.]* | \.\d+ )
660 (?: [eE][+-]?\d+ )? # a number
661 | 0x [a-fA-F\d]+ # a hex constant
662 }xms;
663 }
664 161 ▶ $text =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$text =~ / \A\s* (?: ( [\'\`\"] ) .* \1 # already quoted | \` .* \' # `quoted' | \x{201C} .* \x{201D} # already quoted (Unicode) | . \" # one character and double quote | $extra ) \s*\z /msux and return $text 41 120 161
665 \A\s*
666 (?:
667 ( [\'\`\"] ) .* \1 # already quoted
668 | \` .* \' # `quoted'
669 | \x{201C} .* \x{201D} # already quoted (Unicode)
670 | . \" # one character and double quote
671 | $extra
672 )
673 \s*\z
674 }xms and return $text;
675  
676 # If we didn't return, go ahead and quote the text.
677 return $$self{opt_alt}
678 120 ▶ ? "``$text''"
Branch: 1 branch
BranchTrueFalseTotal
$self->{'opt_alt'} ? : 0 120 120
679 : "$$self{LQUOTE}$text$$self{RQUOTE}";
680 }
681  
682 # Links reduce to the text that we're given, wrapped in angle brackets if it's
683 # a URL.
684 119 ▶sub cmd_l {
Subroutine cmd_l: called CC=5 SCAR=16.1
Pod undocumented
685 119 my ($self, $attrs, $text) = @_;
686 119 ▶ if ($$attrs{type} eq 'url') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'type'} eq 'url') { } 11 108 119
687 11 ▶ if (not defined($$attrs{to}) or $$attrs{to} eq $text) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $attrs->{'to'} or $attrs->{'to'} eq $text) { } 3 8 11
elsif ($self->{'opt_nourls'}) { } 1 7 8
Condition66%2/3
not defined $attrs->{"to"} or $attrs->{"to"} eq $text
l!l&&r!l&&!r
038
MC/DC50%1/2
not defined $attrs->{"to"} or $attrs->{"to"} eq $text
not defined $attrs->{"to"} $attrs->{"to"} eq $text
Truth table66%2/3
not(A) or B
A= not defined $attrs->{"to"}B= $attrs->{"to"} eq $text
ABresult
1X1
011
000
688 3 return "<$text>";
689 } elsif ($$self{opt_nourls}) {
690 1 return $text;
691 } else {
692 7 return "$text <$$attrs{to}>";
693 }
694 } else {
695 108 return $text;
696 }
697 }
698  
699 ##############################################################################
700 # Backwards compatibility
701 ##############################################################################
702  
703 # The old Pod::Text module did everything in a pod2text() function. This
704 # tries to provide the same interface for legacy applications.
705 1 ▶sub pod2text {
Subroutine pod2text: called CC=6 SCAR=30.8
Pod documented
706 1 my @args;
707  
708 # This is really ugly; I hate doing option parsing in the middle of a
709 # module. But the old Pod::Text module supported passing flags to its
710 # entry function, so handle -a and -<number>.
711 1 ▶ while ($_[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^-/u) 0 1 1
712 0 my $flag = shift;
713 0 ▶ if ($flag eq '-a') { push (@args, alt => 1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($flag eq '-a') { } 0 0 0
elsif ($flag =~ /^-(\d+)$/u) { } 0 0 0
714 0 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
715 else {
716 0 unshift (@_, $flag);
717 0 last;
718 }
719 }
720  
721 # Now that we know what arguments we're using, create the parser.
722 1 my $parser = Pod::Text->new (@args);
723  
724 # If two arguments were given, the second argument is going to be a file
725 # handle. That means we want to call parse_from_filehandle(), which means
726 # we need to turn the first argument into a file handle. Magic open will
727 # handle the <&STDIN case automagically.
728 1 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 1 1
729 0 my @fhs = @_;
730 0 local *IN;
731 0 ▶ unless (open (IN, $fhs[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open IN, $fhs[0]) 0 0 0
732 0 croak ("Can't open $fhs[0] for reading: $!\n");
733 0 return;
734 }
735 0 $fhs[0] = \*IN;
736 0 $parser->output_fh ($fhs[1]);
737 0 my $retval = $parser->parse_file ($fhs[0]);
738 0 my $fh = $parser->output_fh ();
739 0 close $fh;
740 0 return $retval;
741 } else {
742 1 $parser->output_fh (\*STDOUT);
743 1 return $parser->parse_file (@_);
744 }
745 }
746  
747 # Reset the underlying Pod::Simple object between calls to parse_from_file so
748 # that the same object can be reused to convert multiple pages.
749 11 ▶sub parse_from_file {
Subroutine parse_from_file: called CC=4 SCAR=14.0
Pod documented
750 11 my $self = shift;
751 11 $self->reinit;
752  
753 # Fake the old cutting option to Pod::Parser. This fiddles with internal
754 # Pod::Simple state and is quite ugly; we need a better approach.
755 11 ▶ if (ref ($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 1 10 11
756 1 my $opts = shift @_;
757 1 ▶ if (defined ($$opts{-cutting}) && !$$opts{-cutting}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"-cutting"} and not $opts->{"-cutting"}) 1 0 1
Condition33%1/3
defined $opts->{"-cutting"} and not $opts->{"-cutting"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts->{"-cutting"} and not $opts->{"-cutting"}
defined $opts->{"-cutting"} not $opts->{"-cutting"}
Truth table33%1/3
A and not(B)
A= defined $opts->{"-cutting"}B= not $opts->{"-cutting"}
ABresult
0X0
100
111
758 1 $$self{in_pod} = 1;
759 1 $$self{last_was_blank} = 1;
760 }
761 }
762  
763 # Do the work.
764 11 my $retval = $self->Pod::Simple::parse_from_file (@_);
765  
766 # Flush output, since Pod::Simple doesn't do this. Ideally we should also
767 # close the file descriptor if we had to open one, but we can't easily
768 # figure this out.
769 11 my $fh = $self->output_fh ();
770 11 my $oldfh = select $fh;
771 11 my $oldflush = $|;
772 11 $| = 1;
773 11 print $fh '';
774 11 $| = $oldflush;
775 11 select $oldfh;
776 11 return $retval;
777 }
778  
779 # Pod::Simple failed to provide this backward compatibility function, so
780 # implement it ourselves. File handles are one of the inputs that
781 # parse_from_file supports.
782 1 ▶sub parse_from_filehandle {
Subroutine parse_from_filehandle: called CC=1 SCAR=0.0
Pod documented
783 1 my $self = shift;
784 1 $self->parse_from_file (@_);
785 }
786  
787 # Pod::Simple's parse_file doesn't set output_fh. Wrap the call and do so
788 # ourself unless it was already set by the caller, since our documentation has
789 # always said that this should work.
790 16 ▶sub parse_file {
Subroutine parse_file: called CC=2 SCAR=7.6
Pod documented
791 16 my ($self, $in) = @_;
792 16 ▶ unless (defined $$self{output_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"output_fh"}) 0 16 16
793 0 $self->output_fh (\*STDOUT);
794 }
795 16 return $self->SUPER::parse_file ($in);
796 }
797  
798 # Do the same for parse_lines, just to be polite. Pod::Simple's man page
799 # implies that the caller is responsible for setting this, but I don't see any
800 # reason not to set a default.
801 661 ▶sub parse_lines {
Subroutine parse_lines: called CC=2 SCAR=7.6
Pod documented
802 661 my ($self, @lines) = @_;
803 661 ▶ unless (defined $$self{output_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"output_fh"}) 0 661 661
804 0 $self->output_fh (\*STDOUT);
805 }
806 661 return $self->SUPER::parse_lines (@lines);
807 }
808  
809 # Likewise for parse_string_document.
810 60 ▶sub parse_string_document {
Subroutine parse_string_document: called CC=2 SCAR=7.6
Pod documented
811 60 my ($self, $doc) = @_;
812 60 ▶ unless (defined $$self{output_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"output_fh"}) 0 60 60
813 0 $self->output_fh (\*STDOUT);
814 }
815 60 return $self->SUPER::parse_string_document ($doc);
816 }
817  
818 ##############################################################################
819 # Module return value and documentation
820 ##############################################################################
821  
822 11 1;
823 __END__
« Man.pm ↑ Summary Text/Termcap.pm »