lib/Pod/Literate.pm

filter: Statementstmt 94.5% 155 / 164 Branchbran 83.3% 50 / 60 Conditioncond 75.0% 12 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 21 / 21 Podpod 6.6% 1 / 15 totaltotal 85.3% 244 / 286 CC 45 SCAR 20.2
CC
45
Cov
90%
prepareSections
14.4
_handle_element_end
11.7
tangle
8.0
CRAP 7.5
↑ 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.4';
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  
7 1 my $cache='--PODLITERATECACHE--';
8 1 my $tagL='#<';
9 1 my $tagR='>#';
10 1 my $reSection=qr/$tagL\h*(.+?)\h*$tagR/;
11  
12 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
13 4 my ($class)=@_;
14 4 my $self=$class->SUPER::new();
15 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 4 4
16 4 $$self{$cache}={
17 ofn =>[], # the output filenames for the tangled application
18 section =>{}, # the collection of named sections
19 mode =>'', # scanning mode, to distinguish begin/for tags
20 target =>undef, # defined and destroyed while scanning
21 collector=>undef, # defined and destroyed while scanning
22 #
23 dep =>{}, # used during tangling to compute dependencies
24 };
25 4 return $self;
26 }
27  
28 11 ▶sub targetSet {
Subroutine targetSet: called CC=1 SCAR=0.0
Pod undocumented
29 11 my ($self,$name)=@_;
30 11 $$self{$cache}{target}=$name;
31 11 return $self;
32 }
33  
34 27 ▶sub target {
Subroutine target: called CC=1 SCAR=0.0
Pod undocumented
35 27 my ($self)=@_;
36 27 return $$self{$cache}{target};
37 }
38  
39 11 ▶sub sectionAppend {
Subroutine sectionAppend: called CC=1 SCAR=0.0
Pod undocumented
40 11 my ($self,$section,$text)=@_;
41 11 $$self{$cache}{section}{$section}.=$text;
42 }
43  
44 3 ▶sub section {
Subroutine section: called CC=1 SCAR=0.0
Pod undocumented
45 3 my ($self,$section)=@_;
46 3 return $$self{$cache}{section}{$section};
47 }
48  
49 10 ▶sub sectionRegexp {
Subroutine sectionRegexp: called CC=1 SCAR=0.0
Pod undocumented
50 10 my ($name)=@_;
51 10 return qr/$tagL\h*\Q$name\E\h*$tagR/;
52 }
53  
54 24 ▶sub initCollection {
Subroutine initCollection: called CC=2 SCAR=7.1
Pod undocumented
55 24 my ($self,$text)=@_;
56 24 ▶ $$self{$cache}{collector}=$text//'';
Condition50%1/2
$text // ""
l!l
024
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
57 24 return $self;
58 }
59  
60 10 ▶sub collected {
Subroutine collected: called CC=1 SCAR=0.0
Pod undocumented
61 10 my ($self)=@_;
62 10 return unindent($$self{$cache}{collector});
63 }
64  
65 18 ▶sub clearCollection {
Subroutine clearCollection: called CC=1 SCAR=0.0
Pod undocumented
66 18 my ($self)=@_;
67 18 delete(@{$$self{$cache}}{qw/target collector/});
68 18 return $self;
69 }
70  
71 13 ▶sub unindent {
Subroutine unindent: called CC=2 SCAR=6.9
Pod undocumented
72 13 my ($s)=@_;
73 13 ▶ if($s=~/^(\h+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^(\h+)/) 12 1 13
74 12 my $indent=$1;
75 12 $s=~s/^\Q$indent\E//mg;
76 }
77 13 return $s;
78 }
79  
80 8 ▶sub indent {
Subroutine indent: called CC=2 SCAR=6.9
Pod undocumented
81 8 my ($code,$indent)=@_;
82 8 ▶ if(!$indent) { return $code }
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent) 6 2 8
83 2 $code=~s/^/$indent/emg;
84 2 return $code;
85 }
86  
87 53 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=8 SCAR=20.8
88 53 my ($self,$element,$attribute)=@_;
89 53 ▶ my $target=$$attribute{target}//'';
Condition100%2/2
$attribute->{"target"} // ""
l!l
2033
MC/DC100%1/1
$attribute->{"target"} // ""
$attribute->{"target"}
Truth table100%2/2
A
A= $attribute->{"target"}
Aresult
11
00
90 53 ▶ if($target eq ':literate') { die 'Non-data paragraphs are not currently supported via ":literate"' }
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq ":literate") 2 51 53
91 51 ▶ if($target!~/^literate(?::command)?$/) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($target =~ /^literate(?::command)?$/) 33 18 51
92 18 ▶ if(my $name=$self->target()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->target) 6 12 18
93 6 $self->sectionAppend($name, $self->collected());
94 6 $self->clearCollection()->initCollection();
95 6 return;
96 }
97 12 ▶ if($element eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq "for") 12 0 12
98 12 ▶ if($target eq 'literate:command') { $$self{$cache}{mode}='command' }
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq 'literate:command') { } 2 10 12
99 10 else { $$self{$cache}{mode}=$$attribute{'~really'} }
100 12 ▶ if($$attribute{title}) { my $s=$$attribute{title}; $s=~s/^\+//; $self->targetSet($s) }
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->{"title"}) 4 8 12
101 12 $self->initCollection();
102 }
103 }
104  
105 50 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=11 SCAR=24.6
106 50 my ($self,$element,$attribute)=@_;
107 50 ▶ if($element eq 'for') {
Branch: 2 branches
BranchTrueFalseTotal
if ($element eq 'for') { } 18 32 50
elsif ($element eq 'Document') { } 1 31 32
108 18 my ($mode,$collected)=@{$$self{$cache}}{qw/mode collector/};
109 18 ▶ if(($mode eq 'command')&&($collected=~/^target=(.+)$/)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($mode eq 'command' and $collected =~ /^target=(.+)$/) { } 2 16 18
elsif ($mode eq '=for' and $collected =~ /^target:(.+)$/) { } 0 16 16
elsif ($mode eq '=for' and $collected eq ".\n") { } 6 10 16
elsif ($mode eq '=for') { } 6 4 10
elsif ($self->target) { } 4 0 4
Condition66%2/3
$mode eq "command" and $collected =~ /^target=(.+)$/
!ll&&!rl&&r
1602
MC/DC50%1/2
$mode eq "command" and $collected =~ /^target=(.+)$/
$mode eq "command" $collected =~ /^target=(.+)$/
Truth table66%2/3
A and B
A= $mode eq "command"B= $collected =~ /^target=(.+)$/
ABresult
0X0
100
111
110 2 push @{$$self{$cache}{ofn}},$1;
111 2 $self->clearCollection();
112 }
113 ▶ elsif(($mode eq '=for')&&($collected=~/^target:(.+)$/)) {
Condition66%2/3
$mode eq "=for" and $collected =~ /^target:(.+)$/
!ll&&!rl&&r
4120
MC/DC0%0/2
$mode eq "=for" and $collected =~ /^target:(.+)$/
$mode eq "=for" $collected =~ /^target:(.+)$/
Truth table66%2/3
A and B
A= $mode eq "=for"B= $collected =~ /^target:(.+)$/
ABresult
0X0
100
111
114 0 print STDERR "=for literate target: is deprecated; use =for literate:command\n";
115 0 push @{$$self{$cache}{ofn}},$1;
116 0 $self->clearCollection();
117 }
118 ▶ elsif(($mode eq '=for')&&($collected eq ".\n")) {
Condition100%3/3
$mode eq "=for" and $collected eq ".\n"
!ll&&!rl&&r
466
MC/DC100%2/2
$mode eq "=for" and $collected eq ".\n"
$mode eq "=for" $collected eq ".\n"
Truth table100%3/3
A and B
A= $mode eq "=for"B= $collected eq ".\n"
ABresult
0X0
100
111
119 6 $self->clearCollection();
120 }
121 elsif($mode eq '=for') {
122 6 my $s=$collected; $s=~s/^\+//; $s=~s/\n$//s;
123 6 $self->targetSet($s);
124 6 $self->initCollection();
125 }
126 elsif($self->target()) {
127 4 $self->sectionAppend($self->target(), $self->collected());
128 4 $self->clearCollection();
129 }
130 }
131 elsif($element eq 'Document') {
132 1 $self->tangle();
133 }
134 }
135  
136 31 ▶sub _handle_text {
Subroutine _handle_text: called CC=2 SCAR=6.9
137 31 my ($self,$text)=@_;
138 31 ▶ if(defined($$self{$cache}{collector})) { $$self{$cache}{collector}.=$text."\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$cache}{"collector"}) 24 7 31
139 }
140  
141 1 ▶sub tangle {
Subroutine tangle: called CC=8 SCAR=20.8
Pod undocumented
142 1 my ($self)=@_;
143 1 my (%tobuild,@todo); # prebuild to keep the loops separate
144 1 $self->prepareSections();
145 1 ▶ while(my ($ka,$refb)=each %{$$self{$cache}{dep}}) { $tobuild{$ka}={map {$_=>1} keys(%$refb)} }
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{$cache}{"dep"};}) 8 1 9
146 1 ▶ while(my @names=sort grep {!%{$tobuild{$_}}} keys(%tobuild)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@names) = sort(grep({not %{$tobuild{$_};};} keys %tobuild))) 3 1 4
147 3 push @todo,@names;
148 3 delete(@tobuild{@names});
149 3 foreach my $name (values %tobuild) { delete(@$name{@names}) }
150 }
151 1 foreach my $name (@todo) { $self->tangleSection($name) }
152 1 foreach my $ofn (@{$$self{$cache}{ofn}}) {
153 2 ▶ if(my $oft=$self->section($ofn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $oft = $self->section($ofn)) 2 0 2
154 2 ▶ open(my $fh,'>',$ofn) or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $ofn or die "open: $!" 2 0 2
155 2 print $fh $oft;
156 2 close($fh);
157 } }
158 1 return $self;
159 }
160  
161 8 ▶sub tangleSection {
Subroutine tangleSection: called CC=3 SCAR=11.0
Pod undocumented
162 8 my ($self,$name)=@_;
163 8 my $section=$$self{$cache}{section}{$name};
164 8 foreach my $import (keys %{$$self{$cache}{dep}{$name}}) {
165 8 my $re=sectionRegexp($import);
166 8 my $code=$$self{$cache}{section}{$import}; $code=~s/\n$//s;
167 8 ▶ while($section=~/^(\h*)$re\h*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /^(\h*)$re\h*$/m) 6 8 14
168 6 my $icode=indent($code,$1);
169 6 $section=~s/^\h*$re\h*$/$icode/em;
170 }
171 8 $section=~s/$re/$code/ge;
172 }
173 8 $$self{$cache}{section}{$name}=$section;
174 }
175  
176 9 ▶sub codeSectionNames {
Subroutine codeSectionNames: called CC=2 SCAR=6.9
Pod undocumented
177 9 my ($code)=@_;
178 9 my %names;
179 9 ▶ while($code=~m/$reSection/g) { $names{$1}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /$reSection/g) 13 9 22
180 9 return keys(%names);
181 }
182  
183 1 ▶sub prepareSections {
Subroutine prepareSections: called CC=13 SCAR=26.7
Pod undocumented
184 1 my ($self)=@_;
185 1 my ($changed,$sections,%dep,%missing,%cyclic)=(1);
186 1 $sections=$$self{$cache}{section};
187 1 ▶ while(my ($k,$code)=each %$sections) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sections) 8 1 9
188 8 foreach my $name (codeSectionNames($code)) {
189 0 ▶ if(!defined($$sections{$name})) { $missing{$name}=1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sections->{$name}) 0 7 7
190 7 $dep{$k}{$name}=1;
191 }
192 }
193 0 ▶ if(%missing) { die 'Name referenced but not defined: '.join(q{; },sort keys %missing) }
Branch: 1 branch
BranchTrueFalseTotal
if (%missing) 0 1 1
194 1 ▶ while($changed) { $changed=0;
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 2 1 3
195 2 foreach my $ka (keys %dep) {
196 11 foreach my $kb (keys %{$dep{$ka}}) {
197 15 foreach my $kc (keys %{$dep{$kb}}) {
198 2 my $d=$dep{$ka}{$kb}+$dep{$kb}{$kc};
199 2 ▶ if(!defined($dep{$ka}{$kc})||($dep{$ka}{$kc}<$d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d) 1 1 2
Condition66%2/3
not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $dep{$ka}{$kc} or $dep{$ka}{$kc} < $d
not defined $dep{$ka}{$kc} $dep{$ka}{$kc} < $d
Truth table66%2/3
not(A) or B
A= not defined $dep{$ka}{$kc}B= $dep{$ka}{$kc} < $d
ABresult
1X1
011
000
200 1 $dep{$ka}{$kc}=$d; $changed=1;
201 0 ▶ if($ka eq $kc) { $cyclic{$ka}=1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ka eq $kc) 0 1 1
202 } }
203 } }
204 }
205 0 ▶ if(%cyclic) { die 'Sections contain cyclic dependencies: '.join(q{, },sort keys %cyclic) }
Branch: 1 branch
BranchTrueFalseTotal
if (%cyclic) 0 1 1
206 1 $$self{$cache}{dep}=\%dep;
207 1 return $self;
208 }
209  
210 1 1;
211  
212 __END__
↑ Summary script/podtangle.pl »