lib/Pod/Literate.pm

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