-
-
Save chalasr/d4aaf9e08d4fc7c4a72c to your computer and use it in GitHub Desktop.
Embed gists in markdown files of a jekyll application by creating a custom Liquid tag
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 characters
| 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__) | |
| FileUtils.mkdir_p @cache_folder | |
| end | |
| def render(context) | |
| @text.strip! | |
| if @text.include? " " | |
| gist, *rest = @text.split(/ /) | |
| file = rest[0] | |
| else | |
| gist = @text | |
| end | |
| 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 | |
| def html_output_for(script_url, code) | |
| code = CGI.escapeHTML code | |
| "<script src='#{script_url}'></script><div><noscript><pre><code>#{code}</code></pre></noscript></div>" | |
| 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://raw.github.com/gist/#{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, '' | |
| unless file.nil? | |
| file = file.gsub bad_chars, '' | |
| end | |
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a fork of imathis/gist_tag.rb which is not working since gists ID contains none-digit characters.
Getting started :
_pluginsdirectory in your jekyll application.gist.rbin the_pluginsfolder.gist_tag.rbof this gist.Usage:
{% gist {gistID} {filename} %}The filename parameter is optional.