Externalize the {% gist %} tag as the jekyll-gist gem.

This commit is contained in:
Parker Moore 2014-06-01 13:18:00 -04:00
parent f9bbe8ed58
commit 879abc0949
3 changed files with 2 additions and 47 deletions

View File

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

View File

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

View File

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