Skip to content
Merged
1 change: 1 addition & 0 deletions jekyll-gist.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "webmock"
spec.add_development_dependency "jekyll", "~> 2.0"
end
32 changes: 31 additions & 1 deletion lib/jekyll-gist/gist_tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'cgi'
require 'open-uri'

module Jekyll
module Gist
class GistTag < Liquid::Tag
Expand All @@ -11,7 +14,9 @@ def render(context)
if context.has_key?(filename)
filename = context[filename]
end
gist_script_tag(gist_id, filename)
noscript_tag = gist_noscript_tag(gist_id, filename)
script_tag = gist_script_tag(gist_id, filename)
"#{noscript_tag}#{script_tag}"
else
raise ArgumentError.new <<-eos
Syntax error in tag 'gist' while parsing the following markup:
Expand Down Expand Up @@ -42,6 +47,31 @@ def gist_script_tag(gist_id, filename = nil)
end
end

def gist_noscript_tag(gist_id, filename = nil)
code = fetch_raw_code(gist_id, filename)
if !code.nil?
"<noscript><pre>#{CGI.escapeHTML(code)}</pre></noscript>"
else
Jekyll.logger.warn "Warning:", "The <noscript> tag for your gist #{gist_id} could not"
Jekyll.logger.warn "", "be generated. This will affect users who do not have"
Jekyll.logger.warn "", "JavaScript available or enabled in their browsers."
end
end

def fetch_raw_code(gist_id, filename = nil)
if filename.empty?
uri = "https://gist.githubusercontent.com/#{gist_id}/raw"
else
uri = "https://gist.githubusercontent.com/#{gist_id}/raw/#{filename}"
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about

uri = "https://gist.githubusercontent.com/#{gist_id}/raw"
url = "#{uri}/#{filename}" unless filename.empty?

to replace the if here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does look cleaner.
I guess the same could be done for the gist_script_tag method:

def gist_script_tag(gist_id, filename = nil)
  url = "https://gist.github.com/#{gist_id}.js"
  url = "#{url}?file=#{filename}" unless filename.empty?
  "<script src=\"#{url}\"> </script>"
end

begin
open(uri).read.chomp
rescue SocketError
nil
rescue OpenURI::HTTPError
nil
end
end
end
end
end
Expand Down
54 changes: 50 additions & 4 deletions spec/gist_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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(:output) do
Expand All @@ -11,56 +12,80 @@

context "valid gist" do
context "with user prefix" do
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>/)
end
it "produces the correct noscript tag" do
expect(output).to match(/<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) }
let(:gist) { "28949e1d5ee2273f9fd3" }

it "produces the correct script tag" do
expect(output).to match(/<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/)
end
end

context "classic Gist id style" do
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>/)
end
it "produces the correct noscript tag" do
expect(output).to match(/<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) }
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>/)
end
it "produces the correct noscript tag" do
expect(output).to match(/<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) }
let(:gist_id) { "1342013" }
let(:gist) { "page.gist_id" }
let(:output) do
doc.data['gist_id'] = "1342013"
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>/)
end
it "produces the correct noscript tag" do
expect(output).to match(/<noscript><pre>&lt;test&gt;true&lt;\/test&gt;<\/pre><\/noscript>\n/)
end
end

context "with variable gist id and filename" do
let(:gist) { "page.gist_id" }
let(:filename) { "page.gist_filename" }
let(:content) { "{% gist #{gist} #{filename} %}" }
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"
Expand All @@ -71,7 +96,28 @@
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>/)
end

it "produces the correct noscript tag" do
expect(output).to match(/<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" }
let(:gist_filename) { "myfile.ext" }
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>/)
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/)
end

end

end


Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
TEST_DIR = File.dirname(__FILE__)
TMP_DIR = File.expand_path("../tmp", TEST_DIR)

require 'webmock/rspec'
require 'cgi'
require 'jekyll'
require File.expand_path("../lib/jekyll-gist.rb", TEST_DIR)

Expand Down