2011-06-15 22:31:22 +00:00
|
|
|
# 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
|
|
|
|
#
|
2011-06-15 22:31:22 +00:00
|
|
|
# Example usage: {% gist 1027674 gist_tag.rb %} //embeds a gist for this plugin
|
2011-04-18 03:49:30 +00:00
|
|
|
|
2011-06-15 22:31:22 +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
|
2011-06-15 22:31:22 +00:00
|
|
|
@text = text
|
|
|
|
@cache_disabled = false
|
2011-09-18 11:45:25 +00:00
|
|
|
@cache_folder = File.expand_path "../.gist-cache", File.dirname(__FILE__)
|
2011-06-15 22:31:22 +00:00
|
|
|
FileUtils.mkdir_p @cache_folder
|
2011-04-18 03:49:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
2011-06-15 22:31:22 +00:00
|
|
|
if parts = @text.match(/([\d]*) (.*)/)
|
|
|
|
gist, file = parts[1].strip, parts[2].strip
|
|
|
|
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
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
2011-04-18 03:49:30 +00:00
|
|
|
|
2011-06-15 22:31:22 +00:00
|
|
|
def html_output_for(script_url, code)
|
|
|
|
code = CGI.escapeHTML code
|
|
|
|
<<-HTML
|
2011-07-20 04:41:21 +00:00
|
|
|
<div><script src='#{script_url}'></script>
|
|
|
|
<noscript><pre><code>#{code}</code></pre></noscript></div>
|
2011-06-15 22:31:22 +00:00
|
|
|
HTML
|
|
|
|
end
|
2011-04-18 03:49:30 +00:00
|
|
|
|
2011-06-15 22:31:22 +00:00
|
|
|
def script_url_for(gist_id, filename)
|
|
|
|
"https://gist.github.com/#{gist_id}.js?file=#{filename}"
|
2011-04-18 03:49:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_gist_url_for(gist, file)
|
2011-06-15 22:31:22 +00:00
|
|
|
"https://raw.github.com/gist/#{gist}/#{file}"
|
2011-04-18 03:49:30 +00:00
|
|
|
end
|
|
|
|
|
2011-06-15 22:31:22 +00:00
|
|
|
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)
|
2011-06-15 22:31:22 +00:00
|
|
|
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)
|
2011-06-15 22:31:22 +00:00
|
|
|
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)
|
2011-06-15 22:31:22 +00:00
|
|
|
gist_url = get_gist_url_for gist, file
|
|
|
|
raw_uri = URI.parse gist_url
|
|
|
|
https = Net::HTTP.new raw_uri.host, raw_uri.port
|
2011-04-18 03:49:30 +00:00
|
|
|
https.use_ssl = true
|
|
|
|
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
2011-06-15 22:31:22 +00:00
|
|
|
request = Net::HTTP::Get.new raw_uri.request_uri
|
|
|
|
data = https.request request
|
2011-04-18 03:49:30 +00:00
|
|
|
data = data.body
|
2011-06-15 22:31:22 +00:00
|
|
|
cache gist, file, data unless @cache_disabled
|
2011-04-18 03:49:30 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GistTagNoCache < GistTag
|
|
|
|
def initialize(tag_name, text, token)
|
|
|
|
super
|
2011-06-15 22:31:22 +00:00
|
|
|
@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)
|