-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlazy_tweet_embedding.rb
52 lines (41 loc) · 1.11 KB
/
lazy_tweet_embedding.rb
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
require "open-uri"
require "json"
module Jekyll
# convert tweet url to embedding html
class LazyTweetEmbedding
def get_html(id)
url = "https://api.twitter.com/1/statuses/oembed.json?id=#{id}"
JSON.parse(URI.open(url).read, { :symbolize_names => true })[:html]
end
def convert(line)
r = /^https?:\/\/twitter\.com\/[a-zA-Z0-9_]+\/status(es)?\/([0-9]+)\/?$/
r =~ line ? get_html($~[2]) : line
end
def embed(content)
content.lines.collect {|line| convert(line) }.join
end
end
# for markdown, extend oroginal parser's convert method
module Converters
class Markdown < Converter
alias_method :parser_converter, :convert
def convert(content)
parser_converter(Jekyll::LazyTweetEmbedding.new.embed(content))
end
end
end
# for html, extend converter as a plugin
class EmbeddingTweetIntoHTML < Converter
safe true
priority :low
def matches(ext)
ext =~ /^\.html$/i
end
def output_ext(ext)
".html"
end
def convert(content)
Jekyll::LazyTweetEmbedding.new.embed(content)
end
end
end