Skip to content

Commit

Permalink
Item12522:
Browse files Browse the repository at this point in the history
   * added more provider (clipfish, google maps, deviantart, aol, screener, ...) 
   * added formatting and templating of oembed results



git-svn-id: http://svn.foswiki.org/trunk/OEmbedPlugin@16808 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Jun 24, 2013
1 parent 4399ccd commit b4e8c31
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 57 deletions.
58 changes: 56 additions & 2 deletions data/System/OEmbedPlugin.txt
Expand Up @@ -16,19 +16,24 @@ code to add it to the page, e.g. add an iframe for videos and the like.

<noautolink>
1 Amazon (embed.ly)
1 Aol
1 Blib.tv
1 Chirbit
1 CircuitLab
1 Clickthrough
1 Clipfish (embed.ly)
1 CollegeHumor
1 !DeviantArt
1 DailyMotion
1 Dotsub
1 Flickr
1 Foursquare (embed.ly)
1 Geograph Britain
1 Geograph Channel Islands
1 Geograph Germany
1 Google Maps (embed.ly)
1 Hulu
1 IMDB (embed.ly)
1 Instagram (embed.ly)
1 Jest
1 Kinomap
Expand All @@ -38,6 +43,8 @@ code to add it to the page, e.g. add an iframe for videos and the like.
1 Qik
1 Quiz.biz
1 Revision3
1 Screencast (embed.ly)
1 Screenr
1 Scribd
1 SlideShare
1 SoundCloud (embed.ly)
Expand All @@ -48,6 +55,7 @@ code to add it to the page, e.g. add an iframe for videos and the like.
1 Viddler
1 Vimeo
1 Vimeon
1 Washington Post (embed.ly)
1 WordPress
1 Youtube
</noautolink>
Expand All @@ -60,9 +68,48 @@ Some of the listed providers have already been added to this plugin, yet not all

* =url=: an external link to the resource to be embedded
* =width=, =height=: optional maximum values for the image/video/... being embedded (note: not all providers properly implement these parameters)
* =format=: a format string to implement a custom renderer
* =template=: the name of a template defined in =oembed.tmpl=; known templates shipped with %TOPIC% are "article" (todo add more)
* =&lt;custom_key>=: any custom key="value" pair may be defined and made use of in the format (or template) using =$custom_key= to expand it;
these have higher precendence than the default values submitted by the oembed provider; for example use a custom =thumbnail_width="100"= parameter
to override the default width.

The keys in the json submitted from the oembed provider can be used in a format string (or a template definition). Some well-known keys are:

* =$author_name=
* =$author_url=
* =$descriptio=
* =$html=
* =$provider_name=
* =$provider_url=
* =$thumbnail_height=
* =$thumbnail_url=
* =$thumbnail_width=
* =$title=
* =$type=
* =$url=
* =$web_page=

In addition all standard format tokens can be used (=$percnt=, =$dollar=, =$nop=, etc).

See the =oembed.tmpl= file on how to use them.

---++ Examples

---+++ Washington Post

<verbatim class="tml">
%EMBED{"http://www.washingtonpost.com/investigations/us-intelligence-mining-data-from-nine-us-internet-companies-in-broad-secret-program/2013/06/06/3a0c0da8-cebf-11e2-8845-d970ccb04497_story.html"
template="article"
thumbnail_width="100"
}%
</verbatim>

%EMBED{"http://www.washingtonpost.com/investigations/us-intelligence-mining-data-from-nine-us-internet-companies-in-broad-secret-program/2013/06/06/3a0c0da8-cebf-11e2-8845-d970ccb04497_story.html"
template="article"
thumbnail_width="100"
}%

---+++ Youbtube
<verbatim class="tml">
%EMBED{"http://www.youtube.com/watch?v=O8e8Ttfz-pY" width="600"}%
Expand All @@ -72,10 +119,10 @@ Some of the listed providers have already been added to this plugin, yet not all

---+++ Soundcloud
<verbatim class="tml">
%EMBED{"https://soundcloud.com/safia-music/listen-to-soul-listen-to-blues" width="600"}%
%EMBED{"http://soundcloud.com/safia-music/listen-to-soul-listen-to-blues" width="600"}%
</verbatim>

%EMBED{"https://soundcloud.com/safia-music/listen-to-soul-listen-to-blues" width="600"}%
%EMBED{"http://soundcloud.com/safia-music/listen-to-soul-listen-to-blues" width="600"}%

