-
-
Save chrisjacob/905511 to your computer and use it in GitHub Desktop.
Revisions
-
BinaryMuse revised this gist
Mar 12, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -46,7 +46,7 @@ def cache(gist, file, data) 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 end def get_cache_file_for(gist, file) -
BinaryMuse revised this gist
Mar 12, 2011 . 1 changed file with 35 additions and 28 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ require 'cgi' require 'digest/md5' require 'net/https' require 'uri' @@ -6,71 +7,77 @@ 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(/([\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 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(gist_id, filename) "https://gist.github.com/#{gist_id}.js?file=#{filename}" end def get_gist_url_for(gist, file) "https://gist.github.com/raw/#{gist}/#{file}" end def cache(gist, file, data) cache_file = get_cache_file_for gist, file File.open(cache_file, "w") do |io| io.write data 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) 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}" File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" end def get_gist_from_web(gist, file) gist_url = get_gist_url_for 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 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) -
BinaryMuse revised this gist
Mar 12, 2011 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -43,9 +43,10 @@ def get_cached_gist(gist, file) 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}" File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" end -
BinaryMuse revised this gist
Mar 12, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,7 @@ def get_cached_gist(gist, file) return nil if @cache == false file = get_cache_file_for gist, file return nil unless File.exist?(file) return File.new(file).read end def get_cache_file_for(gist, file) -
BinaryMuse revised this gist
Jan 31, 2011 . 1 changed file with 13 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,11 +7,12 @@ class GistTag < Liquid::Tag def initialize(tag_name, text, token) super @text = text @cache = true @cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__) end def render(context) return "" unless @text =~ /([\d]*) (.*)/ gist, file = $1.strip, $2.strip script_url = "https://gist.github.com/#{gist}.js?file=#{file}" @@ -35,6 +36,7 @@ def cache_gist(gist, file, data) end def get_cached_gist(gist, file) return nil if @cache == false file = get_cache_file_for gist, file return nil unless File.exist?(file) return File.new(file).readlines.join @@ -56,10 +58,18 @@ def get_gist_from_web(gist, file) request = Net::HTTP::Get.new(raw_uri.request_uri) data = https.request(request) data = data.body cache_gist(gist, file, data) unless @cache == false data end end class GistTagNoCache < GistTag def initialize(tag_name, text, token) super @cache = false end end end Liquid::Template.register_tag('gist', Jekyll::GistTag) Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache) -
BinaryMuse revised this gist
Jan 31, 2011 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ def initialize(tag_name, text, token) end def render(context) return "" unless @text =~ /([\d]*) (.*)( no-cache)?/ gist, file = $1.strip, $2.strip script_url = "https://gist.github.com/#{gist}.js?file=#{file}" @@ -23,7 +23,7 @@ def render(context) return string end def get_gist_url_for(gist, file) "https://gist.github.com/raw/#{gist}/#{file}" end @@ -43,11 +43,12 @@ def get_cached_gist(gist, file) def get_cache_file_for(gist, file) gist.gsub! /[^a-zA-Z0-9\-_\.]/, '' file.gsub! /[^a-zA-Z0-9\-_\.]/, '' md5 = Digest::MD5.hexdigest "#{gist}-#{file}" File.join @cache_folder, "#{gist}-#{file}-#{md5}.cache" end def get_gist_from_web(gist, file) gist_url = get_gist_url_for(gist, file) raw_uri = URI.parse(gist_url) https = Net::HTTP.new(raw_uri.host, raw_uri.port) https.use_ssl = true -
BinaryMuse revised this gist
Jan 31, 2011 . 1 changed file with 16 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,42 +15,47 @@ def render(context) gist, file = $1.strip, $2.strip script_url = "https://gist.github.com/#{gist}.js?file=#{file}" code = get_cached_gist(gist, file) || get_gist_from_web(gist, file) code = code.gsub "<", "<" string = "<script src='#{script_url}'></script>" string += "<noscript><pre><code>#{code}</code></pre></noscript>" return string end def get_gist_url(gist, file) "https://gist.github.com/raw/#{gist}/#{file}" end def cache_gist(gist, file, data) file = get_cache_file_for gist, file File.open(file, "w+") do |f| f.write(data) end end def get_cached_gist(gist, file) file = get_cache_file_for gist, file return nil unless File.exist?(file) return File.new(file).readlines.join end def get_cache_file_for(gist, file) gist.gsub! /[^a-zA-Z0-9\-_\.]/, '' file.gsub! /[^a-zA-Z0-9\-_\.]/, '' File.join @cache_folder, "#{gist}-#{file}.cache" end def get_gist_from_web(gist, file) gist_url = get_gist_url(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_gist(gist, file, data) data end end -
BinaryMuse revised this gist
Jan 31, 2011 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -18,10 +18,10 @@ def render(context) link_url = "https://gist.github.com/#{gist}#file_#{file}" raw_url = "https://gist.github.com/raw/#{gist}/#{file}" code = get_cached_gist(raw_url) || get_gist_from_web(raw_url) code = code.gsub "<", "<" string = "<script src='#{script_url}'></script>" string += "<noscript><pre><code>#{code}</code></pre></noscript>" return string end -
BinaryMuse revised this gist
Jan 31, 2011 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,11 +13,13 @@ def initialize(tag_name, text, token) def render(context) return "" unless @text =~ /([\d]*) (.*)/ gist, file = $1.strip, $2.strip script_url = "https://gist.github.com/#{gist}.js?file=#{file}" link_url = "https://gist.github.com/#{gist}#file_#{file}" raw_url = "https://gist.github.com/raw/#{gist}/#{file}" code = get_cached_gist(raw_url) || get_gist_from_web(raw_url) code = code.gsub "<", "<" string = "<script src='#{script_url}'></script>" string += "<noscript><pre><code>#{code}</code></pre></noscript>" return string -
BinaryMuse created this gist
Jan 31, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ require 'digest/md5' require 'net/https' require 'uri' module Jekyll class GistTag < Liquid::Tag def initialize(tag_name, text, token) super @text = text @cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__) end def render(context) return "" unless @text =~ /([\d]*) (.*)/ script_url = "https://gist.github.com/#{$1}.js?file=#{$2}" link_url = "https://gist.github.com/#{$1}#file_#{$2}" raw_url = "https://gist.github.com/raw/#{$1}/#{$2}" code = get_cached_gist(raw_url) || get_gist_from_web(raw_url) string = "<script src='#{script_url}'></script>" string += "<noscript><pre><code>#{code}</code></pre></noscript>" return string end def cache_gist(gist_url, data) file = get_cache_file_for gist_url File.open(file, "w+") do |f| f.write(data) end end def get_cached_gist(gist_url) file = get_cache_file_for gist_url return nil unless File.exist?(file) return File.new(file).readlines.join end def get_cache_file_for(gist_url) File.join @cache_folder, "#{Digest::MD5.hexdigest(gist_url)}.cache" end def get_gist_from_web(gist_url) 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_gist(gist_url, data) data end end end Liquid::Template.register_tag('gist', Jekyll::GistTag)