Skip to content

Inherit Jekyll's rubocop config for consistency #48

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 2 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
inherit_gem:
jekyll: .rubocop.yml

Metrics/LineLength:
Exclude:
- spec/**/*
- jekyll-gist.gemspec
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
source "https://rubygems.org"
gemspec

if ENV["GH_PAGES"]
Expand Down
13 changes: 7 additions & 6 deletions jekyll-gist.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)

lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jekyll-gist/version'
require "jekyll-gist/version"

Gem::Specification.new do |spec|
spec.name = "jekyll-gist"
spec.version = Jekyll::Gist::VERSION
spec.authors = ["Parker Moore"]
spec.email = ["[email protected]"]
spec.summary = %q{Liquid tag for displaying GitHub Gists in Jekyll sites.}
spec.summary = "Liquid tag for displaying GitHub Gists in Jekyll sites."
spec.homepage = "https://github.com/jekyll/jekyll-gist"
spec.license = "MIT"

spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = ">= 1.9.3"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r!^(test|spec|features)/!)
spec.require_paths = ["lib"]

spec.add_dependency "octokit", "~> 4.2"
Expand Down
42 changes: 21 additions & 21 deletions lib/jekyll-gist/gist_tag.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'cgi'
require 'net/http'
require 'octokit'
require "cgi"
require "net/http"
require "octokit"

Net::OpenTimeout = Class.new(RuntimeError) unless Net.const_defined?(:OpenTimeout)
Net::ReadTimeout = Class.new(RuntimeError) unless Net.const_defined?(:ReadTimeout)

module Jekyll
module Gist
class GistTag < Liquid::Tag

def render(context)
@encoding = context.registers[:site].config['encoding'] || 'utf-8'
@settings = context.registers[:site].config['gist']
@encoding = context.registers[:site].config["encoding"] || "utf-8"
@settings = context.registers[:site].config["gist"]
if tag_contents = determine_arguments(@markup.strip)
gist_id, filename = tag_contents[0], tag_contents[1]
gist_id = tag_contents[0]
filename = tag_contents[1]
if context_contains_key?(context, gist_id)
gist_id = context[gist_id]
end
Expand All @@ -24,7 +24,7 @@ def render(context)
script_tag = gist_script_tag(gist_id, filename)
"#{noscript_tag}#{script_tag}"
else
raise ArgumentError.new <<-eos
raise ArgumentError, <<-eos
Syntax error in tag 'gist' while parsing the following markup:

#{@markup}
Expand All @@ -41,7 +41,7 @@ def render(context)
private

def determine_arguments(input)
matched = input.match(/\A([\S]+|.*(?=\/).+)\s?(\S*)\Z/)
matched = input.match(%r!\A([\S]+|.*(?=\/).+)\s?(\S*)\Z!)
[matched[1].strip, matched[2].strip] if matched && matched.length >= 3
end

Expand Down Expand Up @@ -87,8 +87,8 @@ def fetch_raw_code(gist_id, filename = nil)
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|
:use_ssl => uri.scheme == "https",
:read_timeout => 3, :open_timeout => 3) do |http|
request = Net::HTTP::Get.new uri.to_s
response = http.request(request)
response.body
Expand All @@ -103,15 +103,15 @@ 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]
else
# .files is a hash of :"filename.extension" => data pairs
# Rather than using to_sym on arbitrary user input,
# Find our file by calling to_s on the keys
match = gist.files.find { |name, data| name.to_s == filename }
match[1] if match
end
# No file specified, return the value of the first key/value pair
gist.files.first[1]
else
# .files is a hash of :"filename.extension" => data pairs
# Rather than using to_sym on arbitrary user input,
# Find our file by calling to_s on the keys
match = gist.files.find { |name, _data| name.to_s == filename }
match[1] if match
end

file[:content] if file
end
Expand All @@ -123,4 +123,4 @@ def self.client
end
end

Liquid::Template.register_tag('gist', Jekyll::Gist::GistTag)
Liquid::Template.register_tag("gist", Jekyll::Gist::GistTag)
2 changes: 1 addition & 1 deletion lib/jekyll-gist/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jekyll
module Gist
VERSION = "1.4.1"
VERSION = "1.4.1".freeze
end
end
89 changes: 42 additions & 47 deletions spec/gist_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'
require "spec_helper"