---+++ Flickr
<verbatim class="tml">
Expand Down Expand Up @@ -115,6 +162,11 @@ Some of the listed providers have already been added to this plugin, yet not all
%EMBED{"http://www.slideshare.net/haraldf/business-quotes-for-2011"}%
%EMBED{"http://www.ted.com/talks/hendrik_poinar_bring_back_the_woolly_mammoth.html"}%
%EMBED{"http://www.viddler.com/v/1646c55"}%
%EMBED{"http://www.screencast.com/t/jHhhu39t"}%
%EMBED{"http://www.screenr.com/fTK"}%
%EMBED{"http://goo.gl/maps/22OR8"}%
%EMBED{"https://foursquare.com/v/vapiano/4b212bc4f964a520ac3724e3"}%
%EMBED{"http://feigiap.deviantart.com/art/Shop-380055652"}%
</verbatim>


Expand All @@ -133,6 +185,8 @@ Some of the listed providers have already been added to this plugin, yet not all
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 24 Jun 2013 | added format, template and custom params; \
added a few more providers |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |
23 changes: 13 additions & 10 deletions lib/Foswiki/Plugins/OEmbedPlugin.pm
Expand Up @@ -20,20 +20,13 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '1.00';
our $RELEASE = '1.00';
our $VERSION = '2.00';
our $RELEASE = '2.00';
our $SHORTDESCRIPTION = 'Easy embedding of third party content';
our $NO_PREFS_IN_TOPIC = 1;
our $core;

sub initPlugin {

Foswiki::Func::registerTagHandler('EMBED', sub { return getCore()->EMBED(@_); });

return 1;
}

sub getCore {
sub core {

unless (defined $core) {
require Foswiki::Plugins::OEmbedPlugin::Core;
Expand All @@ -44,4 +37,14 @@ sub getCore {
}


sub initPlugin {

core->init;

Foswiki::Func::registerTagHandler('EMBED', sub { return core->EMBED(@_); });

return 1;
}


1;
27 changes: 24 additions & 3 deletions lib/Foswiki/Plugins/OEmbedPlugin/Config.spec
Expand Up @@ -57,7 +57,7 @@ $Foswiki::cfg{OEmbedPlugin}{Providers} = {
},

Youtube => {
url => 'http://*.youtube.com/*',
url => ['http://*.youtube.com/*', 'https://*.youtube.com/*'],
api => 'http://www.youtube.com/oembed',
},

Expand Down Expand Up @@ -175,15 +175,36 @@ $Foswiki::cfg{OEmbedPlugin}{Providers} = {
url => [
'http://*.amazon.com/*',
'http://*.amazon.de/*',
'http://instagram.com/p/*',
'http://twitpic.com/*',
'http://*.clipfish.de/*',
'http://goo.gl/maps/*',
'http://*.imdb.com/*',
'http://instagram.com/p/*',
'http://*.screencast.com/*',
'https://foursquare.com/*',
'https://maps.google.com/maps/*',
'http://soundcloud.com/*',
'https://soundcloud.com/*',
'http://twitpic.com/*',
'http://www.washingtonpost.com/*',
# TODO: add more
],
api => 'http://api.embed.ly/1/oembed',
},

Screenr => {
url => 'http://*.screenr.com/*',
api => 'http://www.screenr.com/api/oembed.json',
},

Aol => {
url => 'http://on.aol.com/*',
api => 'http://on.aol.com/api',
},

Deviantart => {
url => 'http://*.deviantart.com/art/*',
api => 'http://backend.deviantart.com/oembed',
},

};

Expand Down
48 changes: 46 additions & 2 deletions lib/Foswiki/Plugins/OEmbedPlugin/Core.pm
Expand Up @@ -74,9 +74,29 @@ sub new {
}
}

$this->init();


return $this;
}

sub init {
my $this = shift;

$this->{_doneReadTemplate} = 0;
}

sub expandTemplate {
my ($this, $tmpl) = @_;

unless ($this->{_doneReadTemplate}) {
$this->{_doneReadTemplate} = 1;
Foswiki::Func::readTemplate("oembed");
}

return Foswiki::Func::expandTemplate($tmpl);
}

