blib/lib/Inky.pm

filter: Statementstmt 98.7% 159 / 161 Branchbran 95.6% 44 / 46 Conditioncond 81.5% 31 / 38 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 96.6% 29 / 30 Podpod 100% 1 / 1 totaltotal 92.2% 275 / 298 CC 40 SCAR 16.9
CC
40
Cov
96%
_make_column
14.0
__ANON__
8.0
_make_button
6.0
CRAP 5.4
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Inky;
4 3 $Inky::VERSION = '0.161810';
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # ABSTRACT: Inky templates, in Perl
12  
13 3 const my $DEFAULT_SPACER_SIZE_PX => 16;
14 3 const my $DEFAULT_COLS => 12;
15  
16 42 ▶has 'column_count' => ( is => 'ro', default => sub { $DEFAULT_COLS } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17 42 ▶has '_component_tags' => ( is => 'ro', default => sub { return [qw<
Subroutine __ANON__: called CC=1 SCAR=0.0
18 button row columns container callout inky block-grid menu item center
19 spacer wrapper
20 3 >]});
21  
22 33 ▶sub _classes {
Subroutine _classes: called CC=2 SCAR=6.9
23 33 my ($element, @classes) = @_;
24 33 ▶ if ($element->attr('class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->attr("class")) 9 24 33
25 9 push @classes, split /\s+/xms, $element->attr('class');
26 }
27 33 return join q{ }, @classes;
28 }
29  
30 36 ▶sub _add_standard_attributes {
Subroutine _add_standard_attributes: called CC=4 SCAR=13.9
31 36 my ($element) = @_;
32  
33 # Keep all attributes but these
34 36 my %skipped = map { $_ => 1 } qw<class id href size large no-expander small target>;
35 36 my $result = '';
36 36 my $attrs = $element->attr;
37  
38 36 for my $attr (sort keys %{$attrs}) {
39 40 ▶ next if exists $skipped{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $skipped{$attr} 35 5 40
40 5 ▶ my $value = $attrs->{$attr} // '';
Condition50%1/2
$attrs->{$attr} // ""
l!l
50
MC/DC0%0/1
$attrs->{$attr} // ""
$attrs->{$attr}
Truth table50%1/2
A
A= $attrs->{$attr}
Aresult
11
00
41 5 $result .= qq! $attr="$value"!;
42 }
43 36 return $result;
44 }
45  
46 my %COMPONENTS = (
47 16 ▶ columns => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 16 my ($self, $element) = @_;
49 16 return $self->_make_column($element, 'columns');
50 },
51 4 ▶ row => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 4 my ($self, $element, $inner) = @_;
53 4 return sprintf '<table %s class="%s"><tbody><tr>%s</tr></tbody></table>',
54 _add_standard_attributes($element),
55 _classes($element, 'row'), $inner;
56 },
57 button => \&_make_button,
58 5 ▶ container => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 5 my ($self, $element, $inner) = @_;
60 5 return sprintf '<table %s align="center" class="%s"><tbody><tr><td>%s</td></tr></tbody></table>',
61 _add_standard_attributes($element),
62 _classes($element, 'container'), $inner;
63 },
64 0 ▶ inky => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 return '<tr><td><img src="https://raw.githubusercontent.com/arvida/emoji-cheat-sheet.com/master/public/graphics/emojis/octopus.png" /></tr></td>';
66 },
67 2 ▶ 'block-grid' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 2 my ($self, $element, $inner) = @_;
69 2 return sprintf '<table class="%s"><tr>%s</tr></table>',
70 _classes($element, 'block-grid', join q{}, 'up-', $element->attr('up')),
71 $inner;
72 },
73 5 ▶ menu => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 5 my ($self, $element, $inner) = @_;
75 5 ▶ my $center_attr = $element->attr('align') ? 'align="center"' : q{};
Branch: 1 branch
BranchTrueFalseTotal
$element->attr('align') ? : 1 4 5
76 5 return sprintf '<table %s class="%s"%s><tr><td><table><tr>%s</tr></table></td></tr></table>',
77 _add_standard_attributes($element),
78 _classes($element, 'menu'), $center_attr, $inner;
79 },
80 3 ▶ item => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
81 3 my ($self, $element, $inner) = @_;
82 3 my $target = '';
83 3 ▶ $target = sprintf ' target="%s"', $element->attr('target')
Branch: 1 branch
BranchTrueFalseTotal
if $element->attr("target") 1 2 3
84 if $element->attr('target');
85 3 return sprintf '<th %s class="%s"><a href="%s"%s>%s</a></th>',
86 _add_standard_attributes($element),
87 _classes($element, 'menu-item'), $element->attr('href'), $target, $inner;
88 },
89 center => \&_make_center,
90 2 ▶ callout => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 2 my ($self, $element, $inner) = @_;
92 2 return sprintf '<table %s class="callout"><tr><td class="%s">%s</td><td class="expander"></td></tr></table>',
93 _add_standard_attributes($element),
94 _classes($element, 'callout-inner'), $inner;
95 },
96 6 ▶ spacer => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
97 6 my ($self, $element, $inner) = @_;
98 6 my $size;
99 6 my $html = '';
100 6 ▶ if ($element->attr('size-sm') || $element->attr('size-lg')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->attr('size-sm') or $element->attr('size-lg')) { } 3 3 6
Condition100%3/3
$element->attr("size-sm") or $element->attr("size-lg")
l!l&&r!l&&!r
213
MC/DC100%2/2
$element->attr("size-sm") or $element->attr("size-lg")
$element->attr("size-sm") $element->attr("size-lg")
Truth table100%3/3
A or B
A= $element->attr("size-sm")B= $element->attr("size-lg")
ABresult
1X1
011
000
101 3 ▶ if ($element->attr('size-sm')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->attr("size-sm")) 2 1 3
102 2 $size = $element->attr('size-sm');
103 2 $html .= qq!<table class="%s hide-for-large"><tbody><tr><td height="${size}px" style="font-size:${size}px;line-height:${size}px;">&nbsp;</td></tr></tbody></table>!;
104 }
105 3 ▶ if ($element->attr('size-lg')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->attr("size-lg")) 2 1 3
106 2 $size = $element->attr('size-lg');
107 2 $html .= qq!<table class="%s show-for-large"><tbody><tr><td height="${size}px" style="font-size:${size}px;line-height:${size}px;">&nbsp;</td></tr></tbody></table>!;
108 }
109 } else {
110 3 ▶ $size = $element->attr('size') // $DEFAULT_SPACER_SIZE_PX;
Condition66%2/3
$element->attr("size") // $DEFAULT_SPACER_SIZE_PX
l!l&&r!l&&!r
210
MC/DC0%0/2
$element->attr("size") // $DEFAULT_SPACER_SIZE_PX
$element->attr("size") $DEFAULT_SPACER_SIZE_PX
Truth table66%2/3
A // B
A= $element->attr("size")B= $DEFAULT_SPACER_SIZE_PX
ABresult
1X1
011
000
111 3 $html = qq!<table class="%s"><tbody><tr><td height="${size}px" style="font-size:${size}px;line-height:${size}px;">&nbsp;</td></tr></tbody></table>!;
112 }
113 6 ▶ if ($element->attr('size-sm') && $element->attr('size-lg')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->attr("size-sm") and $element->attr("size-lg")) 1 5 6
Condition100%3/3
$element->attr("size-sm") and $element->attr("size-lg")
!ll&&!rl&&r
411
MC/DC100%2/2
$element->attr("size-sm") and $element->attr("size-lg")
$element->attr("size-sm") $element->attr("size-lg")
Truth table100%3/3
A and B
A= $element->attr("size-sm")B= $element->attr("size-lg")
ABresult
0X0
100
111
114 1 return sprintf $html,
115 _classes($element,'spacer'),
116 _classes($element,'spacer'),
117 }
118 5 return sprintf $html,
119 _classes($element,'spacer');
120 },
121 1 ▶ wrapper => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 my ($self, $element, $inner) = @_;
123 1 return sprintf '<table %s class="%s" align="center"><tr><td class="wrapper-inner">%s</td></tr></table>',
124 _add_standard_attributes($element),
125 _classes($element, 'wrapper'), $inner;
126 },
127 3 );
128  
129 4 ▶sub _make_button {
Subroutine _make_button: called CC=6 SCAR=18.0
130 4 my ($self, $element, $inner) = @_;
131  
132 4 my $expander = q{};
133  
134 # Prepare optional target attribute for the <a> element
135 4 my $target = '';
136 4 ▶ $target = ' target=' . $element->attr('target')
Branch: 1 branch
BranchTrueFalseTotal
if $element->attr("target") 1 3 4
137 if $element->attr('target');
138  
139 # If we have the href attribute we can create an anchor for the inner
140 # of the button
141 4 ▶ $inner = sprintf '<a href="%s"%s>%s</a>',
Branch: 1 branch
BranchTrueFalseTotal
if $element->attr("href") 4 0 4
142 $element->attr('href'), $target, $inner
143 if $element->attr('href');
144  
145 # If the button is expanded, it needs a <center> tag around the content
146 4 ▶ my @el_classes = split /\s+/xms, $element->attr('class') // '';
Condition100%2/2
$element->attr("class") // ""
l!l
22
MC/DC100%1/1
$element->attr("class") // ""
$element->attr("class")
Truth table100%2/2
A
A= $element->attr("class")
Aresult
11
00
147 4 ▶ if (scalar grep { $_ eq 'expand' || $_ eq 'expanded' } @el_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({$_ eq "expand" or $_ eq "expanded";} @el_classes)) 1 3 4
Condition66%2/3
$_ eq 'expand' || $_ eq 'expanded'
l!l&&r!l&&!r
102
MC/DC50%1/2
$_ eq 'expand' || $_ eq 'expanded'
$_ eq 'expand' $_ eq 'expanded'
Truth table66%2/3
A || B
A= $_ eq 'expand'B= $_ eq 'expanded'
ABresult
1X1
011
000
148 1 $inner = sprintf '<center>%s</center>', $inner;
149 1 $expander = qq!\n<td class="expander"></td>!;
150 }
151  
152 # The . button class is always there, along with any others on the <button>
153 # element
154 4 return sprintf '<table class="%s"><tr><td><table><tr><td>%s</td></tr></table></td>%s</tr></table>',
155 _classes($element, 'button'), $inner, $expander;
156 }
157  
158 5 ▶sub _make_center {
Subroutine _make_center: called CC=2 SCAR=7.0
159 5 my ($self, $element, $inner) = @_;
160  
161 5 ▶ if ($element->children->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element->children->size > 0) 4 1 5
162 4 ▶ $element->children->each(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
163 4 my ($e) = @_;
164 4 $e->attr('align', 'center');
165 4 ▶ my @classes = split /\s+/xms, $e->attr('class') // q{};
Condition50%1/2
$e->attr("class") // ""
l!l
04
MC/DC0%0/1
$e->attr("class") // ""
$e->attr("class")
Truth table50%1/2
A
A= $e->attr("class")
Aresult
11
00
166 4 $e->attr('class', join q{ }, @classes, 'float-center');
167 4 });
168 1 ▶ $element->find('item, .menu-item')->each(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
169 1 my ($e) = @_;
170 1 ▶ my @classes = split /\s+/xms, $e->attr('class') // q{};
Condition50%1/2
$e->attr("class") // ""
l!l
01
MC/DC0%0/1
$e->attr("class") // ""
$e->attr("class")
Truth table50%1/2
A
A= $e->attr("class")
Aresult
11
00
171 1 $e->attr('class', join q{ }, @classes, 'float-center');
172 4 });
173 }
174 5 $element->attr('data-parsed', q{});
175 5 return sprintf '%s', $element->to_string;
176 }
177  
178 53 ▶sub _component_factory {
Subroutine _component_factory: called CC=2 SCAR=7.4
179 53 my ($self, $element) = @_;
180  
181 53 my $inner = $element->content;
182  
183 53 my $tag = $element->tag;
184 return $COMPONENTS{$tag}->($self, $element, $inner)
185 53 ▶ if exists $COMPONENTS{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $COMPONENTS{$tag} 53 0 53
186  
187 # If it's not a custom component, return it as-is
188 0 return sprintf '<tr><td>%s</td></tr>', $inner;
189 }
190  
191 16 ▶sub _make_column {
Subroutine _make_column: called CC=14 SCAR=26.4
192 16 my ($self, $col) = @_;
193  
194 16 my $output = q{};
195 16 my $inner = $col->content;
196 16 my @classes = ();
197 16 my $expander = q{};
198  
199 16 my $attributes = $col->attr;
200 my $attr_no_expander = exists $attributes->{'no-expander'}
201 16 ▶ ? $attributes->{'no-expander'}
Branch: 1 branch
BranchTrueFalseTotal
exists $attributes->{'no-expander'} ? : 3 13 16
202 : 0;
203 $attr_no_expander = 1
204 if exists $attributes->{'no-expander'}
205 16 ▶ && !defined $attributes->{'no-expander'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attributes->{"no-expander"} and not defined $attributes->{"no-expander"} 1 15 16
Condition100%3/3
exists $attributes->{"no-expander"} and not defined $attributes->{"no-expander"}
!ll&&!rl&&r
1321
MC/DC100%2/2
exists $attributes->{"no-expander"} and not defined $attributes->{"no-expander"}
exists $attributes->{"no-expander"} not defined $attributes->{"no-expander"}
Truth table100%3/3
A and not(B)
A= exists $attributes->{"no-expander"}B= not defined $attributes->{"no-expander"}
ABresult
0X0
100
111
206 16 ▶ $attr_no_expander = 0
Branch: 1 branch
BranchTrueFalseTotal
if $attr_no_expander eq "false" 1 15 16
207 if $attr_no_expander eq 'false';
208  
209 # Add 1 to include current column
210 16 my $col_count = $col->following->size
211 + $col->preceding->size
212 + 1;
213  
214 # Inherit classes from the <column> tag
215 16 ▶ if ($col->attr('class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->attr("class")) 1 15 16
216 1 push @classes, split /\s+/xms, $col->attr('class');
217 }
218  
219 # Check for sizes. If no attribute is provided, default to small-12.
220 # Divide evenly for large columns
221 16 ▶ my $small_size = $col->attr('small') || $self->column_count;
Condition66%2/3
$col->attr('small') || $self->column_count
l!l&&r!l&&!r
1150
MC/DC0%0/2
$col->attr('small') || $self->column_count
$col->attr('small') $self->column_count
Truth table66%2/3
A || B
A= $col->attr('small')B= $self->column_count
ABresult
1X1
011
000
222 16 ▶ my $large_size = $col->attr('large')
Condition83%5/6
$col->attr('large') || $col->attr('small') || int $self->column_count / $col_count
l!l&&r!l&&!r
1330
$col->attr("large") or $col->attr("small")
l!l&&r!l&&!r
1123
MC/DC0%0/3
$col->attr('large') || $col->attr('small') || int $self->column_count / $col_count
$col->attr("large") $col->attr("small") int $self->column_count / $col_count
Truth table75%3/4
A or B || C
A= $col->attr("large")B= $col->attr("small")C= int $self->column_count / $col_count
ABCresult
1XX1
01X1
0011
0000
223 || $col->attr('small')
224 || int($self->column_count / $col_count);
225  
226 16 push @classes, sprintf 'small-%s', $small_size;
227 16 push @classes, sprintf 'large-%s', $large_size;
228  
229 # Add the basic "columns" class also
230 16 push @classes, 'columns';
231  
232 # Determine if it's the first or last column, or both
233 16 ▶ push @classes, 'first'
Branch: 1 branch
BranchTrueFalseTotal
unless $col->preceding("columns, .columns")->size 11 5 16
234 if !$col->preceding('columns, .columns')->size;
235 16 ▶ push @classes, 'last'
Branch: 1 branch
BranchTrueFalseTotal
unless $col->following("columns, .columns")->size 11 5 16
236 if !$col->following('columns, .columns')->size;
237  
238 # If the column contains a nested row, the .expander class should not be
239 # used. The == on the first check is because we're comparing a string
240 # pulled from $.attr() to a number
241 16 ▶ if ($large_size == $self->column_count && $col->find('.row, row')->size == 0 && !$attr_no_expander) {
Branch: 1 branch
BranchTrueFalseTotal
if ($large_size == $self->column_count and $col->find(".row, row")->size == 0 and not $attr_no_expander) 4 12 16
Condition100%6/6
$large_size == $self->column_count and $col->find(".row, row")->size == 0 and not $attr_no_expander
!ll&&!rl&&r
1024
$large_size == $self->column_count and $col->find(".row, row")->size == 0
!ll&&!rl&&r
916
MC/DC100%3/3
$large_size == $self->column_count and $col->find(".row, row")->size == 0 and not $attr_no_expander
$large_size == $self->column_count $col->find(".row, row")->size == 0 not $attr_no_expander
Truth table100%4/4
A and B and not(C)
A= $large_size == $self->column_countB= $col->find(".row, row")->size == 0C= not $attr_no_expander
ABCresult
0XX0
10X0
1100
1111
242 4 $expander = qq!\n<th class="expander"></th>!;
243 }
244  
245 # Final HTML output
246 16 $output = <<'END';
247 <th class="%s" %s>
248 <table>
249 <tr>
250 <th>%s</th>%s
251 </tr>
252 </table>
253 </th>
254 END
255  
256 16 my $class = join q{ }, @classes;
257 16 return sprintf $output,
258 $class, _add_standard_attributes($col), $inner, $expander
259 }
260  
261 42 ▶sub _extract_raws {
Subroutine _extract_raws: called CC=2 SCAR=6.9
262 42 my ($string) = @_;
263  
264 42 my @raws;
265 42 my $i = 0;
266 42 my $str = $string;
267 42 my $rx = qr{<\s*raw\s*>(.*?)</\s*raw\s*>}xism;
268  
269 42 ▶ while (my ($raw) = $str =~ $rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($raw) = $str =~ /$rx/) 4 42 46
270 4 push @raws, $raw;
271 4 $str =~ s/$rx/###RAW$i###/xsm;
272 4 $i++;
273 }
274 42 return (\@raws, $str);
275 }
276  
277 42 ▶sub _reinject_raws {
Subroutine _reinject_raws: called CC=2 SCAR=6.9
278 42 my ($string, $raws) = @_;
279  
280 42 my $str = $string;
281 42 for my $i (0..$#{$raws}) {
282 4 $str =~ s{\#{3}RAW$i\#{3}}{$raws->[$i]}xms;
283 }
284 42 return $str;
285 }
286  
287 42 ▶sub release_the_kraken {
Subroutine release_the_kraken: called CC=3 SCAR=11.0
Pod documented
288 42 my ($self, $html) = @_;
289  
290 42 my ($raws, $string) = _extract_raws($html);
291  
292 42 my $dom = Mojo::DOM->new( $string );
293 my $tags = join ', ',
294 504 ▶ map { $_ eq 'center' ? "$_:not([data-parsed])" : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'center' ? : 42 462 504
295 42 @{ $self->_component_tags };
296  
297 42 ▶ while ($dom->find($tags)->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dom->find($tags)->size) 53 42 95
298 53 my $elem = $dom->find($tags)->first;
299 53 my $new_html = $self->_component_factory($elem);
300 53 $elem->replace($new_html);
301 }
302 42 $string = $dom->to_string;
303 42 return _reinject_raws($string, $raws);
304 }
305  
306 3 1;
307  
308 __END__
↑ Summary