display single files from gist

This commit is contained in:
Daniel Grieve 2013-03-15 22:37:14 +00:00
parent f8a90d711f
commit 92d9c4301b
3 changed files with 83 additions and 16 deletions

View File

@ -30,7 +30,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('kramdown', "~> 0.14") s.add_runtime_dependency('kramdown', "~> 0.14")
s.add_runtime_dependency('pygments.rb', "~> 0.3.2") s.add_runtime_dependency('pygments.rb', "~> 0.3.2")
s.add_runtime_dependency('commander', "~> 4.1.3") s.add_runtime_dependency('commander', "~> 4.1.3")
s.add_runtime_dependency('safe_yaml', "~> 0.7") s.add_runtime_dependency('safe_yaml', "~> 0.7.0")
s.add_development_dependency('rake', "~> 10.0.3") s.add_development_dependency('rake', "~> 10.0.3")
s.add_development_dependency('rdoc', "~> 3.11") s.add_development_dependency('rdoc', "~> 3.11")

View File

@ -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.match(/(\d+) (.*)/)
@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\">\s</script>"
else
"<script src=\"https://gist.github.com/#{gist_id}.js?file=#{filename}\">\s</script>"
end
end end
end end
end end

View File

@ -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