Skip to content

Refactor GistTag #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 16, 2019
13 changes: 0 additions & 13 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/IneffectiveAccessModifier:
Exclude:
- 'lib/jekyll-gist/gist_tag.rb'

# Offense count: 1
Lint/ShadowedException:
Exclude:
Expand All @@ -21,11 +16,3 @@ Lint/ShadowedException:
Metrics/AbcSize:
Exclude:
- 'lib/jekyll-gist/gist_tag.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: short, verbose
Style/PreferredHashMethods:
Exclude:
- 'lib/jekyll-gist/gist_tag.rb'
41 changes: 16 additions & 25 deletions lib/jekyll-gist/gist_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
module Jekyll
module Gist
class GistTag < Liquid::Tag
def self.client
@client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
end

def render(context)
@encoding = context.registers[:site].config["encoding"] || "utf-8"
@settings = context.registers[:site].config["gist"]
if (tag_contents = determine_arguments(@markup.strip))
gist_id = tag_contents[0]
gist_id = tag_contents[0]
filename = tag_contents[1]
gist_id = context[gist_id] if context_contains_key?(context, gist_id)
filename = context[filename] if context_contains_key?(context, filename)
gist_id = context[gist_id] if context.key?(gist_id)
filename = context[filename] if context.key?(filename)

noscript_tag = gist_noscript_tag(gist_id, filename)
script_tag = gist_script_tag(gist_id, filename)
script_tag = gist_script_tag(gist_id, filename)

"#{noscript_tag}#{script_tag}"
else
raise ArgumentError, <<~ERROR
Expand All @@ -44,36 +50,25 @@ def determine_arguments(input)
[matched[1].strip, matched[2].strip] if matched && matched.length >= 3
end

def context_contains_key?(context, key)
if context.respond_to?(:has_key?)
context.has_key?(key)
else
context.key?(key)
end
end

def gist_script_tag(gist_id, filename = nil)
url = "https://gist.github.com/#{gist_id}.js"
url = "#{url}?file=#{filename}" unless filename.to_s.empty?

"<script src=\"#{url}\"> </script>"
end

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

code = fetch_raw_code(gist_id, filename)
if !code.nil?
if code
code = code.force_encoding(@encoding)
code = CGI.escapeHTML(code)

# CGI.escapeHTML behavior differs in Ruby < 2.0
# See https://github.com/jekyll/jekyll-gist/pull/28
code = code.gsub("'", "&#39;") if RUBY_VERSION < "2.0"
code = CGI.escapeHTML(code).gsub("'", "&#39;")

"<noscript><pre>#{code}</pre></noscript>"
else
Jekyll.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id} "
Jekyll.logger.warn "", "could not be generated. This will affect users who do "
Jekyll.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id}"
Jekyll.logger.warn "", "could not be generated. This will affect users who do"
Jekyll.logger.warn "", "not have JavaScript enabled in their browsers."
end
end
Expand All @@ -84,6 +79,7 @@ def fetch_raw_code(gist_id, filename = nil)
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
url = "#{url}/#{filename}" unless filename.to_s.empty?
uri = URI(url)

Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == "https",
:read_timeout => 3, :open_timeout => 3) do |http|
Expand All @@ -97,7 +93,6 @@ def fetch_raw_code(gist_id, filename = nil)

def code_from_api(gist_id, filename = nil)
gist = GistTag.client.gist gist_id

file = if filename.to_s.empty?
# No file specified, return the value of the first key/value pair
gist.files.first[1]
Expand All @@ -111,10 +106,6 @@ def code_from_api(gist_id, filename = nil)

file[:content] if file
end

def self.client
@client ||= Octokit::Client.new :access_token => ENV["JEKYLL_GITHUB_TOKEN"]
end
end
end
end
Expand Down