blib/lib/Statocles/Plugin/VideoTag.pm

filter: Statementstmt 82.5% 33 / 40 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 100% 2 / 2 totaltotal 80.6% 50 / 62 CC 6 SCAR 8.8
CC
6
Cov
81%
__ANON__
6.0
__ANON__
3.0
video_tag
3.0
CRAP 2.4
↑ Summary
1 package Statocles::Plugin::VideoTag;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Change video file anchors to video elements
5  
6 2 our $VERSION = '0.0400';
7  
8 2 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 with 'Statocles::Plugin';
10  
11  
12 has file_type => (
13 is => 'ro',
14 isa => Str,
15 0 ▶ default => sub { 'mp4' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 2 );
17  
18  
19 has width => (
20 is => 'ro',
21 isa => Int,
22 2 ▶ default => sub { 560 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 2 );
24  
25  
26 has height => (
27 is => 'ro',
28 isa => Int,
29 2 ▶ default => sub { 315 },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 );
31  
32  
33 has frameborder => (
34 is => 'ro',
35 isa => Int,
36 2 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 );
38  
39  
40 has allow => (
41 is => 'ro',
42 isa => Str,
43 2 ▶ default => sub { 'accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture' },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 );
45  
46  
47 has allowfullscreen => (
48 is => 'ro',
49 isa => Int,
50 2 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 );
52  
53  
54 3 ▶sub video_tag {
Subroutine video_tag: called CC=3 SCAR=11.0
Pod documented
55 3 my ($self, $page) = @_;
56 3 ▶ if ($page->has_dom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->has_dom) 3 0 3
57 3 ▶ if ($self->file_type eq 'youtu') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_type eq 'youtu') { } 2 1 3
58 2 ▶ $page->dom->find('a[href*="'. $self->file_type .'"]')->each(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
59 2 my ($el) = @_;
60 2 my $href = $el->attr('href');
61 2 ▶ if ($href =~ /watch\?v=.+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ /watch\?v=.+$/) 1 1 2
62 1 $href =~ s/watch\?v=(.+)$/embed\/$1/;
63 1 ▶ my $replacement = sprintf '<iframe width="%d" height="%d" src="%s" frameborder="%d" allow="%s" %s></iframe>',
Branch: 1 branch
BranchTrueFalseTotal
$self->allowfullscreen ? : 1 0 1
64 $self->width, $self->height,
65 $href,
66 $self->frameborder,
67 $self->allow,
68 $self->allowfullscreen ? 'allowfullscreen' : '';
69 1 $el->replace($replacement);
70 }
71 2 });
72 }
73 else {
74 1 ▶ $page->dom->find('a[href$=.'. $self->file_type .']')->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 1 my ($el) = @_;
76 1 my $replacement = sprintf '<video controls><source type="video/%s" src="%s"></video>',
77 $self->file_type, $el->attr('href');
78 1 $el->replace($replacement);
79 1 });
80 }
81 }
82 3 return $page;
83 }
84  
85  
86 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($self, $site) = @_;
88 0 ▶ $site->on(build => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
89 0 my ($event) = @_;
90 0 for my $page (@{ $event->pages }) {
91 0 $page = $self->video_tag($page);
92 }
93 0 });
94 }
95  
96 2 1;
97  
98 __END__
↑ Summary