lib/Pod/Literate.pm

filter: Statementstmt 92.6% 139 / 150 Branchbran 76.7% 43 / 56 Conditioncond 69.2% 9 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 21 / 21 Podpod 6.6% 1 / 15 totaltotal 82.8% 218 / 263 CC 41 SCAR 22.4
CC
41
Cov
87%
prepareSections
26.6
_handle_element_end
9.0
tangle
8.1
CRAP 9.4
↑ Summary script/podtangle.pl »
1 package Pod::Literate;
2 1 ▶use parent 'Pod::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $VERSION='0.0.2';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 my $cache='--PODLITERATECACHE--';
7 1 my $tagL='#<';
8 1 my $tagR='>#';
9 1 my $reSection=qr/$tagL\s*(.+?)\s*$tagR/;
10 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
11 3 my ($class)=@_;
12 3 my $self=$class->SUPER::new();
13 0 ▶ if(exists($$self{$cache})) { die 'Basic Pod::Simple object unexpectedly contains the Pod::Literate cache key' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$cache}) 0 3 3
14 3 $$self{$cache}={
15 ofn =>{}, # the output filename for the tangled application
16 section =>{}, # the collection of named sections
17 mode =>'', # scanning mode, to distinguish begin/for tags
18 target =>undef, # defined and destroyed while scanning
19 collector=>undef, # defined and destroyed while scanning
20 #
21 dep =>{}, # used during tangling to compute dependencies
22 };
23 3 return $self;
24 }
25 4 ▶sub targetSet {
Subroutine targetSet: called CC=1 SCAR=0.0
Pod undocumented
26 4 my ($self,$name)=@_;
27 4 $$self{$cache}{target}=$name;
28 4 return $self;
29 }
30 10 ▶sub target {
Subroutine target: called CC=1 SCAR=0.0
Pod undocumented
31 10 my ($self)=@_;
32 10 return $$self{$cache}{target};
33 }
34 4 ▶sub sectionAppend {
Subroutine sectionAppend: called CC=1 SCAR=0.0
Pod undocumented
35 4 my ($self,$section,$text)=@_;
36 4 $$self{$cache}{section}{$section}.=$text;
37 }
38 2 ▶sub section {
Subroutine section: called CC=1 SCAR=0.0
Pod undocumented
39 2 my ($self,$section)=@_;
40 2 return $$self{$cache}{section}{$section};
41 }
42 4 ▶sub sectionRegexp {
Subroutine sectionRegexp: called CC=1 SCAR=0.0
Pod undocumented
43 4 my ($name)=@_;
44 4 return qr/$tagL\s*\Q$name\E\s*$tagR/;
45 }
46 6 ▶sub initCollection {
Subroutine initCollection: called CC=2 SCAR=7.1
Pod undocumented
47 6 my ($self,$text)=@_;
48 6 ▶ $$self{$cache}{collector}=$text//'';
Condition50%1/2
$text // ""
l!l
06
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
49 6 return $self;
50 }
51 3 ▶sub collected {
Subroutine collected: called CC=1 SCAR=0.0
Pod undocumented
52 3 my ($self)=@_;
53 3 return unindent($$self{$cache}{collector});
54 }
55 5 ▶sub clearCollection {
Subroutine clearCollection: called CC=1 SCAR=0.0
Pod undocumented
56 5 my ($self)=@_;
57 5 delete(@{$$self{$cache}}{qw/target collector/});
58 5 return $self;
59 }
60 6 ▶sub unindent {
Subroutine unindent: called CC=2 SCAR=6.9
Pod undocumented
61 6 my ($s)=@_;
62 6 ▶ if($s=~/^(\s+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^(\s+)/) 5 1 6
63 5 my $indent=$1;
64 5 $s=~s/^\Q$indent\E//mg;
65 }
66 6 return $s;
67 }
68 4 ▶sub indent {
Subroutine indent: called CC=2 SCAR=6.9
Pod undocumented
69 4 my ($code,$indent)=@_;
70 4 ▶ if(!$indent) { return $code }
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent) 2 2 4
71 2 $code=~s/^/$indent/emg;
72 2 return $code;
73 }
74 13 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=6 SCAR=17.9
75 13 my ($self,$element,$attribute)=@_;
76 13 ▶ my $target=$$attribute{target}//'';
Condition100%2/2
$attribute->{"target"} // ""
l!l
58
MC/DC100%1/1
$attribute->{"target"} // ""
$attribute->{"target"}
Truth table100%2/2
A
A= $attribute->{"target"}
Aresult
11
00
77 13 ▶ if($target ne 'literate') { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($target ne "literate") 8 5 13
78 5 ▶ if(my $name=$self->target()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->target) 1 4 5
79 1 $self->sectionAppend($name, $self->collected());
80 1 $self->clearCollection()->initCollection();
81 1 return;
82 }
83 4 ▶ if($element eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq "for") 4 0 4
84 4 $$self{$cache}{mode}=$$attribute{'~really'};
85 4 ▶ if($$attribute{title}) { $self->targetSet($$attribute{title}=~s/^\+//r) }
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->{"title"}) 2 2 4
86 4 $self->initCollection();
87 }
88 }
89  
90 13 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=9 SCAR=22.0
91 13 my ($self,$element,$attribute)=@_;
92 13 ▶ if($element eq 'for') {
Branch: 2 branches
BranchTrueFalseTotal
if ($element eq 'for') { } 5 8 13
elsif ($element eq 'Document') { } 1 7 8
93 5 my ($mode,$collected)=@{$$self{$cache}}{qw/mode collector/};
94 5 $collected=~s/\n$//s;
95 5 ▶ if(($mode eq '=for')&&($collected=~/^target:(.+)$/)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($mode eq '=for' and $collected =~ /^target:(.+)$/) { } 1 4 5
elsif ($mode eq '=for' and $collected eq '.') { } 1 3 4
elsif ($mode eq '=for') { } 1 2 3
elsif ($self->target) { } 2 0 2
Condition100%3/3
$mode eq "=for" and $collected =~ /^target:(.+)$/
!ll&&!rl&&r
221
MC/DC100%2/2
$mode eq "=for" and $collected =~ /^target:(.+)$/
$mode eq "=for" $collected =~ /^target:(.+)$/
Truth table100%3/3
A and B
A= $mode eq "=for"B= $collected =~ /^target:(.+)$/
ABresult
0X0
100
111
96 1 $$self{$cache}{ofn}=$1;
97 1 $self->clearCollection();
98 }
99 ▶ elsif(($mode eq '=for')&&($collected eq '.')) {
Condition100%3/3
$mode eq "=for" and $collected eq "."
!ll&&!rl&&r
211
MC/DC100%2/2
$mode eq "=for" and $collected eq "."
$mode eq "=for" $collected eq "."
Truth table100%3/3
A and B
A= $mode eq "=for"B= $collected eq "."
ABresult
0X0
100
111
100 1 $self->clearCollection();
101 }
102 elsif($mode eq '=for') {
103 1 $self->targetSet($collected=~s/^\+//r);
104 1 $self->initCollection();
105 }
106 elsif($self->target()) {
107 2 $self->sectionAppend($self->target(), $self->collected());
108 2 $self->clearCollection();
109 }
110 }
111 elsif($element eq 'Document') {
112 1 $self->tangle();
113 }
114 }
115  
116 7 ▶sub _handle_text {
Subroutine _handle_text: called CC=2 SCAR=6.9
117 7 my ($self,$text)=@_;
118 7 ▶ if(defined($$self{$cache}{collector})) { $$self{$cache}{collector}.=$text."\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$cache}{"collector"}) 6 1 7
119 }
120 1 ▶sub tangle {
Subroutine tangle: called CC=8 SCAR=20.9
Pod undocumented
121 1 my ($self)=@_;
122 1 my (%tobuild,@todo); # prebuild to keep the loops separate
123 1 $self->prepareSections();
124 1 ▶ while(my ($ka,$refb)=each %{$$self{$cache}{dep}}) { $tobuild{$ka}={map {$_=>1} keys(%$refb)} }
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{$cache}{"dep"};}) 3 1 4
125 1 ▶ while(my @names=sort grep {!%{$tobuild{$_}}} keys(%tobuild)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@names) = sort(grep({not %{$tobuild{$_};};} keys %tobuild))) 2 1 3
126 2 push @todo,@names;
127 2 delete(@tobuild{@names});
128 2 foreach my $name (values %tobuild) { delete(@$name{@names}) }
129 }
130 1 foreach my $name (@todo) { $self->tangleSection($name) }
131 1 ▶ if(my $ofn=$$self{$cache}{ofn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ofn = $self->{$cache}{"ofn"}) 1 0 1
132 1 ▶ if(my $oft=$self->section($ofn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $oft = $self->section($ofn)) 1 0 1
133 1 ▶ open(my $fh,'>',$ofn) or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $ofn or die "open: $!" 1 0 1
134 1 print $fh $oft;
135 1 close($fh);
136 } }
137 1 return $self;
138 }
139 3 ▶sub tangleSection {
Subroutine tangleSection: called CC=3 SCAR=11.0
Pod undocumented
140 3 my ($self,$name)=@_;
141 3 my $section=$$self{$cache}{section}{$name};
142 3 foreach my $import (keys %{$$self{$cache}{dep}{$name}}) {
143 2 my $re=sectionRegexp($import);
144 2 my $code=$$self{$cache}{section}{$import}; $code=~s/\n+$//s;
145 2 ▶ while($section=~/^(\s*)$re\s*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /^(\s*)$re\s*$/m) 2 2 4
146 2 my $icode=indent($code,$1);
147 2 $section=~s/^\s*$re\s*$/$icode/em;
148 }
149 0 $section=~s/$re/$code/ge;
150 }
151 3 $$self{$cache}{section}{$name}=$section;
152 }
153 4 ▶sub codeSectionNames {
Subroutine codeSectionNames: called CC=2 SCAR=6.9
Pod undocumented
154 4 my ($code)=@_;
155 4 my %names;
156 4 ▶ while($code=~m/$reSection/g) { $names{$1}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /$reSection/g) 8 4 12
157 4 return keys(%names);
158 }
159 1 ▶sub prepareSections {
Subroutine prepareSections: called CC=13 SCAR=32.8
Pod undocumented
160 1 my ($self)=@_;
161 1 my ($changed,$sections,%dep,%missing,%cyclic)=(1);
162 1 $sections=$$self{$cache}{section};
163 1 ▶ while(my ($k,$code)=each %$sections) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sections) 3 1 4
164 3 foreach my $name (codeSectionNames($code)) {
165 0 ▶ if(!defined($$sections{$name})) { $missing{$name}=1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sections->{$name}) 0 2 2
166 2 $dep{$k}{$name}=1;
167 }
168 }
169 0 ▶ if(%missing) { die 'Name referenced but not defined: '.join(q{; },sort keys %missing) }
Branch: 1 branch
BranchTrueFalseTotal
if (%missing) 0 1 1
170 1 ▶ while($changed) { $changed=0;
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1 1 2
171 1 foreach my $ka (keys %dep) {
172 1 foreach my $kb (keys %{$dep{$ka}}) {
173 2 foreach my $kc (keys %{$dep{$kb}}) {
174 0 my $d=$dep{$ka}{$kb}+$dep{$kb}{$kc};
175 0 ▶ if(!defined($dep{$ka}{$kc})||($dep{$ka}{$kc}<$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d) 0 0 0
Condition0%0/3
not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d
not defined $dep{$ka}{$kc} $dep{$ka}{$kc} < $d
Truth table0%0/3
not(A) or B
A= not defined $dep{$ka}{$kc}B= $dep{$ka}{$kc} < $d
ABresult
1X1
011
000
176 0 $dep{$ka}{$kc}=$d; $changed=1;
177 0 ▶ if($ka eq $kc) { $cyclic{$ka}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ka eq $kc) 0 0 0
178 } }
179 } }
180 }
181 0 ▶ if(%cyclic) { die 'Sections contain cyclic dependencies: '.join(q{, },sort keys %cyclic) }
Branch: 1 branch
BranchTrueFalseTotal
if (%cyclic) 0 1 1
182 1 $$self{$cache}{dep}=\%dep;
183 1 return $self;
184 }
185 1 1;
186  
187 __END__
↑ Summary script/podtangle.pl »