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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Tweet Liquid Tag
#
# Example: 
#   \{\% tweet 464180168303456256 \%\}
#

require 'net/http'
require 'json'

module Jekyll
  class TweetTag < Liquid::Tag

    #
    #
    #
    def render(context)
      
      if tag_contents = determine_arguments(@markup.strip)
        tweet_id = tag_contents #
        tweet_script_tag(tweet_id)
      else
        raise ArgumentError.new <<-eos
           Syntax error
          eos
      end

    end

   private

   #
   # 
   #
   def determine_arguments(input)

    return input

   end

   #
   #
   #
   def tweet_script_tag(tweet_id)

      result = Net::HTTP.get(URI.parse("https://api.twitter.com/1/statuses/oembed.json?id=#{tweet_id}"))
      json   = JSON.parser.new(result)
      hash   = json.parse()
      parsed = hash['html']

      return parsed     

   end

  end
end

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