-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstagram-jekyll.rb
More file actions
86 lines (71 loc) · 2.1 KB
/
Copy pathinstagram-jekyll.rb
File metadata and controls
86 lines (71 loc) · 2.1 KB
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
require 'yaml'
require 'jekyll'
require 'instagram'
#
# Usage:
#
# {% instagram accesstokenpath:C:\instagram-access-token.txt %}
# <div>
# <h3>{{ item.caption.text }}</h3>
# <img src="{{ item.images.standard_resolution.url }}" />
# </div>
# {% endinstagram %}
#
# Parameters:
# accesstokenpath: the path to a text file containing an oauth access token for Instagram
class InstagramLoader
class << self
def photos(accesstokenpath)
accesstoken = File.open(accesstokenpath).gets
client = Instagram.client(:access_token => accesstoken)
client.user_recent_media
end
end
end
module Jekyll
class InstagramTag < Liquid::Block
include Liquid::StandardFilters
Syntax = /(#{Liquid::QuotedFragment}+)?/
def initialize(tag_name, markup, tokens)
@variable_name = 'item'
@attributes = {}
# Parse parameters
if markup =~ Syntax
markup.scan(Liquid::TagAttributes) do |key, value|
#p key + ":" + value
@attributes[key] = value
end
else
raise SyntaxError.new("Syntax Error in 'delicious' - Valid syntax: instagram accesstokenpath:x]")
end
@accesstoken = @attributes['accesstokenpath']
@name = 'item'
super
end
def render(context)
context.registers[:instagram] ||= Hash.new(0)
collection = InstagramLoader.photos(@accesstoken)
length = collection.length
result = []
# loop through found photos and render results
context.stack do
collection.each_with_index do |item, index|
attrs = item
context[@variable_name] = attrs
context['forloop'] = {
'name' => @name,
'length' => length,
'index' => index + 1,
'index0' => index,
'rindex' => length - index,
'rindex0' => length - index -1,
'first' => (index == 0),
'last' => (index == length - 1) }
result << render_all(@nodelist, context)
end
end
result
end
end
end
Liquid::Template.register_tag('instagram', Jekyll::InstagramTag)