Skip to content

Commit 4e7d870

Browse files
committed
Added an noscript fallback for browsers without javascript enabled.
1 parent 83c9204 commit 4e7d870

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
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: 16 additions & 1 deletion
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:
@@ -42,6 +47,16 @@ def gist_script_tag(gist_id, filename = nil)
4247
end
4348
end
4449

50+
def gist_noscript_tag(gist_id, filename = nil)
51+
if filename.empty?
52+
uri = "https://gist.githubusercontent.com/#{gist_id}/raw"
53+
else
54+
uri = "https://gist.githubusercontent.com/#{gist_id}/raw/#{filename}"
55+
end
56+
code = open(uri).read.chomp
57+
"<noscript><pre>#{CGI.escapeHTML(code)}</pre></noscript>"
58+
end
59+
4560
end
4661
end
4762
end

spec/gist_tag_spec.rb

Lines changed: 34 additions & 5 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"
@@ -69,7 +94,11 @@
6994
end
7095

7196
it "produces the correct script tag" do
72-
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>/)
97+
expect(output).to match(/<script src="https:\/\/gist.github.com\/#{doc.data['gist_id']}.js\?file=#{doc.data['gist_filename']}">\s<\/script>\n\n/)
98+
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/)
73102
end
74103
end
75104
end

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)