| 1 | # Parse an L<> formatting code in POD text. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # This module implements parsing of the text of an L<> formatting code as | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # defined in perlpodspec. It should be suitable for any POD formatter. It | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # exports only one function, parselink(), which returns the five-item parse | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # defined in perlpodspec. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # Modules and declarations | ||||||||||||||||||||||||||||||||||||||||||
| 12 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | package Pod::ParseLink v6.1.1; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use 5.012; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | our @EXPORT = qw(parselink); | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # Implementation | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # Parse the name and section portion of a link into a name and section. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 23 | ▶ | sub _parse_section { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_section: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 23 | my ($link) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 23 | $link =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 23 | $link =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # If the whole link is enclosed in quotes, interpret it all as a section | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # even if it contains a slash. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 23 | ▶ | return (undef, $1) if ($link =~ /^"\s*(.*?)\s*"$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # Split into page and section on slash, and then clean up quoting in the | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # section. If there is no section and the name contains spaces, also | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # guess that it's an old section link. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 21 | my ($page, $section) = split (/\s*\/\s*/, $link, 2); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 21 | ▶ | $section =~ s/^"\s*(.*?)\s*"$/$1/ if $section; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 21 | ▶ | if ($page && $page =~ / / && !defined ($section)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$page and $page =~ / /u and not defined $section
$page and $page =~ / /u
MC/DC100%3/3
$page and $page =~ / /u and not defined $section
$page
$page =~ / /u
not defined $section
Truth table100%4/4
A and B and not(C)
A= $pageB= $page =~ / /uC= not defined $section
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $section = $page; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | $page = undef; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 20 | ▶ | $page = undef unless $page; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 20 | ▶ | $section = undef unless $section; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 21 | return ($page, $section); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # Infer link text from the page and section. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 17 | ▶ | sub _infer_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_infer_text: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 17 | my ($page, $section) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 17 | my $inferred; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 17 | ▶ | if ($page && !$section) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$page and not $section
MC/DC100%2/2
$page and not $section
$page
not $section
Truth table100%3/3
A and not(B)
A= $pageB= not $section
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | $inferred = $page; | |||||||||||||||||||||||||||||||||||||||||
| 58 | ▶ | } elsif (!$page && $section) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not $page and $section
MC/DC50%1/2
not $page and $section
not $page
$section
Truth table66%2/3
not(A) and B
A= not $pageB= $section
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | $inferred = '"' . $section . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 60 | ▶ | } elsif ($page && $section) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$page and $section
MC/DC0%0/2
$page and $section
$page
$section
Truth table33%1/3
A and B
A= $pageB= $section
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | $inferred = '"' . $section . '" in ' . $page; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 17 | return $inferred; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # Given the contents of an L<> formatting code, parse it into the link text, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # the possibly inferred link text, the name or URL, the section, and the type | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # of link (pod, man, or url). | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 27 | ▶ | sub parselink { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parselink: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 27 | my ($link) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 27 | $link =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 27 | my $text; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 27 | ▶ | if ($link =~ /\|/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | ($text, $link) = split (/\|/, $link, 2); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 27 | ▶ | if ($link =~ /\A\w+:[^:\s]\S*\Z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | my $inferred; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | if (defined ($text) && length ($text) > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and length $text > 0
MC/DC50%1/2
defined $text and length $text > 0
defined $text
length $text > 0
Truth table66%2/3
A and B
A= defined $textB= length $text > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | return ($text, $text, $link, undef, 'url'); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | return ($text, $link, $link, undef, 'url'); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 23 | my ($name, $section) = _parse_section ($link); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 23 | my $inferred; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 23 | ▶ | if (defined ($text) && length ($text) > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and length $text > 0
MC/DC50%1/2
defined $text and length $text > 0
defined $text
length $text > 0
Truth table66%2/3
A and B
A= defined $textB= length $text > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | $inferred = $text; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | $inferred = _infer_text ($name, $section); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 23 | ▶ | my $type = ($name && $name =~ /\(\S*\)/) ? 'man' : 'pod'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name and $name =~ /\(\S*\)/u
MC/DC100%2/2
$name and $name =~ /\(\S*\)/u
$name
$name =~ /\(\S*\)/u
Truth table100%3/3
A and B
A= $nameB= $name =~ /\(\S*\)/u
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 23 | return ($text, $inferred, $name, $section, $type); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # Module return value and documentation | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Ensure we evaluate to true. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||