Skip to content

Commit 730e45b

Browse files
committed
Merge pull request #7 from gr4y/master
Merge pull request 7
2 parents 83c9204 + 13e6357 commit 730e45b

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

jekyll-gist.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
2020
spec.add_development_dependency "bundler", "~> 1.6"
2121
spec.add_development_dependency "rake"
2222
spec.add_development_dependency "rspec"
23+
spec.add_development_dependency "webmock"
2324
spec.add_development_dependency "jekyll", "~> 2.0"
2425
end

lib/jekyll-gist/gist_tag.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'cgi'
2+
require 'open-uri'
3+
14
module Jekyll
25
module Gist
36
class GistTag < Liquid::Tag
@@ -11,7 +14,9 @@ def render(context)
1114
if context.has_key?(filename)
1215
filename = context[filename]
1316
end
14-
gist_script_tag(gist_id, filename)
17+
noscript_tag = gist_noscript_tag(gist_id, filename)
18+
script_tag = gist_script_tag(gist_id, filename)
19+
"#{noscript_tag}#{script_tag}"
1520
else
1621
raise ArgumentError.new <<-eos
1722
Syntax error in tag 'gist' while parsing the following markup:
@@ -35,13 +40,33 @@ def determine_arguments(input)
3540
end
3641

3742
def gist_script_tag(gist_id, filename = nil)
38-
if filename.empty?
39-
"<script src=\"https://gist.github.com/#{gist_id}.js\"> </script>"
43+
url = "https://gist.github.com/#{gist_id}.js"
44+
url = "#{url}?file=#{filename}" unless filename.empty?
45+
"<script src=\"#{url}\"> </script>"
46+
end
47+
48+
def gist_noscript_tag(gist_id, filename = nil)
49+
code = fetch_raw_code(gist_id, filename)
50+
if !code.nil?
51+
"<noscript><pre>#{CGI.escapeHTML(code)}</pre></noscript>"
4052
else
41-
"<script src=\"https://gist.github.com/#{gist_id}.js?file=#{filename}\"> </script>"
53+
Jekyll.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id} could not"
54+
Jekyll.logger.warn "", "be generated. This will affect users who do not have"
55+
Jekyll.logger.warn "", "JavaScript available or enabled in their browsers."
4256
end
4357
end
4458

59+
def fetch_raw_code(gist_id, filename = nil)
60+
begin
61+
url = "https://gist.githubusercontent.com/#{gist_id}/raw"
62+
url = "#{url}/#{filename}" unless filename.empty?
63+
open(url).read.chomp
64+
rescue SocketError
65+
nil
66+
rescue OpenURI::HTTPError
67+
nil
68+
end
69+
end
4570
end
4671
end
4772
end

spec/gist_tag_spec.rb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'spec_helper'
22

33
describe(Jekyll::Gist::GistTag) do
4+
let(:http_output) { "<test>true</test>" }
45
let(:doc) { doc_with_content(content) }
56
let(:content) { "{% gist #{gist} %}" }
67
let(:output) do
@@ -11,56 +12,80 @@
1112

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

1618
it "produces the correct script tag" do
1719
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
1820
end
21+
it "produces the correct noscript tag" do
22+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
23+
end
1924
end
2025

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

2430
it "produces the correct script tag" do
2531
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
2632
end
33+
it "produces the correct noscript tag" do
34+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
35+
end
2736
end
2837

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

3242
it "produces the correct script tag" do
3343
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js">\s<\/script>/)
3444
end
45+
it "produces the correct noscript tag" do
46+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
47+
end
3548
end
3649

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

4256
it "produces the correct script tag" do
4357
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist}.js\?file=#{filename}">\s<\/script>/)
4458
end
59+
it "produces the correct noscript tag" do
60+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
61+
end
4562
end
4663

4764
context "with variable gist id" do
65+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw").to_return(body: http_output) }
66+
let(:gist_id) { "1342013" }
4867
let(:gist) { "page.gist_id" }
4968
let(:output) do
50-
doc.data['gist_id'] = "1342013"
69+
doc.data['gist_id'] = gist_id
5170
doc.content = content
5271
doc.output = Jekyll::Renderer.new(doc.site, doc).run
5372
end
5473

5574
it "produces the correct script tag" do
5675
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js">\s<\/script>/)
5776
end
77+
it "produces the correct noscript tag" do
78+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
79+
end
5880
end
5981

6082
context "with variable gist id and filename" do
61-
let(:gist) { "page.gist_id" }
62-
let(:filename) { "page.gist_filename" }
63-
let(:content) { "{% gist #{gist} #{filename} %}" }
83+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(body: http_output) }
84+
let(:gist_id) { "1342013" }
85+
let(:gist_filename) { "atom.xml" }
86+
let(:gist) { "page.gist_id" }
87+
let(:filename) { "page.gist_filename" }
88+
let(:content) { "{% gist #{gist} #{filename} %}" }
6489
let(:output) do
6590
doc.data['gist_id'] = "1342013"
6691
doc.data['gist_filename'] = "atom.xml"
@@ -71,7 +96,28 @@
7196
it "produces the correct script tag" do
7297
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
7398
end
99+
100+
it "produces the correct noscript tag" do
101+
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
102+
end
74103
end
104+
105+
context "with valid gist id and invalid filename" do
106+
before { stub_request(:get, "https://gist.githubusercontent.com/#{gist_id}/raw/#{gist_filename}").to_return(status: 404) }
107+
let(:gist_id) { "mattr-/24081a1d93d2898ecf0f" }
108+
let(:gist_filename) { "myfile.ext" }
109+
let(:content) { "{% gist #{gist_id} #{gist_filename} %}" }
110+
111+
it "produces the correct script tag" do
112+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{gist_id}.js\?file=#{gist_filename}">\s<\/script>/)
113+
end
114+
115+
it "does not produce the noscript tag" do
116+
expect(output).to_not match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
117+
end
118+
119+
end
120+
75121
end
76122

77123

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
TEST_DIR = File.dirname(__FILE__)
22
TMP_DIR = File.expand_path("../tmp", TEST_DIR)
33

4+
require 'webmock/rspec'
5+
require 'cgi'
46
require 'jekyll'
57
require File.expand_path("../lib/jekyll-gist.rb", TEST_DIR)
68

0 commit comments

Comments
 (0)