Menu

Category

Archive

logo


Adding a new Liquid tag for Jekyll (Embeding tweet)

2014-05-07 21:30:00 +0900
  • このエントリーをはてなブックマークに追加

This article might be too old.

I made new Liquid tag that enables embedding tweets on Jekyll. This Liquid allows you to easily read tweets on articles. It's almost like Jekyll's official gist tag.

{\% tweet 464180168303456256 \%}

After removing the back slash, you can put this simple tag on your markdown. It will get JSON data from twitter. After that, we will use html data on the response. The result should look like this.

Here is the code:

 1 # Tweet Liquid Tag
 2 #
 3 # Example: 
 4 #   \{\% tweet 464180168303456256 \%\}
 5 #
 6 
 7 require 'net/http'
 8 require 'json'
 9 
10 module Jekyll
11   class TweetTag < Liquid::Tag
12 
13     #
14     #
15     #
16     def render(context)
17       
18       if tag_contents = determine_arguments(@markup.strip)
19         tweet_id = tag_contents #
20         tweet_script_tag(tweet_id)
21       else
22         raise ArgumentError.new <<-eos
23            Syntax error
24           eos
25       end
26 
27     end
28 
29    private
30 
31    #
32    # 
33    #
34    def determine_arguments(input)
35 
36     return input
37 
38    end
39 
40    #
41    #
42    #
43    def tweet_script_tag(tweet_id)
44 
45       result = Net::HTTP.get(URI.parse("https://api.twitter.com/1/statuses/oembed.json?id=#{tweet_id}"))
46       json   = JSON.parser.new(result)
47       hash   = json.parse()
48       parsed = hash['html']
49 
50       return parsed     
51 
52    end
53 
54   end
55 end
56 
57 Liquid::Template.register_tag('tweet', Jekyll::TweetTag)

Actually, we can just put the generated code on twitter website and it is so easy to use. Now, I have no idea why I made this.

https://github.com/kzykbys/JekyllPlugins/blob/master/tweet.rb