File Coverage

blib/lib/Swim/Plugin/cpan.pm
Criterion Covered Total %
statement 84 93 90.3
branch 20 42 47.6
condition 1 3 33.3
subroutine 10 11 90.9
pod 0 9 0.0
total 115 158 72.7


line stmt bran cond sub pod time code
1 2     2   48645 use strict; use warnings;
  2     2   5  
  2         73  
  2         8  
  2         2  
  2         2157  
2             package Swim::Plugin::cpan;
3             our $VERSION = '0.0.6';
4              
5             package Swim::Pod;
6              
7             sub block_func_cpan_head {
8 1     1 0 22 my ($self, $args) = @_;
9 1         5 $self->create_output($args, [ qw(name badge version) ]);
10             }
11              
12             sub block_func_cpan_tail {
13 1     1 0 52786 my ($self, $args) = @_;
14 1         6 my $meta = $self->meta;
15 1 50       10 $meta->{author} = [ $meta->{author} ]
16             unless ref($meta->{author}) eq 'ARRAY';
17 1   33     9 $meta->{author}[0]{copyright} ||= $meta->{copyright};
18 1         4 $self->create_output($args, [ qw(author copyright) ]);
19             }
20              
21             sub create_output {
22 2     2 0 4 my ($self, $args, $sections) = @_;
23              
24 2         9 my @args = grep $_, split /\s+/, $args;
25 2 50       7 if (@args) {
26 2 50       9 if (not grep /^[-+]/, @args) {
27 0         0 @$sections = @args;
28             }
29             else {
30 2         5 for my $arg (@args) {
31 2 100       7 if ($arg =~ /^-(.*)/) {
32 1         2 @$sections = grep { $_ ne $1 } @$sections;
  3         7  
33             }
34             else {
35 1         4 $arg =~ s/^\+//;
36 1         3 @$sections = grep { $_ ne $arg } @$sections;
  2         5  
37 1 50       5 if ($arg =~ /^(see)/) {
38 1         4 unshift @$sections, $arg;
39             }
40             else {
41 0         0 push @$sections, $arg;
42             }
43             }
44             }
45             }
46             }
47              
48 2         3 my @output;
49 2         4 for my $section (@$sections) {
50 5         8 my $method = "add_$section";
51 5 50       14 my $output = $self->$method or next;
52 5         9 push @output, $output;
53             }
54 2         8 return join "\n", @output;
55             }
56              
57             sub add_name {
58 1     1 0 2 my ($self) = @_;
59 1         5 my $meta = $self->meta;
60 1         14 my $uc = $self->option->{'pod-upper-head'};
61 1 50       6 my $head_name = $uc ? 'NAME' : 'Name';
62 1         3 (my $name = $meta->{name}) =~ s/-/::/g;
63 1         5 return <<"...";
64             =head1 $head_name
65              
66             $name - $meta->{abstract}
67             ...
68             }
69              
70             sub add_badge {
71 1     1 0 1 my ($self) = @_;
72 1         3 my $meta = $self->meta;
73 1         3 my $out = '';
74 1         2 while (1) {
75 1 50       6 my $badge = $self->{meta}{badge} or return;
76 1 50       3 $badge = [$badge] unless ref $badge;
77 1 50       4 my $repo = $meta->{devel}{git} or return;
78 1 50       12 $repo =~ s!.*[:/]([^/]+)/([^/]+?)(?:\.git)?$!$1/$2!
79             or return;
80 1 50       52 eval "require Swim::Plugin::badge; 1" or return;
81 1         6 $out .= "\n" . $self->phrase_func_badge("@$badge $repo");
82 1         29 $out =~ s/\n+\z/\n/;
83 1         2 $out =~ s/\A\n+//;
84 1         4 return $out;
85             }
86             }
87              
88             sub add_version {
89 0     0 0 0 my ($self) = @_;
90 0         0 my $uc = $self->option->{'pod-upper-head'};
91 0 0       0 my $head_version = $uc ? 'VERSION' : 'Version';
92 0         0 my $meta = $self->meta;
93 0         0 (my $name = $meta->{name}) =~ s/-/::/g;
94 0         0 return <<"...";
95             =head1 $head_version
96              
97             This document describes L<$name> version B<$meta->{version}>.\n";
98             ...
99             }
100              
101             sub add_see {
102 1     1 0 2 my ($self, $args) = @_;
103 1         4 my $meta = $self->meta;
104 1         6 my $uc = $self->option->{'pod-upper-head'};
105 1 50       25 my $head_see = $uc ? 'SEE ALSO' : 'See Also';
106 1         2 my $out = '';
107 1 50       4 if (my $see = $meta->{see}) {
108 1         3 $out .= "=head1 $head_see\n\n=over\n\n";
109 1 50       4 $see = [$see] unless ref $see;
110 1         3 for (@$see) {
111 2         4 $out .= "=item * L<$_>\n\n";
112             }
113 1         2 $out .= "=back\n";
114             }
115 1         4 return $out;
116             }
117              
118             sub add_author {
119 1     1 0 2 my ($self) = @_;
120 1         3 my $meta = $self->meta;
121 1         5 my $uc = $self->option->{'pod-upper-head'};
122 1 50       6 my $head_author = $uc ? 'AUTHOR' : 'Author';
123 1         2 my $authors = $meta->{author};
124 1 50       4 if (@$authors > 1) {
125 0 0       0 $head_author = $uc ? 'AUTHORS' : 'Authors';
126             }
127 1         3 my $out .= "=head1 $head_author\n\n";
128 1         2 for my $author (@$authors) {
129 1         5 $out .= "$author->{name} <$author->{email}>\n\n";
130             }
131 1         2 chomp $out;
132 1         2 return $out;
133             }
134              
135             sub add_copyright {
136 1     1 0 2 my ($self, $args) = @_;
137 1         3 my $meta = $self->meta;
138 1         6 my $uc = $self->option->{'pod-upper-head'};
139 1 50       6 my $head_copyright = $uc
140             ? 'COPYRIGHT AND LICENSE'
141             : 'Copyright and License';
142 1         3 my $authors = $meta->{author};
143 1         2 my $out = "=head1 $head_copyright\n\n";
144 1         3 for my $author (@$authors) {
145 1 50       4 if ($author->{copyright}) {
146 1         5 $out .= "Copyright $author->{copyright}. $author->{name}.\n\n";
147             }
148             }
149              
150 1         4 return $out . <<'...';
151             This program is free software; you can redistribute it and/or modify it under
152             the same terms as Perl itself.
153              
154             See L
155             ...
156             }
157              
158             1;