lib/Perl/ToPerl6/Utils/POD.pm

filter: Statementstmt 86.3% 114 / 132 Branchbran 71.4% 30 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 28 / 36 Podpod 100% 19 / 19 totaltotal 83.4% 191 / 229 CC 22 SCAR 11.7
CC
22
Cov
83%
_get_module_abstract_from_filehandle
9.0
get_raw_pod_section_for_module
6.0
get_raw_module_abstract_for_module
6.0
CRAP 3.2
« lib/Perl/ToPerl6/Transformer/Builtins/RewritePrint.pm ↑ Summary lib/Perl/ToPerl6/Transformation.pm »
1 package Perl::ToPerl6::Utils::POD;
2  
3 27 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 ▶use IO::String ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Pod::PlainText ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Pod::Select ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # TODO: non-fatal generic?
14 27 ▶use Perl::ToPerl6::Exception::Fatal::Generic qw< throw_generic >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Perl::ToPerl6::Exception::IO qw< throw_io >;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Perl::ToPerl6::Utils qw< :characters >;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 27 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #-----------------------------------------------------------------------------
21  
22 27 our @EXPORT_OK = qw(
23 get_pod_file_for_module
24 get_raw_pod_section_from_file
25 get_raw_pod_section_from_filehandle
26 get_raw_pod_section_from_string
27 get_raw_pod_section_for_module
28 get_pod_section_from_file
29 get_pod_section_from_filehandle
30 get_pod_section_from_string
31 get_pod_section_for_module
32 trim_raw_pod_section
33 trim_pod_section
34 get_raw_module_abstract_from_file
35 get_raw_module_abstract_from_filehandle
36 get_raw_module_abstract_from_string
37 get_raw_module_abstract_for_module
38 get_module_abstract_from_file
39 get_module_abstract_from_filehandle
40 get_module_abstract_from_string
41 get_module_abstract_for_module
42 );
43  
44 27 our %EXPORT_TAGS = (
45 all => \@EXPORT_OK,
46 );
47  
48 #-----------------------------------------------------------------------------
49  
50 188 ▶sub get_pod_file_for_module {
Subroutine get_pod_file_for_module: called CC=3 SCAR=11.0
Pod documented
51 188 my ($module_name) = @_;
52  
53 # No File::Spec: %INC always uses forward slashes.
54 188 (my $relative_path = $module_name) =~ s< :: ></>xmsg;
55 188 $relative_path .= '.pm';
56  
57 188 ▶ my $absolute_path = $INC{$relative_path} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $absolute_path = $INC{$relative_path} or return 187 1 188
58  
59 187 (my $pod_path = $absolute_path) =~ s< [.] [^.]+ \z><.pod>xms;
60 187 ▶ return $pod_path if -f $pod_path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $pod_path 0 187 187
61  
62 187 return $absolute_path;
63 }
64  
65 #-----------------------------------------------------------------------------
66  
67 0 ▶sub get_raw_pod_section_from_file {
Subroutine get_raw_pod_section_from_file: not called CC=1 SCAR=6.9
Pod documented
68 0 my ($file_name, $section_name) = @_;
69  
70 0 return _get_pod_section_from_file(
71 $file_name,
72 $section_name,
73 Pod::Select->new(),
74 );
75 }
76  
77 #-----------------------------------------------------------------------------
78  
79 0 ▶sub get_raw_pod_section_from_filehandle {
Subroutine get_raw_pod_section_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($file_handle, $section_name) = @_;
81  
82 0 return _get_pod_section_from_filehandle(
83 $file_handle,
84 $section_name,
85 Pod::Select->new(),
86 );
87 }
88  
89 #-----------------------------------------------------------------------------
90  
91 7 ▶sub get_raw_pod_section_from_string {
Subroutine get_raw_pod_section_from_string: called CC=1 SCAR=0.0
Pod documented
92 7 my ($source, $section_name) = @_;
93  
94 7 return _get_pod_section_from_string(
95 $source,
96 $section_name,
97 Pod::Select->new(),
98 );
99 }
100  
101 #-----------------------------------------------------------------------------
102  
103 0 ▶sub get_raw_pod_section_for_module {
Subroutine get_raw_pod_section_for_module: not called CC=2 SCAR=17.9
Pod documented
104 0 my ($module_name, $section_name) = @_;
105  
106 0 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 0 0 0
107 or throw_generic qq<Could not find POD for "$module_name".>;
108  
109 0 return get_raw_pod_section_from_file($file_name, $section_name);
110 }
111  
112 #-----------------------------------------------------------------------------
113  
114 1 ▶sub get_pod_section_from_file {
Subroutine get_pod_section_from_file: called CC=1 SCAR=0.0
Pod documented
115 1 my ($file_name, $section_name) = @_;
116  
117 1 return _get_pod_section_from_file(
118 $file_name,
119 $section_name,
120 Pod::PlainText->new(),
121 );
122 }
123  
124 #-----------------------------------------------------------------------------
125  
126 0 ▶sub get_pod_section_from_filehandle {
Subroutine get_pod_section_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
127 0 my ($file_handle, $section_name) = @_;
128  
129 0 return _get_pod_section_from_filehandle(
130 $file_handle,
131 $section_name,
132 Pod::PlainText->new(),
133 );
134 }
135  
136 #-----------------------------------------------------------------------------
137  
138 7 ▶sub get_pod_section_from_string {
Subroutine get_pod_section_from_string: called CC=1 SCAR=0.0
Pod documented
139 7 my ($source, $section_name) = @_;
140  
141 7 return _get_pod_section_from_string(
142 $source,
143 $section_name,
144 Pod::PlainText->new(),
145 );
146 }
147  
148 #-----------------------------------------------------------------------------
149  
150 2 ▶sub get_pod_section_for_module {
Subroutine get_pod_section_for_module: called CC=2 SCAR=6.9
Pod documented
151 2 my ($module_name, $section_name) = @_;
152  
153 2 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 1 1 2
154 or throw_generic qq<Could not find POD for "$module_name".>;
155  
156 1 return get_pod_section_from_file($file_name, $section_name);
157 }
158  
159 #-----------------------------------------------------------------------------
160  
161 1 ▶sub _get_pod_section_from_file {
Subroutine _get_pod_section_from_file: called CC=3 SCAR=11.3
162 1 my ($file_name, $section_name, $parser) = @_;
163  
164 1 ▶ open my $file_handle, '<', $file_name
Branch: 1 branch
BranchTrueFalseTotal
open my $file_handle, "<", $file_name or throw_io("message", qq[Could not open "$file_name": $Perl::ToPerl6::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::ToPerl6::Utils::POD::ERRNO) 1 0 1
165 or throw_io
166 message => qq<Could not open "$file_name": $ERRNO>,
167 file_name => $file_name,
168 errno => $ERRNO;
169  
170 1 my $content =
171 _get_pod_section_from_filehandle(
172 $file_handle, $section_name, $parser,
173 );
174  
175 1 ▶ close $file_handle
Branch: 1 branch
BranchTrueFalseTotal
close $file_handle or throw_io("message", qq[Could not close "$file_name": $Perl::ToPerl6::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::ToPerl6::Utils::POD::ERRNO) 1 0 1
176 or throw_io
177 message => qq<Could not close "$file_name": $ERRNO>,
178 file_name => $file_name,
179 errno => $ERRNO;
180  
181 1 return $content;
182 }
183  
184 #-----------------------------------------------------------------------------
185  
186 218 ▶sub _get_pod_section_from_filehandle {
Subroutine _get_pod_section_from_filehandle: called CC=2 SCAR=6.9
187 218 my ($file_handle, $section_name, $parser) = @_;
188  
189 218 $parser->select($section_name);
190  
191 218 my $content = $EMPTY;
192 218 my $content_handle = IO::String->new( \$content );
193  
194 218 $parser->parse_from_filehandle( $file_handle, $content_handle );
195  
196 218 ▶ return if $content eq $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $content eq $Perl::ToPerl6::Utils::POD::EMPTY 8 210 218
197 210 return $content;
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 14 ▶sub _get_pod_section_from_string {
Subroutine _get_pod_section_from_string: called CC=1 SCAR=0.0
203 14 my ($source, $section_name, $parser) = @_;
204  
205 14 my $source_handle = IO::String->new( \$source );
206  
207 return
208 14 _get_pod_section_from_filehandle(
209 $source_handle, $section_name, $parser,
210 );
211 }
212  
213 #-----------------------------------------------------------------------------
214  
215 11 ▶sub trim_raw_pod_section {
Subroutine trim_raw_pod_section: called CC=2 SCAR=7.0
Pod documented
216 11 my ($pod) = @_;
217  
218 11 ▶ return if not defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 11 11
219  
220 11 $pod =~ s< \A =head1 \b [^\n]* \n $ ><>xms;
221 11 $pod =~ s< \A \s+ ><>xms;
222 11 $pod =~ s< \s+ \z ><>xms;
223  
224 11 return $pod;
225 }
226  
227 #-----------------------------------------------------------------------------
228  
229 197 ▶sub trim_pod_section {
Subroutine trim_pod_section: called CC=2 SCAR=7.0
Pod documented
230 197 my ($pod) = @_;
231  
232 197 ▶ return if not defined $pod;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod 0 197 197
233  
234 197 $pod =~ s< \A [^\n]* \n ><>xms;
235 197 $pod =~ s< \A \s* \n ><>xms;
236 197 $pod =~ s< \s+ \z ><>xms;
237  
238 197 return $pod;
239 }
240  
241 #-----------------------------------------------------------------------------
242  
243 0 ▶sub get_raw_module_abstract_from_file {
Subroutine get_raw_module_abstract_from_file: not called CC=1 SCAR=6.9
Pod documented
244 0 my ($file_name) = @_;
245  
246 return
247 0 _get_module_abstract_from_file(
248 $file_name,
249 Pod::Select->new(),
250 \&trim_raw_pod_section,
251 );
252 }
253  
254 #-----------------------------------------------------------------------------
255  
256 0 ▶sub get_raw_module_abstract_from_filehandle {
Subroutine get_raw_module_abstract_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
257 0 my ($file_handle) = @_;
258  
259 return
260 0 _get_module_abstract_from_filehandle(
261 $file_handle,
262 Pod::Select->new(),
263 \&trim_raw_pod_section,
264 );
265 }
266  
267 #-----------------------------------------------------------------------------
268  
269 9 ▶sub get_raw_module_abstract_from_string {
Subroutine get_raw_module_abstract_from_string: called CC=1 SCAR=0.0
Pod documented
270 9 my ($source) = @_;
271  
272 return
273 9 _get_module_abstract_from_string(
274 $source,
275 Pod::Select->new(),
276 \&trim_raw_pod_section,
277 );
278 }
279  
280 #-----------------------------------------------------------------------------
281  
282 0 ▶sub get_raw_module_abstract_for_module {
Subroutine get_raw_module_abstract_for_module: not called CC=2 SCAR=17.9
Pod documented
283 0 my ($module_name) = @_;
284  
285 0 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 0 0 0
286 or throw_generic qq<Could not find POD for "$module_name".>;
287  
288 0 return get_raw_module_abstract_from_file($file_name);
289 }
290  
291 #-----------------------------------------------------------------------------
292  
293 186 ▶sub get_module_abstract_from_file {
Subroutine get_module_abstract_from_file: called CC=1 SCAR=0.0
Pod documented
294 186 my ($file_name) = @_;
295  
296 return
297 186 _get_module_abstract_from_file(
298 $file_name,
299 Pod::PlainText->new(),
300 \&trim_pod_section,
301 );
302 }
303  
304 #-----------------------------------------------------------------------------
305  
306 0 ▶sub get_module_abstract_from_filehandle {
Subroutine get_module_abstract_from_filehandle: not called CC=1 SCAR=6.9
Pod documented
307 0 my ($file_handle) = @_;
308  
309 return
310 0 _get_module_abstract_from_filehandle(
311 $file_handle,
312 Pod::PlainText->new(),
313 \&trim_pod_section,
314 );
315 }
316  
317 #-----------------------------------------------------------------------------
318  
319 8 ▶sub get_module_abstract_from_string {
Subroutine get_module_abstract_from_string: called CC=1 SCAR=0.0
Pod documented
320 8 my ($source) = @_;
321  
322 return
323 8 _get_module_abstract_from_string(
324 $source,
325 Pod::PlainText->new(),
326 \&trim_pod_section,
327 );
328 }
329  
330 #-----------------------------------------------------------------------------
331  
332 186 ▶sub get_module_abstract_for_module {
Subroutine get_module_abstract_for_module: called CC=2 SCAR=7.1
Pod documented
333 186 my ($module_name) = @_;
334  
335 186 ▶ my $file_name = get_pod_file_for_module($module_name)
Branch: 1 branch
BranchTrueFalseTotal
my $file_name = get_pod_file_for_module($module_name) or throw_generic(qq[Could not find POD for "$module_name".]) 186 0 186
336 or throw_generic qq<Could not find POD for "$module_name".>;
337  
338 186 return get_module_abstract_from_file($file_name);
339 }
340  
341 #-----------------------------------------------------------------------------
342  
343 186 ▶sub _get_module_abstract_from_file {
Subroutine _get_module_abstract_from_file: called CC=3 SCAR=11.3
344 186 my ($file_name, $parser, $trimmer) = @_;
345  
346 186 ▶ open my $file_handle, '<', $file_name
Branch: 1 branch
BranchTrueFalseTotal
open my $file_handle, "<", $file_name or throw_io("message", qq[Could not open "$file_name": $Perl::ToPerl6::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::ToPerl6::Utils::POD::ERRNO) 186 0 186
347 or throw_io
348 message => qq<Could not open "$file_name": $ERRNO>,
349 file_name => $file_name,
350 errno => $ERRNO;
351  
352 186 my $module_abstract =
353 _get_module_abstract_from_filehandle(
354 $file_handle, $parser, $trimmer,
355 );
356  
357 186 ▶ close $file_handle
Branch: 1 branch
BranchTrueFalseTotal
close $file_handle or throw_io("message", qq[Could not close "$file_name": $Perl::ToPerl6::Utils::POD::ERRNO], "file_name", $file_name, "errno", $Perl::ToPerl6::Utils::POD::ERRNO) 186 0 186
358 or throw_io
359 message => qq<Could not close "$file_name": $ERRNO>,
360 file_name => $file_name,
361 errno => $ERRNO;
362  
363 186 return $module_abstract;
364 }
365  
366 #-----------------------------------------------------------------------------
367  
368 203 ▶sub _get_module_abstract_from_filehandle {
Subroutine _get_module_abstract_from_filehandle: called CC=9 SCAR=22.0
369 203 my ($file_handle, $parser, $trimmer) = @_;
370  
371 203 my $name_section =
372 _get_pod_section_from_filehandle( $file_handle, 'NAME', $parser );
373 203 ▶ return if not $name_section;
Branch: 1 branch
BranchTrueFalseTotal
unless $name_section 2 201 203
374  
375 201 $name_section = $trimmer->($name_section);
376 201 ▶ return if not $name_section;
Branch: 1 branch
BranchTrueFalseTotal
unless $name_section 2 199 201
377  
378 # Testing for parser class, blech. But it's a lot simpler and it's all
379 # hidden in the implementation.
380 199 ▶ if ('Pod::Select' eq ref $parser) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Pod::Select' eq ref $parser) { } 7 192 199
381 7 ▶ if ( $name_section =~ m< \n >xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \n /msx) 1 6 7
382 1 throw_generic
383 qq<Malformed NAME section in "$name_section". >
384 . q<It must be on a single line>;
385 }
386 }
387 else {
388 192 $name_section =~ s< \s+ >< >xmsg;
389  
390 # Ugh. Pod::PlainText splits up module names.
391 192 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* ( \w [ \w:]+ \w ) ( \s* - .* )? \z /msx) 191 1 192
392 $name_section =~ m<
393 \A
394 \s*
395 (
396 \w [ \w:]+ \w
397 )
398 (
399 \s*
400 -
401 .*
402 )?
403 \z
404 >xms
405 ) {
406 191 my ($module_name, $rest) = ($1, $2);
407  
408 191 $module_name =~ s/ [ ] //xms;
409  
410 191 ▶ $name_section = $module_name . ( $rest ? $rest : $EMPTY );
Branch: 1 branch
BranchTrueFalseTotal
$rest ? : 185 6 191
411 }
412 }
413  
414 198 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* [\w:]+ # Module name. \s+ - # The required single hyphen. \s+ ( \S # At least one non-whitespace. (?: .* \S)? # Everything up to the last non-whitespace. ) \s* \z /msx) 185 13 198
415 $name_section =~ m<
416 \A
417 \s*
418 [\w:]+ # Module name.
419 \s+
420 - # The required single hyphen.
421 \s+
422 (
423 \S # At least one non-whitespace.
424 (?: .* \S)? # Everything up to the last non-whitespace.
425 )
426 \s*
427 \z
428 >xms
429 ) {
430 185 my $module_abstract = $1;
431 185 return $module_abstract;
432 }
433  
434 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($name_section =~ / \A \s* [\w:]+ # Module name. (?: \s* - )? # The single hyphen is now optional. \s* \z /msx) 9 4 13
435 $name_section =~ m<
436 \A
437 \s*
438 [\w:]+ # Module name.
439 (?: \s* - )? # The single hyphen is now optional.
440 \s*
441 \z
442 >xms
443 ) {
444 9 return;
445 }
446  
447 4 throw_generic qq<Malformed NAME section in "$name_section".>;
448 }
449  
450 #-----------------------------------------------------------------------------
451  
452 17 ▶sub _get_module_abstract_from_string {
Subroutine _get_module_abstract_from_string: called CC=1 SCAR=0.0
453 17 my ($source, $parser, $trimmer) = @_;
454  
455 17 my $source_handle = IO::String->new( \$source );
456  
457 return
458 17 _get_module_abstract_from_filehandle(
459 $source_handle, $parser, $trimmer,
460 );
461 }
462  
463 #-----------------------------------------------------------------------------
464  
465 27 1;
466  
467 __END__
« lib/Perl/ToPerl6/Transformer/Builtins/RewritePrint.pm ↑ Summary lib/Perl/ToPerl6/Transformation.pm »