describe(Jekyll::Gist::GistTag) do
let(:http_output) { "<test>true</test>" }
let(:doc) { doc_with_content(content) }
let(:content) { "{% gist #{gist} %}" }
let(:content) { "{% gist #{gist} %}" }
let(:output) do
doc.content = content
doc.output = Jekyll::Renderer.new(doc.site, doc).run
Expand All @@ -13,139 +13,138 @@

context "valid gist" do
context "with user prefix" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
let(:gist) { "mattr-/24081a1d93d2898ecf0f" }

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "without user prefix" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
let(:gist) { "28949e1d5ee2273f9fd3" }

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "classic Gist id style" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw").to_return(:body => http_output) }
let(:gist) { "1234321" }

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>!)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "with file specified" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw/#{filename}").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist}/raw/#{filename}").to_return(:body => http_output) }
let(:gist) { "mattr-/24081a1d93d2898ecf0f" }
let(:filename) { "myfile.ext" }
let(:content) { "{% gist #{gist} #{filename} %}" }

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>!)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "with variable gist id" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(:body => http_output) }
let(:gist_id) { "1342013" }
let(:gist) { "page.gist_id" }
let(:output) do
doc.data['gist_id'] = gist_id
doc.data["gist_id"] = gist_id
doc.content = content
doc.output = Jekyll::Renderer.new(doc.site, doc).run
end

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>!)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "with variable gist id and filename" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(body: http_output) }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:body => http_output) }
let(:gist_id) { "1342013" }
let(:gist_filename) { "atom.xml" }
let(:gist) { "page.gist_id" }
let(:filename) { "page.gist_filename" }
let(:content) { "{% gist #{gist} #{filename} %}" }
let(:output) do
doc.data['gist_id'] = "1342013"
doc.data['gist_filename'] = "atom.xml"
doc.data["gist_id"] = "1342013"
doc.data["gist_filename"] = "atom.xml"
doc.content = content
doc.output = Jekyll::Renderer.new(doc.site, doc).run
end

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>!)
end

it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end
end

context "with valid gist id and invalid filename" do
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(status: 404) }
let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:status => 404) }
let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
let(:gist_filename) { "myfile.ext" }
let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }
let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }

it "produces the correct script tag" do
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>/)
expect(output).to match(%r!<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>!)
end

it "does not produce the noscript tag" do
expect(output).to_not match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
expect(output).to_not match(%r!<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n!)
end

end

context "with token" do
before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" }
before {
stub_request(:get, "https://api.github.com/gists/1342013").
to_return(:status => 200, :body => fixture("single-file"), :headers => {"Content-Type" => "application/json"})
}
before do
stub_request(:get, "https://api.github.com/gists/1342013")
.to_return(:status => 200, :body => fixture("single-file"), :headers => { "Content-Type" => "application/json" })
end
let(:gist_id) { "1342013" }
let(:gist) { "page.gist_id" }
let(:output) do
doc.data['gist_id'] = gist_id
doc.data["gist_id"] = gist_id
doc.content = content
doc.output = Jekyll::Renderer.new(doc.site, doc).run
end

it "produces the noscript tag" do
expect(output).to match(/<noscript><pre>contents of gist<\/pre><\/noscript>/)
expect(output).to match(%r!<noscript><pre>contents of gist<\/pre><\/noscript>!)
end

context "with a filename" do
before {
stub_request(:get, "https://api.github.com/gists/1342013").
to_return(:status => 200, :body => fixture("multiple-files"), :headers => {"Content-Type" => "application/json"})
}
let(:content) { "{% gist 1342013 hello-world.rb %}" }
before do
stub_request(:get, "https://api.github.com/gists/1342013")
.to_return(:status => 200, :body => fixture("multiple-files"), :headers => { "Content-Type" => "application/json" })
end
let(:content) { "{% gist 1342013 hello-world.rb %}" }

it "produces the noscript tag" do
expect(output).to match(/<noscript><pre>puts &#39;hello world&#39;<\/pre><\/noscript>/)
expect(output).to match(%r!<noscript><pre>puts &#39;hello world&#39;<\/pre><\/noscript>!)
end
end
end
Expand All @@ -159,22 +158,18 @@
let(:gist) { "mattr-/24081a1d93d2898ecf0f" }

it "does not produce the noscript tag" do
expect(output).to_not match(/<noscript>/)
expect(output).to_not match(%r!<noscript>!)
end
end

end

context "invalid gist" do

context "no gist id present" do
let(:gist) { "" }

it "raises an error" do
expect(->{ output }).to raise_error
expect(-> { output }).to raise_error
end
end

end

end
Loading