blog/plugins/gist_tag.rb

131 lines
3.9 KiB
Ruby
Raw Permalink Normal View History

# A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
# by: Brandon Tilly
# Source URL: https://gist.github.com/1027674
2011-04-18 03:49:30 +00:00
# Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html
#
# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin
2011-04-18 03:49:30 +00:00
require 'cgi'
2011-04-18 03:49:30 +00:00
require 'digest/md5'
require 'net/https'
require 'uri'
module Jekyll
class GistTag < Liquid::Tag
def initialize(tag_name, text, token)
super
@text = text
@cache_disabled = false
@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
2011-04-18 03:49:30 +00:00
end
def render(context)
if parts = @text.match(/([a-zA-Z\d]*) (.*)/)
gist, file = parts[1].strip, parts[2].strip
else
gist, file = @text.strip, ""
end
if gist.empty?
""
else
script_url = script_url_for gist, file
code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
html_output_for script_url, code
end
end
2011-04-18 03:49:30 +00:00
def html_output_for(script_url, code)
code = CGI.escapeHTML code
<<-HTML
<div><script src='#{script_url}'></script>
<noscript><pre><code>#{code}</code></pre></noscript></div>
HTML
end
2011-04-18 03:49:30 +00:00
def script_url_for(gist_id, filename)
2014-05-23 07:45:58 +00:00
url = "https://gist.github.com/#{gist_id}.js"
url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty?
url
2011-04-18 03:49:30 +00:00
end
def get_gist_url_for(gist, file)
"https://gist.githubusercontent.com/raw/#{gist}/#{file}"
2011-04-18 03:49:30 +00:00
end
def cache(gist, file, data)
cache_file = get_cache_file_for gist, file
File.open(cache_file, "w") do |io|
io.write data
2011-04-18 03:49:30 +00:00
end
end
def get_cached_gist(gist, file)
return nil if @cache_disabled
cache_file = get_cache_file_for gist, file
File.read cache_file if File.exist? cache_file
2011-04-18 03:49:30 +00:00
end
def get_cache_file_for(gist, file)
bad_chars = /[^a-zA-Z0-9\-_.]/
gist = gist.gsub bad_chars, ''
file = file.gsub bad_chars, ''
md5 = Digest::MD5.hexdigest "#{gist}-#{file}"
2011-04-18 03:49:30 +00:00
File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache"
end
def get_gist_from_web(gist, file)
2013-10-06 00:52:12 +00:00
gist_url = get_gist_url_for(gist, file)
data = get_web_content(gist_url)
locations = Array.new
while (data.code.to_i == 301 || data.code.to_i == 302)
2013-10-06 00:52:12 +00:00
data = handle_gist_redirecting(data)
break if locations.include? data.header['Location']
locations << data.header['Location']
2013-10-06 00:52:12 +00:00
end
2013-09-15 13:56:22 +00:00
if data.code.to_i != 200
raise RuntimeError, "Gist replied with #{data.code} for #{gist_url}"
end
2013-10-06 00:52:12 +00:00
cache(gist, file, data.body) unless @cache_disabled
data.body
2013-09-15 13:56:22 +00:00
end
def handle_gist_redirecting(data)
2013-10-06 00:52:12 +00:00
redirected_url = data.header['Location']
2013-10-06 00:57:03 +00:00
if redirected_url.nil? || redirected_url.empty?
raise ArgumentError, "GitHub replied with a 302 but didn't provide a location in the response headers."
end
2013-10-06 00:52:12 +00:00
get_web_content(redirected_url)
2013-09-15 13:56:22 +00:00
end
def get_web_content(url)
raw_uri = URI.parse url
2011-10-18 08:36:45 +00:00
proxy = ENV['http_proxy']
if proxy
proxy_uri = URI.parse(proxy)
https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port
else
https = Net::HTTP.new raw_uri.host, raw_uri.port
end
2011-04-18 03:49:30 +00:00
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new raw_uri.request_uri
data = https.request request
2011-04-18 03:49:30 +00:00
end
end
class GistTagNoCache < GistTag
def initialize(tag_name, text, token)
super
@cache_disabled = true
2011-04-18 03:49:30 +00:00
end
end
end
Liquid::Template.register_tag('gist', Jekyll::GistTag)
Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)