Skip to content

Commit 5096794

Browse files
DirtyFjekyllbot
authored andcommitted
Inherit Jekyll's rubocop config for consistency (#48)
Merge pull request 48
1 parent 6ab6a20 commit 5096794

File tree

7 files changed

+91
-88
lines changed

7 files changed

+91
-88
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
inherit_gem:
2+
jekyll: .rubocop.yml
3+
4+
Metrics/LineLength:
5+
Exclude:
6+
- spec/**/*
7+
- jekyll-gist.gemspec

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22
gemspec
33

44
if ENV["GH_PAGES"]

jekyll-gist.gemspec

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
2+
3+
lib = File.expand_path("lib", __dir__)
34
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'jekyll-gist/version'
5+
require "jekyll-gist/version"
56

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

15-
spec.required_ruby_version = '>= 1.9.3'
16+
spec.required_ruby_version = ">= 1.9.3"
1617

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

2223
spec.add_dependency "octokit", "~> 4.2"

lib/jekyll-gist/gist_tag.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
require 'cgi'
2-
require 'net/http'
3-
require 'octokit'
1+
require "cgi"
2+
require "net/http"
3+
require "octokit"
44

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

88
module Jekyll
99
module Gist
1010
class GistTag < Liquid::Tag
11-
1211
def render(context)
13-
@encoding = context.registers[:site].config['encoding'] || 'utf-8'
14-
@settings = context.registers[:site].config['gist']
12+
@encoding = context.registers[:site].config["encoding"] || "utf-8"
13+
@settings = context.registers[:site].config["gist"]
1514
if tag_contents = determine_arguments(@markup.strip)
16-
gist_id, filename = tag_contents[0], tag_contents[1]
15+
gist_id = tag_contents[0]
16+
filename = tag_contents[1]
1717
if context_contains_key?(context, gist_id)
1818
gist_id = context[gist_id]
1919
end
@@ -24,7 +24,7 @@ def render(context)
2424
script_tag = gist_script_tag(gist_id, filename)
2525
"#{noscript_tag}#{script_tag}"
2626
else
27-
raise ArgumentError.new <<-eos
27+
raise ArgumentError, <<-eos
2828
Syntax error in tag 'gist' while parsing the following markup:
2929
3030
#{@markup}
@@ -41,7 +41,7 @@ def render(context)
4141
private
4242

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

@@ -87,8 +87,8 @@ def fetch_raw_code(gist_id, filename = nil)
8787
url = "#{url}/#{filename}" unless filename.to_s.empty?
8888
uri = URI(url)
8989
Net::HTTP.start(uri.host, uri.port,
90-
use_ssl: uri.scheme == 'https',
91-
read_timeout: 3, open_timeout: 3) do |http|
90+
:use_ssl => uri.scheme == "https",
91+
:read_timeout => 3, :open_timeout => 3) do |http|
9292
request = Net::HTTP::Get.new uri.to_s
9393
response = http.request(request)
9494
response.body
@@ -103,15 +103,15 @@ def code_from_api(gist_id, filename = nil)
103103
gist = GistTag.client.gist gist_id
104104

105105
file = if filename.to_s.empty?
106-
# No file specified, return the value of the first key/value pair
107-
gist.files.first[1]
108-
else
109-
# .files is a hash of :"filename.extension" => data pairs
110-
# Rather than using to_sym on arbitrary user input,
111-
# Find our file by calling to_s on the keys
112-
match = gist.files.find { |name, data| name.to_s == filename }
113-
match[1] if match
114-
end
106+
# No file specified, return the value of the first key/value pair
107+
gist.files.first[1]
108+
else
109+
# .files is a hash of :"filename.extension" => data pairs
110+
# Rather than using to_sym on arbitrary user input,
111+
# Find our file by calling to_s on the keys
112+
match = gist.files.find { |name, _data| name.to_s == filename }
113+
match[1] if match
114+
end
115115

116116
file[:content] if file
117117
end
@@ -123,4 +123,4 @@ def self.client
123123
end
124124
end
125125

126-
Liquid::Template.register_tag('gist', Jekyll::Gist::GistTag)
126+
Liquid::Template.register_tag("gist", Jekyll::Gist::GistTag)

lib/jekyll-gist/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Jekyll
22
module Gist
3-
VERSION = "1.4.1"
3+
VERSION = "1.4.1".freeze
44
end
55
end

spec/gist_tag_spec.rb

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
require 'spec_helper'
1+
require "spec_helper"
22

33
describe(Jekyll::Gist::GistTag) do
44
let(:http_output) { "<test>true</test>" }
55
let(:doc) { doc_with_content(content) }
6-
let(:content) { "{% gist #{gist} %}" }
6+
let(:content) { "{% gist #{gist} %}" }
77
let(:output) do
88
doc.content = content
99
doc.output = Jekyll::Renderer.new(doc.site, doc).run
@@ -13,139 +13,138 @@
1313

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

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

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

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

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

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

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

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

6565
context "with variable gist id" do
66-
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(body: http_output) }
66+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(:body => http_output) }
6767
let(:gist_id) { "1342013" }
6868
let(:gist) { "page.gist_id" }
6969
let(:output) do
70-
doc.data['gist_id'] = gist_id
70+
doc.data["gist_id"] = gist_id
7171
doc.content = content
7272
doc.output = Jekyll::Renderer.new(doc.site, doc).run
7373
end
7474

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

8383
context "with variable gist id and filename" do
84-
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(body: http_output) }
84+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(:body => http_output) }
8585
let(:gist_id) { "1342013" }
8686
let(:gist_filename) { "atom.xml" }
8787
let(:gist) { "page.gist_id" }
8888
let(:filename) { "page.gist_filename" }
8989
let(:content) { "{% gist #{gist} #{filename} %}" }
9090
let(:output) do
91-
doc.data['gist_id'] = "1342013"
92-
doc.data['gist_filename'] = "atom.xml"
91+
doc.data["gist_id"] = "1342013"
92+
doc.data["gist_filename"] = "atom.xml"
9393
doc.content = content
9494
doc.output = Jekyll::Renderer.new(doc.site, doc).run
9595
end
9696

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

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

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

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

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

122121
context "with token" do
123122
before { ENV["JEKYLL_GITHUB_TOKEN"] = "1234" }
124-
before {
125-
stub_request(:get, "https://api.github.com/gists/1342013").
126-
to_return(:status => 200, :body => fixture("single-file"), :headers => {"Content-Type" => "application/json"})
127-
}
123+
before do
124+
stub_request(:get, "https://api.github.com/gists/1342013")
125+
.to_return(:status => 200, :body => fixture("single-file"), :headers => { "Content-Type" => "application/json" })
126+
end
128127
let(:gist_id) { "1342013" }
129128
let(:gist) { "page.gist_id" }
130129
let(:output) do
131-
doc.data['gist_id'] = gist_id
130+
doc.data["gist_id"] = gist_id
132131
doc.content = content
133132
doc.output = Jekyll::Renderer.new(doc.site, doc).run
134133
end
135134

136135
it "produces the noscript tag" do
137-
expect(output).to match(/<noscript><pre>contents of gist<\/pre><\/noscript>/)
136+
expect(output).to match(%r!<noscript><pre>contents of gist<\/pre><\/noscript>!)
138137
end
139138

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

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

161160
it "does not produce the noscript tag" do
162-
expect(output).to_not match(/<noscript>/)
161+
expect(output).to_not match(%r!<noscript>!)
163162
end
164163
end
165-
166164
end
167165

168166
context "invalid gist" do
169-
170167
context "no gist id present" do
171168
let(:gist) { "" }
172169

173170
it "raises an error" do
174-
expect(->{ output }).to raise_error
171+
expect(-> { output }).to raise_error
175172
end
176173
end
177-
178174
end
179-
180175
end

0 commit comments

Comments
 (0)