Skip to content

Commit 75e4a78

Browse files
committedJul 27, 2015
Auto merge of #26216 - azerupi:doc-experiments, r=steveklabnik
So I have tried to improve the rustbook engine: - The sidebar now looks a lot more like gitbook (I thinks it cleaner) - Added the Open Sans font, in my opinion more readable for prolonged periods of time - Changed the style for code blocks a little I encountered 1 problem. In `build.rs` I added this google font url (I commented out the non-relevant parts for clarity) ```rust let rustdoc_args: &[String] = &[ //"".to_string(), //preprocessed_path.display().to_string(), //format!("-o{}", out_path.display()), //format!("--html-before-content={}", prelude.display()), //format!("--html-after-content={}", postlude.display()), //format!("--markdown-playground-url=http://play.rust-lang.org"), //format!("--markdown-css={}", item.path_to_root.join("rust-book.css").display()), format!("--markdown-css=http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700"), //"--markdown-no-toc".to_string(), ]; ``` As you can see, I had to escape `=` with `=` because the string would get truncated if I didn't. Is that normal behaviour? Is that for security measures? If it is, isn't it a little weak if you can circumvent it by escaped characters? I don't know the reason behind, but I thought it was at least worth mentioning :) Take your time for this PR, I still want to add multiple improvements: - Like gitbook, possibility to change font by user - Put `css` and `js` in their respective files (not hardcoded in rust) - button to hide sidebar - ... So I'm not in a hurry to get this merged ;) But if you think it's good enough to be merged, go ahead. I will make another PR when I have other improvements. In the image below is a screen of the improvements ![rustbook](https://cloud.githubusercontent.com/assets/7647338/8105345/bf545c74-1038-11e5-962e-b04ebfaf8257.png)
2 parents 3e6b03c + f6e9240 commit 75e4a78

File tree

5 files changed

+135
-90
lines changed

5 files changed

+135
-90
lines changed
 

Diff for: ‎src/rustbook/build.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use term::Term;
2222
use error::{err, CliResult, CommandResult};
2323
use book;
2424
use book::{Book, BookItem};
25-
use css;
25+
2626
use javascript;
2727

2828
use rustdoc;
@@ -195,9 +195,16 @@ impl Subcommand for Build {
195195
}
196196
try!(fs::create_dir(&tgt));
197197

198-
try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| {
199-
f.write_all(css::STYLE.as_bytes())
200-
}));
198+
// Copy static files
199+
let css = include_bytes!("static/rustbook.css");
200+
let js = include_bytes!("static/rustbook.js");
201+
202+
let mut css_file = try!(File::create(tgt.join("rust-book.css")));
203+
try!(css_file.write_all(css));
204+
205+
let mut js_file = try!(File::create(tgt.join("rust-book.js")));
206+
try!(js_file.write_all(js));
207+
201208

