-
Notifications
You must be signed in to change notification settings - Fork 8
/
jekyll-linkpreview.rb
274 lines (241 loc) · 7.01 KB
/
jekyll-linkpreview.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
require "digest"
require "json"
require 'uri'
require "metainspector"
require "jekyll-linkpreview/version"
module Jekyll
module Linkpreview
class OpenGraphProperties
@@template_file = 'linkpreview.html'
def initialize(title, url, image, description, domain)
@title = title
@url = url
@image = image
@description = description
@domain = domain
end
def to_hash()
{
'title' => @title,
'url' => @url,
'image' => @image,
'description' => @description,
'domain' => @domain,
}
end
def to_hash_for_custom_template()
{
'link_title' => @title,
'link_url' => @url,
'link_image' => @image,
'link_description' => @description,
'link_domain' => @domain
}
end
def template_file()
@@template_file
end
end
class OpenGraphPropertiesFactory
def from_page(page)
og_properties = page.meta_tags['property']
image_url = get_og_property(og_properties, 'og:image')
title = get_og_property(og_properties, 'og:title')
url = get_og_property(og_properties, 'og:url')
image = convert_to_absolute_url(image_url, page.root_url)
description = get_og_property(og_properties, 'og:description')
domain = page.host
OpenGraphProperties.new(title, url, image, description, domain)
end
def from_hash(hash)
OpenGraphProperties.new(
hash['title'], hash['url'], hash['image'], hash['description'], hash['domain'])
end
private
def get_og_property(properties, key)
if !properties.key? key then
return nil
end
properties[key].first
end
private
def convert_to_absolute_url(url, domain)
if url.nil? then
return nil
end
# root relative url
if url[0] == '/' then
return URI.join(domain, url).to_s
end
url
end
end
class NonOpenGraphProperties
@@template_file = 'linkpreview_nog.html'
def initialize(title, url, description, domain)
@title = title
@url = url
@description = description
@domain = domain
end
def to_hash()
{
'title' => @title,
'url' => @url,
'description' => @description,
'domain' => @domain,
}
end
def to_hash_for_custom_template()
{
'link_title' => @title,
'link_url' => @url,
'link_description' => @description,
'link_domain' => @domain
}
end
def template_file()
@@template_file
end
end
class NonOpenGraphPropertiesFactory
def from_page(page)
NonOpenGraphProperties.new(
page.title, page.url, get_description(page), page.host)
end
def from_hash(hash)
NonOpenGraphProperties.new(
hash['title'], hash['url'], hash['description'], hash['domain'])
end
private
def get_description(page)
if !page.parsed.xpath('//p[normalize-space()]').empty? then
return page.parsed.xpath('//p[normalize-space()]').map(&:text).first[0..180] + "..."
else
return "..."
end
end
end
class LinkpreviewTag < Liquid::Tag
@@cache_dir = '_cache'
@@template_dir = '_includes'
def initialize(tag_name, markup, parse_context)
super
@markup = markup.strip()
end
def render(context)
url = get_url_from(context)
properties = get_properties(url)
render_linkpreview properties
end
def get_properties(url)
cache_filepath = "#{@@cache_dir}/%s.json" % Digest::MD5.hexdigest(url)
if File.exist?(cache_filepath) then
hash = load_cache_file(cache_filepath)
return create_properties_from_hash(hash)
end
page = fetch(url)
properties = create_properties_from_page(page)
if Dir.exists?(@@cache_dir) then
save_cache_file(cache_filepath, properties)
else
# TODO: This message will be shown at all linkprevew tag
warn "'#{@@cache_dir}' directory does not exist. Create it for caching."
end
properties
end
private
def get_url_from(context)
context[@markup]
end
private
def fetch(url)
MetaInspector.new(url)
end
private
def load_cache_file(filepath)
JSON.parse(File.open(filepath).read)
end
protected
def save_cache_file(filepath, properties)
File.open(filepath, 'w') { |f| f.write JSON.generate(properties.to_hash) }
end
private
def create_properties_from_page(page)
if page.meta_tags['property'].empty? then
factory = NonOpenGraphPropertiesFactory.new
else
factory = OpenGraphPropertiesFactory.new
end
factory.from_page(page)
end
private
def create_properties_from_hash(hash)
if hash['image'] then
factory = OpenGraphPropertiesFactory.new
else
factory = NonOpenGraphPropertiesFactory.new
end
factory.from_hash(hash)
end
private
def render_linkpreview(properties)
template_path = get_custom_template_path properties
if File.exist?(template_path)
hash = properties.to_hash_for_custom_template
gen_custom_template template_path, hash
else
gen_default_template properties.to_hash
end
end
private
def get_custom_template_path(properties)
File.join Dir.pwd, @@template_dir, properties.template_file
end
private
def gen_default_template(hash)
title = hash['title']
url = hash['url']
description = hash['description']
domain = hash['domain']
image = hash['image']
image_html = ""
if image then
image_html = <<-EOS
<div class="jekyll-linkpreview-image">
<a href="#{url}" target="_blank">
<img src="#{image}" />
</a>
</div>
EOS
end
html = <<-EOS
<div class="jekyll-linkpreview-wrapper">
<p><a href="#{url}" target="_blank">#{url}</a></p>
<div class="jekyll-linkpreview-wrapper-inner">
<div class="jekyll-linkpreview-content">
#{image_html}
<div class="jekyll-linkpreview-body">
<h2 class="jekyll-linkpreview-title">
<a href="#{url}" target="_blank">#{title}</a>
</h2>
<div class="jekyll-linkpreview-description">#{description}</div>
</div>
</div>
<div class="jekyll-linkpreview-footer">
<a href="#{domain}" target="_blank">#{domain}</a>
</div>
</div>
</div>
EOS
html
end
private
def gen_custom_template(template_path, hash)
template = File.read template_path
Liquid::Template.parse(template).render!(hash)
end
end
end
end
Liquid::Template.register_tag("linkpreview", Jekyll::Linkpreview::LinkpreviewTag)