lib/Text/AsciidocDown/Refs.pm

filter: Statementstmt 100% 44 / 44 Branchbran 60.7% 17 / 28 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 71.8% 79 / 110 CC 21 SCAR 18.6
CC
21
Cov
76%
register_heading
9.9
_auto_id
8.0
resolve_target
6.5
CRAP 6.4
« lib/Text/AsciidocDown/Include.pm ↑ Summary lib/Text/AsciidocDown/Subs.pm »
1 package Text::AsciidocDown::Refs;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 our $VERSION = '0.1.3';
7  
8 sub new_index
9 99 ▶{
Subroutine new_index: called CC=1 SCAR=0.0
Pod documented
10 return {
11 99 by_id => {},
12 by_title => {},
13 };
14 }
15  
16 sub register_heading
17 36 ▶{
Subroutine register_heading: called CC=7 SCAR=23.0
Pod documented
18 36 my ($idx, $attrs, $title, $explicit_id, $reftext) = @_;
19 36 ▶ return unless ref($idx) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $idx eq "HASH" 0 36 36
20  
21 36 my $auto_id = _auto_id($attrs, $title);
22 36 ▶ my $id = defined($explicit_id) && $explicit_id ne '' ? $explicit_id : $auto_id;
Branch: 1 branch
BranchTrueFalseTotal
defined $explicit_id && $explicit_id ne '' ? : 0 36 36
Condition33%1/3
defined $explicit_id and $explicit_id ne ""
!ll&&!rl&&r
3600
MC/DC0%0/2
defined $explicit_id and $explicit_id ne ""
defined $explicit_id $explicit_id ne ""
Truth table33%1/3
A and B
A= defined $explicit_idB= $explicit_id ne ""
ABresult
0X0
100
111
23  
24 36 ▶ my $entry = {
Branch: 1 branch
BranchTrueFalseTotal
defined $reftext && $reftext ne '' ? : 0 36 36
Condition33%1/3
defined $reftext and $reftext ne ""
!ll&&!rl&&r
3600
MC/DC0%0/2
defined $reftext and $reftext ne ""
defined $reftext $reftext ne ""
Truth table33%1/3
A and B
A= defined $reftextB= $reftext ne ""
ABresult
0X0
100
111
25 id => $id,
26 auto_id => $auto_id,
27 title => $title,
28 reftext => (defined($reftext) && $reftext ne '') ? $reftext : $title,
29 };
30  
31 36 $idx->{by_id}{$id} = $entry;
32 36 ▶ $idx->{by_title}{$title} ||= $entry;
Condition33%1/3
$idx->{"by_title"}{$title} ||= $entry
l!l&&r!l&&!r
0360
MC/DC0%0/2
$idx->{"by_title"}{$title} ||= $entry
$idx->{"by_title"}{$title} $entry
Truth table33%1/3
A ||= B
A= $idx->{"by_title"}{$title}B= $entry
ABresult
1X1
011
000
33 36 return $entry;
34 }
35  
36 sub resolve_target
37 6 ▶{
Subroutine resolve_target: called CC=6 SCAR=18.7
Pod documented
38 6 my ($idx, $target) = @_;
39 6 ▶ return undef unless ref($idx) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $idx eq "HASH" 0 6 6
40 6 ▶ return undef unless defined $target && $target ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and $target ne "" 0 6 6
Condition33%1/3
defined $target and $target ne ""
!ll&&!rl&&r
006
MC/DC0%0/2
defined $target and $target ne ""
defined $target $target ne ""
Truth table33%1/3
A and B
A= defined $targetB= $target ne ""
ABresult
0X0
100
111
41  
42 6 ▶ return $idx->{by_id}{$target} if exists $idx->{by_id}{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $idx->{"by_id"}{$target} 1 5 6
43 5 ▶ return $idx->{by_title}{$target} if exists $idx->{by_title}{$target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $idx->{"by_title"}{$target} 1 4 5
44 4 return undef;
45 }
46  
47 sub rewrite_links
48 98 ▶{
Subroutine rewrite_links: called CC=3 SCAR=11.2
Pod documented
49 98 my ($idx, $markdown) = @_;
50 98 ▶ return '' unless defined $markdown;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $markdown 0 98 98
51 98 ▶ return $markdown unless ref($idx) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $idx eq "HASH" 0 98 98
52  
53 98 $markdown =~ s{\[([^\]]+)\]\(#!([^\)]+)\)}{_rewrite_link($idx, $1, $2)}ge;
54 98 return $markdown;
55 }
56  
57 sub _rewrite_link
58 6 ▶{
Subroutine _rewrite_link: called CC=2 SCAR=6.9
59 6 my ($idx, $label, $target) = @_;
60 6 my $entry = resolve_target($idx, $target);
61 6 ▶ if ($entry)
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 2 4 6
62 {
63 2 return '[' . $label . '](#' . $entry->{auto_id} . ')';
64 }
65 4 return '[' . $label . '](#' . $target . ')';
66 }
67  
68 sub _auto_id
69 36 ▶{
Subroutine _auto_id: called CC=7 SCAR=20.8
70 36 my ($attrs, $title) = @_;
71 36 ▶ $title = '' unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 36 36
72 36 ▶ my $idprefix = ref($attrs) eq 'HASH' ? ($attrs->{idprefix} // '_') : '_';
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs eq 'HASH' ? : 36 0 36
Condition50%1/2
$attrs->{"idprefix"} // "_"
l!l
360
MC/DC0%0/1
$attrs->{"idprefix"} // "_"
$attrs->{"idprefix"}
Truth table50%1/2
A
A= $attrs->{"idprefix"}
Aresult
11
00
73 36 ▶ my $idseparator = ref($attrs) eq 'HASH' ? ($attrs->{idseparator} // '_') : '_';
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs eq 'HASH' ? : 36 0 36
Condition50%1/2
$attrs->{"idseparator"} // "_"
l!l
360
MC/DC0%0/1
$attrs->{"idseparator"} // "_"
$attrs->{"idseparator"}
Truth table50%1/2
A
A= $attrs->{"idseparator"}
Aresult
11
00
74  
75 36 my $slug = lc $title;
76 36 $slug =~ s/[^a-z0-9]+/ /g;
77 36 $slug =~ s/^\s+|\s+$//g;
78 36 $slug =~ s/\s+/$idseparator/g;
79 36 ▶ $slug = 'section' if $slug eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $slug eq "" 0 36 36
80 36 return $idprefix . $slug;
81 }
82  
83 18 1;
84  
85 __END__
« lib/Text/AsciidocDown/Include.pm ↑ Summary lib/Text/AsciidocDown/Subs.pm »