/
YouTube.pm
95 lines (62 loc) · 2.29 KB
/
YouTube.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package MojoMojo::Formatter::YouTube;
use base qw/MojoMojo::Formatter/;
use URI::Fetch;
=head1 NAME
MojoMojo::Formatter::YouTube - Embed YouTube player
=head1 DESCRIPTION
Embed Youtube video player for given video by writing =youtube <url>.
=head1 METHODS
=over 4
=item format_content_order
Format order can be 1-99. The YouTube formatter runs on 6
=cut
sub format_content_order { 6 }
=item format_content
calls the formatter. Takes a ref to the content as well as the
context object.
=cut
sub format_content {
my ( $class, $content, $c ) = @_;
my @lines = split /\n/, $$content;
$$content = "";
my $re=$class->gen_re(qr/youtube\s+(.*?)/);
my $lang=$c->session->{lang} || $c->pref('default_lang') || 'en';
foreach my $line (@lines) {
if ( $line =~ m/$re/){
$line=$class->process($c,$line,$re,$lang);
}
$$content .= $line . "\n";
}
}
sub process {
my ( $class, $c, $line, $re, $lang) = @_;
my $youtube=$c->loc('YouTube Video');
my $video_id;
$line =~ m/$re/;
$url = URI->new($1);
unless ($url){
$line =~ s/$re/"$youtube: $url ".$c->loc('is not a valid url')/e;
return $line;
}
if ($url =~ m!youtube.com/.*?v=([A-Za-z0-9_]+)!){
$video_id=$1;
} else {
$line =~ s/$re/"$youtube: $url ".$c->loc('is not a valid link to youtube video')/e;
return $line;
}
if ( ($c->action->reverse eq 'pageadmin/edit') || ($c->action->reverse eq 'jsrpc/render') ){
$line =~ s!$re!<div style='width: 425px;height: 344px; border: 1px black dotted;'>$youtube<br /><a href="$url">$url</a></div>!;
} else {
$line =~ s!$re!<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$video_id&hl=$lang"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$video_id&hl=$lang" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>!;
}
return $line;
}
=back
=head1 SEE ALSO
L<MojoMojo>,L<Module::Pluggable::Ordered>,L<URI::Fetch>
=head1 AUTHORS
Robert 'LiNiO' Litwiniec <linio@wonder.pl>
=head1 License
This module is licensed under the same terms as Perl itself.
=cut
1;