File Coverage

blib/lib/Badge/Depot.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1 1     1   13940 use strict;
  1         1  
  1         24  
2 1     1   4 use warnings;
  1         0  
  1         48  
3              
4             package Badge::Depot;
5              
6             # ABSTRACT: A framework for badges
7             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8             our $VERSION = '0.0104';
9              
10 1     1   371 use Moose::Role;
  1         313063  
  1         4  
11 1     1   4013 use Types::URI qw/Uri/;
  1         132464  
  1         11  
12 1     1   290 use Types::Standard qw/Str InstanceOf/;
  1         1  
  1         6  
13 1     1   889 use MooseX::AttributeShortcuts;
  1         228969  
  1         4  
14 1     1   23447 use namespace::autoclean;
  1         1  
  1         7  
15              
16             has image_url => (
17             is => 'rw',
18             isa => Uri,
19             init_arg => undef,
20             coerce => 1,
21             predicate => 1,
22             );
23             has image_alt => (
24             is => 'rw',
25             isa => Str,
26             init_arg => undef,
27             predicate => 1,
28             );
29             has link_url => (
30             is => 'rw',
31             isa => Uri,
32             init_arg => undef,
33             coerce => 1,
34             predicate => 1,
35             );
36             has zilla => (
37             is => 'ro',
38             isa => InstanceOf['Dist::Zilla'],
39             predicate => 1,
40             );
41              
42              
43             around qw/to_html to_markdown/ => sub {
44             my $next = shift;
45             my $self = shift;
46              
47             return '' if !$self->has_image_url;
48             $self->$next;
49             };
50              
51             sub to_html {
52 4     4 1 3 my $self = shift;
53              
54 4 100       103 my $image_text = $self->has_image_alt ? sprintf ' alt="%s"', $self->image_alt : '';
55              
56 4 100       101 if($self->has_link_url) {
57 2         44 return sprintf q{<a href="%s"><img src="%s"%s /></a>}, $self->link_url, $self->image_url, $image_text;
58             }
59             else {
60 2         45 return sprintf q{<img src="%s"%s />}, $self->image_url, $image_text;
61             }
62             }
63             sub to_markdown {
64 4     4 1 5 my $self = shift;
65              
66 4 100       101 if($self->has_link_url) {
67 2   100     56 return sprintf q<[![%s](%s)](%s)>, $self->image_alt // '', $self->image_url, $self->link_url;
68             }
69             else {
70 2   100     45 return sprintf q<![%s](%s)>, $self->image_alt // '', $self->image_url;
71             }
72             }
73              
74             1;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             Badge::Depot - A framework for badges
85              
86              
87              
88             =begin html
89              
90             <p>
91             <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
92             <a href="https://travis-ci.org/Csson/p5-Badge-Depot"><img src="https://api.travis-ci.org/Csson/p5-Badge-Depot.svg?branch=master" alt="Travis status" /></a>
93             <a href="http://cpants.cpanauthors.org/dist/Badge-Depot-0.0104"><img src="https://badgedepot.code301.com/badge/kwalitee/Badge-Depot/0.0104" alt="Distribution kwalitee" /></a>
94             <a href="http://matrix.cpantesters.org/?dist=Badge-Depot%200.0104"><img src="https://badgedepot.code301.com/badge/cpantesters/Badge-Depot/0.0104" alt="CPAN Testers result" /></a>
95             <img src="https://img.shields.io/badge/coverage-100.0%-brightgreen.svg" alt="coverage 100.0%" />
96             </p>
97              
98             =end html
99              
100             =head1 VERSION
101              
102             Version 0.0104, released 2016-02-19.
103              
104             =head1 SYNOPSIS
105              
106             # Define a badge class
107             package Badge::Depot::Plugin::Example;
108              
109             use Moose;
110             with 'Badge::Depot';
111              
112             has user => (
113             is => 'ro',
114             isa => 'Str',
115             required => 1,
116             );
117              
118             sub BUILD {
119             my $self = shift;
120             $self->link_url(sprintf 'https://example.com/users/%s', $self->user);
121             $self->image_url(sprintf 'https://example.com/users/%s.svg', $self->user);
122             $self->image_alt('Example text');
123             }
124              
125             # Somewhere else
126             my $badge = Badge::Depot::Plugin::Example->new(user => 'my_username');
127              
128             print $badge->to_html;
129             # prints '<a href="https://example.com/users/my_username"><img src="https://example.com/users/my_username.svg" alt="Example text" /></a>'
130              
131             =head1 DESCRIPTION
132              
133             C<Badge::Depot> is a framework for documentation badges. Using badges in your documentation can give
134             end users of your distribution dynamic information without you having to update the documentation.
135              
136             You only need use this distribution directly if you want to create a new badge in the C<Badge::Depot::Plugin> namespace. See L<Task::Badge::Depot> for
137             a list of existing badges.
138              
139             For use together with L<Pod::Weaver>, see L<Pod::Weaver::Section::Badges>.
140              
141             =head1 OVERVIEW
142              
143             C<Badge::Depot> is a L<Moose> role that adds a few attributes and methods.
144              
145             =head1 ATTRIBUTES
146              
147             These attributes are expected to be set when the badge class returns from C<new>. See L<synopsis|/SYNOPSIS>.
148              
149             =head2 image_url
150              
151             Required. L<Uri|Types::URI>.
152              
153             The url to the actual badge. The src attribute for the img tag when rendered to html.
154              
155             =head2 image_alt
156              
157             Optional. L<Str|Types::Standard>.
158              
159             The alternative text of the badge. The alt attribute for the img tag when rendered to html. No alternative text is created if this isn't set.
160              
161             =head2 link_url
162              
163             Optional (but recommended). L<Uri|Types::URI>.
164              
165             The url to link the badge to. The href attribute for the a tag when rendered to html. No link is created if this isn't set.
166              
167             =head2 zilla
168              
169             This is not a user setable attribute, but rather can be used by plugins that might need to look at distribution meta data. This is only usable when using L<Pod::Weaver::Section::Badges> together with L<Dist::Zilla>.
170              
171             =head1 METHODS
172              
173             These methods are used when rendering the badge, and are not useful inside badge classes.
174              
175             =head2 to_html
176              
177             Returns a string with the badge rendered as html.
178              
179             =head2 to_markdown
180              
181             Returns a string with the badge rendered as markdown.
182              
183             =head1 SEE ALSO
184              
185             =over 4
186              
187             =item *
188              
189             L<Task::Badge::Depot> - List of available badges
190              
191             =item *
192              
193             L<Pod::Weaver::Section::Badges> - Weave the badges
194              
195             =item *
196              
197             L<WWW::StatusBadge> - An alternative badge framework
198              
199             =back
200              
201             =head1 SOURCE
202              
203             L<https://github.com/Csson/p5-Badge-Depot>
204              
205             =head1 HOMEPAGE
206              
207             L<https://metacpan.org/release/Badge-Depot>
208              
209             =head1 AUTHOR
210              
211             Erik Carlsson <info@code301.com>
212              
213             =head1 COPYRIGHT AND LICENSE
214              
215             This software is copyright (c) 2016 by Erik Carlsson.
216              
217             This is free software; you can redistribute it and/or modify it under
218             the same terms as the Perl 5 programming language system itself.
219              
220             =cut