Skip to content

Commit 447afa5

Browse files
author
Tom Clark
committed
Use underscore to render the sidebar
1 parent 273fc16 commit 447afa5

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

_layouts/default.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<link rel="stylesheet" href="/stylesheets/styles.css">
1313
<link rel="stylesheet" href="/stylesheets/pygment_trac.css">
1414
<script src="/javascripts/scale.fix.js"></script>
15+
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
16+
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
1517
<script src="/javascripts/sidebar.js"></script>
1618
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
1719
<!--[if lt IE 9]>
@@ -23,8 +25,9 @@
2325
<header>
2426
<h1>tutorials.github.com</h1>
2527
<p>Decentralized polyglot tutorials</p>
28+
<nav id="sidebar"></nav>
2629
<p class="view"><a href="https://github.com/tutorials/tutorials.github.com">View the Project on GitHub <small>tutorials/tutorials.github.com</small></a></p>
27-
<ul>
30+
<ul id="download">
2831
<li><a href="https://github.com/tutorials/tutorials.github.com/zipball/master">Download <strong>ZIP File</strong></a></li>
2932
<li><a href="https://github.com/tutorials/tutorials.github.com/tarball/master">Download <strong>TAR Ball</strong></a></li>
3033
<li><a href="https://github.com/tutorials/tutorials.github.com">Fork On <strong>GitHub</strong></a></li>

javascripts/sidebar.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var tutorials;
2+
var sidebar;
3+
var sidebar_counts;
4+
5+
$(function(){
6+
// Grab all tutorials
7+
tutorials = $("section.tutorial");
8+
9+
// Generate the sidebar
10+
sidebar = _.chain(tutorials)
11+
.map(function(tutorial) {
12+
return $(tutorial).data().facets;
13+
})
14+
.reduce(function(counts, facets) {
15+
_(facets).each(function(val, key) {
16+
if(!_(counts).has(key)) {
17+
counts[key] = {};
18+
}
19+
if(!_(counts[key]).has(val)) {
20+
counts[key][val] = 0;
21+
}
22+
counts[key][val] += 1;
23+
});
24+
return counts;
25+
}, {})
26+
.value();
27+
28+
// Generate counts for the sidebar
29+
counts = {};
30+
_(sidebar).each(function(val, key) {
31+
counts[key] = _(val).keys().length;
32+
});
33+
sidebar_counts = counts;
34+
35+
// Draw the sidebar
36+
redraw();
37+
});
38+
39+
function redraw() {
40+
template = "<% _(sidebar).each(function(val, key) { %><h3><%= key %> (<%= sidebar_counts[key] %>)</h3><ul><% _(val).each(function(v, k) { %><li><%= k %> (<%= v %>)</li><% }) %></ul><% }) %>";
41+
list = _.template(template);
42+
$("#sidebar").append(list);
43+
};

pages/test.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,51 @@ Javascript Testing
88
==================
99

1010
This is a test of the javascript-based filtering code.
11-
The idea is that there are sections with `data-foo=bar` data attributes that are used to list key-value pairs that javascript uses to "facet" the document with.
11+
The idea is that there are sections with `data-facets={"some": "json"}` data attributes that are used to list key-value pairs that javascript uses to "facet" the document with.
1212

13-
We use `markdown="1"` to tell kramdown that we want to parse the content within the section as markdown. You can indent your code or not, the choice is up to you (although I prefer to not indent so my editor's syntax highlighting doesn't break).
13+
We store JSON because jQuery automatically converts HTML5 data attributes into `camelCase`, so we'd lose formatting.
14+
15+
Valid sections should have `class="tutorial"` (because we allow nested sections).
16+
17+
We also use the `markdown="1"` attribute to tell kramdown that we want to parse the content within the section as markdown. You can indent your code or not, the choice is up to you (although I prefer to not indent so my editor's syntax highlighting doesn't break).
1418

1519
For example, a document like this:
1620

1721
{% highlight html %}
18-
<section markdown="1" data-operating-system="OS X" data-package-management="Homebrew">
22+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Homebrew"}'>
1923
...
2024
</section>
2125

22-
<section markdown="1" data-operating-system="OS X" data-package-management="Source">
26+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Macports"}'>
2327
...
2428
</section>
2529

26-
<section markdown="1" data-operating-system="Ubuntu" data-package-management="Source">
30+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "Ubuntu", "Package Management": "Source"}'>
2731
...
2832
</section>
2933
{% endhighlight %}
3034

3135
should show the following sidebar:
3236

33-
Operating System
37+
Operating System (2)
3438
OS X (2)
3539
Ubuntu (1)
3640

37-
Package Management
41+
Package Management (3)
3842
Homebrew (1)
39-
Source (2)
43+
Macports (1)
44+
Source (1)
4045

4146
This allows users to filter the document by those attributes which are relevant to them.
4247

4348
Note the following empty liquid call used to force syntax highlighting: {{ }}
4449

45-
<section markdown="1" data-operating-system="OS X" data-package-management="Source">
50+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Homebrew"}'>
4651
This is the tutorial written for _OS X_ and _Homebrew_.
4752
It has data attributes on the preceeding `hr` tag.
4853
</section>
4954

50-
<section markdown="1" data-operating-system="OS X" data-package-management="Source">
55+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "OS X", "Package Management": "Macports"}'>
5156
This is the tutorial written for _OS X_ and _Source_.
5257
It has data attributes on a surrounding section tag (written in html).
5358
It uses kramdown's `markdown="1"` attribute to enable markdown processing within an html tag.
@@ -61,6 +66,6 @@ Here's some code:
6166
Yay!
6267
</section>
6368

64-
<section markdown="1" data-operating-system="Ubuntu" data-package-management="Source">
69+
<section class="tutorial" markdown="1" data-facets='{"Operating System": "Ubuntu", "Package Management": "Source"}'>
6570
This is the tutorial written for _Ubuntu_ and _Source_.
6671
</section>

stylesheets/styles.css

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ header {
101101
position:fixed;
102102
}
103103

104-
header ul {
104+
#download {
105105
list-style:none;
106106
height:40px;
107107

@@ -121,14 +121,14 @@ header ul {
121121
width:270px;
122122
}
123123

124-
header li {
124+
#download li {
125125
width:89px;
126126
float:left;
127127
border-right:1px solid #d2d2d2;
128128
height:40px;
129129
}
130130

131-
header ul a {
131+
#download a {
132132
line-height:1;
133133
font-size:11px;
134134
color:#999;
@@ -143,22 +143,26 @@ strong {
143143
font-weight:700;
144144
}
145145

146-
header ul li + li {
146+
#download li + li {
147147
width:88px;
148148
border-left:1px solid #fff;
149149
}
150150

151-
header ul li + li + li {
151+
#download li + li + li {
152152
border-right:none;
153153
width:89px;
154154
}
155155

156-
header ul a strong {
156+
#download a strong {
157157
font-size:14px;
158158
display:block;
159159
color:#222;
160160
}
161161

162+
#sidebar ul h3 {
163+
margin: 0;
164+
}
165+
162166
section#main {
163167
width:560px;
164168
float:right;
@@ -211,7 +215,7 @@ footer {
211215
display:inline;
212216
}
213217

214-
header ul {
218+
#download {
215219
position:absolute;
216220
right:50px;
217221
top:52px;
@@ -227,7 +231,7 @@ footer {
227231
padding:0;
228232
}
229233

230-
header ul, header p.view {
234+
#download, header p.view {
231235
position:static;
232236
}
233237
}
@@ -237,7 +241,7 @@ footer {
237241
padding:15px;
238242
}
239243

240-
header ul {
244+
#download {
241245
display:none;
242246
}
243247
}

0 commit comments

Comments
 (0)