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:
|
||||
# {% 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
|
||||
|
|
|
@ -204,21 +204,77 @@ CONTENT
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue