blib/lib/Pod/Thread.pm

filter: Statementstmt 98.5% 344 / 349 Branchbran 87.2% 89 / 102 Conditioncond 71.8% 23 / 32 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 100% 55 / 55 Podpod 100% 1 / 1 totaltotal 93.5% 523 / 559 CC 68 SCAR 14.2
CC
68
Cov
94%
_cmd_l
10.1
_end_document
9.0
_navbar
7.0
CRAP 4.1
↑ Summary
1 # Convert POD data to the HTML macro language thread.
2 #
3 # This module converts POD to the HTML macro language thread. It's intended
4 # for use with the spin program to include POD documentation in a
5 # spin-generated web page complex.
6 #
7 # SPDX-License-Identifier: MIT
8  
9 ##############################################################################
10 # Modules and declarations
11 ##############################################################################
12  
13 package Pod::Thread 3.01;
14  
15 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use base qw(Pod::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Text::Balanced qw(extract_bracketed);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Text::Wrap qw(wrap);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Pod::Simple uses subroutines named as if they're private for subclassing,
27 # and we dynamically construct method names on the fly.
28 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
29  
30 ##############################################################################
31 # Internal constants
32 ##############################################################################
33  
34 # Regex matching a manpage-style entry in the NAME header. $1 is set to the
35 # list of things documented by the man page, and $2 is set to the description.
36 2 my $NAME_REGEX = qr{ \A ( \S+ (?:,\s*\S+)* ) [ ] - [ ] (.*) }xms;
37  
38 # Maximum length of each line when constructing a navbar.
39 2 my $NAVBAR_LENGTH = 65;
40  
41 # Margin at which to wrap thread output.
42 2 my $WRAP_MARGIN = 75;
43  
44 ##############################################################################
45 # Initialization
46 ##############################################################################
47  
48 # Initialize the object and set various Pod::Simple options that we need.
49 # Here, we also process any additional options passed to the constructor or
50 # set up defaults if none were given. Note that all internal object keys are
51 # in all-caps, reserving all lower-case object keys for Pod::Simple and user
52 # arguments. User options are rewritten to start with opt_ to avoid conflicts
53 # with Pod::Simple.
54 #
55 # %opts - Our options as key/value pairs
56 #
57 # Returns: Newly constructed Pod::Thread object
58 # Throws: Whatever Pod::Simple's constructor might throw
59 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60 20 my ($class, %opts) = @_;
61 20 my $self = $class->SUPER::new;
62  
63 # Tell Pod::Simple to handle S<> by automatically inserting E<nbsp>.
64 20 $self->nbsp_for_S(1);
65  
66 # The =for and =begin targets that we accept.
67 20 $self->accept_targets('thread');
68  
69 # Ensure that contiguous blocks of code are merged together.
70 20 $self->merge_text(1);
71  
72 # Preserve whitespace whenever possible to make debugging easier.
73 20 $self->preserve_whitespace(1);
74  
75 # Always send errors to standard error.
76 20 $self->no_errata_section(1);
77 20 $self->complain_stderr(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, so rename them with an opt_ prefix.
83 20 my @opts = map { ("opt_$_", $opts{$_}) } keys %opts;
84 20 %{$self} = (%{$self}, @opts);
85  
86 20 return $self;
87 }
88  
89 ##############################################################################
90 # Core parsing
91 ##############################################################################
92  
93 # This is the glue that connects the code below with Pod::Simple itself. The
94 # goal is to convert the event stream coming from the POD parser into method
95 # calls to handlers once the complete content of a tag has been seen. Each
96 # paragraph or POD command will have textual content associated with it, and
97 # as soon as all of a paragraph or POD command has been seen, that content
98 # will be passed in to the corresponding method for handling that type of
99 # object. The exceptions are handlers for lists, which have opening tag
100 # handlers and closing tag handlers that will be called right away.
101 #
102 # The internal hash key PENDING is used to store the contents of a tag until
103 # all of it has been seen. It holds a stack of open tags, each one
104 # represented by a tuple of the attributes hash for the tag and the contents
105 # of the tag.
106  
107 # Add a block of text to the contents of the current node, protecting any
108 # thread metacharacters unless we're in a literal (=for or =begin) block.
109 #
110 # $text - A block of ordinary text seen in the POD
111 104 ▶sub _handle_text {
Subroutine _handle_text: called CC=2 SCAR=6.9
112 104 my ($self, $text) = @_;
113 104 ▶ if (!$self->{LITERAL}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"LITERAL"}) 100 4 104
114 100 $text =~ s{ \\ }{\\\\}xmsg;
115 100 $text =~ s{ ([\[\]]) }{'\\entity[' . ord($1) . ']'}xmseg;
116 }
117 104 my $tag = $self->{PENDING}[-1];
118 104 $tag->[1] .= $text;
119 104 return;
120 }
121  
122 # Given an element name, get the corresponding portion of a method name. The
123 # real methods will be formed by prepending cmd_, start_, or end_.
124 #
125 # $element - Name of the POD element by Pod::Simple's naming scheme.
126 #
127 # Returns: The element transformed into part of a method name.
128 264 ▶sub _method_for_element {
Subroutine _method_for_element: called CC=1 SCAR=0.0
129 264 my ($self, $element) = @_;
130 264 $element =~ tr{-}{_};
131 264 $element =~ tr{A-Z}{a-z};
132 264 $element =~ tr{_a-z0-9}{}cd;
133 264 return $element;
134 }
135  
136 # Handle the start of a new element. If cmd_element is defined, assume that
137 # we need to collect the entire tree for this element before passing it to the
138 # element method, and create a new tree into which we'll collect blocks of
139 # text and nested elements. Otherwise, if start_element is defined, call it.
140 #
141 # $element - The name of the POD element that was started
142 # $attrs - The attribute hash for that POD element.
143 132 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=3 SCAR=11.0
144 132 my ($self, $element, $attrs) = @_;
145 132 my $method = $self->_method_for_element($element);
146  
147 # If we have a command handler, we need to accumulate the contents of the
148 # tag before calling it. If we have a start handler, call it immediately.
149 132 ▶ if ($self->can("_cmd_$method")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can("_cmd_$method")) { } 99 33 132
elsif ($self->can("_start_$method")) { } 33 0 33
150 99 push($self->{PENDING}->@*, [$attrs, q{}]);
151 } elsif ($self->can("_start_$method")) {
152 33 $method = '_start_' . $method;
153 33 $self->$method($attrs, q{});
154 }
155 132 return;
156 }
157  
158 # Handle the end of an element. If we had a cmd_ method for this element,
159 # this is where we pass along the text that we've accumulated. Otherwise, if
160 # we have an end_ method for the element, call that.
161 #
162 # $element - The name of the POD element that was started
163 132 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=5 SCAR=16.4
164 132 my ($self, $element) = @_;
165 132 my $method = $self->_method_for_element($element);
166  
167 # If we have a command handler, pull off the pending text and pass it to
168 # the handler along with the saved attribute hash. Otherwise, if we have
169 # an end method, call it.
170 132 ▶ if ($self->can("_cmd_$method")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can("_cmd_$method")) { } 99 33 132
elsif ($self->can("_end_$method")) { } 33 0 33
171 99 my $tag_ref = pop($self->{PENDING}->@*);
172 99 $method = '_cmd_' . $method;
173 99 my $text = $self->$method($tag_ref->@*);
174  
175 # If the command returned some text, check if the element stack is
176 # non-empty. If so, add that text to the next open element.
177 # Otherwise, we're at the top level and can output the text directly.
178 99 ▶ if (defined($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 20 79 99
179 20 ▶ if ($self->{PENDING}->@* > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'PENDING'}} > 1) { } 20 0 20
180 20 $self->{PENDING}[-1][1] .= $text;
181 } else {
182 0 $self->_output($text);
183 }
184 }
185 99 return;
186 } elsif ($self->can("_end_$method")) {
187 33 $method = '_end_' . $method;
188 33 return $self->$method();
189 } else {
190 0 return;
191 }
192 }
193  
194 ##############################################################################
195 # Output formatting
196 ##############################################################################
197  
198 # Ensure text ends in two newlines.
199 #
200 # $text - Text to reformat
201 #
202 # Returns: Text with whitespace fixed
203 33 ▶sub _reformat {
Subroutine _reformat: called CC=1 SCAR=0.0
204 33 my ($self, $text) = @_;
205 33 $text =~ s{ \s* \z }{\n\n}xms;
206 33 return $text;
207 }
208  
209 # Accumulate output text. We may have some accumulated whitespace in the
210 # SPACE internal variable; if so, add that after any closing bracket at the
211 # start of our output. Then, save any whitespace at the end of our output and
212 # defer it for next time. (This creates much nicer association of closing
213 # brackets.)
214 #
215 # $text - Text to output
216 101 ▶sub _output {
Subroutine _output: called CC=4 SCAR=13.9
217 101 my ($self, $text) = @_;
218  
219 # If we have deferred whitespace, output it before the text, but after any
220 # closing bracket at the start of the text.
221 101 ▶ if ($self->{SPACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"SPACE"}) 62 39 101
222 62 ▶ if ($text =~ s{ \A \] \s* \n }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \A \] \s* \n //msux) 11 51 62
223 11 $self->{OUTPUT} .= "]\n";
224 }
225 62 $self->{OUTPUT} .= $self->{SPACE};
226 62 undef $self->{SPACE};
227 }
228  
229 # Defer any trailing newlines beyond a single newline.
230 101 ▶ if ($text =~ s{ \n (\n+) \z }{\n}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \n (\n+) \z /\n/msux) 62 39 101
231 62 $self->{SPACE} = $1;
232 }
233  
234 # Append the text to the output.
235 101 $self->{OUTPUT} .= $text;
236 101 return;
237 }
238  
239 # Flush the output at the end of a document by sending it to the correct
240 # output file handle. Force the encoding to UTF-8 unless we've found that we
241 # already have a UTF-8 encoding layer.
242 20 ▶sub _flush_output {
Subroutine _flush_output: called CC=3 SCAR=11.1
243 20 my ($self) = @_;
244 20 my $output = $self->{OUTPUT};
245  
246 # Encode if necessary and then output.
247 20 ▶ if ($self->{ENCODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENCODE"}) 20 0 20
248 20 $output = encode('UTF-8', $output);
249 }
250 20 ▶ print { $self->{output_fh} } $output
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"output_fh"};} $output or die "Cannot write to output: $!\n" 20 0 20
251 or die "Cannot write to output: $!\n";
252  
253 # Clear the output to avoid sending it twice.
254 20 $self->{OUTPUT} = q{};
255 20 return;
256 }
257  
258 ##############################################################################
259 # Document start and finish
260 ##############################################################################
261  
262 # Construct a table of contents from the headings seen throughout the
263 # document.
264 #
265 # Returns: The thread code for the table of contents
266 5 ▶sub _contents {
Subroutine _contents: called CC=3 SCAR=11.0
267 5 my ($self) = @_;
268 5 ▶ return q{} if !$self->{HEADINGS}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"HEADINGS"}} 1 4 5
269  
270 # Construct and return the table of contents.
271 4 my $output = "\\h2[Table of Contents]\n\n";
272 4 for my $i (0 .. $self->{HEADINGS}->$#*) {
273 9 my $tag = 'S' . ($i + 1);
274 9 my $section = $self->{HEADINGS}[$i];
275 9 $output .= "\\number(packed)[\\link[#$tag][$section]]\n";
276 }
277 4 $output .= "\n";
278 4 return $output;
279 }
280  
281 # Capitalize a heading for the navigation bar. Normally we want to use
282 # title case, but don't lowercase elements containing an underscore.
283 #
284 # $heading - The heading to capitalize
285 #
286 # Returns: The properly capitalized heading.
287 11 ▶sub _capitalize_for_navbar {
Subroutine _capitalize_for_navbar: called CC=5 SCAR=16.1
288 11 my ($self, $heading) = @_;
289 11 my @words = split(m{ (\s+) }xms, $heading);
290 11 for my $word (@words) {
291 63 ▶ if ($word !~ m{ _ }xms && $word !~ m{ \A \\ }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($word =~ / _ /msux or $word =~ / \A \\ /msux) 57 6 63
Condition100%3/3
$word =~ / _ /msux or $word =~ / \A \\ /msux
l!l&&r!l&&!r
1557
MC/DC100%2/2
$word =~ / _ /msux or $word =~ / \A \\ /msux
$word =~ / _ /msux $word =~ / \A \\ /msux
Truth table100%3/3
A or B
A= $word =~ / _ /msuxB= $word =~ / \A \\ /msux
ABresult
1X1
011
000
292 57 $word = lc($word);
293 57 ▶ if ($word ne 'and') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word ne "and") 55 2 57
294 55 $word = ucfirst($word);
295 }
296 }
297 }
298 11 return join(q{}, @words);
299 }
300  
301 # Construct a navigation bar. This is like a table of contents, but lists the
302 # sections separated by vertical bars and tries to limit the number of
303 # sections per line. The navbar will be presented in the sorted order of the
304 # tags.
305 #
306 # Returns: The thread code for the navbar
307 6 ▶sub _navbar {
Subroutine _navbar: called CC=7 SCAR=19.5
308 6 my ($self) = @_;
309 6 ▶ return q{} if !$self->{HEADINGS}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"HEADINGS"}} 1 5 6
310  
311 # Build the start of the navbar.
312 5 my $output = "\\class(navbar)[\n ";
313  
314 # Format the navigation bar, accumulating each line in $output. Store the
315 # formatted length in $length. We can't use length($output) because that
316 # would count all the thread commands. This won't be quite right if
317 # headings contain formatting.
318 5 my $pending = q{};
319 5 my $length = 0;
320 5 for my $i (0 .. scalar($self->{HEADINGS}->$#*)) {
321 11 my $tag = 'S' . ($i + 1);
322 11 my $section = $self->{HEADINGS}[$i];
323  
324 # If adding this section would put us over 60 characters, output the
325 # current line with a line break.
326 11 ▶ if ($length > 0 && $length + length($section) > $NAVBAR_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0 and $length + length($section) > $NAVBAR_LENGTH) 1 10 11
Condition100%3/3
$length > 0 and $length + length($section) > $NAVBAR_LENGTH
!ll&&!rl&&r
551
MC/DC100%2/2
$length > 0 and $length + length($section) > $NAVBAR_LENGTH
$length > 0 $length + length($section) > $NAVBAR_LENGTH
Truth table100%3/3
A and B
A= $length > 0B= $length + length($section) > $NAVBAR_LENGTH
ABresult
0X0
100
111
327 1 $output .= "$pending\\break\n ";
328 1 $pending = q{};
329 1 $length = 0;
330 }
331  
332 # If this isn't the first thing on a line, add the separator.
333 11 ▶ if (length($pending) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pending != 0) 5 6 11
334 5 $pending .= q{ | };
335 5 $length += length(q{ | });
336 }
337  
338 # Convert the section names to titlecase.
339 11 my $name = $self->_capitalize_for_navbar($section);
340  
341 # Add it to the current line.
342 11 $pending .= "\\link[#$tag][$name]\n";
343 11 $length += length($name);
344 }
345  
346 # Collect any remaining partial line and the end of the navbar.
347 5 ▶ if (length($pending) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pending > 0) 5 0 5
348 5 $output .= $pending;
349 }
350 5 $output .= "]\n\n";
351 5 return $output;
352 }
353  
354 # Construct the header and title of the document, including any navigation bar
355 # and contents section if we have any.
356 #
357 # $title - Document title
358 # $subheading - Document subheading (may be undef)
359 #
360 # Returns: The thread source for the document heading
361 20 ▶sub _header {
Subroutine _header: called CC=6 SCAR=17.9
362 20 my ($self) = @_;
363 20 ▶ my $style = $self->{opt_style} || q{};
Condition50%1/2
$self->{'opt_style'} || ''
l!l
020
MC/DC0%0/1
$self->{'opt_style'} || ''
$self->{'opt_style'}
Truth table50%1/2
A
A= $self->{'opt_style'}
Aresult
11
00
364 20 my $output = q{};
365  
366 # Add the basic title, page heading, and style if we saw a title.
367 20 ▶ if ($self->{TITLE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TITLE'}) { } 5 15 20
368 5 $output .= "\\heading[$self->{TITLE}][$style]\n\n";
369 5 $output .= "\\h1[$self->{TITLE}]\n\n";
370 } else {
371 15 $output .= "\\heading[][]\n\n";
372 }
373  
374 # If there is a subheading, add it.
375 20 ▶ if (defined($self->{SUBHEADING})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"SUBHEADING"}) 4 16 20
376 4 $output .= "\\class(subhead)[($self->{SUBHEADING})]\n\n";
377 }
378  
379 # If a navbar or table of contents was requested, add it.
380 20 ▶ if ($self->{opt_navbar}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt_navbar"}) 6 14 20
381 6 $output .= $self->_navbar();
382 }
383 20 ▶ if ($self->{opt_contents}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"opt_contents"}) 5 15 20
384 5 $output .= $self->_contents();
385 }
386  
387 # Return the results.
388 20 return $output;
389 }
390  
391 # Handle the beginning of a POD file. We only output something if title is
392 # set, in which case we output the title and other header information at the
393 # beginning of the resulting output file.
394 #
395 # $attrs - Attributes of the start document tag
396 20 ▶sub _start_document {
Subroutine _start_document: called CC=5 SCAR=16.2
397 20 my ($self, $attrs) = @_;
398  
399 # If the document has no content, set the appropriate internal flag.
400 20 ▶ if ($attrs->{contentless}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'contentless'}) { } 1 19 20
401 1 $self->{CONTENTLESS} = 1;
402 } else {
403 19 delete $self->{CONTENTLESS};
404 }
405  
406 # Initialize per-document variables.
407 #<<<
408 20 $self->{HEADINGS} = [];
409 20 $self->{IN_NAME} = 0;
410 20 $self->{ITEM_OPEN} = 0;
411 20 $self->{ITEM_PENDING} = 0;
412 20 $self->{ITEMS} = [];
413 20 $self->{LITERAL} = 0;
414 20 $self->{OUTPUT} = q{};
415 20 $self->{PENDING} = [[]];
416 20 $self->{SUBHEADING} = undef;
417 20 ▶ $self->{TITLE} = $self->{opt_title} // q{};
Condition100%2/2
$self->{"opt_title"} // ""
l!l
119
MC/DC100%1/1
$self->{"opt_title"} // ""
$self->{"opt_title"}
Truth table100%2/2
A
A= $self->{"opt_title"}
Aresult
11
00
418 #>>>
419  
420 # Check whether our output file handle already has a PerlIO encoding layer
421 # set. If it does not, we'll need to encode our output before printing
422 # it. Wrap the check in an eval to handle versions of Perl without
423 # PerlIO.
424 20 $self->{ENCODE} = 1;
425 20 eval {
426 20 my @options = (output => 1, details => 1);
427 20 my @layers = PerlIO::get_layers($self->{output_fh}->**, @options);
428 20 ▶ if ($layers[-1] && ($layers[-1] & PerlIO::F_UTF8())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layers[-1] and $layers[-1] & 32768) 0 20 20
Condition33%1/3
$layers[-1] and $layers[-1] & 32768
!ll&&!rl&&r
2000
MC/DC0%0/2
$layers[-1] and $layers[-1] & 32768
$layers[-1] $layers[-1] & 32768
Truth table33%1/3
A and B
A= $layers[-1]B= $layers[-1] & 32768
ABresult
0X0
100
111
429 0 $self->{ENCODE} = 0;
430 }
431 };
432 20 return;
433 }
434  
435 # Canonicalize a heading for internal links. We run both the anchor text and
436 # the heading itself through this function so that whitespace differences
437 # don't cause us to fail to create the link.
438 #
439 # Note that this affects only the end-of-document rewriting, not the links we
440 # create as we go, because this case is rare and doing it as we go would
441 # require more state tracking.
442 #
443 # $heading - Text of heading
444 #
445 # Returns: Canonicalized heading text
446 23 ▶sub _canonicalize_heading {
Subroutine _canonicalize_heading: called CC=1 SCAR=0.0
447 23 my ($self, $heading) = @_;
448 23 $heading =~ s{ \s+ }{ }xmsg;
449 23 return $heading;
450 }
451  
452 # Handle the end of the document. Tack \signature onto the end, output the
453 # header and the accumulated output, and die if we saw any errors.
454 #
455 # Throws: Text exception if there were any errata
456 20 ▶sub _end_document {
Subroutine _end_document: called CC=9 SCAR=22.0
457 20 my ($self) = @_;
458  
459 # Output the \signature command if we saw any content.
460 20 ▶ if (!$self->{CONTENTLESS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"CONTENTLESS"}) 19 1 20
461 19 $self->_output("\\signature\n");
462 }
463  
464 # Search for any unresolved links and try to fix their anchors. If we
465 # never saw the heading in question, remove the \link command. We have to
466 # use Text::Balanced and substr surgery to extract the anchor text since
467 # it may contain arbitrary markup.
468 #
469 # This is very inefficient for large documents, but I doubt anything
470 # processed by this module will be large enough to matter.
471 20 my $i = 1;
472 20 my $search = '\\link[#PLACEHOLDER]';
473 20 my $start = 0;
474 18 my %headings = map { ('[' . $self->_canonicalize_heading($_) . ']', $i++) }
475 20 $self->{HEADINGS}->@*;
476 20 ▶ while (($start = index($self->{OUTPUT}, $search, $start)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (($start = index($self->{"OUTPUT"}, $search, $start)) != -1) 5 20 25
477 5 my $text = substr($self->{OUTPUT}, $start + length($search));
478 5 my ($anchor) = extract_bracketed($text, '[]', undef);
479 5 my $heading;
480 5 ▶ if ($anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchor) 5 0 5
481 5 $heading = $self->_canonicalize_heading($anchor);
482 }
483  
484 # If this is a known heading, replace #PLACEHOLDER with the link to
485 # that heading and continue processing with the anchor text.
486 # Otherwise, replace the entire \link command with the anchor text and
487 # continue processing after it.
488 5 ▶ if (defined($anchor) && defined($headings{$heading})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor and defined $headings{$heading}) { } 4 1 5
Condition66%2/3
defined $anchor and defined $headings{$heading}
!ll&&!rl&&r
014
MC/DC50%1/2
defined $anchor and defined $headings{$heading}
defined $anchor defined $headings{$heading}
Truth table66%2/3
A and B
A= defined $anchorB= defined $headings{$heading}
ABresult
0X0
100
111
489 4 $start += length('\\link[');
490 4 my $link = "#S$headings{$heading}";
491 4 substr($self->{OUTPUT}, $start, length('#PLACEHOLDER'), $link);
492 } else {
493 1 my $length = length('\\link[#PLACEHOLDER]') + length($anchor);
494 1 $anchor = substr($anchor, 1, -1);
495 1 substr($self->{OUTPUT}, $start, $length, $anchor);
496 1 $start += length($anchor);
497 }
498 }
499  
500 # Output the header.
501 20 my $header = $self->_header();
502 20 ▶ if ($self->{ENCODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ENCODE"}) 20 0 20
503 20 $header = encode('UTF-8', $header);
504 }
505 20 ▶ print { $self->{output_fh} } $header
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"output_fh"};} $header or die "Cannot write to output: $!\n" 20 0 20
506 or die "Cannot write to output: $!\n";
507  
508 # Flush the rest of the output.
509 20 $self->_flush_output();
510  
511 # Die if we saw any errors.
512 20 ▶ if ($self->any_errata_seen()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->any_errata_seen) 1 19 20
513 1 croak('POD document had syntax errors');
514 }
515 19 return;
516 }
517  
518 ##############################################################################
519 # Text blocks
520 ##############################################################################
521  
522 # Called for a regular text block. There are two tricky parts here. One is
523 # that if there is a pending item tag, we need to format this as an item
524 # paragraph. The second is that if we're in the NAME section and see the name
525 # and description of the page, we should print out the header.
526 #
527 # $attrs - Attributes for this command
528 # $text - The text of the block
529 33 ▶sub _cmd_para {
Subroutine _cmd_para: called CC=4 SCAR=13.9
530 33 my ($self, $attrs, $text) = @_;
531  
532 # Ensure the text block ends with a single newline.
533 33 $text =~ s{ \s+ \z }{\n}xms;
534  
535 # If we're inside an item block, handle this as an item.
536 33 ▶ if (@{ $self->{ITEMS} } > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'ITEMS'};} > 0) { } 8 25 33
elsif ($self->{'IN_NAME'} and $text =~ /$NAME_REGEX/u) { } 4 21 25
537 8 $self->_item($self->_reformat($text));
538 }
539  
540 # If we're in the NAME section and see a line that looks like the special
541 # NAME section of a man page, stash that information for the page heading.
542 ▶ elsif ($self->{IN_NAME} && $text =~ $NAME_REGEX) {
Condition66%2/3
$self->{"IN_NAME"} and $text =~ /$NAME_REGEX/u
!ll&&!rl&&r
2104
MC/DC50%1/2
$self->{"IN_NAME"} and $text =~ /$NAME_REGEX/u
$self->{"IN_NAME"} $text =~ /$NAME_REGEX/u
Truth table66%2/3
A and B
A= $self->{"IN_NAME"}B= $text =~ /$NAME_REGEX/u
ABresult
0X0
100
111
543 4 my ($name, $description) = ($1, $2);
544 4 $self->{TITLE} = $name;
545 4 $self->{SUBHEADING} = $description;
546 }
547  
548 # Otherwise, this is a regular text block, so just output it with a
549 # trailing blank line.
550 else {
551 21 $self->_output($self->_reformat($text . "\n"));
552 }
553 33 return;
554 }
555  
556 # Called for a verbatim paragraph. The only trick is knowing whether to use
557 # the item method to handle it or just print it out directly.
558 #
559 # $attrs - Attributes for this command
560 # $text - The text of the block
561 4 ▶sub _cmd_verbatim {
Subroutine _cmd_verbatim: called CC=3 SCAR=11.1
562 4 my ($self, $attrs, $text) = @_;
563  
564 # Ignore empty verbatim paragraphs.
565 4 ▶ if ($text =~ m{ \A \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / \A \s* \z /msux) 0 4 4
566 0 return;
567 }
568  
569 # Ensure the paragraph ends in a bracket and two newlines.
570 4 $text =~ s{ \s* \z }{\]\n\n}xms;
571  
572 # Pass the text to either item or output.
573 4 ▶ if (@{ $self->{ITEMS} } > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'ITEMS'};} > 0) { } 2 2 4
574 2 $self->_item("\\pre\n[$text");
575 } else {
576 2 $self->_output("\\pre\n[$text");
577 }
578 4 return;
579 }
580  
581 # Called for literal text produced by =for and similar constructs. Just
582 # output the text verbatim with cleaned-up whitespace.
583 #
584 # $attrs - Attributes for this command
585 # $text - The text of the block
586 4 ▶sub _cmd_data {
Subroutine _cmd_data: called CC=1 SCAR=0.0
587 4 my ($self, $attrs, $text) = @_;
588 4 $text =~ s{ \A (\s*\n)+ }{}xms;
589 4 $text =~ s{ \s* \z }{\n\n}xms;
590 4 $self->_output($text);
591 4 return;
592 }
593  
594 # Called when =for and similar constructs are started or ended. Set or clear
595 # the literal flag so that we won't escape the text on the way in.
596 #<<<
597 2 ▶sub _start_for { my ($self) = @_; $self->{LITERAL} = 1; return; }
Subroutine _start_for: called CC=1 SCAR=0.0
598 2 ▶sub _end_for { my ($self) = @_; $self->{LITERAL} = 0; return; }
Subroutine _end_for: called CC=1 SCAR=0.0
599 #>>>
600  
601 ##############################################################################
602 # Headings
603 ##############################################################################
604  
605 # The common code for handling all headings. Take care of any pending items
606 # or lists and then output the thread code for the heading.
607 #
608 # $text - The text of the heading itself
609 # $level - The level of the heading as a number (2..5)
610 # $tag - An optional tag for the heading
611 22 ▶sub _heading {
Subroutine _heading: called CC=2 SCAR=6.9
612 22 my ($self, $text, $level, $tag) = @_;
613  
614 # If there is a waiting item or a pending close bracket, output it now.
615 22 $self->_finish_item();
616  
617 # Strip any trailing whitespace.
618 22 $text =~ s{ \s+ \z }{}xms;
619  
620 # Output the heading thread.
621 22 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) { } 18 4 22
622 18 $self->_output("\\h$level($tag)[$text]\n\n");
623 } else {
624 4 $self->_output("\\h$level" . "[$text]\n\n");
625 }
626 22 return;
627 }
628  
629 # First level heading. This requires some special handling to update the
630 # IN_NAME setting based on whether we're currently in the NAME section. Also
631 # add a tag to the heading if we have section information.
632 #
633 # $attrs - Attributes for this command
634 # $text - The text of the block
635 #
636 # Returns: The result of the heading method
637 22 ▶sub _cmd_head1 {
Subroutine _cmd_head1: called CC=3 SCAR=11.0
638 22 my ($self, $attrs, $text) = @_;
639  
640 # Strip whitespace from the text since we're going to compare it to other
641 # things.
642 22 $text =~ s{ \s+ \z }{}xms;
643  
644 # If we're in the NAME section and no title was explicitly set, set the
645 # flag used in cmd_para to parse the NAME text specially and then do
646 # nothing else (since we won't print out the NAME section as itself.
647 22 ▶ if ($text eq 'NAME' && !defined($self->{opt_title})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq "NAME" and not defined $self->{"opt_title"}) 4 18 22
Condition66%2/3
$text eq "NAME" and not defined $self->{"opt_title"}
!ll&&!rl&&r
1804
MC/DC50%1/2
$text eq "NAME" and not defined $self->{"opt_title"}
$text eq "NAME" not defined $self->{"opt_title"}
Truth table66%2/3
A and not(B)
A= $text eq "NAME"B= not defined $self->{"opt_title"}
ABresult
0X0
100
111
648 4 $self->{IN_NAME} = 1;
649 4 return;
650 }
651 18 $self->{IN_NAME} = 0;
652  
653 # Not in the name section. Record the heading and a tag to the header.
654 18 push($self->{HEADINGS}->@*, $text);
655 18 my $tag = 'S' . scalar($self->{HEADINGS}->@*);
656 18 return $self->_heading($text, 2, "#$tag");
657 }
658  
659 # All the other headings, which just hand off to the heading method.
660 2 ▶sub _cmd_head2 { my ($self, $j, $text) = @_; return $self->_heading($text, 3) }
Subroutine _cmd_head2: called CC=1 SCAR=0.0
661 1 ▶sub _cmd_head3 { my ($self, $j, $text) = @_; return $self->_heading($text, 4) }
Subroutine _cmd_head3: called CC=1 SCAR=0.0
662 1 ▶sub _cmd_head4 { my ($self, $j, $text) = @_; return $self->_heading($text, 5) }
Subroutine _cmd_head4: called CC=1 SCAR=0.0
663  
664 ##############################################################################
665 # List handling
666 ##############################################################################
667  
668 # Called for each paragraph of text that we see inside an item. It's also
669 # called with no text when it's time to start an item even though there wasn't
670 # any text associated with it (which happens for description lists). The top
671 # of the ITEMS stack will hold the command that should be used to open the
672 # item block in thread.
673 #
674 # $text - Contents of the text block inside =item
675 19 ▶sub _item {
Subroutine _item: called CC=4 SCAR=13.9
676 19 my ($self, $text) = @_;
677  
678 # If there wasn't anything waiting, we're in the second or subsequent
679 # paragraph of the item text. Just output it.
680 19 ▶ if (!$self->{ITEM_PENDING}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ITEM_PENDING"}) 5 14 19
681 5 $self->_output($text);
682 5 return;
683 }
684  
685 # We're starting a new item. Close any pending =item block.
686 14 ▶ if ($self->{ITEM_OPEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ITEM_OPEN"}) 3 11 14
687 3 $self->_output("]\n");
688 3 $self->{ITEM_OPEN} = 0;
689 }
690  
691 # Now, output the start of the item tag plus the text, if any.
692 14 my $tag = $self->{ITEMS}[-1];
693 14 ▶ $self->_output($tag . "\n[" . ($text // q{}));
Condition100%2/2
$text // ""
l!l
95
MC/DC100%1/1
$text // ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
694 14 $self->{ITEM_OPEN} = 1;
695 14 $self->{ITEM_PENDING} = 0;
696 14 return;
697 }
698  
699 # Output any waiting items and close any pending blocks.
700 33 ▶sub _finish_item {
Subroutine _finish_item: called CC=3 SCAR=11.0
701 33 my ($self) = @_;
702 33 ▶ if ($self->{ITEM_PENDING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ITEM_PENDING"}) 1 32 33
703 1 $self->_item();
704 }
705 33 ▶ if ($self->{ITEM_OPEN}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ITEM_OPEN"}) 11 22 33
706 11 $self->_output("]\n");
707 11 $self->{ITEM_OPEN} = 0;
708 }
709 33 return;
710 }
711  
712 # Handle the beginning of an =over block. This is called by the handlers for
713 # the four different types of lists (bullet, number, desc, and block). Update
714 # our internal tracking for =over blocks.
715 11 ▶sub _over_start {
Subroutine _over_start: called CC=2 SCAR=6.9
716 11 my ($self) = @_;
717  
718 # If an item was already pending, we have nested =over blocks. Open the
719 # outer block here before we start processing items for the inside block.
720 11 ▶ if ($self->{ITEM_PENDING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ITEM_PENDING"}) 2 9 11
721 2 $self->_item();
722 }
723  
724 # Start a new block.
725 11 $self->{ITEM_OPEN} = 0;
726 11 push($self->{ITEMS}->@*, q{});
727 11 return;
728 }
729  
730 # Handle the end of a list. Output any waiting items, close any pending
731 # blocks, and pop one level of item off the item stack.
732 11 ▶sub _over_end {
Subroutine _over_end: called CC=2 SCAR=6.9
733 11 my ($self) = @_;
734  
735 # If there is a waiting item or a pending close bracket, output it now.
736 11 $self->_finish_item();
737  
738 # Pop the item off the stack.
739 11 pop($self->{ITEMS}->@*);
740  
741 # Set pending based on whether there's still another level of item open.
742 11 ▶ if ($self->{ITEMS}->@* > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"ITEMS"}} > 0) 4 7 11
743 4 $self->{ITEM_OPEN} = 1;
744 }
745 11 return;
746 }
747  
748 # All the individual start commands for the specific types of lists. These
749 # are all dispatched to the relevant common routine except for block.
750 # Pod::Simple gives us the type information on both the =over and the =item.
751 # We ignore it here and use it when we see the =item.
752 #<<<
753 3 ▶sub _start_over_bullet { my ($self) = @_; return $self->_over_start() }
Subroutine _start_over_bullet: called CC=1 SCAR=0.0
754 1 ▶sub _start_over_number { my ($self) = @_; return $self->_over_start() }
Subroutine _start_over_number: called CC=1 SCAR=0.0
755 5 ▶sub _start_over_text { my ($self) = @_; return $self->_over_start() }
Subroutine _start_over_text: called CC=1 SCAR=0.0
756 #>>>
757  
758 # Over of type block (which is =over without any =item) has to be handled
759 # specially, since normally we defer issuing the tag until we see the first
760 # =item and that won't happen here.
761 2 ▶sub _start_over_block {
Subroutine _start_over_block: called CC=1 SCAR=0.0
762 2 my ($self) = @_;
763 2 $self->_over_start();
764 2 $self->{ITEMS}[-1] = '\\block';
765 2 $self->{ITEM_PENDING} = 1;
766 2 $self->_item();
767 2 return;
768 }
769  
770 # Likewise for the end commands.
771 #<<<
772 2 ▶sub _end_over_block { my ($self) = @_; return $self->_over_end() }
Subroutine _end_over_block: called CC=1 SCAR=0.0
773 3 ▶sub _end_over_bullet { my ($self) = @_; return $self->_over_end() }
Subroutine _end_over_bullet: called CC=1 SCAR=0.0
774 1 ▶sub _end_over_number { my ($self) = @_; return $self->_over_end() }
Subroutine _end_over_number: called CC=1 SCAR=0.0
775 5 ▶sub _end_over_text { my ($self) = @_; return $self->_over_end() }
Subroutine _end_over_text: called CC=1 SCAR=0.0
776 #>>>
777  
778 # An individual list item command. Note that this fires when the =item
779 # command is seen, not when we've accumulated all the text that's part of that
780 # item. We may have some body text and we may not, but we have to defer the
781 # end of the item until the surrounding =over is closed.
782 #
783 # $type - The type of the item
784 # $attrs - Attributes for this command
785 # $text - The text of the block
786 12 ▶sub _item_common {
Subroutine _item_common: called CC=3 SCAR=11.1
787 12 my ($self, $type, $attrs, $text) = @_;
788  
789 # If we saw an =item command, any previous item block is finished, so
790 # output that now.
791 12 ▶ if ($self->{ITEM_PENDING}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ITEM_PENDING"}) 0 12 12
792 0 $self->_item();
793 }
794  
795 # The top of the stack should now contain our new type of item.
796 12 $self->{ITEMS}[-1] = "\\$type";
797  
798 # We now have an item waiting for output.
799 12 $self->{ITEM_PENDING} = 1;
800  
801 # If the type is desc, anything in $text is the description title and
802 # needs to be appended to our ITEM.
803 12 ▶ if ($self->{ITEMS}[-1] eq '\\desc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ITEMS'}[-1] eq '\\desc') { } 8 4 12
804 8 $text =~ s{ \s+ \z }{}xms;
805 8 $self->{ITEMS}[-1] .= "[$text]";
806 }
807  
808 # Otherwise, anything in $text is body text. Handle that now.
809 else {
810 4 $self->_item($self->_reformat($text));
811 }
812  
813 12 return;
814 }
815  
816 # All the various item commands just call item_common.
817 ## no critic (Subroutines::RequireArgUnpacking)
818 #<<<
819 3 ▶sub _cmd_item_bullet { my $s = shift; return $s->_item_common('bullet', @_) }
Subroutine _cmd_item_bullet: called CC=1 SCAR=0.0
820 1 ▶sub _cmd_item_number { my $s = shift; return $s->_item_common('number', @_) }
Subroutine _cmd_item_number: called CC=1 SCAR=0.0
821 8 ▶sub _cmd_item_text { my $s = shift; return $s->_item_common('desc', @_) }
Subroutine _cmd_item_text: called CC=1 SCAR=0.0
822 #>>>
823 ## use critic
824 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
825  
826 ##############################################################################
827 # Formatting codes
828 ##############################################################################
829  
830 # The simple ones. These are here mostly so that subclasses can override them
831 # and do more complicated things.
832 #
833 # $attrs - Attributes for this command
834 # $text - The text of the block
835 #
836 # Returns: The formatted text
837 2 ▶sub _cmd_b { my ($self, $attrs, $text) = @_; return "\\bold[$text]" }
Subroutine _cmd_b: called CC=1 SCAR=0.0
838 2 ▶sub _cmd_c { my ($self, $attrs, $text) = @_; return "\\code[$text]" }
Subroutine _cmd_c: called CC=1 SCAR=0.0
839 2 ▶sub _cmd_f { my ($self, $attrs, $text) = @_; return "\\italic(file)[$text]" }
Subroutine _cmd_f: called CC=1 SCAR=0.0
840 2 ▶sub _cmd_i { my ($self, $attrs, $text) = @_; return "\\italic[$text]" }
Subroutine _cmd_i: called CC=1 SCAR=0.0
841 1 ▶sub _cmd_x { return q{} }
Subroutine _cmd_x: called CC=1 SCAR=0.0
842  
843 # Format a link. Don't try to generate hyperlinks for anything other than
844 # normal URLs and section links within our same document. For the latter, we
845 # can only do that for sections we've already seen; for everything else, use a
846 # PLACEHOLDER tag that we'll try to replace with a real link as the last step
847 # of formatting the document.
848 #
849 # $attrs - Attributes for this command
850 # $text - The text of the block
851 #
852 # Returns: The formatted link
853 11 ▶sub _cmd_l {
Subroutine _cmd_l: called CC=10 SCAR=23.2
854 11 my ($self, $attrs, $text) = @_;
855 11 ▶ if ($attrs->{type} eq 'url') {
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs->{'type'} eq 'url') { } 2 9 11
elsif ($attrs->{'type'} eq 'pod') { } 9 0 9
856 2 ▶ if (!defined($attrs->{to}) || $attrs->{to} eq $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attrs->{'to'} or $attrs->{'to'} eq $text) { } 1 1 2
Condition66%2/3
not defined $attrs->{"to"} or $attrs->{"to"} eq $text
l!l&&r!l&&!r
011
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
857 1 return "<\\link[$text][$text]>";
858 } else {
859 1 return "\\link[$attrs->{to}][$text]";
860 }
861 } elsif ($attrs->{type} eq 'pod') {
862 9 my $page = $attrs->{to};
863 9 my $section = $attrs->{section};
864 9 ▶ if (!defined($page) && defined($section)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $page and defined $section) 7 2 9
Condition66%2/3
not defined $page and defined $section
!ll&&!rl&&r
207
MC/DC50%1/2
not defined $page and defined $section
not defined $page defined $section
Truth table66%2/3
not(A) and B
A= not defined $pageB= defined $section
ABresult
0X0
100
111
865 7 my $tag = 'PLACEHOLDER';
866 7 for my $i (0 .. scalar($self->{HEADINGS}->$#*)) {
867 10 ▶ if ($self->{HEADINGS}[$i] eq $section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"HEADINGS"}[$i] eq $section) 2 8 10
868 2 $tag = 'S' . ($i + 1);
869 2 last;
870 }
871 }
872 7 $text =~ s{ \A \" }{}xms;
873 7 $text =~ s{ \" \z }{}xms;
874 7 return "\\link[#$tag][$text]";
875 }
876 }
877  
878 # Fallthrough just returns the preformatted text from Pod::Simple.
879 2 ▶ return $text // q{};
Condition50%1/2
$text // ""
l!l
20
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
880 }
881  
882 ##############################################################################
883 # Module return value and documentation
884 ##############################################################################
885  
886 2 1;
887 __END__
↑ Summary