Skip to content

Commit 1153df3

Browse files
committed
create a preprocess function to handle grouping site structure into chapters by folder instead of doing this logic in templates
1 parent cdd7f19 commit 1153df3

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

Rules

+34
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,41 @@ end
7272

7373
Nanoc3::Filter.register 'CodeBlocks', :code_blocks
7474

75+
preprocess do
76+
@chapterOrder = [
77+
"getting-started",
78+
"javascript-101",
79+
"jquery-basics",
80+
"using-jquery-core",
81+
"events",
82+
"effects",
83+
"ajax",
84+
"plugins",
85+
"performance",
86+
"code-organization",
87+
"custom-events",
88+
"how-to"
89+
]
90+
91+
@chapters = {}
92+
@items.each do |item|
93+
item[:chapter] = item[:filename].split('/')[1]
94+
item[:chapter_title] = item[:chapter].gsub(/-/, " ").upcase
95+
end
96+
97+
@groupedItems = @items.group_by {|item| item[:chapter]}
7598

99+
@chapterOrder.each do |folder|
100+
myitems = @groupedItems[ folder ]
101+
@chapters [ folder] = {}
102+
@chapters[ folder ][ :items ] = @groupedItems[folder].sort_by {|i| i[:section] || 0 }
103+
@chapters[ folder ][ :title ] = folder.gsub(/-/, " ").upcase
104+
@chapters[ folder ][ :folder ] = folder
105+
end
106+
107+
@site.config[:chapterOrder] = @chapterOrder
108+
@site.config[:chapters] = @chapters
109+
end
76110

77111
compile '/assets/*' do
78112
# don’t filter or layout

layouts/chapter_contents.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<% myFolder = @item[:filename].split('/')[1] %>
2-
<% folderItems = @items.group_by {|item| item[:filename].split('/')[1] } %>
31
<ul>
4-
<% folderItems[ myFolder ].sort_by {|i| i[:section] || 0 }. each do |i| %>
5-
<% if i[:title] && !(i[:filename] =~ /\/dex\.md$/) %>
6-
<li <% if item[:filename] == i[:filename] %>class="active"<% end %>><a href="<%= i.path %>"><%= i[:title] %></a></li>
2+
<% if @site.config[:chapters][item[:chapter]] %>
3+
<% @site.config[:chapters][item[:chapter]][:items].each do|i| %>
4+
<% if i[:title] && !(i[:filename] =~ /\/dex\.md$/) %>
5+
<li <% if @item[:filename] == i[:filename] %>class="active"<% end %>><a href="<%= i.path %>"><%= i[:title] %></a></li>
6+
<% end %>
77
<% end %>
88
<% end %>
99
</ul>

layouts/sidebar.html

+2-18
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,8 @@ <h2>Categories</h2>
55
<hr>
66

77
<ul class="nav">
8-
<% chapters = [
9-
"getting-started",
10-
"javascript-101",
11-
"jquery-basics",
12-
"using-jquery-core",
13-
"events",
14-
"effects",
15-
"ajax",
16-
"plugins",
17-
"performance",
18-
"code-organization",
19-
"custom-events",
20-
"how-to"
21-
] %>
22-
<% currentChapter = @item[:filename].split('/')[1] %>
23-
<% folders = @items.group_by {|item| item[:filename].split('/')[1] } %>
24-
<% chapters.each do |chapter| %>
25-
<li <% if chapter == currentChapter %>class="active"<% end %>><a href="/<%= chapter %>"><%= chapter.gsub(/-/, " ").upcase %></a></li>
8+
<% @site.config[:chapters].each do |folder, chapter| %>
9+
<li <% if folder == @item[:chapter] %>class="active"<% end %>><a href="/<%= folder %>"><%= chapter[:title] %></a></li>
2610
<% end %>
2711
</ul>
2812

0 commit comments

Comments
 (0)