Debora/Pod.pm

filter: Statementstmt 98.4% 128 / 130 Branchbran 68.4% 26 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 92.7% 180 / 194 CC 23 SCAR 12.4
CC
23
Cov
92%
description
7.0
_handle_element_start
4.4
_copyrights_from_text
4.0
CRAP 3.5
« Debora/License.pm ↑ Summary Debora/Package/Mageia.pm »
1 package CPANPLUS::Dist::Debora::Pod;
2  
3 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4  
5 9 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION = '0.019';
10  
11 9 ▶use parent qw(Pod::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use Pod::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶sub find {
Subroutine find: called CC=2 SCAR=7.0
Pod documented
17 4 my ($class, $module_name, @dirs) = @_;
18  
19 4 my $pod;
20  
21 4 my $podfile = Pod::Simple::Search->new->inc(0)->find($module_name, @dirs);
22 4 ▶ if ($podfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($podfile) 4 0 4
23 4 $pod = CPANPLUS::Dist::Debora::Pod->new;
24 4 $pod->parse_file($podfile);
25 }
26  
27 4 return $pod;
28 }
29  
30 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 4 my $class = shift;
32  
33 4 my $self = $class->SUPER::new(@_);
34  
35 4 $self->{buf} = q{};
36 4 $self->{text} = q{};
37  
38 4 return $self;
39 }
40  
41 4 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
42 4 my $self = shift;
43  
44 4 return $self->{text};
45 }
46  
47 2 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
Pod documented
48 2 my $self = shift;
49  
50 2 return $self->section(q{1}, qr{NAME}xmsi);
51 }
52  
53 2 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.2
Pod documented
54 2 my $self = shift;
55  
56 2 my $summary = $self->title;
57 2 ▶ if ($summary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($summary) 2 0 2
58 2 ▶ if ($summary =~ m{\h+ - \h+ (.*)}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($summary =~ /\h+ - \h+ (.*)/msux) 2 0 2
59 2 $summary = $1;
60 }
61 }
62  
63 2 return $summary;
64 }
65  
66 4 ▶sub description {
Subroutine description: called CC=7 SCAR=19.5
Pod documented
67 4 my $self = shift;
68  
69 4 my $length = 500;
70  
71 my @headings
72 4 = (qr{DESCRIPTION}xmsi, qr{INTRODUCTION}xmsi, qr{SYNOPSIS}xmsi);
73  
74 4 my $description;
75 SECTION:
76 4 for my $heading (@headings) {
77 4 my $section = $self->section(q{1}, $heading);
78 4 ▶ next SECTION if !$section;
Branch: 1 branch
BranchTrueFalseTotal
unless $section 0 4 4
79  
80 4 $description = q{};
81  
82 # Remove subheadings.
83 4 $section =~ s{^ =head\d \h (\V+) \v+}{}xmsg;
84  
85 # Add the first paragraphs to the description.
86 PARAGRAPH:
87 4 for my $paragraph (split qr{\v\v+}xms, $section) {
88 32 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 28 4 32
89 28 $description .= "\n\n";
90 }
91 32 $description .= $paragraph;
92 32 ▶ last PARAGRAPH if length $description > $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $description > $length 4 28 32
93 }
94  
95 # Remove the last sentence if the sentence ends with ":".
96 4 $description =~ s{[.] [^.]* : \z}{.}xms;
97  
98 # Remove the last sentence if the sentence contains the word "below".
99 4 $description =~ s{[.] [^.]+ \b (?:below) \b [^.]* [.] \z}{.}xms;
100  
101 4 ▶ last SECTION if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 4 0 4
102 }
103  
104 4 return $description;
105 }
106  
107 5 ▶sub _copyrights_from_text {
Subroutine _copyrights_from_text: called CC=4 SCAR=13.9
108 5 my ($self, $text) = @_;
109  
110 5 my $COPYRIGHT = qr{Copyright (?:\h+ (?:[(]c[)] | ©))?}xmsi;
111 5 my $YEAR = qr{\d+ (?: \s* [-,] \s* \d+)*}xms;
112 5 my $HOLDER = qr{[^\v]+}xms;
113  
114 5 my $COPYRIGHT_NOTICE = qr{
115 $COPYRIGHT
116 \s+
117 ($YEAR) [-,]?
118 \s+
119 ($HOLDER)
120 }xms;
121  
122 5 my $MARKS = qr{[.,;!?:]}xms;
123  
124 5 my $AUTHOR_REFERENCE = qr{
125 \b (?:above | aforementioned ) \b
126 | "AUTHORS?"
127 }xmsi;
128  
129 # Put a newline before any copyright notice so that we can find
130 # consecutive copyright notices.
131 5 $text =~ s{($COPYRIGHT \s+ $YEAR)}{\n$1}xmsg;
132  
133 # Remove some phrases.
134 5 my @phrases = (
135 qr{\b by \b}xmsi, # "by"
136 qr{[(] [^)]* [)]}xms, # text in parens
137 qr{(?:All | Some) \h+ rights \h+ reserved \V*}xmsi,
138 qr{This [\h\w]* \h+ is \h+ free \h+ software \V*}xmsi,
139 qr{This [\h\w]* \h+ is \h+ made \h+ available \V*}xmsi,
140 qr{License [\h\w]* \h+ granted \V*}xmsi,
141 qr{Licensed \h+ under \V*}xmsi,
142 qr{Same \h+ license \V*}xmsi,
143 qr{You \h+ (?:may | should) \V*}xmsi,
144 qr{[^.,;:]+ \h+ is \h+ (?:distributed | released) \V*}xmsi,
145 qr{<? \S+@\S+[.]\S+ >?}xms, # email addresses
146 qr{https?://[^\h]+}xms, # URLs
147 );
148  
149 5 for my $phrase (@phrases) {
150 60 $text =~ s{$MARKS* \h* $phrase}{}xmsg;
151 }
152  
153 5 my %unique_copyrights;
154 COPYRIGHT_NOTICE:
155 5 ▶ while ($text =~ m{$COPYRIGHT_NOTICE}xmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$COPYRIGHT_NOTICE/gmsux) 6 5 11
156 6 my $year = $1;
157 6 my $holder = $2;
158  
159 6 $year =~ s{\h* -+ \h*}{-}xmsg; # Remove spaces from hyphens.
160 6 $year =~ s{,(\S)}{, $1}xmsg; # Put a space after commas.
161 6 $year =~ s{\s+}{ }xmsg; # Squeeze spaces.
162  
163 6 $holder =~ s{\s+ \z}{}xms; # Remove trailing spaces.
164 6 $holder =~ s{\s+}{ }xmsg; # Squeeze spaces.
165 6 $holder =~ s{$MARKS+ \z}{}xms; # Remove trailing punctuation marks.
166  
167 6 ▶ next COPYRIGHT_NOTICE if $holder =~ $AUTHOR_REFERENCE;
Branch: 1 branch
BranchTrueFalseTotal
if $holder =~ /$AUTHOR_REFERENCE/u 0 6 6
168  
169 6 $unique_copyrights{"$year $holder"}
170 = {year => $year, holder => $holder};
171 }
172  
173 my @copyrights
174 5 = sort { $a->{year} cmp $b->{year} } values %unique_copyrights;
175  
176 5 return \@copyrights;
177 }
178  
179 4 ▶sub copyrights {
Subroutine copyrights: called CC=2 SCAR=7.0
Pod documented
180 4 my $self = shift;
181  
182 4 my $COPYRIGHT_HEADINGS = qr{
183 (?: LICEN[CS]E | LICENSING | COPYRIGHT | LEGAL ) \b [^\v]*
184 }xmsi;
185  
186 4 my @copyrights;
187  
188 4 my $section = $self->section(qr{\d}xms, $COPYRIGHT_HEADINGS);
189 4 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 4 0 4
190 4 push @copyrights, @{$self->_copyrights_from_text($section)};
191 }
192  
193 4 return \@copyrights;
194 }
195  
196 10 ▶sub section {
Subroutine section: called CC=2 SCAR=7.0
Pod documented
197 10 my ($self, $level, $title) = @_;
198  
199 10 my $section;
200 10 ▶ if ($self->{text} =~ m{^ =head($level) \h $title \v+ (.*)}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"text"} =~ /^ =head($level) \h $title \v+ (.*)/msux) 10 0 10
201 10 my $n = $1;
202 10 $section = $2;
203 10 $section =~ s{\v* ^ =head$n \h .*}{}xms; # Remove other sections.
204 10 $section =~ s{\v+ \z}{}xms; # Remove trailing newlines.
205 }
206  
207 10 return $section;
208 }
209  
210 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
211  
212 632 ▶sub _handle_element_start {
Subroutine _handle_element_start: called CC=4 SCAR=14.8
213 632 my ($self, $name, $attrs) = @_;
214  
215 632 my %do_clear = map { $_ => 1 } qw(
216 head1
217 head2
218 head3
219 head4
220 item-text
221 Para
222 Verbatim
223 );
224  
225 632 ▶ if ($name eq 'item-bullet') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'item-bullet') { } 0 632 632
elsif ($name eq 'item-number') { } 0 632 632
elsif ($do_clear{$name}) { } 480 152 632
226 0 $self->{buf} = q{ * };
227 }
228 elsif ($name eq 'item-number') {
229 0 $self->{buf} = q{ } . $attrs->{number} . q{. };
230 }
231 elsif ($do_clear{$name}) {
232 480 $self->{buf} = q{};
233 }
234  
235 632 return;
236 }
237  
238 632 ▶sub _handle_element_end {
Subroutine _handle_element_end: called CC=4 SCAR=13.9
239 632 my ($self, $name) = @_;
240  
241 632 my %do_output = map { $_ => 1 } qw(
242 head1
243 head2
244 head3
245 head4
246 item-bullet
247 item-number
248 item-text
249 Para
250 Verbatim
251 );
252  
253 632 my %do_newline = map { $_ => 1 } qw(
254 head1
255 head2
256 head3
257 head4
258 Para
259 Verbatim
260 );
261  
262 632 ▶ if ($name =~ m{^ head\d}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^ head\d/msux) 144 488 632
263 144 $self->{text} .= "=$name ";
264 }
265  
266 632 ▶ if ($do_output{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_output{$name}) 480 152 632
267 480 $self->{text} .= $self->{buf};
268 480 $self->{text} .= "\n";
269 480 ▶ if ($do_newline{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_newline{$name}) 440 40 480
270 440 $self->{text} .= "\n";
271 }
272 480 $self->{buf} = q{};
273 }
274  
275 632 return;
276 }
277  
278 684 ▶sub _handle_text {
Subroutine _handle_text: called CC=3 SCAR=11.2
279 684 my ($self, $text) = @_;
280  
281 # Pod::Simple provides nbsp and shy since Perl 5.24.
282 ## no critic (Variables::ProhibitPackageVars)
283 684 ▶ if (defined $Pod::Simple::nbsp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Pod::Simple::nbsp) 684 0 684
284 684 $text =~ s{$Pod::Simple::nbsp}{ }xmsg;
285 }
286 684 ▶ if (defined $Pod::Simple::shy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Pod::Simple::shy) 684 0 684
287 684 $text =~ s{$Pod::Simple::shy}{}xmsg;
288 }
289 684 $self->{buf} .= $text;
290  
291 684 return;
292 }
293  
294 9 1;
295 __END__
« Debora/License.pm ↑ Summary Debora/Package/Mageia.pm »