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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
diff --cc plugins/gist_tag.rb
index 1620345,0000000..58acd2a
mode 100644,000000..100644
--- a/plugins/gist_tag.rb
+++ b/plugins/gist_tag.rb
@@@ -1,105 -1,0 +1,105 @@@
+# A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
+# by: Brandon Tilly
+# Source URL: https://gist.github.com/1027674
+# Post http://brandontilley.com/2011/01/31/gist-tag-for-jekyll.html
+#
+# Example usage: //embeds a gist for this plugin
+
+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)
- if parts = @text.match(/([a-zA-Z\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)
++ if parts = @text.match(/([a-zA-Z]*) ([a-zA-Z\d]*) (.*)/)
++ gist_id, gist, file = parts[1].strip, parts[2].strip, parts[3].strip
++ script_url = script_url_for gist_id, gist, file
++ code = get_cached_gist(gist_id, gist, file) || get_gist_from_web(gist_id, gist, file)
+ html_output_for script_url, code
+ else
+ ""
+ end
+ end
+
+ def html_output_for(script_url, code)
+ code = CGI.escapeHTML code
+ <<-HTML
+<div><script src='#{script_url}'></script>
+<noscript><pre><code>#{code}</code></pre></noscript></div>
+ HTML
+ end
+
- def script_url_for(gist_id, filename)
- url = "https://gist.github.com/#{gist_id}.js"
++ def script_url_for(gist_id, gist, filename)
++ url = "https://gist.github.com/#{gist_id}/#{gist}.js"
+ url = "#{url}?file=#{filename}" unless filename.nil? or filename.empty?
+ url
+ end
+
- def get_gist_url_for(gist, file)
- "https://raw.github.com/gist/#{gist}/#{file}"
++ def get_gist_url_for(gist_id, gist, file)
++ "https://gist.github.com/#{gist_id}/#{gist}/raw/#{file}"
+ end
+
- def cache(gist, file, data)
- cache_file = get_cache_file_for gist, file
++ def cache(gist_id, gist, file, data)
++ cache_file = get_cache_file_for gist_id, gist, file
+ File.open(cache_file, "w") do |io|
+ io.write data
+ end
+ end
+
- def get_cached_gist(gist, file)
++ def get_cached_gist(gist_id, gist, file)
+ return nil if @cache_disabled
- cache_file = get_cache_file_for gist, file
++ cache_file = get_cache_file_for gist_id, gist, file
+ File.read cache_file if File.exist? cache_file
+ end
+
- def get_cache_file_for(gist, file)
++ def get_cache_file_for(gist_id, 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"
++ md5 = Digest::MD5.hexdigest "#{gist_id}-#{gist}-#{file}"
++ File.join @cache_folder, "#{gist_id}-#{gist}-#{file}-#{md5}.cache"
+ end
+
- def get_gist_from_web(gist, file)
- gist_url = get_gist_url_for gist, file
++ def get_gist_from_web(gist_id, gist, file)
++ gist_url = get_gist_url_for gist_id, gist, file
+ raw_uri = URI.parse gist_url
+ proxy = ENV['http_proxy']
+ if proxy
+ proxy_uri = URI.parse(proxy)
+ https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new raw_uri.host, raw_uri.port
+ else
+ https = Net::HTTP.new raw_uri.host, raw_uri.port
+ end
+ https.use_ssl = true
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ request = Net::HTTP::Get.new raw_uri.request_uri
+ data = https.request request
+ if data.code.to_i != 200
+ raise RuntimeError, "Gist replied with #{data.code} for #{gist_url}"
+ end
+ data = data.body
- cache gist, file, data unless @cache_disabled
++ cache gist_id, 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)
|