Skip to content

Instantly share code, notes, and snippets.

@danvine
Created October 12, 2013 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvine/6951697 to your computer and use it in GitHub Desktop.
Save danvine/6951697 to your computer and use it in GitHub Desktop.
# place this file in _plugins/url2png.rb
# add this to your _config.yml
# url2png:
# apikey: <apikey>
# secret: <secret>
require 'liquid'
require 'cgi' unless defined?(CGI)
require 'digest' unless defined?(Digest)
class Url2png < Liquid::Tag
@@URL2PNG_PARAMETERS = ['viewport', 'thumbnail_max_width', 'fullpage']
# more options available at url2png.com/docs
def initialize(tag_name, text, tokens)
super
tokens = text.split /\,\s/
@url = tokens.shift
raise "No URL provided" if @url.nil?
raise "TODO: Relative URLs are tricky with Jekyll..." if @url =~ /^\//
@options = {}
tokens.each do |arg|
key, value = arg.split /:/
value ||= "true"
@options[key.strip] = value.strip
end
@options['url'] = @url
end
def render(context)
config = context.registers[:site].config['url2png']
@apikey = config['apikey']
@secret = config['secret']
raise "Url2png apikey/token required" if @apikey.nil? or @secret.nil?
generate_request
end
private
def generate_request
query_string = @options.sort.map { |k,v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join("&")
token = Digest::MD5.hexdigest(query_string + @secret)
"https://api.url2png.com/v6/#{@apikey}/#{token}/png/?#{query_string}"
end
Liquid::Template.register_tag "url2png", self
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment