Skip to content

Commit 061bbdf

Browse files
committed
move github api fetching to preprocess, only get each github user a maximum of one time per compile from API, and implement ordinal positioning of items for next-previous footer navigation
1 parent 1153df3 commit 061bbdf

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

Rules

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,52 @@ preprocess do
8989
]
9090

9191
@chapters = {}
92+
93+
@github_users = {
94+
"jquery" => nil
95+
}
96+
9297
@items.each do |item|
9398
item[:chapter] = item[:filename].split('/')[1]
9499
item[:chapter_title] = item[:chapter].gsub(/-/, " ").upcase
100+
if item[:github]
101+
@github_users[ item[:github] ] = nil
102+
else
103+
item[:github] = "jquery"
104+
end
95105
end
96106

107+
@github_users.each do |username, wat|
108+
request = Curl::Easy.http_get("https://api.github.com/users/"+username)
109+
request.perform
110+
@github_users[ username ] = JSON.parse request.body_str
111+
end
112+
113+
97114
@groupedItems = @items.group_by {|item| item[:chapter]}
98115

116+
@orderedItems = []
117+
99118
@chapterOrder.each do |folder|
100119
myitems = @groupedItems[ folder ]
101120
@chapters [ folder] = {}
102121
@chapters[ folder ][ :items ] = @groupedItems[folder].sort_by {|i| i[:section] || 0 }
122+
@orderedItems = @orderedItems + @chapters[ folder ][ :items ]
103123
@chapters[ folder ][ :title ] = folder.gsub(/-/, " ").upcase
104124
@chapters[ folder ][ :folder ] = folder
105125
end
106126

107-
@site.config[:chapterOrder] = @chapterOrder
127+
@items.each do |item|
128+
i = item[:ordinal_index] = @orderedItems.index(item)
129+
if i
130+
item[:next_item] = @orderedItems[ i+1 ]
131+
item[:previous_item] = @orderedItems[ i-1 ]
132+
end
133+
item[:github_user] = @github_users[ item[:github] ]
134+
end
135+
108136
@site.config[:chapters] = @chapters
137+
@site.config[:orderedItems] = @orderedItems
109138
end
110139

111140
compile '/assets/*' do

layouts/article_footer.html

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
<div class="clearfix meta">
22
<div class="github">
3-
<%if @item[:github] %>
4-
<% github_user = @item[:github] || "jquery" %>
5-
<% user_request = Curl::Easy.http_get("https://api.github.com/users/"+github_user) %>
6-
<% user_request.perform %>
7-
<% user_obj = JSON.parse(user_request.body_str) %>
8-
<img src="<%= user_obj["avatar_url"] %>" class="avatar" />
9-
<h3 class="author">By <a href="<%= user_obj["html_url"] %>"><%= user_obj["name"] %></a></h3>
10-
<% else %>
11-
<h3 class="author">By <%= @item[:attribution] %></h3>
12-
<% end %>
3+
<img src="<%= @item[:github_user]["avatar_url"] %>" class="avatar" />
4+
<h3 class="author">By <a href="<%= @item[:github_user]["html_url"] %>"><%= @item[:github_user]["name"] %></a></h3>
135
</div>
146
<div class="feedback">
157
<h4>Suggestions? Problems? Feedback?</h4>
@@ -22,6 +14,12 @@ <h4>Suggestions? Problems? Feedback?</h4>
2214
</div>
2315

2416
<div class="clearfix pagination">
25-
<a href="#" class="previous"><span class="article">jQuery for Designers</span> <span class="direction">Previous</span></a>
26-
<a href="#" class="next"><span class="article">$(document).ready()</span> <span class="direction">Next</span></a>
17+
18+
<% if @item[:previous_item] %>
19+
<a href="<%= @item[:previous_item].path %>" class="previous"><span class="article"><%= @item[:previous_item][:title] %></span> <span class="direction">Previous</span></a>
20+
<% end %>
21+
<% if @item[:next_item] %>
22+
<a href="<%= @item[:next_item].path %>" class="next"><span class="article"><%= @item[:next_item][:title] %></span> <span class="direction">Next</span></a>
23+
<% end %>
24+
2725
</div>

0 commit comments

Comments
 (0)