202209
let mut summary = try!(File::open(&src.join("SUMMARY.md")));
203210
match book::parse_summary(&mut summary, &src) {

Diff for: ‎src/rustbook/javascript.rs

+1-62
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,6 @@
1111
// The rust-book JavaScript in string form.
1212

1313
pub static JAVASCRIPT: &'static str = r#"
14-
<script type="text/javascript">
15-
document.addEventListener("DOMContentLoaded", function(event) {
16-
document.getElementById("toggle-nav").onclick = toggleNav;
17-
function toggleNav() {
18-
var toc = document.getElementById("toc");
19-
var pagewrapper = document.getElementById("page-wrapper");
20-
toggleClass(toc, "mobile-hidden");
21-
toggleClass(pagewrapper, "mobile-hidden");
22-
};
23-
24-
function toggleClass(el, className) {
25-
// from http://youmightnotneedjquery.com/
26-
if (el.classList) {
27-
el.classList.toggle(className);
28-
} else {
29-
var classes = el.className.split(' ');
30-
var existingIndex = classes.indexOf(className);
31-
32-
if (existingIndex >= 0) {
33-
classes.splice(existingIndex, 1);
34-
} else {
35-
classes.push(className);
36-
}
37-
38-
el.className = classes.join(' ');
39-
}
40-
}
41-
42-
// The below code is used to add prev and next navigation links to the bottom
43-
// of each of the sections.
44-
// It works by extracting the current page based on the url and iterates over
45-
// the menu links until it finds the menu item for the current page. We then
46-
// create a copy of the preceding and following menu links and add the
47-
// correct css class and insert them into the bottom of the page.
48-
var toc = document.getElementById('toc').getElementsByTagName('a');
49-
var href = document.location.pathname.split('/').pop();
50-
if (href === 'index.html' || href === '') {
51-
href = 'README.html';
52-
}
53-
54-
for (var i = 0; i < toc.length; i++) {
55-
if (toc[i].attributes['href'].value.split('/').pop() === href) {
56-
var nav = document.createElement('p');
57-
if (i > 0) {
58-
var prevNode = toc[i-1].cloneNode(true);
59-
prevNode.className = 'left';
60-
prevNode.setAttribute('rel', 'prev');
61-
nav.appendChild(prevNode);
62-
}
63-
if (i < toc.length - 1) {
64-
var nextNode = toc[i+1].cloneNode(true);
65-
nextNode.className = 'right';
66-
nextNode.setAttribute('rel', 'next');
67-
nav.appendChild(nextNode);
68-
}
69-
document.getElementById('page').appendChild(nav);
70-
break;
71-
}
72-
}
73-
74-
});
75-
</script>
14+
<script type="text/javascript" src="rust-book.js"></script>
7615
<script type="text/javascript" src="playpen.js"></script>
7716
"#;

Diff for: ‎src/rustbook/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ mod build;
3535
mod serve;
3636
mod test;
3737

38-
mod css;
3938
mod javascript;
4039

4140
static EXIT_STATUS: AtomicIsize = ATOMIC_ISIZE_INIT;

Diff for: ‎src/rustbook/css.rs renamed to ‎src/rustbook/static/rustbook.css

+50-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2-
// file at the top-level directory of this distribution and at
3-
// http://rust-lang.org/COPYRIGHT.
4-
//
5-
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6-
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8-
// option. This file may not be copied, modified, or distributed
9-
// except according to those terms.
10-
11-
// The rust-book CSS in string form.
12-
13-
pub static STYLE: &'static str = r#"
1+
/**
2+
* Copyright 2013 The Rust Project Developers. See the COPYRIGHT
3+
* file at the top-level directory of this distribution and at
4+
* http://rust-lang.org/COPYRIGHT.
5+
*
6+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
* option. This file may not be copied, modified, or distributed
10+
* except according to those terms.
11+
*/
12+
1413
@import url("../rust.css");
1514

1615
body {
1716
max-width:none;
17+
font: 16px/1.4 'Source Serif Pro', Georgia, Times, 'Times New Roman', serif;
18+
line-height: 1.6;
19+
color: #333;
20+
}
21+
22+
h1, h2, h3, h4, h5, h6 {
23+
font-family: 'Open Sans', 'Fira Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
24+
font-weight: bold;
25+
color: #333;
1826
}
1927

2028
@media only screen {
@@ -23,20 +31,21 @@ body {
2331
left: 0px;
2432
top: 0px;
2533
bottom: 0px;
26-
width: 250px;
34+
width: 300px;
2735
overflow-y: auto;
2836
border-right: 1px solid rgba(0, 0, 0, 0.07);
2937
padding: 10px 10px;
30-
font-size: 16px;
31-
background: none repeat scroll 0% 0% #FFF;
38+
font-size: 14px;
3239
box-sizing: border-box;
3340
-webkit-overflow-scrolling: touch;
41+
background-color: #fafafa;
42+
color: #364149;
3443
}
3544

3645
#page-wrapper {
3746
position: absolute;
3847
overflow-y: auto;
39-
left: 260px;
48+
left: 310px;
4049
right: 0px;
4150
top: 0px;
4251
bottom: 0px;
@@ -47,7 +56,7 @@ body {
4756
}
4857

4958
@media only print {
50-
#toc, #nav {
59+
#toc, #nav, #menu-bar {
5160
display: none;
5261
}
5362
}
@@ -84,7 +93,7 @@ body {
8493
.section {
8594
list-style: none outside none;
8695
padding-left: 20px;
87-
line-height: 30px;
96+
line-height: 40px;
8897
}
8998

9099
.section li {
@@ -94,12 +103,17 @@ body {
94103
}
95104

96105
.chapter li a {
97-
color: #000000;
106+
color: #333;
107+
padding: 5px 0;
98108
}
99109

100110
.chapter li a.active {
101-
text-decoration: underline;
102-
font-weight: bold;
111+
color: #008cff;
112+
}
113+
114+
.chapter li a:hover {
115+
color: #008cff;
116+
text-decoration: none;
103117
}
104118

105119
#toggle-nav {
@@ -138,11 +152,24 @@ body {
138152
padding: 0;
139153
}
140154

155+
pre {
156+
padding: 16px;
157+
overflow: auto;
158+
font-size: 85%;
159+
line-height: 1.45;
160+
background-color: #f7f7f7;
161+
border: 0;
162+
border-radius: 3px;
163+
}
164+
165+
.nav-previous-next {
166+
margin-top: 60px;
167+
}
168+
141169
.left {
142170
float: left;
143171
}
144172

145173
.right {
146174
float: right;
147175
}
148-
"#;

Diff for: ‎src/rustbook/static/rustbook.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
document.addEventListener("DOMContentLoaded", function(event) {
13+
14+
document.getElementById("toggle-nav").onclick = toggleNav;
15+
16+
function toggleNav() {
17+
var toc = document.getElementById("toc");
18+
var pagewrapper = document.getElementById("page-wrapper");
19+
toggleClass(toc, "mobile-hidden");
20+
toggleClass(pagewrapper, "mobile-hidden");
21+
}
22+
23+
function toggleClass(el, className) {
24+
// from http://youmightnotneedjquery.com/
25+
if (el.classList) {
26+
el.classList.toggle(className);
27+
} else {
28+
var classes = el.className.split(' ');
29+
var existingIndex = classes.indexOf(className);
30+
31+
if (existingIndex >= 0) {
32+
classes.splice(existingIndex, 1);
33+
} else {
34+
classes.push(className);
35+
}
36+
37+
el.className = classes.join(' ');
38+
}
39+
}
40+
41+
// The below code is used to add prev and next navigation links to the bottom
42+
// of each of the sections.
43+
// It works by extracting the current page based on the url and iterates over
44+
// the menu links until it finds the menu item for the current page. We then
45+
// create a copy of the preceding and following menu links and add the
46+
// correct css class and insert them into the bottom of the page.
47+
var toc = document.getElementById('toc').getElementsByTagName('a');
48+
var href = document.location.pathname.split('/').pop();
49+
if (href === 'index.html' || href === '') {
50+
href = 'README.html';
51+
}
52+
53+
for (var i = 0; i < toc.length; i++) {
54+
if (toc[i].attributes.href.value.split('/').pop() === href) {
55+
var nav = document.createElement('p');
56+
if (i > 0) {
57+
var prevNode = toc[i-1].cloneNode(true);
58+
prevNode.className = 'left';
59+
prevNode.setAttribute('rel', 'prev');
60+
nav.appendChild(prevNode);
61+
}
62+
if (i < toc.length - 1) {
63+
var nextNode = toc[i+1].cloneNode(true);
64+
nextNode.className = 'right';
65+
nextNode.setAttribute('rel', 'next');
66+
nav.appendChild(nextNode);
67+
}
68+
document.getElementById('page').appendChild(nav);
69+
break;
70+
}
71+
}
72+
73+
});

0 commit comments

Comments
 (0)
Please sign in to comment.