From 92d9c4301b0b97ae7bb4b9db55957606ebda72de Mon Sep 17 00:00:00 2001 From: Daniel Grieve Date: Fri, 15 Mar 2013 22:37:14 +0000 Subject: [PATCH 1/4] display single files from gist --- jekyll.gemspec | 2 +- lib/jekyll/tags/gist.rb | 21 +++++++++--- test/test_tags.rb | 76 +++++++++++++++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/jekyll.gemspec b/jekyll.gemspec index 17fb421b..5c9fdaee 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('kramdown', "~> 0.14") s.add_runtime_dependency('pygments.rb', "~> 0.3.2") 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('rdoc', "~> 3.11") diff --git a/lib/jekyll/tags/gist.rb b/lib/jekyll/tags/gist.rb index d3eb0b37..f1603b9e 100644 --- a/lib/jekyll/tags/gist.rb +++ b/lib/jekyll/tags/gist.rb @@ -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.match(/(\d+) (.*)/) + 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) - "" + private + + def gist_script_tag(gist_id, filename=nil) + if filename.empty? + "" + else + "" + end end end end diff --git a/test/test_tags.rb b/test/test_tags.rb index caefb9ac..f7916fc5 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -170,7 +170,7 @@ CONTENT assert_match %r{FINISH HIM}, @result end end - + context "using Redcarpet" do setup do create_post(@content, 'markdown' => 'redcarpet') @@ -203,22 +203,78 @@ CONTENT assert_match %r{/2008/11/21/complex/}, @result end end - - context "simple gist inclusion" do - setup do - @gist = 358471 - content = < '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 "", @result + end end - - should "write script tag" do - assert_match %r{}, @result + + context "with specific file" do + setup do + @gist = 358471 + @filename = 'somefile.rb' + content = < 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true}) + end + + should "write script tag with specific file in gist" do + assert_match "", @result + end + end + + context "with blank gist id" do + setup do + content = < '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 = < '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 From 6c5d001986d7c71c04e60b10d2a6b2dc4b96a2ba Mon Sep 17 00:00:00 2001 From: Daniel Grieve Date: Sat, 16 Mar 2013 19:20:54 +0000 Subject: [PATCH 2/4] replace \s with space --- lib/jekyll/tags/gist.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/tags/gist.rb b/lib/jekyll/tags/gist.rb index f1603b9e..efeb62c3 100644 --- a/lib/jekyll/tags/gist.rb +++ b/lib/jekyll/tags/gist.rb @@ -19,9 +19,9 @@ module Jekyll def gist_script_tag(gist_id, filename=nil) if filename.empty? - "" + "" else - "" + "" end end end From 22d1fdab540bb10c2058884fe3bb43c904058730 Mon Sep 17 00:00:00 2001 From: Daniel Grieve Date: Sun, 17 Mar 2013 11:29:42 +0000 Subject: [PATCH 3/4] tie down the regex match on gist filenames --- lib/jekyll/tags/gist.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tags/gist.rb b/lib/jekyll/tags/gist.rb index efeb62c3..0b40a5fd 100644 --- a/lib/jekyll/tags/gist.rb +++ b/lib/jekyll/tags/gist.rb @@ -7,7 +7,7 @@ module Jekyll class GistTag < Liquid::Tag def render(context) - if tag_contents = @markup.match(/(\d+) (.*)/) + 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 From b9cbce5e5f98d19963f0918356933e96f84bfd75 Mon Sep 17 00:00:00 2001 From: Daniel Grieve Date: Sun, 17 Mar 2013 15:04:03 +0000 Subject: [PATCH 4/4] fix regex --- lib/jekyll/tags/gist.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tags/gist.rb b/lib/jekyll/tags/gist.rb index 0b40a5fd..0d981af1 100644 --- a/lib/jekyll/tags/gist.rb +++ b/lib/jekyll/tags/gist.rb @@ -7,7 +7,7 @@ module Jekyll class GistTag < Liquid::Tag def render(context) - if tag_contents = @markup.strip.match(/\A(\d+) ?(\S*)\z/) + 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