public
Last active — forked from chrisjacob/gist_tag.rb

A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.

  • Download Gist
gist_tag.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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]*) (.*)/
 
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 "<", "&lt;"
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
end
 
Liquid::Template.register_tag('gist', Jekyll::GistTag)

Hi. Please could you share an example of how to use this plugin?

{% gist 1027674 %} will embed this gist.

Thanks. Using your code and example, I get the following error when I run jekyll

Configuration from C:/Users/Matt/Documents/matt-hickford.github.com/_config.yml
Building site: C:/Users/Matt/Documents/matt-hickford.github.com -> C:/Users/Matt/Documents/matt-hickford.github.com/_site

 ___________________________________________________________________________
| Maruku tells you:
+---------------------------------------------------------------------------
| Could you please format this better?
| I see that "<div><noscript><pre><code>require 'cgi'" is left after the raw HTML.
| At line 4
|       text     |Hello|
|      empty     ||
|   raw_html     |<script src='https://gist.github.com/1027674.js?file='></script><div><noscript><pre><code>require 'cgi'|
|       text --> |require 'digest/md5'|
|       text     |require 'net/https'|
|       text     |require 'uri'|
|      empty     ||
+---------------------------------------------------------------------------

If I run jekyll --server I can browse to the post. All my content from below the gist tag is lost, but the first half and the gist bit appears okay.

If you use RDiscount you shouldn't have that problem. Maruku seems to have a hard time with HTML mixed into markdown.

Anyway to port this over to a non jekyll based platform?

@esparkman I suppose that depends on the platform but theoretically yes, you should be able to build this feature for other platforms.

You can also embed private gists if you change line 17 to:

if parts = @text.match(/([\w]*) (.*)/)

This is because private Gist ID's not only contain digits but also alpha characters as well.

You could also do a pull against my fork (git://gist.github.com/3706958.git) if you have a local clone. :)

While playing around with embedding gists, I realized gists could be obtained in json format. The url is almost identical to the javascript url:

https://gist.github.com/jehoshua02/4681039.json?file=.gitignore

The html content is right there in the "div" property of the json string.

I've also been having problems with the gist tag in octopress. Even though I specify a file, all files in the gist are output. Very rarely do I want to embed every file from a gist, but would rather embed one file, explain it, embed another file, etc.

def get_gist_url_for(gist, filename)
  file = filename == '' ? '' : "/#{filename}"
  "https://raw.github.com/gist/#{gist}#{file}"
end

Without adding that second line, gist tags created with just an id and no filename don't work.

Works great, thanks!

The gist URL has changed as noted here (or with the urls generated for a gist):

http://stackoverflow.com/a/18835385/588702

Line 33 and line 37 need altering to include a username.

Same problem here. Any ideas on how to fix this without changing all the posts again?

Here's the fix: https://gist.github.com/creativepsyco/6821946 Changed the plugin to read the config and adjust the url accordingly . This means you can embed your own gists only

you would need to add your github username to Jekyll Config file though.

In _config.yml set github_user to your github username

@imathis Is it possible for you to fork the jekyll-project and replace the built in gist tag with your version? I think that would be a great improvement for jekyll!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.