Skip to content

Instantly share code, notes, and snippets.

@chrisjacob
Forked from BinaryMuse/gist_tag.rb
Created April 6, 2011 11:34
Show Gist options
  • Select an option

  • Save chrisjacob/905511 to your computer and use it in GitHub Desktop.

Select an option

Save chrisjacob/905511 to your computer and use it in GitHub Desktop.

Revisions

  1. @BinaryMuse BinaryMuse revised this gist Mar 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gist_tag.rb
    Original 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)
    File.read cache_file if File.exist? cache_file
    end

    def get_cache_file_for(gist, file)
  2. @BinaryMuse BinaryMuse revised this gist Mar 12, 2011. 1 changed file with 35 additions and 28 deletions.
    63 changes: 35 additions & 28 deletions gist_tag.rb
    Original 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 = true
    @cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__)
    @text = text
    @cache_disabled = false
    @cache_folder = File.expand_path "../_gist_cache", File.dirname(__FILE__)
    end

    def render(context)
    return "" unless @text =~ /([\d]*) (.*)/
    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

    gist, file = $1.strip, $2.strip
    script_url = "https://gist.github.com/#{gist}.js?file=#{file}"
    def html_output_for(script_url, code)
    code = CGI.escapeHTML code
    "<script src='#{script_url}'></script><noscript><pre><code>#{code}</code></pre></noscript>"
    end

    code = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
    code = code.gsub "<", "&lt;"
    string = "<script src='#{script_url}'></script>"
    string += "<noscript><pre><code>#{code}</code></pre></noscript>"
    return string
    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(gist, file, data)
    file = get_cache_file_for gist, file
    File.open(file, "w+") do |f|
    f.write(data)
    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 == false
    file = get_cache_file_for gist, file
    return nil unless File.exist?(file)
    return File.new(file).read
    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\-_\.]/
    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)
    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)
    request = Net::HTTP::Get.new raw_uri.request_uri
    data = https.request request
    data = data.body
    cache_gist(gist, file, data) unless @cache == false
    cache gist, file, data unless @cache_disabled
    data
    end
    end

    class GistTagNoCache < GistTag
    def initialize(tag_name, text, token)
    super
    @cache = false
    @cache_disabled = true
    end
    end
    end

    Liquid::Template.register_tag('gist', Jekyll::GistTag)
    Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
    Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
  3. @BinaryMuse BinaryMuse revised this gist Mar 12, 2011. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions gist_tag.rb
    Original 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)
    gist.gsub! /[^a-zA-Z0-9\-_\.]/, ''
    file.gsub! /[^a-zA-Z0-9\-_\.]/, ''
    md5 = Digest::MD5.hexdigest "#{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

  4. @BinaryMuse BinaryMuse revised this gist Mar 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gist_tag.rb
    Original 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).readlines.join
    return File.new(file).read
    end

    def get_cache_file_for(gist, file)
  5. @BinaryMuse BinaryMuse revised this gist Jan 31, 2011. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions gist_tag.rb
    Original 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]*) (.*)( no-cache)?/
    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)
    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('gist', Jekyll::GistTag)
    Liquid::Template.register_tag('gistnocache', Jekyll::GistTagNoCache)
  6. @BinaryMuse BinaryMuse revised this gist Jan 31, 2011. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions gist_tag.rb
    Original 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]*) (.*)/
    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(gist, file)
    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\-_\.]/, ''
    File.join @cache_folder, "#{gist}-#{file}.cache"
    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(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
  7. @BinaryMuse BinaryMuse revised this gist Jan 31, 2011. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions gist_tag.rb
    Original 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}"
    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 = get_cached_gist(gist, file) || get_gist_from_web(gist, file)
    code = code.gsub "<", "&lt;"
    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
    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_url)
    file = get_cache_file_for gist_url
    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_url)
    File.join @cache_folder, "#{Digest::MD5.hexdigest(gist_url)}.cache"
    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_url)
    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_url, data)
    cache_gist(gist, file, data)
    data
    end
    end
  8. @BinaryMuse BinaryMuse revised this gist Jan 31, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gist_tag.rb
    Original 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 "<", "&lt;"
    string = "<script src='#{script_url}'></script>"
    string += "<noscript><pre><code>#{code}</code></pre></noscript>"
    code = get_cached_gist(raw_url) || get_gist_from_web(raw_url)
    code = code.gsub "<", "&lt;"
    string = "<script src='#{script_url}'></script>"
    string += "<noscript><pre><code>#{code}</code></pre></noscript>"
    return string
    end

  9. @BinaryMuse BinaryMuse revised this gist Jan 31, 2011. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions gist_tag.rb
    Original 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]*) (.*)/

    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}"
    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 = get_cached_gist(raw_url) || get_gist_from_web(raw_url)
    code = code.gsub "<", "&lt;"
    string = "<script src='#{script_url}'></script>"
    string += "<noscript><pre><code>#{code}</code></pre></noscript>"
    return string
  10. @BinaryMuse BinaryMuse created this gist Jan 31, 2011.
    57 changes: 57 additions & 0 deletions gist_tag.rb
    Original 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)