-
-
Save rvteja92/296248edef357bd7ecaea8606ff048d3 to your computer and use it in GitHub Desktop.
A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers (updated)
This file contains 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
# Updated for new gist urls | |
require 'cgi' | |
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__) | |
end | |
def render(context) | |
if parts = @text.match(/([^ ]*) ([^ ]*) ([^ ]*)/) | |
user, gist, file = parts[1].strip, parts[2].strip, parts[3].strip | |
script_url = script_url_for user, gist | |
code = get_cached_gist(user, gist, file) || get_gist_from_web(user, gist, file) | |
html_output_for script_url, code | |
else | |
"" | |
end | |
end | |
def html_output_for(script_url, code) | |
code = CGI.escapeHTML code | |
"<script src='#{script_url}'></script><noscript><pre><code>#{code}</code></pre></noscript>" | |
end | |
def script_url_for(user, gist_id) | |
"https://gist.github.com/#{user}/#{gist_id}.js" | |
end | |
def get_gist_url_for(user, gist, file) | |
"https://gist.githubusercontent.com/#{user}/#{gist}/#{file}" | |
end | |
def cache(user, gist, file, data) | |
cache_file = get_cache_file_for user, gist, file | |
File.open(cache_file, "w") do |io| | |
io.write data | |
end | |
end | |
def get_cached_gist(user, gist, file) | |
return nil if @cache_disabled | |
cache_file = get_cache_file_for user, gist, file | |
File.read cache_file if File.exist? cache_file | |
end | |
def get_cache_file_for(user, gist, file) | |
bad_chars = /[^a-zA-Z0-9\-_.]/ | |
user = user.gsub bad_chars, '' | |
gist = gist.gsub bad_chars, '' | |
file = file.gsub bad_chars, '' | |
md5 = Digest::MD5.hexdigest "#{user}-#{gist}-#{file}" | |
File.join @cache_folder, "#{user}-#{gist}-#{file}-#{md5}.cache" | |
end | |
def get_gist_from_web(user, gist, file) | |
gist_url = get_gist_url_for user, gist, file | |
raw_uri = URI.parse gist_url | |
https = Net::HTTP.new raw_uri.host, raw_uri.port | |
https.use_ssl = true | |
https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Get.new raw_uri.request_uri | |
data = https.request request | |
data = data.body | |
cache user, gist, file, data unless @cache_disabled | |
data | |
end | |
end | |
class GistTagNoCache < GistTag | |
def initialize(tag_name, text, token) | |
super | |
@cache_disabled = true | |
end | |
end | |
end | |
Liquid::Template.register_tag('gist', Jekyll::GistTag) | |
Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache) | |
# Usage | |
# {% gist username gist_id filename %} | |
# For example, for this file it is given in comments (because id is not known before committing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, to embed this gist in jekyll page, use
{% gist rvteja92 296248edef357bd7ecaea8606ff048d3 gist_tag.rb %}