Instantly share code, notes, and snippets.

@internaut /README.md forked from vanto/README.md
Last active Jan 30, 2016

Embed
What would you like to do?

OEmbed Liquid Tag for Jekyll

This is a simple liquid tag that helps to easily embed images, videos or slides from OEmbed enabled providers. It uses Magnus Holm's great oembed gem which connects to the OEmbed endpoint of the link's provider and retrieves the HTML code to embed the content properly (i.e. an in-place YouTube player, Image tag for Flickr, in-place slideshare viewer etc.). By default it supports the following OEmbed providers (but can fallback to Embed.ly or OoEmbed for other providers):

  • Youtube
  • Flickr
  • Viddler
  • Qik
  • Revision3
  • Hulu
  • Vimeo
  • Instagram
  • Slideshare
  • Yfrog
  • MlgTv

How to install

  1. Make sure you have the ruby-oembed gem (Rubygems, Github) installed.
  2. Copy oembed.rb to <your-jekyll-project>/_plugins
  3. You're done.

If you're experiencing troubles with Ruby 2.x, please also add require 'openssl' to the script.

How to use

Place a oembed tag in your content file. E.g.

<h1>An embedded video</h1>
{{ oembed http://www.youtube.com/watch?v=Sv5iEK-IEzw }}

<h1>An embedded video with width/height dimensions</h1>
{{ oembed http://vimeo.com/13207279 640 480 }}

<h1>An embedded presentation</h1>
{{ oembed http://www.slideshare.net/AmitRanjan/quick-tour }}

The oembed tag behaves almost compatible to Robert Böhnke's Embed.ly Tag, i.e. it wraps the embed code in a <div> tag that has classes matching the embeds type, provider as well as the generic embed. In contrast to the embed.ly tag, we don't support overriding certain oembed properties.

Author

Tammo van Lessen -- http://www.taval.de

Author of max.width/height modification

Markus Konrad -- http://mkonrad.net

License

This code snippet is licensed under Apache License 2.0

##
# OEmbed Liquid Tag for Jekyll
# - requires https://github.com/judofyr/ruby-oembed/
#
# Copyright 2011 Tammo van Lessen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
require 'oembed'
require 'uri'
# register all default OEmbed providers
::OEmbed::Providers.register_all()
# since register_all does not register all default providers, we need to do this here. See https://github.com/judofyr/ruby-oembed/issues/18
::OEmbed::Providers.register(::OEmbed::Providers::Instagram, ::OEmbed::Providers::Slideshare, ::OEmbed::Providers::Yfrog, ::OEmbed::Providers::MlgTv)
::OEmbed::Providers.register_fallback(::OEmbed::ProviderDiscovery, ::OEmbed::Providers::Embedly, ::OEmbed::Providers::OohEmbed)
module Jekyll
class OEmbed < Liquid::Tag
def initialize(tag_name, markup, tokens)
super
# parse optional max. width/height parameters
@text, @embed_w, @embed_h = markup.split
@text.strip!
end
def render(context)
# pipe param through liquid to make additional replacements possible
url = Liquid::Template.parse(@text).render context
# oembed look up
# additionally add max. width/height parameters
result = ::OEmbed::Providers.get(url, { :format => :xml, :maxwidth => @embed_w, :maxheight => @embed_h })
# Odd: slideshare uses provider-name instead of provider_name
provider = result.fields['provider_name'] || result.fields['provider-name'] || 'unknown'
"<div class=\"embed #{result.type} #{provider}\">#{result.html}</div>"
end
end
end
Liquid::Template.register_tag('oembed', Jekyll::OEmbed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment