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   14213 use strict;
  1         2  
  1         27  
2 1     1   3 use warnings;
  1         5  
  1         39  
3              
4             package Badge::Depot;
5              
6             our $VERSION = '0.0103'; # VERSION
7             # ABSTRACT: A framework for badges
8              
9 1     1   348 use Moose::Role;
  1         302044  
  1         5  
10 1     1   4223 use Types::URI qw/Uri/;
  1         257814  
  1         9  
11 1     1   287 use Types::Standard qw/Str/;
  1         1  
  1         5  
12 1     1   812 use MooseX::AttributeShortcuts;
  1         205933  
  1         6  
13 1     1   21992 use namespace::autoclean;
  1         2  
  1         7  
14              
15             has image_url => (
16             is => 'rw',
17             isa => Uri,
18             init_arg => undef,
19             coerce => 1,
20             predicate => 1,
21             );
22             has image_alt => (
23             is => 'rw',
24             isa => Str,
25             init_arg => undef,
26             predicate => 1,
27             );
28             has link_url => (
29             is => 'rw',
30             isa => Uri,
31             init_arg => undef,
32             coerce => 1,
33             predicate => 1,
34             );
35              
36             around qw/to_html to_markdown/ => sub {
37             my $next = shift;
38             my $self = shift;
39              
40             return '' if !$self->has_image_url;
41             $self->$next;
42             };
43              
44             sub to_html {
45 4     4 1 5 my $self = shift;
46              
47 4 100       101 my $image_text = $self->has_image_alt ? sprintf ' alt="%s"', $self->image_alt : '';
48              
49 4 100       101 if($self->has_link_url) {
50 2         43 return sprintf q{<a href="%s"><img src="%s"%s /></a>}, $self->link_url, $self->image_url, $image_text;
51             }
52             else {
53 2         43 return sprintf q{<img src="%s"%s />}, $self->image_url, $image_text;
54             }
55             }
56             sub to_markdown {
57 4     4 1 5 my $self = shift;
58              
59 4 100       100 if($self->has_link_url) {
60 2   100     44 return sprintf q<[![%s](%s)](%s)>, $self->image_alt // '', $self->image_url, $self->link_url;
61             }
62             else {
63 2   100     195 return sprintf q<![%s](%s)>, $self->image_alt // '', $self->image_url;
64             }
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Badge::Depot - A framework for badges
78              
79             =head1 VERSION
80              
81             Version 0.0103, released 2015-03-19.
82              
83             =head1 SYNOPSIS
84              
85             # Define a badge class
86             package Badge::Depot::Plugin::Example;
87              
88             use Moose;
89             with 'Badge::Depot';
90              
91             has user => (
92             is => 'ro',
93             isa => 'Str',
94             required => 1,
95             );
96              
97             sub BUILD {
98             my $self = shift;
99             $self->link_url(sprintf 'https://example.com/users/%s', $self->user);
100             $self->image_url(sprintf 'https://example.com/users/%s.svg', $self->user);
101             $self->image_alt('Example text');
102             }
103              
104             # Somewhere else
105             my $badge = Badge::Depot::Plugin::Example->new(user => 'my_username');
106              
107             print $badge->to_html;
108             # prints '<a href="https://example.com/users/my_username"><img src="https://example.com/users/my_username.svg" alt="Example text" /></a>'
109              
110             =head1 DESCRIPTION
111              
112             C<Badge::Depot> is a framework for documentation badges. Using badges in your documentation can give
113             end users of your distribution dynamic information without you having to update the documentation.
114              
115             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
116             a list of existing badges.
117              
118             For use together with L<Pod::Weaver>, see L<Pod::Weaver::Section::Badges>.
119              
120             =head1 OVERVIEW
121              
122             C<Badge::Depot> is a L<Moose> role that adds a few attributes and methods.
123              
124             =head1 ATTRIBUTES
125              
126             These attributes are expected to be set when the badge class returns from C<new>. See L<synopsis|/SYNOPSIS>.
127              
128             =head2 image_url
129              
130             Required. L<Uri|Types::URI>.
131              
132             The url to the actual badge. The src attribute for the img tag when rendered to html.
133              
134             =head2 image_alt
135              
136             Optional. L<Str|Types::Standard>.
137              
138             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.
139              
140             =head2 link_url
141              
142             Optional (but recommended). L<Uri|Types::URI>.
143              
144             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.
145              
146             =head1 METHODS
147              
148             These methods are used when rendering the badge, and are not useful inside badge classes.
149              
150             =head2 to_html
151              
152             Returns a string with the badge rendered as html.
153              
154             =head2 to_markdown
155              
156             Returns a string with the badge rendered as markdown.
157              
158             =head1 SEE ALSO
159              
160             =over 4
161              
162             =item *
163              
164             L<Task::Badge::Depot> - List of available badges
165              
166             =item *
167              
168             L<Pod::Weaver::Section::Badges> - Weave the badges
169              
170             =item *
171              
172             L<WWW::StatusBadge> - An alternative badge framework
173              
174             =back
175              
176             =head1 SOURCE
177              
178             L<https://github.com/Csson/p5-Badge-Depot>
179              
180             =head1 HOMEPAGE
181              
182             L<https://metacpan.org/release/Badge-Depot>
183              
184             =head1 AUTHOR
185              
186             Erik Carlsson <info@code301.com>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut