From b4e8c31a84de4da85bd45a7f965a7ffa96947609 Mon Sep 17 00:00:00 2001 From: MichaelDaum Date: Mon, 24 Jun 2013 14:25:07 +0000 Subject: [PATCH] Item12522: * 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 --- data/System/OEmbedPlugin.txt | 58 +++++++++++++- lib/Foswiki/Plugins/OEmbedPlugin.pm | 23 +++--- lib/Foswiki/Plugins/OEmbedPlugin/Config.spec | 27 ++++++- lib/Foswiki/Plugins/OEmbedPlugin/Core.pm | 48 +++++++++++- lib/Foswiki/Plugins/OEmbedPlugin/DEPENDENCIES | 6 +- lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST | 1 + lib/Web/oEmbed/Response.pm | 75 ++++++++++--------- templates/oembed.tmpl | 12 +++ 8 files changed, 193 insertions(+), 57 deletions(-) create mode 100644 templates/oembed.tmpl diff --git a/data/System/OEmbedPlugin.txt b/data/System/OEmbedPlugin.txt index ea428bb..ceb9541 100644 --- a/data/System/OEmbedPlugin.txt +++ b/data/System/OEmbedPlugin.txt @@ -16,19 +16,24 @@ code to add it to the page, e.g. add an iframe for videos and the like. 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 @@ -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) @@ -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 @@ -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) + * =<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 + + +%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" +}% + + +%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 %EMBED{"http://www.youtube.com/watch?v=O8e8Ttfz-pY" width="600"}% @@ -72,10 +119,10 @@ Some of the listed providers have already been added to this plugin, yet not all ---+++ Soundcloud -%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"}% -%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 @@ -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"}% @@ -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: |   | +| 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% | diff --git a/lib/Foswiki/Plugins/OEmbedPlugin.pm b/lib/Foswiki/Plugins/OEmbedPlugin.pm index b407878..f67bd93 100644 --- a/lib/Foswiki/Plugins/OEmbedPlugin.pm +++ b/lib/Foswiki/Plugins/OEmbedPlugin.pm @@ -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; @@ -44,4 +37,14 @@ sub getCore { } +sub initPlugin { + + core->init; + + Foswiki::Func::registerTagHandler('EMBED', sub { return core->EMBED(@_); }); + + return 1; +} + + 1; diff --git a/lib/Foswiki/Plugins/OEmbedPlugin/Config.spec b/lib/Foswiki/Plugins/OEmbedPlugin/Config.spec index f7c11b5..4797e3e 100644 --- a/lib/Foswiki/Plugins/OEmbedPlugin/Config.spec +++ b/lib/Foswiki/Plugins/OEmbedPlugin/Config.spec @@ -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', }, @@ -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', + }, }; diff --git a/lib/Foswiki/Plugins/OEmbedPlugin/Core.pm b/lib/Foswiki/Plugins/OEmbedPlugin/Core.pm index ea48af0..eb0560a 100644 --- a/lib/Foswiki/Plugins/OEmbedPlugin/Core.pm +++ b/lib/Foswiki/Plugins/OEmbedPlugin/Core.pm @@ -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) = @_; @@ -85,6 +105,7 @@ sub EMBED { my $url = $params->{_DEFAULT} || $params->{url}; return "ERROR: no url param" unless $url; + my $width = $params->{width} || Foswiki::Func::getPreferencesValue("OEMBED_MAXWIDTH") || ''; my $height = $params->{height} || Foswiki::Func::getPreferencesValue("OEMBED_MAXHEIGHT") || ''; @@ -104,8 +125,31 @@ sub EMBED { $response->web_page($url); # SMELL: hook in the orig url - my $result = $response->render($opts); - return ''.$result.'' 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/^$/$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 ''.$result.''; + } + } #print STDERR "WARNING: Hm, can't render response from $url\n"; return $url; diff --git a/lib/Foswiki/Plugins/OEmbedPlugin/DEPENDENCIES b/lib/Foswiki/Plugins/OEmbedPlugin/DEPENDENCIES index 5523a63..ac5ba02 100644 --- a/lib/Foswiki/Plugins/OEmbedPlugin/DEPENDENCIES +++ b/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 diff --git a/lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST b/lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST index 54ce80b..3f61dbf 100644 --- a/lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST +++ b/lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST @@ -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 diff --git a/lib/Web/oEmbed/Response.pm b/lib/Web/oEmbed/Response.pm index ab9fcbe..d4893a0 100644 --- a/lib/Web/oEmbed/Response.pm +++ b/lib/Web/oEmbed/Response.pm @@ -24,6 +24,7 @@ has 'width', is => 'rw'; has 'height', is => 'rw'; has 'html', is => 'rw'; +has 'data', is => 'rw'; use HTML::Element; @@ -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}; } } @@ -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/^$/$1/; + return $result; } } diff --git a/templates/oembed.tmpl b/templates/oembed.tmpl new file mode 100644 index 0000000..51a35cd --- /dev/null +++ b/templates/oembed.tmpl @@ -0,0 +1,12 @@ +%TMPL:DEF{"article"}%
+
+ +
+

$title

+
+$description +... $author_name, $provider_name +%IF{"'$type' = 'video'" then="

$html

"}% +
+%CLEAR% +
%TMPL:END%