Skip to content

Commit 6709685

Browse files
committed
separate css and js from rust
1 parent 98633de commit 6709685

File tree

5 files changed

+75
-82
lines changed

5 files changed

+75
-82
lines changed

src/rustbook/build.rs

Lines changed: 13 additions & 5 deletions
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;
@@ -146,7 +146,8 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
146146
format!("--markdown-playground-url=http://play.rust-lang.org"),
147147
format!("--markdown-css={}", item.path_to_root.join("rust-book.css").display()),
148148
format!("--markdown-css={}",
149-
"http://fonts.googleapis.com/css?family&#61;Open+Sans:400italic,700italic,400,700"),
149+
"http://fonts.googleapis.com/css?family&#61;Open+Sans:400italic,700italic,400,700"
150+
),
150151
"--markdown-no-toc".to_string(),
151152
];
152153
let output_result = rustdoc::main_args(rustdoc_args);
@@ -197,9 +198,16 @@ impl Subcommand for Build {
197198
}
198199
try!(fs::create_dir(&tgt));
199200

200-
try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| {
201-
f.write_all(css::STYLE.as_bytes())
202-
}));
201+
// Copy static files
202+
try!(fs::copy(
203+
&cwd.join("src/rustbook/static/rustbook.css"),
204+
&tgt.join("rust-book.css")
205+
));
206+
207+
try!(fs::copy(
208+
&cwd.join("src/rustbook/static/rustbook.js"),
209+
&tgt.join("rust-book.js")
210+
));
203211

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

src/rustbook/javascript.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,66 +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-
nav.className = 'nav-previous-next'
58-
if (i > 0) {
59-
var prevNode = toc[i-1].cloneNode(true);
60-
prevNode.className = 'left';
61-
nav.appendChild(prevNode);
62-
}
63-
if (i < toc.length - 1) {
64-
var nextNode = toc[i+1].cloneNode(true);
65-
nextNode.className = 'right';
66-
nav.appendChild(nextNode);
67-
}
68-
document.getElementById('page').appendChild(nav);
69-
break;
70-
}
71-
}
72-
73-
});
74-
</script>
14+
<script type="text/javascript" src="rust-book.js"></script>
7515
<script type="text/javascript" src="playpen.js"></script>
7616
"#;

src/rustbook/main.rs

Lines changed: 0 additions & 1 deletion
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;

src/rustbook/css.rs renamed to src/rustbook/static/rustbook.css

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
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#"
141
@import url("../rust.css");
152

163
body {
@@ -57,7 +44,7 @@ h1, h2, h3, h4, h5, h6 {
5744
}
5845

5946
@media only print {
60-
#toc, #nav {
47+
#toc, #nav, #menu-bar {
6148
display: none;
6249
}
6350
}
@@ -174,4 +161,3 @@ pre {
174161
.right {
175162
float: right;
176163
}
177-
"#;

src/rustbook/static/rustbook.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
document.addEventListener("DOMContentLoaded", function(event) {
2+
3+
document.getElementById("toggle-nav").onclick = toggleNav;
4+
function toggleNav() {
5+
var toc = document.getElementById("toc");
6+
var pagewrapper = document.getElementById("page-wrapper");
7+
toggleClass(toc, "mobile-hidden");
8+
toggleClass(pagewrapper, "mobile-hidden");
9+
}
10+
11+
function toggleClass(el, className) {
12+
// from http://youmightnotneedjquery.com/
13+
if (el.classList) {
14+
el.classList.toggle(className);
15+
} else {
16+
var classes = el.className.split(' ');
17+
var existingIndex = classes.indexOf(className);
18+
19+
if (existingIndex >= 0) {
20+
classes.splice(existingIndex, 1);
21+
} else {
22+
classes.push(className);
23+
}
24+
25+
el.className = classes.join(' ');
26+
}
27+
}
28+
29+
// The below code is used to add prev and next navigation links to the bottom
30+
// of each of the sections.
31+
// It works by extracting the current page based on the url and iterates over
32+
// the menu links until it finds the menu item for the current page. We then
33+
// create a copy of the preceding and following menu links and add the
34+
// correct css class and insert them into the bottom of the page.
35+
var toc = document.getElementById('toc').getElementsByTagName('a');
36+
var href = document.location.pathname.split('/').pop();
37+
if (href === 'index.html' || href === '') {
38+
href = 'README.html';
39+
}
40+
41+
for (var i = 0; i < toc.length; i++) {
42+
if (toc[i].attributes['href'].value.split('/').pop() === href) {
43+
var nav = document.createElement('p');
44+
nav.className = 'nav-previous-next';
45+
if (i > 0) {
46+
var prevNode = toc[i-1].cloneNode(true);
47+
prevNode.className = 'left';
48+
nav.appendChild(prevNode);
49+
}
50+
if (i < toc.length - 1) {
51+
var nextNode = toc[i+1].cloneNode(true);
52+
nextNode.className = 'right';
53+
nav.appendChild(nextNode);
54+
}
55+
document.getElementById('page').appendChild(nav);
56+
break;
57+
}
58+
}
59+
60+
});

0 commit comments

Comments
 (0)