lib/Pod/Tree.pm

filter: Statementstmt 75.8% 116 / 153 Branchbran 72.5% 29 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.9% 20 / 26 Podpod 100% 13 / 13 totaltotal 75.5% 179 / 237 CC 27 SCAR 19.3
CC
27
Cov
74%
_add_paragraph
30.0
_walk
20.0
_make_nodes
6.0
CRAP 6.9
↑ Summary lib/Pod/Tree/PerlUtil.pm »
1 # Copyright (c) 1999-2009 by Steven McDougall. This module is free
2 # software; you can redistribute it and/or modify it under the same
3 # terms as Perl itself.
4  
5 package Pod::Tree;
6 34 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Pod::Tree::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Pod::Tree::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 our $VERSION = '1.31';
15  
16 194 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 194 my $class = shift;
18 194 my $tree = {
19 loaded => 0,
20 paragraphs => []
21 };
22 194 bless $tree, $class;
23 }
24  
25 185 ▶sub load_file {
Subroutine load_file: called CC=2 SCAR=7.0
Pod documented
26 185 my ( $tree, $file, %options ) = @_;
27  
28 185 Pod::Tree::Node->set_filename($file);
29  
30 185 my $fh = IO::File->new;
31 185 ▶ $fh->open($file) or return '';
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file) or return "" 185 0 185
32 185 $tree->load_fh( $fh, %options );
33  
34 185 Pod::Tree::Node->set_filename("");
35 185 1;
36 }
37  
38 187 ▶sub load_fh {
Subroutine load_fh: called CC=4 SCAR=14.0
Pod documented
39 187 my ( $tree, $fh, %options ) = @_;
40  
41 187 $tree->{in_pod} = 0;
42 187 $tree->_load_options(%options);
43 187 my $limit = $tree->{limit};
44  
45 187 my $stream = Pod::Tree::Stream->new($fh);
46 187 my $paragraph;
47 my @paragraphs;
48 187 ▶ while ( $paragraph = $stream->get_paragraph ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph = $stream->get_paragraph) 3035 187 3222
49 3035 push @paragraphs, $paragraph;
50 3035 ▶ $limit and $limit == @paragraphs and last;
Branch: 1 branch
BranchTrueFalseTotal
$limit and $limit == @paragraphs and last 0 3035 3035
Condition33%1/3
$limit and $limit == @paragraphs
!ll&&!rl&&r
303500
MC/DC0%0/2
$limit and $limit == @paragraphs
$limit $limit == @paragraphs
Truth table33%1/3
A and B
A= $limitB= $limit == @paragraphs
ABresult
0X0
100
111
51 }
52  
53 187 $tree->{paragraphs} = \@paragraphs;
54 187 $tree->_parse;
55 }
56  
57 5 ▶sub load_string {
Subroutine load_string: called CC=2 SCAR=6.9
Pod documented
58 5 my ( $tree, $string, %options ) = @_;
59  
60 5 my @chunks = split /( \n\s*\n | \r\s*\r | \r\n\s*\r\n )/x, $string;
61  
62 5 my (@paragraphs);
63 5 ▶ while (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 167 5 172
64 167 push @paragraphs, join '', splice @chunks, 0, 2;
65 }
66  
67 5 $tree->load_paragraphs( \@paragraphs, %options );
68 }
69  
70 7 ▶sub load_paragraphs {
Subroutine load_paragraphs: called CC=2 SCAR=7.0
Pod documented
71 7 my ( $tree, $paragraphs, %options ) = @_;
72  
73 7 $tree->{in_pod} = 1;
74 7 $tree->_load_options(%options);
75  
76 7 my $limit = $tree->{limit};
77 7 my @paragraphs = @$paragraphs;
78 7 ▶ $limit and splice @paragraphs, $limit;
Branch: 1 branch
BranchTrueFalseTotal
$limit and splice @paragraphs, $limit 0 7 7
79  
80 7 $tree->{paragraphs} = \@paragraphs;
81 7 $tree->_parse;
82 }
83  
84 115 ▶sub loaded { shift->{'loaded'} }
Subroutine loaded: called CC=1 SCAR=0.0
Pod documented
85  
86 194 ▶sub _load_options {
Subroutine _load_options: called CC=2 SCAR=6.9
87 194 my ( $tree, %options ) = @_;
88  
89 194 my ( $key, $value );
90 194 ▶ while ( ( $key, $value ) = each %options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 4 194 198
91 4 $tree->{$key} = $value;
92 }
93 }
94  
95 194 ▶sub _parse {
Subroutine _parse: called CC=1 SCAR=0.0
96 194 my $tree = shift;
97  
98 194 $tree->_make_nodes;
99 194 $tree->_make_for;
100 194 $tree->_make_sequences;
101  
102 194 my $root = $tree->{root};
103  
104 194 $root->parse_links;
105 194 $root->unescape;
106 194 $root->consolidate;
107 194 $root->make_lists;
108  
109 194 $tree->{'loaded'} = 1;
110 }
111  
112 0 ▶sub _add_paragraph {
Subroutine _add_paragraph: not called CC=5 SCAR=34.0
113 0 my ( $tree, $paragraph ) = @_;
114  
115 0 for ($paragraph) {
116 0 ▶ /^=cut/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^=cut/ and do { $tree->{"in_pod"} = 0; last } 0 0 0
117 0 $tree->{in_pod} = 0;
118 0 last;
119 };
120 0 ▶ $tree->{in_pod} and do {
Branch: 1 branch
BranchTrueFalseTotal
$tree->{"in_pod"} and do { CORE::push @{$$tree{"paragraphs"};}, $paragraph; last } 0 0 0
121 0 push @{ $tree->{paragraphs} }, $paragraph;
122 0 last;
123 };
124 0 ▶ /^=\w/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^=\w/ and do { $tree->{"in_pod"} = 1; CORE::push @{$$tree{"paragraphs"};}, $paragraph; last } 0 0 0
125 0 $tree->{in_pod} = 1;
126 0 push @{ $tree->{paragraphs} }, $paragraph;
127 0 last;
128 };
129 }
130 }
131  
132 34 my %Command = map { $_ => 1 } qw(=pod =cut
133 =head1 =head2 =head3 =head4
134 =over =item =back
135 =for =begin =end);
136  
137 194 ▶sub _make_nodes {
Subroutine _make_nodes: called CC=6 SCAR=17.9
138 194 my $tree = shift;
139 194 my $paragraphs = $tree->{paragraphs};
140 194 my $in_pod = $tree->{in_pod};
141 194 my @children;
142  
143 194 for my $paragraph (@$paragraphs) {
144 3345 my ($word) = split( /\s/, $paragraph );
145 3345 my $node;
146  
147 3345 ▶ if ($in_pod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_pod) { } 3082 263 3345
148 3082 ▶ if ( $paragraph =~ /^\s/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($paragraph =~ /^\s/) { } 91 2991 3082
elsif ($Command{$word}) { } 1535 1456 2991
149 91 $node = Pod::Tree::Node->verbatim($paragraph);
150 }
151 elsif ( $Command{$word} ) {
152 1535 $node = Pod::Tree::Node->command($paragraph);
153 1535 $in_pod = $word ne '=cut';
154 }
155 else {
156 1456 $node = Pod::Tree::Node->ordinary($paragraph);
157 }
158 }
159 else {
160 263 ▶ if ( $Command{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Command{$word}) { } 189 74 263
161 189 $node = Pod::Tree::Node->command($paragraph);
162 189 $in_pod = $word ne '=cut';
163 }
164 else {
165 74 $node = Pod::Tree::Node->code($paragraph);
166 }
167 }
168  
169 3345 push @children, $node;
170 }
171  
172 194 $tree->{root} = Pod::Tree::Node->root( \@children );
173 }
174  
175 194 ▶sub _make_for {
Subroutine _make_for: called CC=4 SCAR=13.9
176 194 my $tree = shift;
177 194 my $root = $tree->{root};
178 194 my $old = $root->get_children;
179  
180 194 my @new;
181 194 ▶ while (@$old) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$old) 3301 194 3495
182 3301 my $node = shift @$old;
183 3301 ▶ $node->is_c_for and $node->force_for;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_c_for and $node->force_for 31 3270 3301
184 3301 ▶ $node->is_c_begin and $node->parse_begin($old);
Branch: 1 branch
BranchTrueFalseTotal
$node->is_c_begin and $node->parse_begin($old) 11 3290 3301
185 3301 push @new, $node;
186 }
187  
188 194 $root->set_children( \@new );
189 }
190  
191 194 ▶sub _make_sequences {
Subroutine _make_sequences: called CC=5 SCAR=16.1
192 194 my $tree = shift;
193 194 my $root = $tree->{root};
194 194 my $nodes = $root->get_children;
195  
196 194 for my $node (@$nodes) {
197 3301 ▶ $node->is_code and next;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_code and next 74 3227 3301
198 3227 ▶ $node->is_verbatim and next;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_verbatim and next 80 3147 3227
199 3147 ▶ $node->is_for and next;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_for and next 42 3105 3147
200 3105 $node->make_sequences;
201 }
202 }
203  
204 15 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
205 15 my $tree = shift;
206 15 $tree->{root}->dump;
207 }
208  
209 303 ▶sub get_root { shift->{root} }
Subroutine get_root: called CC=1 SCAR=0.0
Pod documented
210  
211 0 ▶sub set_root {
Subroutine set_root: not called CC=1 SCAR=6.9
Pod documented
212 0 my ( $tree, $root ) = @_;
213 0 $tree->{root} = $root;
214 }
215  
216 0 ▶sub push {
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
217 0 my ( $tree, @nodes ) = @_;
218 0 my $root = $tree->{root};
219 0 my $children = $root->get_children;
220 0 push @$children, @nodes;
221 }
222  
223 0 ▶sub pop {
Subroutine pop: not called CC=1 SCAR=6.9
Pod documented
224 0 my $tree = shift;
225 0 my $root = $tree->get_root;
226 0 my $children = $root->get_children;
227 0 pop @$children;
228 }
229  
230 0 ▶sub walk {
Subroutine walk: not called CC=1 SCAR=6.9
Pod documented
231 0 my ( $tree, $sub ) = @_;
232  
233 0 my $root = $tree->get_root;
234 0 _walk( $root, $sub );
235 }
236  
237 0 ▶sub _walk {
Subroutine _walk: not called CC=4 SCAR=30.0
238 0 my ( $tree, $sub ) = @_;
239  
240 0 my $descend = &$sub($tree); # :TRICKY: sub can modify node
241 0 ▶ $descend or return;
Branch: 1 branch
BranchTrueFalseTotal
$descend or return 0 0 0
242  
243 0 my $node = $tree;
244  
245 0 my $children = $node->get_children;
246 0 for my $child (@$children) {
247 0 _walk( $child, $sub );
248 }
249  
250 0 my $siblings = $node->get_siblings;
251 0 for my $sibling (@$siblings) {
252 0 _walk( $sibling, $sub );
253 }
254 }
255  
256 127 ▶sub has_pod {
Subroutine has_pod: called CC=1 SCAR=0.0
Pod documented
257 127 my $tree = shift;
258 127 my $root = $tree->get_root;
259 127 my $children = $root->get_children;
260  
261 127 scalar grep { $_->get_type ne 'code' } @$children;
262 }
263  
264 34 1
265  
266 __END__
↑ Summary lib/Pod/Tree/PerlUtil.pm »