Merge pull request #861 from danielgrieve/gist-tag
display single files from gist
This commit is contained in:
commit
a054ce2788
|
@ -2,16 +2,27 @@
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# {% gist 1234567 %}
|
# {% gist 1234567 %}
|
||||||
|
# {% gist 1234567 file.rb %}
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class GistTag < Liquid::Tag
|
class GistTag < Liquid::Tag
|
||||||
def initialize(tag_name, gist, tokens)
|
def render(context)
|
||||||
super
|
if tag_contents = @markup.strip.match(/\A(\d+) ?(\S*)\Z/)
|
||||||
@gist = gist.strip
|
gist_id, filename = tag_contents[1].strip, tag_contents[2].strip
|
||||||
|
gist_script_tag(gist_id, filename)
|
||||||
|
else
|
||||||
|
"Error parsing gist id"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
private
|
||||||
"<script src=\"https://gist.github.com/#{@gist}.js\"> </script>"
|
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -170,7 +170,7 @@ CONTENT
|
||||||
assert_match %r{<em>FINISH HIM</em>}, @result
|
assert_match %r{<em>FINISH HIM</em>}, @result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "using Redcarpet" do
|
context "using Redcarpet" do
|
||||||
setup do
|
setup do
|
||||||
create_post(@content, 'markdown' => 'redcarpet')
|
create_post(@content, 'markdown' => 'redcarpet')
|
||||||
|
@ -203,22 +203,78 @@ CONTENT
|
||||||
assert_match %r{/2008/11/21/complex/}, @result
|
assert_match %r{/2008/11/21/complex/}, @result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "simple gist inclusion" do
|
context "gist tag" do
|
||||||
setup do
|
context "simple" do
|
||||||
@gist = 358471
|
setup do
|
||||||
content = <<CONTENT
|
@gist = 358471
|
||||||
|
content = <<CONTENT
|
||||||
---
|
---
|
||||||
title: My Cool Gist
|
title: My Cool Gist
|
||||||
---
|
---
|
||||||
|
|
||||||
{% gist #{@gist} %}
|
{% gist #{@gist} %}
|
||||||
CONTENT
|
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
|
end
|
||||||
|
|
||||||
should "write script tag" do
|
context "with specific file" do
|
||||||
assert_match %r{<script src='https://gist.github.com/#{@gist}.js'>\s</script>}, @result
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue