Skip to content

Commit bc23c25

Browse files
ashmarolijekyllbot
authored andcommitted
Refactor GistTag (#65)
Merge pull request 65
1 parent ab3e64b commit bc23c25

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
Lint/IneffectiveAccessModifier:
11-
Exclude:
12-
- 'lib/jekyll-gist/gist_tag.rb'
13-
149
# Offense count: 1
1510
Lint/ShadowedException:
1611
Exclude:
@@ -21,11 +16,3 @@ Lint/ShadowedException:
2116
Metrics/AbcSize:
2217
Exclude:
2318
- 'lib/jekyll-gist/gist_tag.rb'
24-
25-
# Offense count: 1
26-
# Cop supports --auto-correct.
27-
# Configuration parameters: EnforcedStyle.
28-
# SupportedStyles: short, verbose
29-
Style/PreferredHashMethods:
30-
Exclude:
31-
- 'lib/jekyll-gist/gist_tag.rb'

lib/jekyll-gist/gist_tag.rb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@
1010
module Jekyll
1111
module Gist
1212
class GistTag < Liquid::Tag
13+
def self.client
14+
@client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
15+
end
16+
1317
def render(context)
1418
@encoding = context.registers[:site].config["encoding"] || "utf-8"
1519
@settings = context.registers[:site].config["gist"]
1620
if (tag_contents = determine_arguments(@markup.strip))
17-
gist_id = tag_contents[0]
21+
gist_id = tag_contents[0]
1822
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+
2126
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+
2329
"#{noscript_tag}#{script_tag}"
2430
else
2531
raise ArgumentError, <<~ERROR
@@ -44,36 +50,25 @@ def determine_arguments(input)
4450
[matched[1].strip, matched[2].strip] if matched && matched.length >= 3
4551
end
4652

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-
5553
def gist_script_tag(gist_id, filename = nil)
5654
url = "https://gist.github.com/#{gist_id}.js"
5755
url = "#{url}?file=#{filename}" unless filename.to_s.empty?
56+
5857
"<script src=\"#{url}\"> </script>"
5958
end
6059

6160
def gist_noscript_tag(gist_id, filename = nil)
6261
return if @settings && @settings["noscript"] == false
6362

6463
code = fetch_raw_code(gist_id, filename)
65-
if !code.nil?
64+
if code
6665
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("'", "&#39;") if RUBY_VERSION < "2.0"
66+
code = CGI.escapeHTML(code).gsub("'", "&#39;")
7267

7368
"<noscript><pre>#{code}</pre></noscript>"
7469
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"
7772
Jekyll.logger.warn "", "not have JavaScript enabled in their browsers."
7873
end
7974
end
@@ -84,6 +79,7 @@ def fetch_raw_code(gist_id, filename = nil)
8479
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
8580
url = "#{url}/#{filename}" unless filename.to_s.empty?
8681
uri = URI(url)
82+
8783
Net::HTTP.start(uri.host, uri.port,
8884
:use_ssl => uri.scheme == "https",
8985
:read_timeout => 3, :open_timeout => 3) do |http|
@@ -97,7 +93,6 @@ def fetch_raw_code(gist_id, filename = nil)
9793

9894
def code_from_api(gist_id, filename = nil)
9995
gist = GistTag.client.gist gist_id
100-
10196
file = if filename.to_s.empty?
10297
# No file specified, return the value of the first key/value pair
10398
gist.files.first[1]
@@ -111,10 +106,6 @@ def code_from_api(gist_id, filename = nil)
111106

112107
file[:content] if file
113108
end
114-
115-
def self.client
116-
@client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
117-
end
118109
end
119110
end
120111
end

0 commit comments

Comments
 (0)