File Coverage

lib/Pod/Elemental/Transformer/Splint/MethodRenderer/HtmlDefault.pm
Criterion Covered Total %
statement 130 130 100.0
branch 44 62 70.9
condition 7 11 63.6
subroutine 17 17 100.0
pod 0 8 0.0
total 198 228 86.8


line stmt bran cond sub pod time code
1 3     3   2180 use 5.14.0;
  3         12  
2 3     3   17 use strict;
  3         6  
  3         80  
3 3     3   18 use warnings;
  3         5  
  3         272  
4              
5             package Pod::Elemental::Transformer::Splint::MethodRenderer::HtmlDefault;
6              
7             our $VERSION = '0.1100'; # VERSION
8             # ABSTRACT: Default html method renderer
9              
10 3     3   16 use Moose;
  3         6  
  3         21  
11 3     3   19534 use Path::Tiny;
  3         7  
  3         178  
12 3     3   16 use Pod::Simple::XHTML;
  3         7  
  3         220  
13 3     3   2625 use List::AllUtils qw/any uniq/;
  3         14039  
  3         338  
14 3     3   18 use syntax 'qs';
  3         5  
  3         23  
15              
16             with 'Pod::Elemental::Transformer::Splint::MethodRenderer';
17              
18             sub render_method {
19 4     4 0 7 my $self = shift;
20 4         6 my $data = shift;
21              
22 4         8 my $positional_params = $data->{'positional_params'};
23 4         8 my $named_params = $data->{'named_params'};
24 4         6 my $return_types = $data->{'return_types'};
25              
26 4         10 my @html = ('');
27 4         6 my $table_style = q{style="margin-bottom: 10px; margin-left: 10px; border-collapse: bollapse;" cellpadding="0" cellspacing="0"};
28 4         6 my $th_style = q{style="text-align: left; color: #444; padding-left: 5px; font-weight: bold; background-color:};
29 4         5 my $tr_style = q{style="vertical-align: top;"};
30              
31 4         8 my $method_doc = undef;
32              
33 4         15 my $colspan = $self->get_colspan([ @$positional_params, @$named_params, @$return_types]);
34              
35 4 50       29 if(scalar @$positional_params) {
36              
37 4         17 my @fake_colspans = (qq{ <td $th_style #eee8e8;">&#160;</td>}) x ($colspan - 1);
38 4         17 push @html => (qq{<tr $tr_style>}, qq{ <td $th_style #eee8e8;">Positional parameters</td>}, @fake_colspans, '</tr>');
39              
40 4         9 foreach my $param (@$positional_params) {
41              
42 8 50       21 $method_doc = $param->{'method_doc'} if defined $param->{'method_doc'};
43              
44 8         17 push @html => "<tr $tr_style>";
45 8         47 push @html => $self->make_cell_with_border(nowrap => 1, text => $self->parse_pod(sprintf 'C<%s>', $param->{'name'}));
46 8         22 push @html => $self->make_cell_with_border(nowrap => 2, text => $param->{'type'});
47 8 100       45 push @html => $self->make_cell_with_border(nowrap => 3, text => (join ', ' => $param->{'required_text'}, $param->{'is_required'} ? defined $param->{'default'} ? $self->param_default_text($param) : () : $self->param_default_text($param)));
    50          
48 8         24 push @html => $self->make_cell_with_border(nowrap => 4, text => $self->param_trait_text($param));
49 8         17 push @html => $self->make_cell_without_border(nowrap => 0, text => join '' => map { "$_<br />" } @{ $param->{'docs'} });
  8         30  
  8         17  
50              
51 8         21 push @html => '</tr>';
52             }
53             }
54 4 100       13 if(scalar @$named_params) {
55              
56 2         8 my @fake_colspans = (qq{ <td $th_style #e8eee8;">&#160;</td>}) x ($colspan - 1);
57 2         12 push @html => (qq{<tr $tr_style>}, qq{ <td $th_style #e8eee8;">Named parameters</td>}, @fake_colspans, '</tr>');
58              
59 2         5 foreach my $param (@$named_params) {
60 6 100       19 $method_doc = $param->{'method_doc'} if defined $param->{'method_doc'};
61              
62 6         14 push @html => "<tr $tr_style>";
63 6         31 push @html => $self->make_cell_with_border(nowrap => 5, text => $self->parse_pod(sprintf 'C<%s =E<gt> %s>', $param->{'name_without_sigil'}, '$value'));
64 6         18 push @html => $self->make_cell_with_border(nowrap => 6, text => $param->{'type'});
65 6 50 33     39 push @html => $self->make_cell_with_border(nowrap => 7, text => join ', ' => $param->{'required_text'}, $param->{'is_required'} && defined $param->{'default'} ? $self->param_default_text($param) : $param->{'is_required'} ? () : $self->param_default_text($param));
    50          
66 6         19 push @html => $self->make_cell_with_border(nowrap => 8, text => $self->param_trait_text($param));
67 6         26 push @html => $self->make_cell_without_border(nowrap => 0, text => join '<br />' => @{ $param->{'docs'} });
  6         23  
68              
69 6         15 push @html => '</tr>';
70             }
71             }
72 4 100       11 if(scalar @$return_types) {
73 2         10 my @fake_colspans = (qq{ <td $th_style #e8e8ee;">&#160;</td>}) x ($colspan - 1);
74 2         8 push @html => (qq{<tr $tr_style>}, qq{ <td $th_style #e8e8ee;">Returns</td>}, @fake_colspans, '</tr>');
75              
76 2         6 foreach my $return_type (@$return_types) {
77 2 50       7 $method_doc = $return_type->{'method_doc'} if defined $return_type->{'method_doc'};
78 2         3 my $has_doc = scalar @{ $return_type->{'docs'} };
  2         15  
79 2 50       6 my $return_colspan = $has_doc ? $colspan - 1 : $colspan;
80              
81 2         4 push @html => qq{<tr $tr_style>};
82             push @html => $has_doc ? $self->make_cell_with_border(nowrap => 0, colspan => $return_colspan, text => $return_type->{'type'})
83 2 50       10 : $self->make_cell_without_border(nowrap => 0, colspan => $return_colspan, text => $return_type->{'type'})
84             ;
85 2         6 push @html => $self->make_cell_without_border(nowrap => 0, text => join '<br />' => @{ $return_type->{'docs'} });
  2         9  
86 2         8 push @html => '</tr>';
87             }
88             }
89 4 50       9 if(scalar @html) {
90 4         14 unshift @html => '<!-- -->', qq{<table $table_style>};
91 4         8 push @html => '</table>';
92             }
93              
94 4   100     16 my $content = sprintf qs{
95             =begin %s
96 4         126  
97             <p>%s</p>
98              
99             %s
100 4     4 0 6  
101 4         6 =end %s
102             }, $self->for, $method_doc // '', join ("\n" => @html), $self->for;
103 4 50   16   25  
  4 50       15  
  4 50       29  
  16         37  
104             return $content;
105             }
106              
107             sub get_colspan {
108             my $self = shift;
109             my $params = shift;
110 14     14 0 19  
111 14         17 return (any { defined $_->{'docs'} && scalar @{ $_->{'docs'} } } @$params) ? (any { ref $_->{'docs'} eq 'HASH' } @$params) ? 6
112             : 5
113 14 100       16 : 4
  22         79  
  14         29  
114             ;
115 14 100       60 }
116 2 50       3  
  2         11  
117             sub param_trait_text {
118             my $self = shift;
119             my $param = shift;
120 14     14 0 18  
121 14         17 my @traits = grep { $_ ne 'doc' && $_ ne 'optional' } @{ $self->param_trait_list($param) };
122              
123 14         20 return undef if !scalar @traits;
  14         17  
  14         98  
  14         32  
124             return join ', ' => map { $_ eq 'slurpy' ? $_ : sprintf '<a href="https://metacpan.org/pod/Kavorka/TraitFor/Parameter/%s">$_</a>', $_ } @traits;
125 14         43 }
126              
127             sub param_trait_list {
128             my $self = shift;
129             my $param = shift;
130 8     8 0 13  
131 8         10 my $trait_list = [ uniq sort map { keys %{ $_ } } @{ $param->{'traits'} } ];
132              
133 8 100       28 return $trait_list;
134 6 100 66     19  
  2         19  
135 4 50       11 }
136              
137 4 50 66     25 sub param_default_text {
  2         10  
138 4 100       25 my $self = shift;
139             my $param = shift;
140 2 50       6  
141 2 50       15 return q{<span style="color: #999;">no default</span>} if !defined $param->{'default'};
142             return $self->parse_pod(sprintf q(default C<%s { }>), $param->{'default_when'}) if ref $param->{'default'} eq 'HASH' && scalar keys %{ $param->{'default'} } == 0;
143             return $self->parse_pod(sprintf q(default C<%s hashref>), $param->{'default_when'}) if ref $param->{'default'} eq 'HASH';
144              
145 16     16 0 22 return $self->parse_pod(sprintf q(default C<%s [ ]>), $param->{'default_when'}) if ref $param->{'default'} eq 'ARRAY' && scalar @{ $param->{'default'} } == 0;
146 16         34 return $self->parse_pod(sprintf q(default C<%s arrayref>), $param->{'default_when'}) if ref $param->{'default'} eq 'ARRAY';
147 16 50       31  
148             return $self->parse_pod(sprintf q{default C<%s coderef>}, $param->{'default_when'}) if ref $param->{'default'} eq 'CODE';
149 16         32 return $self->parse_pod(sprintf q{default C<%s %s>}, $param->{'default_when'}, $param->{'default'} eq '' ? "''" : $param->{'default'});
150 16         28 }
151              
152 16         51 sub make_cell_without_border {
153             my $self = shift;
154             my($text, $nowrap, $colspan) = $self->fix_cell_args(@_);
155 58     58 0 86 $text = defined $text ? $text : '';
156              
157 58         124 my $style = qq{style="padding: 3px 6px; vertical-align: top; $nowrap border-bottom: 1px solid #eee;"};
158 58 100       117 my @colspans = (qq{ <td $style>&#160;</td>}) x $colspan;
159 58 100       103  
160             return (qq{ <td $style>$text</td>}, @colspans);
161 58         122 }
162 58         112 sub make_cell_with_border {
163             my $self = shift;
164 58         214  
165             my($text, $nowrap, $colspan) = $self->fix_cell_args(@_);
166             my $padding = defined $text ? ' padding: 3px 6px;' : '';
167             $text = defined $text ? $text : '';
168 74     74 0 84  
169 74         177 my $style = qq{style="vertical-align: top; border-right: 1px solid #eee;$nowrap $padding border-bottom: 1px solid #eee;"};
170             my @colspans = (qq{ <td $style>&#160;</td>}) x $colspan;
171 74         114  
172 74 100       156 return (qq{ <td $style>$text</td>}, @colspans);
173 74 100       145 }
174              
175 74         202 sub fix_cell_args {
176             my $self = shift;
177             my %args = @_;
178              
179             my $text = $args{'text'};
180             my $nowrap = !$args{'nowrap'} ? '' : ' white-space: nowrap;';
181             my $colspan = !exists $args{'colspan'} ? 0 : $args{'colspan'} - 1; # Since we add cells *after* the current one.
182              
183             return ($text, $nowrap, $colspan);
184             }
185              
186             1;
187              
188             __END__
189              
190             =pod
191              
192             =encoding UTF-8
193              
194             =head1 NAME
195              
196             Pod::Elemental::Transformer::Splint::MethodRenderer::HtmlDefault - Default html method renderer
197              
198             =head1 VERSION
199              
200             Version 0.1100, released 2016-01-12.
201              
202             =head1 SOURCE
203              
204             L<https://github.com/Csson/p5-Pod-Elemental-Transformer-Splint>
205              
206             =head1 HOMEPAGE
207              
208             L<https://metacpan.org/release/Pod-Elemental-Transformer-Splint>
209              
210             =head1 AUTHOR
211              
212             Erik Carlsson <info@code301.com>
213              
214             =head1 COPYRIGHT AND LICENSE
215              
216             This software is copyright (c) 2016 by Erik Carlsson.
217              
218             This is free software; you can redistribute it and/or modify it under
219             the same terms as the Perl 5 programming language system itself.
220              
221             =cut