blib/lib/Pod/Elemental/PerlMunger.pm

filter: Statementstmt 93.6% 88 / 94 Branchbran 80.5% 29 / 36 Conditioncond 80.0% 16 / 20 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 93.7% 15 / 16 Podpod 100% 3 / 3 totaltotal 87.7% 158 / 180 CC 28 SCAR 25.2
CC
28
Cov
89%
__ANON__
22.6
__ANON__
3.0
__ANON__
3.0
CRAP 12.5
↑ Summary
1 package Pod::Elemental::PerlMunger 0.200007;
2 # ABSTRACT: a thing that takes a string of Perl and rewrites its documentation
3  
4 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #pod =head1 OVERVIEW
7 #pod
8 #pod This role is to be included in classes that rewrite the documentation of a Perl
9 #pod document, stripping out all the Pod, munging it, and replacing it into the
10 #pod Perl.
11 #pod
12 #pod The only relevant method is C<munge_perl_string>, which must be implemented
13 #pod with a different interface than will be exposed.
14 #pod
15 #pod When calling the C<munge_perl_string> method, arguments should be passed like
16 #pod this:
17 #pod
18 #pod $object->munge_perl_string($perl_string, \%arg);
19 #pod
20 #pod C<$perl_string> should be a character string containing Perl source code.
21 #pod
22 #pod C<%arg> may contain any input for the underlying procedure. Defined keys for
23 #pod C<%arg> are:
24 #pod
25 #pod =for :list
26 #pod = filename
27 #pod the name of the file whose contents are being munged; optional, used for error
28 #pod messages
29 #pod = no_strip_bom
30 #pod If given, the BOM character (U+FEFF) won't be stripped from the input.
31 #pod Probably best to leave this one off.
32 #pod
33 #pod The method will return a character string containing the rewritten and combined
34 #pod document.
35 #pod
36 #pod Classes including this role must implement a C<munge_perl_string> that expects
37 #pod to be called like this:
38 #pod
39 #pod $object->munge_perl_string(\%doc, \%arg);
40 #pod
41 #pod C<%doc> will have two entries:
42 #pod
43 #pod ppi - a PPI::Document of the Perl document with all its Pod removed
44 #pod pod - a Pod::Elemental::Document with no transformations yet performed
45 #pod
46 #pod This C<munge_perl_string> method should return a hashref in the same format as
47 #pod C<%doc>.
48 #pod
49 #pod =cut
50  
51 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use List::Util 1.33 qw(any max);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use Params::Util qw(_INSTANCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 requires 'munge_perl_string';
59  
60 10 ▶around munge_perl_string => sub {
Subroutine __ANON__: called CC=22 SCAR=31.2
61 10 my ($orig, $self, $perl, $arg) = @_;
62  
63 10 ▶ $perl =~ s/^\x{FEFF}// unless $arg->{no_strip_bom};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"no_strip_bom"} 10 0 10
64  
65 10 my $ppi_document = PPI::Document->new(\$perl);
66 10 ▶ confess(PPI::Document->errstr) unless $ppi_document;
Branch: 1 branch
BranchTrueFalseTotal
unless $ppi_document 0 10 10
67  
68 10 my $last_code_elem;
69 210 ▶ my $code_elems = $ppi_document->find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
70 210 ▶ return if grep { $_[1]->isa("PPI::Token::$_") }
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_[1]->isa("PPI::Token::$_");} "Comment", "Pod", "Whitespace", "Separator", "Data", "End" 116 94 210
71 qw(Comment Pod Whitespace Separator Data End);
72 94 return 1;
73 10 });
74  
75 10 ▶ $code_elems ||= [];
Condition50%1/2
$code_elems ||= []
l!l
100
MC/DC0%0/1
$code_elems ||= []
$code_elems
Truth table50%1/2
A
A= $code_elems
Aresult
11
00
76 10 for my $elem (@$code_elems) {
77 # Really, we might get two elements on the same line, and one could be
78 # later in position because it could have a later column — but we don't
79 # care, because we're only thinking about Pod, which is linewise.
80 94 ▶ next if $last_code_elem
Branch: 1 branch
BranchTrueFalseTotal
if $last_code_elem and $elem->line_number <= $last_code_elem->line_number 75 19 94
Condition100%3/3
$last_code_elem and $elem->line_number <= $last_code_elem->line_number
!ll&&!rl&&r
10975
MC/DC100%2/2
$last_code_elem and $elem->line_number <= $last_code_elem->line_number
$last_code_elem $elem->line_number <= $last_code_elem->line_number
Truth table100%3/3
A and B
A= $last_code_elemB= $elem->line_number <= $last_code_elem->line_number
ABresult
0X0
100
111
81 and $elem->line_number <= $last_code_elem->line_number;
82  
83 19 $last_code_elem = $elem;
84 }
85  
86 10 my @pod_tokens;
87  
88 {
89 10 my @queue = $ppi_document->children;
90 10 ▶ while (my $element = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = shift @queue) 210 10 220
91 210 ▶ if ($element->isa('PPI::Token::Pod')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->isa("PPI::Token::Pod")) 13 197 210
92 13 ▶ my $after_last = $last_code_elem
Condition66%2/3
$last_code_elem && $last_code_elem->line_number > $element->line_number
!ll&&!rl&&r
067
MC/DC50%1/2
$last_code_elem && $last_code_elem->line_number > $element->line_number
$last_code_elem $last_code_elem->line_number > $element->line_number
Truth table66%2/3
A && B
A= $last_code_elemB= $last_code_elem->line_number > $element->line_number
ABresult
0X0
100
111
93 && $last_code_elem->line_number > $element->line_number;
94 13 my @replacements = $self->_replacements_for($element, $after_last);
95  
96 # save the text for use in building the Pod-only document
97 13 push @pod_tokens, "$element";
98  
99 13 my $last = $element;
100 13 ▶ while (my $next = shift @replacements) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @replacements) 4 13 17
101 4 my $ok = $last->insert_after($next);
102 4 ▶ confess("error inserting replacement!") unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
103 4 $last = $next;
104 }
105  
106 13 $element->delete;
107  
108 13 next;
109 }
110  
111 197 ▶ if ( _INSTANCE($element, 'PPI::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($element, "PPI::Node")) 19 178 197
112 # Depth-first keeps the queue size down
113 19 unshift @queue, $element->children;
114 }
115 }
116 }
117  
118 201 ▶ my $finder = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
119 201 my $node = $_[1];
120 593 ▶ return 0 unless any { $node->isa($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
121 201 ▶ qw( PPI::Token::Quote PPI::Token::QuoteLike PPI::Token::HereDoc );
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $node->isa($_); } , "PPI::Token::Quote", "PPI::Token::QuoteLike", "PPI::Token::HereDoc") 196 5 201
122 5 ▶ return 1 if $node->content =~ /^=[a-z]/m;
Branch: 1 branch
BranchTrueFalseTotal
if $node->content =~ /^=[a-z]/m 0 5 5
123 5 return 0;
124 10 };
125  
126 10 ▶ if ($ppi_document->find_first($finder)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppi_document->find_first($finder)) 0 10 10
127 $self->log(
128 sprintf "can't invoke %s on %s: there is POD inside string literals",
129 $self->plugin_name,
130 0 ▶ (defined $arg->{filename} ? $arg->{filename} : 'input')
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'filename'} ? : 0 0 0
131 );
132 }
133  
134 # TODO: I should add a $weaver->weave_* like the Linewise methods to take the
135 # input, get a Document, perform the stock transformations, and then weave.
136 # -- rjbs, 2009-10-24
137 10 my $pod_str = join "\n", @pod_tokens;
138 10 my $pod_utf8 = Encode::encode('utf-8', $pod_str, Encode::FB_CROAK);
139 10 my $pod_document = Pod::Elemental->read_string($pod_utf8);
140  
141 10 my $doc = $self->$orig(
142 {
143 ppi => $ppi_document,
144 pod => $pod_document,
145 },
146 $arg,
147 );
148  
149 10 my $new_pod = $doc->{pod}->as_pod_string;
150  
151 390 ▶ my $end_finder = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
152 390 ▶ return 1 if $_[1]->isa('PPI::Statement::End')
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]->isa("PPI::Statement::End") or $_[1]->isa("PPI::Statement::Data") 8 382 390
Condition100%3/3
$_[1]->isa("PPI::Statement::End") or $_[1]->isa("PPI::Statement::Data")
l!l&&r!l&&!r
62382
MC/DC100%2/2
$_[1]->isa("PPI::Statement::End") or $_[1]->isa("PPI::Statement::Data")
$_[1]->isa("PPI::Statement::End") $_[1]->isa("PPI::Statement::Data")
Truth table100%3/3
A or B
A= $_[1]->isa("PPI::Statement::End")B= $_[1]->isa("PPI::Statement::Data")
ABresult
1X1
011
000
153 || $_[1]->isa('PPI::Statement::Data');
154 382 return 0;
155 10 };
156  
157 10 my $end = do {
158 10 my $end_elem = $doc->{ppi}->find($end_finder);
159  
160 # If there's nothing after __END__, we can put the POD there:
161 10 ▶ if (not $end_elem or (@$end_elem == 1 and
Branch: 1 branch
BranchTrueFalseTotal
if (not $end_elem or @$end_elem == 1 and $end_elem->[0]->isa("PPI::Statement::End") and $end_elem->[0] =~ /^__END__\s*\z/) 9 1 10
Condition77%7/9
not $end_elem or @$end_elem == 1 and $end_elem->[0]->isa("PPI::Statement::End") and $end_elem->[0] =~ /^__END__\s*\z/
l!l&&r!l&&!r
631
@$end_elem == 1 and $end_elem->[0]->isa("PPI::Statement::End") and $end_elem->[0] =~ /^__END__\s*\z/
!ll&&!rl&&r
103
@$end_elem == 1 and $end_elem->[0]->isa("PPI::Statement::End")
!ll&&!rl&&r
013
MC/DC50%2/4
not $end_elem or @$end_elem == 1 and $end_elem->[0]->isa("PPI::Statement::End") and $end_elem->[0] =~ /^__END__\s*\z/
not $end_elem @$end_elem == 1 $end_elem->[0]->isa("PPI::Statement::End") $end_elem->[0] =~ /^__END__\s*\z/
Truth table60%3/5
not(A) or B and C and D
A= not $end_elemB= @$end_elem == 1C= $end_elem->[0]->isa("PPI::Statement::End")D= $end_elem->[0] =~ /^__END__\s*\z/
ABCDresult
1XXX1
01111
00XX0
010X0
01100
162 $end_elem->[0]->isa('PPI::Statement::End') and
163 $end_elem->[0] =~ /^__END__\s*\z/)) {
164 9 $end_elem = [];
165 }
166  
167 10 ▶ @$end_elem ? join q{}, @$end_elem : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$end_elem ? : 1 9 10
168 };
169  
170 10 $doc->{ppi}->prune($end_finder);
171  
172 10 my $new_perl = $doc->{ppi}->serialize;
173  
174 10 s/\n\s*\z// for $new_perl, $new_pod;
175  
176 10 ▶ return defined $end
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 1 9 10
177 ? "$new_perl\n\n$new_pod\n\n$end"
178 : "$new_perl\n\n__END__\n\n$new_pod\n";
179 1 };
180  
181 #pod =attr replacer
182 #pod
183 #pod The replacer is either a method name or code reference used to produces PPI
184 #pod elements used to replace removed Pod. By default, it is
185 #pod C<L</replace_with_nothing>>, which just removes Pod tokens entirely. This
186 #pod means that the line numbers of the code in the newly-produced document are
187 #pod changed, if the Pod had been interleaved with the code.
188 #pod
189 #pod See also C<L</replace_with_comment>> and C<L</replace_with_blank>>.
190 #pod
191 #pod If no further code follows the Pod being replaced, C<L</post_code_replacer>> is
192 #pod used instead.
193 #pod
194 #pod =attr post_code_replacer
195 #pod
196 #pod This attribute is used just like C<L</replacer>>, and defaults to its value,
197 #pod but is used for building replacements for Pod removed after the last hunk of
198 #pod code. The idea is that if you're only concerned about altering your code's
199 #pod line numbers, you can stop replacing stuff after there's no more code to be
200 #pod affected.
201 #pod
202 #pod =cut
203  
204 1 has replacer => (
205 is => 'ro',
206 default => 'replace_with_nothing',
207 );
208  
209 has post_code_replacer => (
210 is => 'ro',
211 lazy => 1,
212 5 ▶ default => sub { $_[0]->replacer },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 1 );
214  
215 13 ▶sub _replacements_for {
Subroutine _replacements_for: called CC=2 SCAR=6.9
216 13 my ($self, $element, $after_last) = @_;
217  
218 13 ▶ my $replacer = $after_last ? $self->replacer : $self->post_code_replacer;
Branch: 1 branch
BranchTrueFalseTotal
$after_last ? : 7 6 13
219 13 return $self->$replacer($element);
220 }
221  
222 #pod =method replace_with_nothing
223 #pod
224 #pod This method returns nothing. It's the default C<L</replacer>>. It's not very
225 #pod interesting.
226 #pod
227 #pod =cut
228  
229 9 ▶sub replace_with_nothing { return }
Subroutine replace_with_nothing: called CC=1 SCAR=0.0
Pod documented
230  
231 #pod =method replace_with_comment
232 #pod
233 #pod This replacer replaces removed Pod elements with a comment containing their
234 #pod text. In other words:
235 #pod
236 #pod =head1 A header!
237 #pod
238 #pod This is great!
239 #pod
240 #pod =cut
241 #pod
242 #pod ...is replaced with:
243 #pod
244 #pod # =head1 A header!
245 #pod #
246 #pod # This is great!
247 #pod #
248 #pod # =cut
249 #pod
250 #pod =cut
251  
252 4 ▶sub replace_with_comment {
Subroutine replace_with_comment: called CC=1 SCAR=0.0
Pod documented
253 4 my ($self, $element) = @_;
254  
255 4 my $text = "$element";
256  
257 4 (my $pod = $text) =~ s/^(.)/#pod $1/mg;
258 4 $pod =~ s/^$/#pod/mg;
259 4 my $commented_out = PPI::Token::Comment->new($pod);
260  
261 4 return $commented_out;
262 }
263  
264 #pod =method replace_with_blank
265 #pod
266 #pod This replacer replaces removed Pod elements with vertical whitespace of equal
267 #pod line count. In other words:
268 #pod
269 #pod =head1 A header!
270 #pod
271 #pod This is great!
272 #pod
273 #pod =cut
274 #pod
275 #pod ...is replaced with five blank lines.
276 #pod
277 #pod =cut
278  
279 0 ▶sub replace_with_blank {
Subroutine replace_with_blank: not called CC=1 SCAR=6.9
Pod documented
280 0 my ($self, $element) = @_;
281  
282 0 my $text = "$element";
283 0 my @lines = split /\n/, $text;
284 0 my $blank = PPI::Token::Whitespace->new("\n" x (@lines));
285  
286 0 return $blank;
287 }
288  
289  
290 1 1;
291  
292 __END__
↑ Summary