Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Forked from jalcine/bitly.rb
Last active August 29, 2015 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/6feef8547b1ca817937b to your computer and use it in GitHub Desktop.
Save carlosdelfino/6feef8547b1ca817937b to your computer and use it in GitHub Desktop.
An Octopress / Jekyll plugin to provide a tag that automatically generates a bit.ly short-link for the current page in reference.
##
## @author Jacky Alcine <me@jalcine.me>
## @see [TODO: Add URL to post about bit.ly URLs.]
##
## Add in Bit.ly URL support to pages.
require "bitly"
# Ensure use of new API.
Bitly.use_api_version_3
module Jekyll
module Bitly
module Methods
private
def _config (option)
Jekyll.configuration({})["bitly_#{option}"]
end
public
def api_obj
login = _config("login")
api_key = _config("api_key")
::Bitly.new login, api_key
end
end
class ShortenTag < Liquid::Tag
include Jekyll::Bitly::Methods
def initialize (tag_name, text, tokens)
@bitly = api_obj
@link = text
super
end
def render (context)
@bitly.shorten(@link).short_url
end
end
end
end
Liquid::Template.register_tag('bitly_shorten', Jekyll::Bitly::ShortenTag)
bitly_api_key: FOOO
bitly_login: BARRR
<a href="{% bitly_shorten page.url %}">short link</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment