Stardoc/Module/Perl.pm

filter: Statementstmt 90.3% 84 / 93 Branchbran 62.5% 20 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 12.5% 1 / 8 totaltotal 80.8% 118 / 146 CC 20 SCAR 13.7
CC
20
Cov
83%
parse_author
6.4
make_sections
5.4
merge_meta
5.0
CRAP 3.9
« Stardoc/Document/Pod.pm ↑ Summary Stardoc.pm »
1 ##
2 # name: Stardoc::Module::Perl
3 # abstract: Stardoc Perl Module
4 # author: Ingy döt Net <ingy@cpan.org>
5 # copyright: 2011
6 # license: perl
7  
8 package Stardoc::Module::Perl;
9 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 extends 'Stardoc::Module';
11  
12 1 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has meta => (
16 is => 'ro',
17 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 {
19 2 markup => 'pod',
20 encoding => 'utf8',
21 }
22 },
23 1 );
24 has other => (
25 is => 'ro',
26 2 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 );
28 1 has name => (
29 is => 'rw',
30 );
31 1 has synopsis => (
32 is => 'rw',
33 );
34 1 has description => (
35 is => 'rw',
36 );
37 1 has usage => (
38 is => 'rw',
39 );
40 1 has see => (
41 is => 'rw',
42 );
43 1 has author => (
44 is => 'rw',
45 );
46 1 has copyright => (
47 is => 'rw',
48 );
49  
50 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
51 2 my ($self) = @_;
52 2 $self->parse();
53 }
54  
55 1 my $pod_re = qr/^=\w+.*?(?:^=cut\s*\n|(?=^=\w)|\z)/ms;
56 # XXX - Need to account for perlcritic ## at some point.
57 1 my $star_re = qr/^##\s.*\n(?:#.*\n)*/m;
58 1 my $end_re = qr/^__(?:END|DATA)__.*/ms;
59  
60 2 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
61 2 my ($self) = @_;
62 2 my $text = io($self->file)->all;
63 2 my $sections = $self->sections;
64 2 for ($text =~ /($star_re|$pod_re|$end_re)/g) {
65 8 push @$sections, $self->make_sections($_)
66 }
67 }
68  
69 2 ▶sub merge_meta {
Subroutine merge_meta: called CC=5 SCAR=16.1
Pod undocumented
70 2 my ($self, $data) = @_;
71 2 my $meta = $self->meta;
72 2 for my $key (keys %$data) {
73 11 my $val = $data->{$key};
74 11 ▶ if ($key eq 'author') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'author') { } 2 9 11
elsif ($key eq 'see') { } 1 8 9
75 2 $meta->{author} = $self->parse_author($val);
76 }
77 elsif ($key eq 'see') {
78 1 ▶ $meta->{see} = ref($val) ? $val : [$val];
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 1 0 1
79 }
80 else {
81 8 $meta->{$key} = $data->{$key};
82 }
83 }
84 }
85  
86 2 ▶sub parse_author {
Subroutine parse_author: called CC=6 SCAR=18.6
Pod undocumented
87 2 my ($self, $val) = @_;
88 2 ▶ return [$val] if ref $val eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" 0 2 2
89 2 ▶ my $list = [ (ref $val eq 'ARRAY') ? @$val : $val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 2 2
90 2 ▶ for (my $i = 0; $i < @$list; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$list) 2 2 4
91 2 ▶ if (not ref $list->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list->[$i]) 2 0 2
92 2 my $string = $list->[$i];
93 2 my $hash = $list->[$i] = {};
94 2 ▶ if ($string =~ /^\s*(.*?)\s*<(.*)>$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\s*(.*?)\s*<(.*)>$/) { } 2 0 2
95 2 $hash->{name} = $1;
96 2 $hash->{email} = $2;
97 }
98 else {
99 0 $hash->{name} = $val;
100 }
101 }
102 }
103 2 return $list;
104 }
105  
106  
107 8 ▶sub make_sections {
Subroutine make_sections: called CC=4 SCAR=16.9
Pod undocumented
108 8 my ($self, $text) = @_;
109 8 $text =~ s/\s\z/\n/;
110 8 ▶ if ($text =~ /^##\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^##\s/) 2 6 8
111 2 return $self->make_comment_sections($text);
112 }
113 6 ▶ if ($text =~ /^=\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^=\w/) 6 0 6
114 6 $text =~ s/^\s*\n=cut\s*\n/\n/m;
115 6 return $self->make_pod_sections($text);
116 }
117 0 ▶ if ($text =~ s/^__(END|DATA)__\s\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^__(END|DATA)__\s\n//) 0 0 0
118 0 $self->merge_meta;
119 return map {
120 0 $self->make_sections($_)
121 } ($text =~ /($star_re|$pod_re)/g);
122 }
123 0 die $text;
124 }
125  
126 2 ▶sub make_comment_sections {
Subroutine make_comment_sections: called CC=4 SCAR=14.8
Pod undocumented
127 2 my ($self, $text) = @_;
128 2 $text =~ s/^##.*\n//;
129 2 $text =~ s/^# ?//gm;
130 2 my @sections;
131 2 ▶ while ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 2 2 4
132 2 $text =~ s/^\s*//;
133 2 ▶ if ($text =~ s/(^\w+:\s.*?\n)(\.\.\.\s*\n|\n\s*|\z)//s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ s/(^\w+:\s.*?\n)(\.\.\.\s*\n|\n\s*|\z)//s) { } 2 0 2
elsif ($text =~ s/^(=\w.*)//s) { } 0 0 0
134 2 push @sections, $self->make_meta_data($1);
135 }
136 elsif ($text =~ s/^(=\w.*)//s) {
137 0 push @sections, $self->make_pod_sections($1);
138 }
139 else {
140 0 die;
141 }
142 }
143 2 return @sections;
144 }
145  
146 1 my $name_re = qr/\S+\s+(NAME|DESCRIPTION|SYNOPSIS)/;
147 6 ▶sub make_pod_sections {
Subroutine make_pod_sections: called CC=3 SCAR=11.0
Pod undocumented
148 6 my ($self, $text) = @_;
149 6 my $other = $self->other;
150 6 for my $section ($text =~ /(=\w.*?\n)\s*(?=^=\w|\z)/gms) {
151 6 my $hash = {
152 type => 'pod',
153 text => $section,
154 };
155 6 ▶ if ($section =~ $name_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /$name_re/) { } 4 2 6
156 4 my $name = lc($1);
157 4 $self->$name($hash);
158 }
159 else {
160 2 push @$other, $hash;
161 }
162 }
163 6 return ();
164 }
165  
166 2 ▶sub make_meta_data {
Subroutine make_meta_data: called CC=2 SCAR=7.1
Pod undocumented
167 2 my ($self, $text) = @_;
168 2 my $data;
169 2 eval { $data = Load($text) };
170 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
171 0 warn "Invalid YAML in Stardoc. Skipping section.\n$@";
172 }
173 2 $self->merge_meta($data);
174 2 return;
175 }
176  
177 1 1;
« Stardoc/Document/Pod.pm ↑ Summary Stardoc.pm »