Skip to content

Commit b45216c

Browse files
committed
Add Gist code.
1 parent c1024b7 commit b45216c

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

lib/jekyll-gist.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
require "jekyll/gist/version"
22

33
module Jekyll
4-
module Gist
5-
# Your code goes here...
4+
class Gist < Liquid::Tag
5+
6+
def render(context)
7+
if tag_contents = determine_arguments(@markup.strip)
8+
gist_id, filename = tag_contents[0], tag_contents[1]
9+
gist_script_tag(gist_id, filename)
10+
else
11+
raise ArgumentError.new <<-eos
12+
Syntax error in tag 'gist' while parsing the following markup:
13+
14+
#{@markup}
15+
16+
Valid syntax:
17+
for all gists: {% gist user/1234567 %}
18+
eos
19+
end
20+
end
21+
22+
private
23+
24+
def determine_arguments(input)
25+
matched = if input.include?("/")
26+
input.match(/\A([a-zA-Z0-9\/\-_]+) ?(\S*)\Z/)
27+
else
28+
input.match(/\A(\d+) ?(\S*)\Z/)
29+
end
30+
[matched[1].strip, matched[2].strip] if matched && matched.length >= 3
31+
end
32+
33+
def gist_script_tag(gist_id, filename = nil)
34+
if filename.empty?
35+
"<script src=\"https://gist.github.com/#{gist_id}.js\"> </script>"
36+
else
37+
"<script src=\"https://gist.github.com/#{gist_id}.js?file=#{filename}\"> </script>"
38+
end
39+
end
640
end
741
end
42+
43+
Liquid::Template.register_tag('gist', Jekyll::GistTag)

0 commit comments

Comments
 (0)