sub EMBED {
my ($this, $session, $params, $theTopic, $theWeb) = @_;

Expand All @@ -85,6 +105,7 @@ sub EMBED {
my $url = $params->{_DEFAULT} || $params->{url};
return "<span class='foswikiAlert'>ERROR: no url param</span>" unless $url;


my $width = $params->{width} || Foswiki::Func::getPreferencesValue("OEMBED_MAXWIDTH") || '';
my $height = $params->{height} || Foswiki::Func::getPreferencesValue("OEMBED_MAXHEIGHT") || '';

Expand All @@ -104,8 +125,31 @@ sub EMBED {

$response->web_page($url); # SMELL: hook in the orig url

my $result = $response->render($opts);
return '<literal>'.$result.'</literal>' if defined $result;
my $format = $params->{format};
my $template = $params->{template};
$format = $this->expandTemplate($template) if defined $template;

my $result;
if (defined $format) {
my $data = $response->data();
$result = $format;
while (my ($key, $val) = each %$data) {
$val =~ s/^<!\[CDATA\[([^]+]*)\]\]>$/$1/;
$val = $params->{$key} if defined $params->{$key};
$result =~ s/\$$key\b/$val/g;
}
$result =~ s/\$url\b/$url/g; # ... if left over

# clean up some
$result =~ s/\$(thumbnail_url|thumbnail_width|thumbnail_height|html|provider_url|provider_name|description|title|author_name|height|width|author_url|version|type)\b//g;
return Foswiki::Func::decodeFormatTokens($result);

} else {
$result = $response->render($opts);
if (defined $result) {
return '<literal>'.$result.'</literal>';
}
}

#print STDERR "WARNING: Hm, can't render response from $url\n";
return $url;
Expand Down
6 changes: 1 addition & 5 deletions lib/Foswiki/Plugins/OEmbedPlugin/DEPENDENCIES
@@ -1,5 +1 @@
# Dependencies for OEmbedPlugin
# Example:
# Time::ParseDate,>=2003.0211,cpan,Required.
# Foswiki::Plugins,>=1.2,perl,Requires version 1.2 of handler API.

Any::Moose,>=0,cpan,Required
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST
Expand Up @@ -5,3 +5,4 @@ lib/Foswiki/Plugins/OEmbedPlugin/Core.pm 0644
lib/Foswiki/Plugins/OEmbedPlugin.pm 0644
lib/Web/oEmbed.pm 0644
lib/Web/oEmbed/Response.pm 0644
templates/oembed.tmpl 0644
75 changes: 40 additions & 35 deletions lib/Web/oEmbed/Response.pm
Expand Up @@ -24,6 +24,7 @@ has 'width', is => 'rw';
has 'height', is => 'rw';

has 'html', is => 'rw';
has 'data', is => 'rw';

use HTML::Element;

Expand All @@ -44,11 +45,11 @@ sub new_from_response {
croak "Content-Type is not either JSON or XML: " . $http_res->content_type;
}

$res->data($data);

for my $key (keys %$data) {
if ($res->can($key)) {
$res->$key( $data->{$key} );
} else {
$res->{$key} = $data->{$key};
}
}

Expand All @@ -70,42 +71,46 @@ sub parse_xml {
sub render {
my ($self, $opts) = @_;

if ($self->type eq 'photo') {
my $width = $self->width;
my $height = $self->height;

if ($opts->{maxwidth} && $width > $opts->{maxwidth}) {
$width = $opts->{maxwidth};
$height = "auto"; # TODO set according to aspect ratio
}

if ($opts->{maxheight} && $height > $opts->{maxheight}) {
$height = $opts->{maxheight};
$width = "auto"; # TODO set according to aspect ratio
}

my $element = HTML::Element->new('a', href => $self->web_page || $self->url);
$element->attr(title => $self->title) if defined $self->title;
my $img = HTML::Element->new(
'img',
src => $self->url,
width => $width,
height => $height,
);
$img->attr(alt => $self->title) if defined $self->title;

$element->push_content($img);
return $element->as_HTML;
}

if ($self->type eq 'link') {
my $element = HTML::Element->new('a', href => $self->url);
$element->push_content(defined $self->title ? $self->title : $self->url);
return $element->as_HTML;
if ($self->type) {
if ($self->type eq 'photo') {
my $width = $self->width;
my $height = $self->height;

if ($opts->{maxwidth} && $width > $opts->{maxwidth}) {
$width = $opts->{maxwidth};
$height = "auto"; # TODO set according to aspect ratio
}

if ($opts->{maxheight} && $height > $opts->{maxheight}) {
$height = $opts->{maxheight};
$width = "auto"; # TODO set according to aspect ratio
}

my $element = HTML::Element->new('a', href => $self->web_page || $self->url);
$element->attr(title => $self->title) if defined $self->title;
my $img = HTML::Element->new(
'img',
src => $self->url,
width => $width,
height => $height,
);
$img->attr(alt => $self->title) if defined $self->title;

$element->push_content($img);
return $element->as_HTML;
}

if ($self->type eq 'link') {
my $element = HTML::Element->new('a', href => $self->url);
$element->push_content(defined $self->title ? $self->title : $self->url);
return $element->as_HTML;
}
}

if ($self->html) {
return $self->html;
my $result = $self->html;
$result =~ s/^<!\[CDATA\[([^]+]*)\]\]>$/$1/;
return $result;
}
}

Expand Down

0 comments on commit b4e8c31

Please sign in to comment.