line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Statocles::Plugin::VideoTag; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Change video file anchors to video elements |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.0301'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
773
|
use Statocles::Base 'Class'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
13
|
|
9
|
|
|
|
|
|
|
with 'Statocles::Plugin'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has file_type => ( |
13
|
|
|
|
|
|
|
is => 'ro', |
14
|
|
|
|
|
|
|
isa => Str, |
15
|
|
|
|
|
|
|
default => sub { 'mp4' }, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has width => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => Int, |
22
|
|
|
|
|
|
|
default => sub { 560 }, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has height => ( |
27
|
|
|
|
|
|
|
is => 'ro', |
28
|
|
|
|
|
|
|
isa => Int, |
29
|
|
|
|
|
|
|
default => sub { 315 }, |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has frameborder => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
isa => Int, |
36
|
|
|
|
|
|
|
default => sub { 0 }, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has allow => ( |
41
|
|
|
|
|
|
|
is => 'ro', |
42
|
|
|
|
|
|
|
isa => Str, |
43
|
|
|
|
|
|
|
default => sub { 'accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture' }, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has allowfullscreen => ( |
48
|
|
|
|
|
|
|
is => 'ro', |
49
|
|
|
|
|
|
|
isa => Int, |
50
|
|
|
|
|
|
|
default => sub { 1 }, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub video_tag { |
55
|
2
|
|
|
2
|
1
|
1156430
|
my ($self, $page) = @_; |
56
|
2
|
50
|
|
|
|
13
|
if ($page->has_dom) { |
57
|
2
|
100
|
|
|
|
25
|
if ($self->file_type eq 'youtu') { |
58
|
|
|
|
|
|
|
$page->dom->find('a[href*="'. $self->file_type .'"]')->each(sub { |
59
|
1
|
|
|
1
|
|
3866
|
my ($el) = @_; |
60
|
1
|
|
|
|
|
7
|
my $href = $el->attr('href'); |
61
|
1
|
|
|
|
|
30
|
$href =~ s/watch\?v=(.+)$/embed\/$1/; |
62
|
1
|
50
|
|
|
|
30
|
my $replacement = sprintf '<iframe width="%d" height="%d" src="%s" frameborder="%d" allow="%s" %s></iframe>', |
63
|
|
|
|
|
|
|
$self->width, $self->height, |
64
|
|
|
|
|
|
|
$href, |
65
|
|
|
|
|
|
|
$self->frameborder, |
66
|
|
|
|
|
|
|
$self->allow, |
67
|
|
|
|
|
|
|
$self->allowfullscreen ? 'allowfullscreen' : ''; |
68
|
1
|
|
|
|
|
7
|
$el->replace($replacement); |
69
|
1
|
|
|
|
|
26
|
}); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
|
|
|
|
|
|
$page->dom->find('a[href$=.'. $self->file_type .']')->each(sub { |
73
|
1
|
|
|
1
|
|
5308
|
my ($el) = @_; |
74
|
1
|
|
|
|
|
12
|
my $replacement = sprintf '<video controls><source type="video/%s" src="%s"></video>', |
75
|
|
|
|
|
|
|
$self->file_type, $el->attr('href'); |
76
|
1
|
|
|
|
|
31
|
$el->replace($replacement); |
77
|
1
|
|
|
|
|
25
|
}); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
2
|
|
|
|
|
868
|
return $page; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub register { |
85
|
0
|
|
|
0
|
1
|
|
my ($self, $site) = @_; |
86
|
|
|
|
|
|
|
$site->on(build => sub { |
87
|
0
|
|
|
0
|
|
|
my ($event) = @_; |
88
|
0
|
|
|
|
|
|
for my $page (@{ $event->pages }) { |
|
0
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
$page = $self->video_tag($page); |
90
|
|
|
|
|
|
|
} |
91
|
0
|
|
|
|
|
|
}); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=pod |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=encoding UTF-8 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 NAME |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Statocles::Plugin::VideoTag - Change video file anchors to video elements |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 VERSION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
version 0.0301 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# site.yml |
113
|
|
|
|
|
|
|
site: |
114
|
|
|
|
|
|
|
class: Statocles::Site |
115
|
|
|
|
|
|
|
args: |
116
|
|
|
|
|
|
|
plugins: |
117
|
|
|
|
|
|
|
video_tag: |
118
|
|
|
|
|
|
|
$class: Statocles::Plugin::VideoTag |
119
|
|
|
|
|
|
|
$args: |
120
|
|
|
|
|
|
|
file_type: 'youtu' |
121
|
|
|
|
|
|
|
width: 500 |
122
|
|
|
|
|
|
|
height: 300 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
C<Statocles::Plugin::VideoTag> changes video file anchor elements to |
127
|
|
|
|
|
|
|
video elements. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 file_type |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
The file type to replace. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Default: C<mp4> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 width |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Width of the iframe for an embedded YouTube video. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Default: C<560> |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 height |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Height of the iframe for an embedded YouTube video. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Default: C<315> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 frameborder |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Whether to have a frameborder on the iframe for a YouTube video. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Default: C<0> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 allow |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The iframe B<allow> attribute string for a YouTube video. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Default: C<accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 allowfullscreen |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Whether to allow full-screen for the iframe for a YouTube video. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Default: C<1> |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 METHODS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 video_tag |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
$page = $plugin->video_tag($page); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Process the video links on a L<Statocles::Page>. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
If the B<file_type> is given as C<youtu>, YouTube links of this exact |
176
|
|
|
|
|
|
|
form will be converted to an embedded iframe: |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
https://www.youtube.com/watch?v=abcdefg1234567 |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Where the C<abcdefg1234567> is a placeholder for the actual video. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
* Currently, for YouTube links, including a start time (e.g. C<&t=42>) |
183
|
|
|
|
|
|
|
in the link is not honored. In fact including any argument other than |
184
|
|
|
|
|
|
|
C<v> will not render the embedded video correctly at this time... |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 register |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Register this plugin to install its event handlers. (This method is |
189
|
|
|
|
|
|
|
called automatically.) |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 SEE ALSO |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L<Statocles> |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
L<Statocles::Plugin> |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
L<Statocles::Plugin::AudioTag> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
L<https://ology.github.io/2020/12/06/making-a-statocles-plugin/> |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 AUTHOR |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Gene Boggs <gene@cpan.org> |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
This software is copyright (c) 2021 by Gene Boggs. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
210
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |