Skip to content

Commit

Permalink
Merge pull request #861 from danielgrieve/gist-tag
Browse files Browse the repository at this point in the history
display single files from gist
  • Loading branch information
parkr committed Mar 17, 2013
2 parents 309e03b + b9cbce5 commit a054ce2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
21 changes: 16 additions & 5 deletions lib/jekyll/tags/gist.rb
Expand Up @@ -2,16 +2,27 @@
#
# Example:
# {% gist 1234567 %}
# {% gist 1234567 file.rb %}

module Jekyll
class GistTag < Liquid::Tag
def initialize(tag_name, gist, tokens)
super
@gist = gist.strip
def render(context)
if tag_contents = @markup.strip.match(/\A(\d+) ?(\S*)\Z/)
gist_id, filename = tag_contents[1].strip, tag_contents[2].strip
gist_script_tag(gist_id, filename)
else
"Error parsing gist id"
end
end

def render(context)
"<script src=\"https://gist.github.com/#{@gist}.js\"> </script>"
private

def gist_script_tag(gist_id, filename=nil)
if filename.empty?
"<script src=\"https://gist.github.com/#{gist_id}.js\"> </script>"
else
"<script src=\"https://gist.github.com/#{gist_id}.js?file=#{filename}\"> </script>"
end
end
end
end
Expand Down
76 changes: 66 additions & 10 deletions test/test_tags.rb
Expand Up @@ -170,7 +170,7 @@ def fill_post(code, override = {})
assert_match %r{<em>FINISH HIM</em>}, @result
end
end

context "using Redcarpet" do
setup do
create_post(@content, 'markdown' => 'redcarpet')
Expand Down Expand Up @@ -203,22 +203,78 @@ def fill_post(code, override = {})
assert_match %r{/2008/11/21/complex/}, @result
end
end

context "simple gist inclusion" do
setup do
@gist = 358471
content = <<CONTENT

context "gist tag" do
context "simple" do
setup do
@gist = 358471
content = <<CONTENT
---
title: My Cool Gist
---
{% gist #{@gist} %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end

should "write script tag" do
assert_match "<script src='https://gist.github.com/#{@gist}.js'>\s</script>", @result
end
end

should "write script tag" do
assert_match %r{<script src='https://gist.github.com/#{@gist}.js'>\s</script>}, @result

context "with specific file" do
setup do
@gist = 358471
@filename = 'somefile.rb'
content = <<CONTENT
---
title: My Cool Gist
---
{% gist #{@gist} #{@filename} %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end

should "write script tag with specific file in gist" do
assert_match "<script src='https://gist.github.com/#{@gist}.js?file=#{@filename}'>\s</script>", @result
end
end

context "with blank gist id" do
setup do
content = <<CONTENT
---
title: My Cool Gist
---
{% gist %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end

should "output error message" do
assert_match "Error parsing gist id", @result
end
end

context "with invalid gist id" do
setup do
invalid_gist = 'invalid'
content = <<CONTENT
---
title: My Cool Gist
---
{% gist #{invalid_gist} %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end

should "output error message" do
assert_match "Error parsing gist id", @result
end
end
end
end

0 comments on commit a054ce2

Please sign in to comment.