10
10
module Jekyll
11
11
module Gist
12
12
class GistTag < Liquid ::Tag
13
+ def self . client
14
+ @client ||= Octokit ::Client . new :access_token => ENV [ "JEKYLL_GITHUB_TOKEN" ]
15
+ end
16
+
13
17
def render ( context )
14
18
@encoding = context . registers [ :site ] . config [ "encoding" ] || "utf-8"
15
19
@settings = context . registers [ :site ] . config [ "gist" ]
16
20
if ( tag_contents = determine_arguments ( @markup . strip ) )
17
- gist_id = tag_contents [ 0 ]
21
+ gist_id = tag_contents [ 0 ]
18
22
filename = tag_contents [ 1 ]
19
- gist_id = context [ gist_id ] if context_contains_key? ( context , gist_id )
20
- filename = context [ filename ] if context_contains_key? ( context , filename )
23
+ gist_id = context [ gist_id ] if context . key? ( gist_id )
24
+ filename = context [ filename ] if context . key? ( filename )
25
+
21
26
noscript_tag = gist_noscript_tag ( gist_id , filename )
22
- script_tag = gist_script_tag ( gist_id , filename )
27
+ script_tag = gist_script_tag ( gist_id , filename )
28
+
23
29
"#{ noscript_tag } #{ script_tag } "
24
30
else
25
31
raise ArgumentError , <<~ERROR
@@ -44,36 +50,25 @@ def determine_arguments(input)
44
50
[ matched [ 1 ] . strip , matched [ 2 ] . strip ] if matched && matched . length >= 3
45
51
end
46
52
47
- def context_contains_key? ( context , key )
48
- if context . respond_to? ( :has_key? )
49
- context . has_key? ( key )
50
- else
51
- context . key? ( key )
52
- end
53
- end
54
-
55
53
def gist_script_tag ( gist_id , filename = nil )
56
54
url = "https://gist.github.com/#{ gist_id } .js"
57
55
url = "#{ url } ?file=#{ filename } " unless filename . to_s . empty?
56
+
58
57
"<script src=\" #{ url } \" > </script>"
59
58
end
60
59
61
60
def gist_noscript_tag ( gist_id , filename = nil )
62
61
return if @settings && @settings [ "noscript" ] == false
63
62
64
63
code = fetch_raw_code ( gist_id , filename )
65
- if ! code . nil?
64
+ if code
66
65
code = code . force_encoding ( @encoding )
67
- code = CGI . escapeHTML ( code )
68
-
69
- # CGI.escapeHTML behavior differs in Ruby < 2.0
70
- # See https://github.com/jekyll/jekyll-gist/pull/28
71
- code = code . gsub ( "'" , "'" ) if RUBY_VERSION < "2.0"
66
+ code = CGI . escapeHTML ( code ) . gsub ( "'" , "'" )
72
67
73
68
"<noscript><pre>#{ code } </pre></noscript>"
74
69
else
75
- Jekyll . logger . warn "Warning:" , "The <noscript> tag for your gist #{ gist_id } "
76
- Jekyll . logger . warn "" , "could not be generated. This will affect users who do "
70
+ Jekyll . logger . warn "Warning:" , "The <noscript> tag for your gist #{ gist_id } "
71
+ Jekyll . logger . warn "" , "could not be generated. This will affect users who do"
77
72
Jekyll . logger . warn "" , "not have JavaScript enabled in their browsers."
78
73
end
79
74
end
@@ -84,6 +79,7 @@ def fetch_raw_code(gist_id, filename = nil)
84
79
url = "https://gist.githubusercontent.com/#{ gist_id } /raw"
85
80
url = "#{ url } /#{ filename } " unless filename . to_s . empty?
86
81
uri = URI ( url )
82
+
87
83
Net ::HTTP . start ( uri . host , uri . port ,
88
84
:use_ssl => uri . scheme == "https" ,
89
85
:read_timeout => 3 , :open_timeout => 3 ) do |http |
@@ -97,7 +93,6 @@ def fetch_raw_code(gist_id, filename = nil)
97
93
98
94
def code_from_api ( gist_id , filename = nil )
99
95
gist = GistTag . client . gist gist_id
100
-
101
96
file = if filename . to_s . empty?
102
97
# No file specified, return the value of the first key/value pair
103
98
gist . files . first [ 1 ]
@@ -111,10 +106,6 @@ def code_from_api(gist_id, filename = nil)
111
106
112
107
file [ :content ] if file
113
108
end
114
-
115
- def self . client
116
- @client ||= Octokit ::Client . new :access_token => ENV [ "JEKYLL_GITHUB_TOKEN" ]
117
- end
118
109
end
119
110
end
120
111
end
0 commit comments