Skip to content

Commit

Permalink
separate css and js from rust
Browse files Browse the repository at this point in the history
  • Loading branch information
azerupi committed Jun 24, 2015
1 parent 98633de commit 6709685
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 82 deletions.
18 changes: 13 additions & 5 deletions src/rustbook/build.rs
Expand Up @@ -22,7 +22,7 @@ use term::Term;
use error::{err, CliResult, CommandResult};
use book;
use book::{Book, BookItem};
use css;

use javascript;

use rustdoc;
Expand Down Expand Up @@ -146,7 +146,8 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
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&#61;Open+Sans:400italic,700italic,400,700"),
"http://fonts.googleapis.com/css?family&#61;Open+Sans:400italic,700italic,400,700"
),
"--markdown-no-toc".to_string(),
];
let output_result = rustdoc::main_args(rustdoc_args);
Expand Down Expand Up @@ -197,9 +198,16 @@ impl Subcommand for Build {
}
try!(fs::create_dir(&tgt));

try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| {
f.write_all(css::STYLE.as_bytes())
}));
// Copy static files
try!(fs::copy(
&cwd.join("src/rustbook/static/rustbook.css"),
&tgt.join("rust-book.css")
));

try!(fs::copy(
&cwd.join("src/rustbook/static/rustbook.js"),
&tgt.join("rust-book.js")
));

let mut summary = try!(File::open(&src.join("SUMMARY.md")));
match book::parse_summary(&mut summary, &src) {
Expand Down
62 changes: 1 addition & 61 deletions src/rustbook/javascript.rs
Expand Up @@ -11,66 +11,6 @@
// The rust-book JavaScript in string form.

pub static JAVASCRIPT: &'static str = r#"
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("toggle-nav").onclick = toggleNav;
function toggleNav() {
var toc = document.getElementById("toc");
var pagewrapper = document.getElementById("page-wrapper");
toggleClass(toc, "mobile-hidden");
toggleClass(pagewrapper, "mobile-hidden");
};
function toggleClass(el, className) {
// from http://youmightnotneedjquery.com/
if (el.classList) {
el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0) {
classes.splice(existingIndex, 1);
} else {
classes.push(className);
}
el.className = classes.join(' ');
}
}
// The below code is used to add prev and next navigation links to the bottom
// of each of the sections.
// It works by extracting the current page based on the url and iterates over
// the menu links until it finds the menu item for the current page. We then
// create a copy of the preceding and following menu links and add the
// correct css class and insert them into the bottom of the page.
var toc = document.getElementById('toc').getElementsByTagName('a');
var href = document.location.pathname.split('/').pop();
if (href === 'index.html' || href === '') {
href = 'README.html';
}
for (var i = 0; i < toc.length; i++) {
if (toc[i].attributes['href'].value.split('/').pop() === href) {
var nav = document.createElement('p');
nav.className = 'nav-previous-next'
if (i > 0) {
var prevNode = toc[i-1].cloneNode(true);
prevNode.className = 'left';
nav.appendChild(prevNode);
}
if (i < toc.length - 1) {
var nextNode = toc[i+1].cloneNode(true);
nextNode.className = 'right';
nav.appendChild(nextNode);
}
document.getElementById('page').appendChild(nav);
break;
}
}
});
</script>
<script type="text/javascript" src="rust-book.js"></script>
<script type="text/javascript" src="playpen.js"></script>
"#;
1 change: 0 additions & 1 deletion src/rustbook/main.rs
Expand Up @@ -35,7 +35,6 @@ mod build;
mod serve;
mod test;

mod css;
mod javascript;

static EXIT_STATUS: AtomicIsize = ATOMIC_ISIZE_INIT;
Expand Down
16 changes: 1 addition & 15 deletions src/rustbook/css.rs → src/rustbook/static/rustbook.css
@@ -1,16 +1,3 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// The rust-book CSS in string form.

pub static STYLE: &'static str = r#"
@import url("../rust.css");

body {
Expand Down Expand Up @@ -57,7 +44,7 @@ h1, h2, h3, h4, h5, h6 {
}

@media only print {
#toc, #nav {
#toc, #nav, #menu-bar {
display: none;
}
}
Expand Down Expand Up @@ -174,4 +161,3 @@ pre {
.right {
float: right;
}
"#;
60 changes: 60 additions & 0 deletions src/rustbook/static/rustbook.js
@@ -0,0 +1,60 @@
document.addEventListener("DOMContentLoaded", function(event) {

document.getElementById("toggle-nav").onclick = toggleNav;
function toggleNav() {
var toc = document.getElementById("toc");
var pagewrapper = document.getElementById("page-wrapper");
toggleClass(toc, "mobile-hidden");
toggleClass(pagewrapper, "mobile-hidden");
}

function toggleClass(el, className) {
// from http://youmightnotneedjquery.com/
if (el.classList) {
el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);

if (existingIndex >= 0) {
classes.splice(existingIndex, 1);
} else {
classes.push(className);
}

el.className = classes.join(' ');
}
}

// The below code is used to add prev and next navigation links to the bottom
// of each of the sections.
// It works by extracting the current page based on the url and iterates over
// the menu links until it finds the menu item for the current page. We then
// create a copy of the preceding and following menu links and add the
// correct css class and insert them into the bottom of the page.
var toc = document.getElementById('toc').getElementsByTagName('a');
var href = document.location.pathname.split('/').pop();
if (href === 'index.html' || href === '') {
href = 'README.html';
}

for (var i = 0; i < toc.length; i++) {
if (toc[i].attributes['href'].value.split('/').pop() === href) {
var nav = document.createElement('p');
nav.className = 'nav-previous-next';
if (i > 0) {
var prevNode = toc[i-1].cloneNode(true);
prevNode.className = 'left';
nav.appendChild(prevNode);
}
if (i < toc.length - 1) {
var nextNode = toc[i+1].cloneNode(true);
nextNode.className = 'right';
nav.appendChild(nextNode);
}
document.getElementById('page').appendChild(nav);
break;
}
}

});

0 comments on commit 6709685

Please sign in to comment.