PluginCatalog.pm

filter: Statementstmt 33.3% 43 / 129 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.4% 10 / 26 Podpod 57.1% 4 / 7 totaltotal 27.4% 57 / 208 CC 29 SCAR 40.9
CC
29
Cov
25%
add_document
182.0
generate_tag_page
72.0
generate_index_page
30.0
CRAP 59.7
↑ Summary PluginCatalog/Entry.pm »
1 #---------------------------------------------------------------------
2 package Pod::PluginCatalog;
3 #
4 # Copyright 2012 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 18 Jul 2012
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Format a catalog of plugin modules
18 #---------------------------------------------------------------------
19  
20 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '0.02';
25 # This file is part of Pod-PluginCatalog 0.02 (January 3, 2015)
26  
27 1 ▶use autodie ':io';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Pod::PluginCatalog::Entry ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Pod::Elemental ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Pod::Elemental::Selectors qw(s_command s_flat);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Pod::Elemental::Transformer::Nester ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Text::Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #=====================================================================
36  
37  
38 1 has namespace_rewriter => (
39 is => 'ro',
40 isa => 'CodeRef',
41 required => 1,
42 );
43  
44 1 has pod_formatter => (
45 is => 'ro',
46 isa => 'CodeRef',
47 required => 1,
48 );
49  
50 has _plugins => (
51 is => 'ro',
52 isa => 'HashRef[Pod::PluginCatalog::Entry]',
53 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 1 );
55  
56 has _tags => (
57 is => 'ro',
58 isa => 'HashRef[Str]',
59 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 1 traits => ['Hash'],
61 handles => {
62 tags => 'keys',
63 },
64 );
65  
66 1 has _author_selector => (
67 is => 'ro',
68 lazy => 1,
69 builder => '_build_author_selector',
70 );
71  
72 0 ▶sub _build_author_selector { s_command('author') }
Subroutine _build_author_selector: not called CC=1 SCAR=6.9
73  
74 1 has _plugin_selector => (
75 is => 'ro',
76 lazy => 1,
77 builder => '_build_plugin_selector',
78 );
79  
80 0 ▶sub _build_plugin_selector { s_command('plugin') }
Subroutine _build_plugin_selector: not called CC=1 SCAR=6.9
81  
82 1 has _tag_selector => (
83 is => 'ro',
84 lazy => 1,
85 builder => '_build_tag_selector',
86 );
87  
88 0 ▶sub _build_tag_selector { s_command('tag') }
Subroutine _build_tag_selector: not called CC=1 SCAR=6.9
89  
90 1 has _nester => (
91 is => 'ro',
92 lazy => 1,
93 builder => '_build_nester',
94 );
95  
96 sub _build_nester
97 0 ▶{
Subroutine _build_nester: not called CC=1 SCAR=6.9
98 0 Pod::Elemental::Transformer::Nester->new({
99 top_selector => s_command(['plugin', 'tag']),
100 content_selectors => [
101 s_command([ qw(head3 head4 over item back) ]),
102 s_flat,
103 ],
104 });
105 }
106  
107  
108 has delimiters => (
109 is => 'ro',
110 isa => 'ArrayRef',
111 lazy => 1,
112 0 ▶ default => sub { [ qw( {{ }} ) ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 1 );
114  
115 1 has file_extension => (
116 is => 'ro',
117 isa => 'Str',
118 default => '.html',
119 );
120  
121 1 has perlio_layers => (
122 is => 'ro',
123 isa => 'Str',
124 default => ':utf8',
125 );
126  
127 #=====================================================================
128 sub _err
129 0 ▶{
Subroutine _err: not called CC=2 SCAR=17.9
130 0 my ($source, $node, $err) = @_;
131  
132 0 my $line = $node->start_line;
133 0 ▶ $line = ($line ? "$line:" : '');
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 0 0 0
134 0 confess "$source:$line $err";
135 } # end _err
136 #---------------------------------------------------------------------
137  
138  
139 sub add_file
140 0 ▶{
Subroutine add_file: not called CC=1 SCAR=6.9
Pod documented
141 0 my ($self, $filename) = @_;
142  
143 0 $self->add_document($filename => Pod::Elemental->read_file($filename));
144 } # end add_file
145 #---------------------------------------------------------------------
146  
147  
148 sub add_document
149 0 ▶{
Subroutine add_document: not called CC=13 SCAR=52.0
Pod documented
150 0 my ($self, $source, $doc) = @_;
151  
152 0 my $plugins = $self->_plugins;
153 0 my $tags = $self->_tags;
154 0 my $rewriter = $self->namespace_rewriter;
155 0 my $author_selector = $self->_author_selector;
156 0 my $plugin_selector = $self->_plugin_selector;
157 0 my $tag_selector = $self->_tag_selector;
158  
159 0 $self->_nester->transform_node($doc);
160  
161 0 my @author;
162  
163 0 foreach my $node (@{ $doc->children }) {
164 0 ▶ if ($author_selector->($node)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&$author_selector($node)) { } 0 0 0
elsif (&$tag_selector($node)) { } 0 0 0
elsif (&$plugin_selector($node)) { } 0 0 0
165 0 my $author = $node->content;
166 0 chomp $author;
167 0 ▶ if (length $author) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $author) { } 0 0 0
168 0 @author = (author => $author);
169 } else {
170 0 @author = ();
171 }
172 } elsif ($tag_selector->($node)) {
173 0 my $tag = $node->content;
174 0 ▶ _err($source, $node, "=tag without tag name") unless length $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless length $tag 0 0 0
175 _err($source, $node, "Duplicate description for tag $tag")
176 0 ▶ if defined $tags->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tags->{$tag} 0 0 0
177 0 $tags->{$tag} = $self->format_description($node);
178 } elsif ($plugin_selector->($node)) {
179 0 my ($name, @tags) = split(' ', $node->content);
180  
181 0 ▶ _err($source, $node, "Plugin $name has no tags") unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 0 0 0
182  
183 _err($source, $node, "Plugin $name already seen in " .
184 ($plugins->{$name}->source_file // 'unknown file'))
185 0 ▶ if $plugins->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $plugins->{$name} 0 0 0
Condition0%0/2
$plugins->{$name}->source_file // "unknown file"
l!l
00
MC/DC0%0/1
$plugins->{$name}->source_file // "unknown file"
$plugins->{$name}->source_file
Truth table0%0/2
A
A= $plugins->{$name}->source_file
Aresult
11
00
186  
187 0 ▶ $tags->{$_} //= undef for @tags;
Condition0%0/2
$tags->{$_} //= undef
l!l
00
MC/DC0%0/1
$tags->{$_} //= undef
$tags->{$_}
Truth table0%0/2
A
A= $tags->{$_}
Aresult
11
00
188  
189 0 my $module = $rewriter->($name);
190  
191 0 $plugins->{$name} = Pod::PluginCatalog::Entry->new(
192 name => $name, module => $module,
193 description => $self->format_description($node),
194 source_file => $source, tags => \@tags,
195 @author,
196 );
197 }
198 } # end foreach $node
199  
200 } # end add_document
201 #---------------------------------------------------------------------
202  
203 sub format_description
204 0 ▶{
Subroutine format_description: not called CC=1 SCAR=6.9
Pod undocumented
205 0 my ($self, $node) = @_;
206  
207 0 my $pod = join('', map { $_->as_pod_string } @{ $node->children });
208  
209 0 $self->pod_formatter->("=pod\n\n$pod");
210 } # end format_description
211 #---------------------------------------------------------------------
212  
213  
214 sub generate_tag_pages
215 0 ▶{
Subroutine generate_tag_pages: not called CC=2 SCAR=17.9
Pod documented
216 0 my ($self, $header, $template, $footer) = @_;
217  
218 0 $self->compile_templates($header, $template, $footer);
219  
220 $self->generate_tag_page($_, $header, $template, $footer)
221 0 for sort $self->tags;
222 } # end generate_tag_pages
223 #---------------------------------------------------------------------
224  
225 sub generate_tag_page
226 0 ▶{
Subroutine generate_tag_page: not called CC=8 SCAR=42.8
Pod undocumented
227 0 my ($self, $tag, $header, $template, $footer) = @_;
228  
229 0 ▶ confess "index is a reserved name" if $tag eq 'index';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "index" 0 0 0
230  
231 0 my %data = (tag => $tag, tag_description => $self->_tags->{$tag});
232  
233 0 ▶ warn "No description for tag $tag\n" unless $data{tag_description};
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"tag_description"} 0 0 0
234  
235 0 my @plugins = sort { $a->name cmp $b->name }
236 0 grep { $_->has_tag($tag) }
237 0 values %{ $self->_plugins };
238  
239 0 ▶ unless (@plugins) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@plugins) 0 0 0
240 0 warn "No plugins for tag $tag\n";
241 0 return;
242 }
243  
244 0 open(my $out, '>' . $self->perlio_layers, $tag . $self->file_extension);
245  
246 0 ▶ $header->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$header->fill_in("HASH", \%data, "OUTPUT", $out) or confess("Filling in the header template failed for $tag") 0 0 0
247 or confess("Filling in the header template failed for $tag");
248  
249 0 for my $plugin (@plugins) {
250 my %data = (
251 %data,
252 other_tags => [ $plugin->other_tags($tag) ],
253 0 map { $_ => $plugin->$_() } qw(name module description author)
254 );
255  
256 0 ▶ $template->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$template->fill_in("HASH", \%data, "OUTPUT", $out) or confess(qq[Filling in the entry template failed for $data{"name"}]) 0 0 0
257 or confess("Filling in the entry template failed for $data{name}");
258 }
259  
260 0 ▶ $footer->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$footer->fill_in("HASH", \%data, "OUTPUT", $out) or confess("Filling in the footer template failed for $tag") 0 0 0
261 or confess("Filling in the footer template failed for $tag");
262  
263 0 close $out;
264 } # end generate_tag_page
265 #---------------------------------------------------------------------
266  
267  
268 sub generate_index_page
269 0 ▶{
Subroutine generate_index_page: not called CC=5 SCAR=34.0
Pod documented
270 0 my ($self, $header, $template, $footer) = @_;
271  
272 0 $self->compile_templates($header, $template, $footer);
273  
274 0 open(my $out, '>' . $self->perlio_layers, 'index' . $self->file_extension);
275  
276 0 my %data = (tag => undef, tag_description => undef);
277  
278 0 ▶ $header->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$header->fill_in("HASH", \%data, "OUTPUT", $out) or confess("Filling in the index header template failed") 0 0 0
279 or confess("Filling in the index header template failed");
280  
281 0 my $tags = $self->_tags;
282  
283 0 for my $tag (sort keys %$tags) {
284 0 my %data = (tag => $tag, description => $tags->{$tag});
285  
286 0 ▶ $template->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$template->fill_in("HASH", \%data, "OUTPUT", $out) or confess("Filling in the entry template failed for $tag") 0 0 0
287 or confess("Filling in the entry template failed for $tag");
288 }
289  
290 0 ▶ $footer->fill_in(HASH => \%data, OUTPUT => $out)
Branch: 1 branch
BranchTrueFalseTotal
$footer->fill_in("HASH", \%data, "OUTPUT", $out) or confess("Filling in the index footer template failed") 0 0 0
291 or confess("Filling in the index footer template failed");
292  
293 0 close $out;
294 } # end generate_index_page
295 #---------------------------------------------------------------------
296  
297 0 ▶sub compile_templates {
Subroutine compile_templates: not called CC=4 SCAR=30.0
Pod undocumented
298 0 my $self = shift;
299  
300 0 foreach my $string (@_) {
301 0 ▶ confess("Cannot use undef as a template string") unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
302  
303 my $tmpl = Text::Template->new(
304 TYPE => 'STRING',
305 SOURCE => $string,
306 DELIMITERS => $self->delimiters,
307 0 ▶ BROKEN => sub { my %hash = @_; die $hash{error}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
308 0 STRICT => 1,
309 );
310  
311 0 ▶ confess("Could not create a Text::Template object from:\n$string")
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpl 0 0 0
312 unless $tmpl;
313  
314 0 $string = $tmpl; # Modify arguments in-place
315 } # end for each $string in @_
316 } # end compile_templates
317  
318 #=====================================================================
319 # Package Return Value:
320  
321 1 __PACKAGE__->meta->make_immutable;
322 1 1;
323  
324 __END__
↑ Summary PluginCatalog/Entry.pm »