Merge pull request #2469 from jekyll/extract-gist-tag
This commit is contained in:
commit
781e606e5a
|
@ -39,6 +39,7 @@ Gem::Specification.new do |s|
|
|||
s.add_runtime_dependency('redcarpet', "~> 3.1")
|
||||
s.add_runtime_dependency('toml', '~> 0.1.0')
|
||||
s.add_runtime_dependency('jekyll-paginate', '~> 1.0')
|
||||
s.add_runtime_dependency('jekyll-gist', '~> 1.0')
|
||||
s.add_runtime_dependency('jekyll-coffeescript', '~> 1.0')
|
||||
s.add_runtime_dependency('jekyll-sass-converter', '~> 1.0')
|
||||
|
||||
|
|
|
@ -75,6 +75,7 @@ require_all 'jekyll/tags'
|
|||
require 'jekyll-coffeescript'
|
||||
require 'jekyll-sass-converter'
|
||||
require 'jekyll-paginate'
|
||||
require 'jekyll-gist'
|
||||
|
||||
SafeYAML::OPTIONS[:suppress_warnings] = true
|
||||
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
# Gist Liquid Tag
|
||||
#
|
||||
# Example:
|
||||
# {% gist username/1234567 %}
|
||||
# {% gist username/1234567 file.rb %}
|
||||
|
||||
module Jekyll
|
||||
class GistTag < Liquid::Tag
|
||||
|
||||
def render(context)
|
||||
if tag_contents = determine_arguments(@markup.strip)
|
||||
gist_id, filename = tag_contents[0], tag_contents[1]
|
||||
gist_script_tag(gist_id, filename)
|
||||
else
|
||||
raise ArgumentError.new <<-eos
|
||||
Syntax error in tag 'gist' while parsing the following markup:
|
||||
|
||||
#{@markup}
|
||||
|
||||
Valid syntax:
|
||||
for all gists: {% gist user/1234567 %}
|
||||
eos
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def determine_arguments(input)
|
||||
matched = if input.include?("/")
|
||||
input.match(/\A([a-zA-Z0-9\/\-_]+) ?(\S*)\Z/)
|
||||
else
|
||||
input.match(/\A(\d+) ?(\S*)\Z/)
|
||||
end
|
||||
[matched[1].strip, matched[2].strip] if matched && matched.length >= 3
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
Liquid::Template.register_tag('gist', Jekyll::GistTag)
|
|
@ -259,115 +259,6 @@ CONTENT
|
|||
end
|
||||
end
|
||||
|
||||
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})
|
||||
end
|
||||
|
||||
should "write script tag" do
|
||||
assert_match "<script src=\"https://gist.github.com/#{@gist}.js\">\s</script>", @result
|
||||
end
|
||||
end
|
||||
|
||||
context "for private gist" do
|
||||
context "when valid" do
|
||||
setup do
|
||||
@gist = "mattr-/24081a1d93d2898ecf0f"
|
||||
@filename = "myfile.ext"
|
||||
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
|
||||
|
||||
should "raise ArgumentError when invalid" do
|
||||
@gist = "mattr-24081a1d93d2898ecf0f"
|
||||
@filename = "myfile.ext"
|
||||
content = <<CONTENT
|
||||
---
|
||||
title: My Cool Gist
|
||||
---
|
||||
|
||||
{% gist #{@gist} #{@filename} %}
|
||||
CONTENT
|
||||
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
should "raise ArgumentError" do
|
||||
content = <<CONTENT
|
||||
---
|
||||
title: My Cool Gist
|
||||
---
|
||||
|
||||
{% gist %}
|
||||
CONTENT
|
||||
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid gist id" do
|
||||
should "raise ArgumentError" do
|
||||
invalid_gist = 'invalid'
|
||||
content = <<CONTENT
|
||||
---
|
||||
title: My Cool Gist
|
||||
---
|
||||
|
||||
{% gist #{invalid_gist} %}
|
||||
CONTENT
|
||||
|
||||
assert_raise ArgumentError do
|
||||
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "include tag with parameters" do
|
||||
|
||||
context "with symlink'd include" do
|
||||
|
|
Loading…
Reference in New Issue