Created
August 15, 2012 17:53
-
-
Save tbjers/3361948 to your computer and use it in GitHub Desktop.
Liquid Filter to convert URLs to short URLs automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bitly' | |
module Jekyll | |
class BitlyFilterCache | |
def initialize | |
@result_cache = {} | |
config = Jekyll.configuration({}) | |
@username = config['bitly']['username'] | |
@key = config['bitly']['api_key'] | |
Bitly.use_api_version_3 | |
end | |
@@instance = BitlyFilterCache.new | |
def self.instance | |
@@instance | |
end | |
def shorten(input) | |
input.strip! | |
return @result_cache[input] if @result_cache.has_key?(input) | |
puts "Shortening #{input}..." | |
bitly = Bitly.new(@username, @key) | |
u = bitly.shorten(input, :history => 1) | |
@result_cache[input] = u.short_url | |
puts "New url: #{u.short_url}" | |
return u.short_url | |
end | |
end | |
module Filters | |
def bitly(input) | |
BitlyFilterCache.instance.shorten(input) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a link to <a href="http://xorcode.net/PZFMOb">Xorcode</a>. | |
You could also use a variable, http://xorcode.net/QCpD1G. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a link to [Xorcode]({{ 'http://xorcode.com/' | bitly }}). | |
You could also use a variable, {{ site.production_url + '/' + page.url | bitly }}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment