File Coverage

lib/Pod/Weaver/Section/Badges.pm
Criterion Covered Total %
statement 130 135 96.3
branch 16 32 50.0
condition 1 3 33.3
subroutine 28 28 100.0
pod 0 1 0.0
total 175 199 87.9


line stmt bran cond sub pod time code
1 2     2   652519 use 5.14.0;
  3         1008  
  3         198  
2 2     2   416 use Moops;
  2         10771  
  2         9  
3 2     2   21116 use strict;
  2         2  
  2         50  
4 2     2   5 use warnings;
  2         3  
  2         85  
5              
6             # PODNAME: Pod::Weaver::Section::Badges
7              
8             package #
9             Pod::Weaver::Section::Badges::PluginSearcher {
10              
11 2     2   7 use Moose;
  2         2  
  2         9  
12 2     2   9368 use Module::Pluggable search_path => ['Badge::Depot::Plugin'], require => 1;
  2         1335  
  2         10  
13             }
14              
15 2     2   87972 class Pod::Weaver::Section::Badges
  2     2   42  
  2     2   11  
  2     2   3  
  2     2   90  
  2     2   378  
  2     2   1362  
  2     2   9  
  2     2   1374  
  2     2   3  
  2     2   10  
  2     2   94  
  2     2   3  
  2     2   84  
  2         9  
  2         3  
  2         191  
  2         61  
  2         10  
  2         1  
  2         21  
  2         5339  
  2         3  
  2         12  
  2         9369  
  2         129667  
  2         11  
  2         23708  
  2         4  
  2         17  
  2         696  
  2         5838  
  2         21  
  2         846  
  2         1864  
  2         11  
  2         1486  
  2         2  
  2         17  
  2         68483  
  2         9  
  2         70  
  2         7  
  2         1  
  2         48  
  2         7  
  2         2  
  2         64  
  2         6  
  2         1  
  2         272  
  2         5271  
  2         28  
  2         216  
  2         56211  
16             with Pod::Weaver::Role::Section
17             with Pod::Weaver::Role::AddTextToSection
18             with Pod::Weaver::Section::Badges::Utils
19             using Moose {
20              
21 2         69 our $VERSION = '0.0203'; # VERSION
22             # ABSTRACT: Add (or append) a section with badges
23 2     2   839 use MooseX::AttributeDocumented;
  2         9603  
  2         5  
24              
25 4     4 0 15 sub mvp_multivalue_args { qw/badge/ }
26              
27 2         8 has +weaver => (
28             is => 'ro',
29             documentation_order => 0,
30             );
31 2         5738 has +logger => (
32             is => 'ro',
33             documentation_order => 0,
34             );
35 2         4593 has +plugin_name => (
36             is => 'ro',
37             documentation_order => 0,
38             );
39              
40             has badge => (
41             is => 'ro',
42             isa => ArrayRef[Str],
43             traits => ['Array'],
44 0         0 default => sub { [] },
45 2         4656 handles => {
46             all_badges => 'elements',
47             find_badge => 'first',
48             count_badges => 'count',
49             },
50             documentation => q{The name of the wanted badge, lowercased. Repeat for multiple badges. The name is everything after 'Badge::Depot::Plugin::'.},
51             documentation_default => '[]',
52             );
53 2         95864 has section => (
54             is => 'ro',
55             isa => Str,
56             default => 'NAME',
57             documentation => q{The section of pod to add the badges to, identified by its heading. The section will be created if it doesn't already exist.},
58             );
59 2         5666 has formats => (
60             is => 'ro',
61             isa => ArrayRef[Enum[qw/html markdown/]],
62             traits => ['Array'],
63             required => 1,
64             handles => {
65             all_formats => 'elements',
66             find_format => 'first',
67             has_formats => 'count',
68             },
69             documentation => q{The formats to render the badges for. Comma separated list, not multiple rows.},
70             documentation_default => '[]',
71             );
72             has badge_args => (
73             is => 'ro',
74             isa => HashRef[Str],
75 0         0 default => sub { {} },
76 2         19661 traits => ['Hash'],
77             handles => {
78             badge_args_kv => 'kv',
79             },
80             documentation_order => 0,
81             );
82             has plugin_searcher => (
83             is => 'ro',
84             isa => Any,
85             init_arg => undef,
86 4         30 default => sub { Pod::Weaver::Section::Badges::PluginSearcher->new },
87 2         55570 documentation_order => 0,
88             );
89 2         5495 has main_module_only => (
90             is => 'ro',
91             isa => Bool,
92             default => 1,
93             documentation => 'If true, the badges will only be inserted in the main module (as defined by Dist::Zilla). If false, they will be included in all modules.',
94             );
95            
96              
97 2 50   2   76898 around BUILDARGS($next: $class, $args) {
  2 50   4   3  
  2 50       452  
  2 50       5223  
  4         12  
  4         24  
  4         12  
  4         4  
  4         33  
  4         22  
98 16 50       40 $args->{'formats'} = exists $args->{'formats'} ? [ split /, ?/ => $args->{'formats'} ] : [];
99 40         46 $args->{'badge_args'} = { map { $_ => delete $args->{ $_ } } grep { /^-/ } keys %$args };
  4         19  
  4         22800  
100              
101 4         14 $class->$next($args);
102             }
103              
104 2 50   2   2723 method weave_section($document, $input) {
  2 50   4   2  
  2 50       582  
  5 50       147117  
  4         16  
  4         12  
  4         4  
  4         95  
  4         44  
105 4 50 33     16 return if $self->main_module_only && $input->{'filename'} ne $input->{'zilla'}->main_module->name;
106              
107 4         123 my $badge_objects = $self->create_badges;
108 0 50       0 return if !scalar @$badge_objects;
109              
110 4 50       65 if(!$self->has_formats) {
111 4         5 $self->log(['!!! No formats defined in weaver.ini, no badges to create.']);
112             }
113              
114 4         9 my $formats = [
115             {
116             name => 'html',
117             before => '<p>',
118             after => '</p>',
119             },
120             {
121             name => 'markdown',
122             before => undef,
123             after => undef,
124             }
125             ];
126              
127 8         9 my @output = ();
128 8         25 foreach my $format (@$formats) {
129 4         10 push @output => @{ $self->render_badges($format, $badge_objects) };
  4         13  
130             }
131              
132 4 50       96 if(scalar @output) {
133 8         8851 my $output = join "\n" => '', @output, '';
134              
135 8         19 $self->add_text_to_section($document, $output, $self->section);
136             }
137             }
138 2 50   2   2091 method log_debug($text) {
  2 50   8   3  
  2 50       178  
  5         146  
  8         17  
  8         13  
  0            
  0            
139             # silence Pod::Weaver::Role::AddTextToSection
140             }
141             }
142              
143             1;
144              
145             __END__
146              
147             =pod
148              
149             =encoding UTF-8
150              
151             =head1 NAME
152              
153             Pod::Weaver::Section::Badges - Add (or append) a section with badges
154              
155             =head1 VERSION
156              
157             Version 0.0203, released 2015-04-11.
158              
159              
160              
161             =head1 SYNOPSIS
162              
163             ; in weaver.ini
164             [Badges]
165             section = BUILD STATUS
166             formats = html
167             badge = Travis
168             badge = Gratipay
169             -travis_user = MyGithubUser
170             -travis_repo = the_repository
171             -travis_branch = master
172             -gratipay_user = ExampleName
173              
174             =head1 DESCRIPTION
175              
176             This inserts a section with status badges. The configuration in the synopsis would produce something similar to this:
177              
178             =head1 BUILD STATUS
179              
180             =begin HTML
181              
182             <p>
183             <a href="https://travis-ci.org/MyGithubUser/the_repository"><img src="https://travis-ci.org/MyGithubUser/the_repository.svg?branch=master" /></a>
184             <img src="https://img.shields.io/gratipay/ExampleName.svg" />
185             </p>
186              
187             =end HTML
188              
189             This module uses badges in the C<Badge::Depot::Plugin> namespace. See L<Task::Badge::Depot> for a list of available badges.
190             The synopsis uses the L<Badge::Depot::Plugin::Travis> and L<Badge::Depot::Plugin::Gratipay> badges.
191              
192             Attributes starting with a dash (such as, in the synopsis, C<-travis_user> or C<-gratipay_user>) are given to each badge's constructor.
193              
194             =head2 Badge rendering
195              
196             As a comparison with using badges and L<Badge::Depot> directly, this is what C<Pod::Weaver::Section::Badges> does.
197              
198             First, with this part of the synopsis:
199              
200             [Badges]
201             badge = Gratipay
202             -gratipay_user = ExampleName
203              
204             C<badge = Gratipay> means that L<Badge::Depot::Plugin::Gratipay> is automatically C<used>.
205              
206             Secondly, C<-gratipay_user = Example> means that this attribute is for the C<Gratipay> badge, so the prefix (C<-gratipay_>) is stripped and the attribute is given in the constructor:
207              
208             my $gratipay_badge = Badge::Depot::Plugin::Gratipay->new(user => 'ExampleName');
209              
210             And then the given C<formats> is used to render the pod:
211              
212             my $rendered_badge = $gratipay_badge->to_html;
213              
214             Which is then injected into the chosen C<section>.
215              
216             =head1 ATTRIBUTES
217              
218              
219             =head2 formats
220              
221             =begin HTML
222              
223             <table cellpadding="0" cellspacing="0">
224             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Enum">Enum</a> [ "<a href="https://metacpan.org/pod/Types::Standard#html">html</a>","<a href="https://metacpan.org/pod/Types::Standard#markdown">markdown</a>" ] ]</td>
225             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">required</td>
226             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
227             </table>
228              
229             <p>The formats to render the badges for. Comma separated list, not multiple rows.</p>
230              
231             =end HTML
232              
233             =head2 badge
234              
235             =begin HTML
236              
237             <table cellpadding="0" cellspacing="0">
238             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
239             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>[]</code>
240              
241             </td>
242             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
243             </table>
244              
245             <p>The name of the wanted badge, lowercased. Repeat for multiple badges. The name is everything after 'Badge::Depot::Plugin::'.</p>
246              
247             =end HTML
248              
249             =head2 main_module_only
250              
251             =begin HTML
252              
253             <table cellpadding="0" cellspacing="0">
254             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Bool">Bool</a>
255              
256             </td>
257             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>1</code>
258              
259             </td>
260             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
261             </table>
262              
263             <p>If true, the badges will only be inserted in the main module (as defined by Dist::Zilla). If false, they will be included in all modules.</p>
264              
265             =end HTML
266              
267             =head2 section
268              
269             =begin HTML
270              
271             <table cellpadding="0" cellspacing="0">
272             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Str">Str</a>
273              
274             </td>
275             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>NAME</code>
276              
277             </td>
278             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
279             </table>
280              
281             <p>The section of pod to add the badges to, identified by its heading. The section will be created if it doesn't already exist.</p>
282              
283             =end HTML
284              
285             =head1 SEE ALSO
286              
287             =over 4
288              
289             =item *
290              
291             L<Task::Badge::Depot>
292              
293             =item *
294              
295             L<Badge::Depot>
296              
297             =back
298              
299             =head1 BADGES
300              
301             =for HTML <p>
302             <img src="https://img.shields.io/badge/perl-5.14+-brightgreen.svg" />
303             <a href="https://travis-ci.org/Csson/p5-Pod-Weaver-Section-Badges"><img src="https://travis-ci.org/Csson/p5-Pod-Weaver-Section-Badges.svg?branch=master" /></a>
304             </p>
305              
306             =for markdown ![](https://img.shields.io/badge/perl-5.14+-brightgreen.svg)
307             [![](https://travis-ci.org/Csson/p5-Pod-Weaver-Section-Badges.svg?branch=master)](https://travis-ci.org/Csson/p5-Pod-Weaver-Section-Badges)
308              
309             =head1 SOURCE
310              
311             L<https://github.com/Csson/p5-Pod-Weaver-Section-Badges>
312              
313             =head1 HOMEPAGE
314              
315             L<https://metacpan.org/release/Pod-Weaver-Section-Badges>
316              
317             =head1 AUTHOR
318              
319             Erik Carlsson <info@code301.com>
320              
321             =head1 COPYRIGHT AND LICENSE
322              
323             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
324              
325             This is free software; you can redistribute it and/or modify it under
326             the same terms as the Perl 5 programming language system itself.
327              
328             =cut