blib/lib/Venus/Text.pm

filter: Statementstmt 92.0% 81 / 88 Branchbran 68.1% 30 / 44 Conditioncond 85.7% 12 / 14 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 12 / 12 Podpod 71.4% 5 / 7 totaltotal 84.3% 146 / 173 CC 30 SCAR 29.9
CC
30
Cov
84%
search
32.9
explode
6.0
string
3.0
CRAP 19.8
« blib/lib/Venus/Module.pm ↑ Summary blib/lib/Venus.pm »
1 package Venus::Text;
2  
3 119 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 119 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 119 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Venus
9  
10 119 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # IMPORTS
13  
14 119 ▶use Venus::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # INHERITS
17  
18 119 base 'Venus::Kind::Utility';
19  
20 # INTEGRATES
21  
22 119 with 'Venus::Role::Buildable';
23  
24 # ATTRIBUTES
25  
26 119 attr 'file';
27 119 attr 'stag';
28 119 attr 'etag';
29  
30 # BUILDERS
31  
32 446 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
33 446 my ($self, $data) = @_;
34  
35 return {
36 446 file => $data,
37 };
38 }
39  
40 # METHODS
41  
42 1991 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
43 1991 my ($self, $data) = @_;
44  
45 1991 my @result = ($self->search($data));
46  
47 1990 return scalar @result;
48 }
49  
50 16 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
51 16 my ($self) = @_;
52  
53 16 my $file = $self->file;
54  
55 16 my $data = Venus::Path->new($file)->read;
56  
57 16 return $data;
58 }
59  
60 12204 ▶sub explode {
Subroutine explode: called CC=6 SCAR=17.9
Pod undocumented
61 12204 my ($self) = @_;
62  
63 12204 my $data = $self->data;
64 12203 my $stag = $self->stag;
65 12203 my $etag = $self->etag;
66  
67 12203 my @chunks = split /^(?:\@$stag|$stag)\s*(.+?)\s*\r?\n/m, ($data . "\n");
68  
69 12203 shift @chunks;
70  
71 12203 my $items = [];
72  
73 12203 ▶ while (my ($meta, $data) = splice @chunks, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@chunks, 0, 2)) 7526700 12203 7538903
74 7526700 ▶ next unless $meta && $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta and $data 650489 6876211 7526700
Condition66%2/3
$meta and $data
!ll&&!rl&&r
06504896876211
MC/DC50%1/2
$meta and $data
$meta $data
Truth table66%2/3
A and B
A= $metaB= $data
ABresult
0X0
100
111
75 6876211 ▶ next unless $meta ne $etag;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta ne $etag 0 6876211 6876211
76  
77 6876211 my @info = split /\s/, $meta, 2;
78 6876211 ▶ my ($list, $name) = @info == 2 ? @info : (undef, @info);
Branch: 1 branch
BranchTrueFalseTotal
@info == 2 ? : 4719513 2156698 6876211
79  
80 6876211 $data =~ s/(\r?\n)\+$stag/$1$stag/g; # auto-escape nested syntax
81 6876211 $data = [split /\r?\n\r?\n/, $data];
82  
83 6876211 my $item = {name => $name, data => $data, index => @$items + 1, list => $list};
84  
85 6876211 push @$items, $item;
86 }
87  
88 12203 return $items;
89 }
90  
91 9887 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
92 9887 my ($self, $list, $name) = @_;
93  
94 9887 return $self->search({list => $list, name => $name});
95 }
96  
97 12204 ▶sub search {
Subroutine search: called CC=23 SCAR=34.9
Pod documented
98 12204 my ($self, $data) = @_;
99  
100 12204 ▶ $data //= {};
Condition100%2/2
$data //= {}
l!l
122004
MC/DC100%1/1
$data //= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
101  
102 12204 my $exploded = $self->explode;
103  
104 12203 ▶ return wantarray ? (@$exploded) : $exploded if !keys %$data;
Branch: 2 branches
BranchTrueFalseTotal
unless keys %$data 4 12199 12203
wantarray ? : 4 0 4
105  
106 12199 my @result;
107  
108 12199 my $sought = {map +($_, 1), keys %$data};
109  
110 12199 for my $item (sort {$a->{index} <=> $b->{index}} @$exploded) {
111 6876191 my $found = {};
112  
113 6876191 my $text;
114 6876191 ▶ if ($text = $data->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text = $data->{"data"}) 0 6876191 6876191
115 0 ▶ $text = ref($text) eq 'Regexp' ? $text : qr/^@{[quotemeta($text)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'Regexp' ? : 0 0 0
116 0 ▶ $found->{data} = 1 if "@{$item->{data}}" =~ $text;
Branch: 1 branch
BranchTrueFalseTotal
if join($", @{$item->{"data"};}) =~ /$text/u 0 0 0
117 }
118  
119 6876191 my $index;
120 6876191 ▶ if ($index = $data->{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index = $data->{"index"}) 0 6876191 6876191
121 0 ▶ $index = ref($index) eq 'Regexp' ? $index : qr/^@{[quotemeta($index)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'Regexp' ? : 0 0 0
122 0 ▶ $found->{index} = 1 if $item->{index} =~ $index;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"index"} =~ /$index/u 0 0 0
123 }
124  
125 6876191 my $list;
126 6876191 ▶ if ($list = $data->{list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list = $data->{'list'}) { } 4149414 2726777 6876191
127 4149414 ▶ $list = (ref($list) eq 'Regexp' ? $list : qr/^@{[quotemeta($list)]}$/);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'Regexp' ? : 0 4149414 4149414
128 4149414 ▶ $found->{list} = 1 if defined $item->{list} && $item->{list} =~ $list;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"list"} and $item->{"list"} =~ /$list/u 390566 3758848 4149414
Condition100%3/3
defined $item->{"list"} and $item->{"list"} =~ /$list/u
!ll&&!rl&&r
13191562439692390566
MC/DC100%2/2
defined $item->{"list"} and $item->{"list"} =~ /$list/u
defined $item->{"list"} $item->{"list"} =~ /$list/u
Truth table100%3/3
A and B
A= defined $item->{"list"}B= $item->{"list"} =~ /$list/u
ABresult
0X0
100
111
129 }
130 else {
131 $found->{list} = 1 if (exists $data->{list} && !defined $data->{list})
132 2726777 ▶ && !defined $item->{list};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"} 777150 1949627 2726777
Condition83%5/6
exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"}
!ll&&!rl&&r
2436011706026777150
exists $data->{"list"} and not defined $data->{"list"}
!ll&&!rl&&r
24360102483176
MC/DC66%2/3
exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"}
exists $data->{"list"} not defined $data->{"list"} not defined $item->{"list"}
Truth table75%3/4
A and not(B) and not(C)
A= exists $data->{"list"}B= not defined $data->{"list"}C= not defined $item->{"list"}
ABCresult
0XX0
10X0
1100
1111
133 }
134  
135 6876191 my $name;
136 6876191 ▶ if ($name = $data->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name = $data->{"name"}) 6751518 124673 6876191
137 6751518 ▶ $name = ref($name) eq 'Regexp' ? $name : qr/^@{[quotemeta($name)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'Regexp' ? : 0 6751518 6751518
138 6751518 ▶ $found->{name} = 1 if $item->{name} =~ $name;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"name"} =~ /$name/u 52561 6698957 6751518
139 }
140  
141 6876191 ▶ if (not(grep(not(defined($found->{$_})), keys(%$sought)))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep((!defined($found->{$_})), keys %$sought)) 12973 6863218 6876191
142 12973 push @result, $item;
143 }
144 }
145  
146 12199 ▶ return wantarray ? (@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12183 16 12199
147 }
148  
149 26 ▶sub string {
Subroutine string: called CC=3 SCAR=11.0
Pod documented
150 26 my ($self, $list, $name) = @_;
151  
152 26 my @result;
153  
154 26 for my $item ($self->find($list, $name)) {
155 25 push @result, join "\n\n", @{$item->{data}};
156 }
157  
158 26 ▶ return wantarray ? (@result) : join "\n", @result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 24 26
159 }
160  
161 119 1;
162  
163  
164  
165 =head1 NAME
166  
167 Venus::Text - Text Class
168  
169 =cut
170  
171 =head1 ABSTRACT
172  
173 Text Class for Perl 5
174  
175 =cut
176  
177 =head1 SYNOPSIS
178  
179 package main;
180  
181 use Venus::Text;
182  
183 my $text = Venus::Text->new('t/data/sections');
184  
185 # $text->find(undef, 'name');
186  
187 =cut
188  
189 =head1 DESCRIPTION
190  
191 This package provides methods for extracting C<DATA> sections and POD blocks
192 from any file or package. The package can be configured to parse either POD or
193 DATA blocks, and it defaults to being configured for POD blocks.
194  
195 =head2 DATA syntax
196  
197 __DATA__
198  
199 # data syntax
200  
201 @@ name
202  
203 Example Name
204  
205 @@ end
206  
207 @@ titles #1
208  
209 Example Title #1
210  
211 @@ end
212  
213 @@ titles #2
214  
215 Example Title #2
216  
217 @@ end
218  
219 =head2 DATA syntax (nested)
220  
221 __DATA__
222  
223 # data syntax (nested)
224  
225 @@ nested
226  
227 Example Nested
228  
229 +@@ demo
230  
231 blah blah blah
232  
233 +@@ end
234  
235 @@ end
236  
237 =head2 POD syntax
238  
239 # pod syntax
240  
241 =head1 NAME
242  
243 Example #1
244  
245 =cut
246  
247 =head1 NAME
248  
249 Example #2
250  
251 =cut
252  
253 # pod-ish syntax
254  
255 =name
256  
257 Example #1
258  
259 =cut
260  
261 =name
262  
263 Example #2
264  
265 =cut
266  
267 =head2 POD syntax (nested)
268  
269 # pod syntax (nested)
270  
271 =nested
272  
273 Example #1
274  
275 +=head1 WHY?
276  
277 blah blah blah
278  
279 +=cut
280  
281 More information on the same topic as was previously mentioned in the
282 previous section demonstrating the topic, obviously from said section.
283  
284 =cut
285  
286 =cut
287  
288 =head1 METHODS
289  
290 This package provides the following methods:
291  
292 =cut
293  
294 =head2 count
295  
296 count(hashref $criteria) (number)
297  
298 The count method uses the criteria provided to L</search> for and return the
299 number of blocks found.
300  
301 I<Since C<0.01>>
302  
303 =over 4
304  
305 =item count example 1
306  
307 # given: synopsis;
308  
309 $text->stag('=');
310  
311 $text->etag('=cut');
312  
313 my $count = $text->count;
314  
315 # 7
316  
317 =back
318  
319 =over 4
320  
321 =item count example 2
322  
323 # given: synopsis;
324  
325 $text->stag('@@ ');
326  
327 $text->etag('@@ end');
328  
329 my $count = $text->count;
330  
331 # 3
332  
333 =back
334  
335 =cut
336  
337 =head2 data
338  
339 data() (string)
340  
341 The data method returns the contents of the L</file> to be parsed.
342  
343 I<Since C<0.01>>
344  
345 =over 4
346  
347 =item data example 1
348  
349 # given: synopsis;
350  
351 $text = $text->data;
352  
353 # ...
354  
355 =back
356  
357 =cut
358  
359 =head2 find
360  
361 find(maybe[string] $list, maybe[string] $name) (arrayref)
362  
363 The find method is a wrapper around L</search> as shorthand for searching by
364 C<list> and C<name>.
365  
366 I<Since C<0.01>>
367  
368 =over 4
369  
370 =item find example 1
371  
372 # given: synopsis;
373  
374 $text->stag('=');
375  
376 $text->etag('=cut');
377  
378 my $find = $text->find(undef, 'name');
379  
380 # [
381 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
382 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
383 # ]
384  
385 =back
386  
387 =over 4
388  
389 =item find example 2
390  
391 # given: synopsis;
392  
393 $text->stag('=');
394  
395 $text->etag('=cut');
396  
397 my $find = $text->find('head1', 'NAME');
398  
399 # [
400 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
401 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
402 # ]
403  
404 =back
405  
406 =over 4
407  
408 =item find example 3
409  
410 # given: synopsis;
411  
412 $text->stag('@@ ');
413  
414 $text->etag('@@ end');
415  
416 my $find = $text->find(undef, 'name');
417  
418 # [
419 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
420 # ]
421  
422 =back
423  
424 =over 4
425  
426 =item find example 4
427  
428 # given: synopsis;
429  
430 $text->stag('@@ ');
431  
432 $text->etag('@@ end');
433  
434 my $find = $text->find('titles', '#1');
435  
436 # [
437 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
438 # ]
439  
440 =back
441  
442 =cut
443  
444 =head2 new
445  
446 new(any @args) (Venus::Text)
447  
448 The new method constructs an instance of the package.
449  
450 I<Since C<4.15>>
451  
452 =over 4
453  
454 =item new example 1
455  
456 package main;
457  
458 use Venus::Text;
459  
460 my $new = Venus::Text->new;
461  
462 # bless(..., "Venus::Text")
463  
464 =back
465  
466 =over 4
467  
468 =item new example 2
469  
470 package main;
471  
472 use Venus::Text;
473  
474 my $new = Venus::Text->new('t/data/sections');
475  
476 # bless(..., "Venus::Text")
477  
478 =back
479  
480 =over 4
481  
482 =item new example 3
483  
484 package main;
485  
486 use Venus::Text;
487  
488 my $new = Venus::Text->new(file => 't/data/sections');
489  
490 # bless(..., "Venus::Text")
491  
492 =back
493  
494 =cut
495  
496 =head2 search
497  
498 find(hashref $criteria) (arrayref)
499  
500 The search method returns the set of blocks matching the criteria provided.
501 This method can return a list of values in list-context.
502  
503 I<Since C<0.01>>
504  
505 =over 4
506  
507 =item search example 1
508  
509 # given: synopsis;
510  
511 $text->stag('=');
512  
513 $text->etag('=cut');
514  
515 my $search = $text->search({list => undef, name => 'name'});
516  
517 # [
518 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
519 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
520 # ]
521  
522 =back
523  
524 =over 4
525  
526 =item search example 2
527  
528 # given: synopsis;
529  
530 $text->stag('=');
531  
532 $text->etag('=cut');
533  
534 my $search = $text->search({list => 'head1', name => 'NAME'});
535  
536 # [
537 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
538 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
539 # ]
540  
541 =back
542  
543 =over 4
544  
545 =item search example 3
546  
547 # given: synopsis;
548  
549 $text->stag('@@ ');
550  
551 $text->etag('@@ end');
552  
553 my $find = $text->search({list => undef, name => 'name'});
554  
555 # [
556 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
557 # ]
558  
559 =back
560  
561 =over 4
562  
563 =item search example 4
564  
565 # given: synopsis;
566  
567 $text->stag('@@ ');
568  
569 $text->etag('@@ end');
570  
571 my $search = $text->search({list => 'titles', name => '#1'});
572  
573 # [
574 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
575 # ]
576  
577 =back
578  
579 =cut
580  
581 =head2 string
582  
583 string(maybe[string] $list, maybe[string] $name) (string)
584  
585 The string method is a wrapper around L</find> as shorthand for searching by
586 C<list> and C<name>, returning only the strings found.
587  
588 I<Since C<1.67>>
589  
590 =over 4
591  
592 =item string example 1
593  
594 # given: synopsis;
595  
596 $text->stag('=');
597  
598 $text->etag('=cut');
599  
600 my $string = $text->string(undef, 'name');
601  
602 # "Example #1\nExample #2"
603  
604 =back
605  
606 =over 4
607  
608 =item string example 2
609  
610 # given: synopsis;
611  
612 $text->stag('=');
613  
614 $text->etag('=cut');
615  
616 my $string = $text->string('head1', 'NAME');
617  
618 # "Example #1\nExample #2"
619  
620 =back
621  
622 =over 4
623  
624 =item string example 3
625  
626 # given: synopsis;
627  
628 $text->stag('@@ ');
629  
630 $text->etag('@@ end');
631  
632 my $string = $text->string(undef, 'name');
633  
634 # "Example Name"
635  
636 =back
637  
638 =over 4
639  
640 =item string example 4
641  
642 # given: synopsis;
643  
644 $text->stag('@@ ');
645  
646 $text->etag('@@ end');
647  
648 my $string = $text->string('titles', '#1');
649  
650 # "Example Title #1"
651  
652 =back
653  
654 =over 4
655  
656 =item string example 5
657  
658 # given: synopsis;
659  
660 $text->stag('=');
661  
662 $text->etag('=cut');
663  
664 my @string = $text->string('head1', 'NAME');
665  
666 # ("Example #1", "Example #2")
667  
668 =back
669  
670 =cut
671  
672 =head1 AUTHORS
673  
674 Awncorp, C<awncorp@cpan.org>
675  
676 =cut
677  
678 =head1 LICENSE
679  
680 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
681  
682 This program is free software, you can redistribute it and/or modify it under
683 the terms of the Apache license version 2.0.
684  
685 =cut
« blib/lib/Venus/Module.pm ↑ Summary blib/lib/Venus.pm »