Skip to content

Commit d6d1ba0

Browse files
committed
Merge pull request #21071 from sfaxon/mobile-friendly-book
make the book more mobile friendly Reviewed-by: Gankro
2 parents 1692428 + 9af8a64 commit d6d1ba0

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

src/librustdoc/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
9191
<html lang="en">
9292
<head>
9393
<meta charset="utf-8">
94+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
9495
<meta name="generator" content="rustdoc">
9596
<title>{title}</title>
9697

src/rustbook/build.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use error::{Error, CliResult, CommandResult};
2020
use book;
2121
use book::{Book, BookItem};
2222
use css;
23+
use javascript;
2324

2425
use regex::Regex;
2526

@@ -63,7 +64,7 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()>
6364
Ok(())
6465
}
6566

66-
try!(writeln!(out, "<div id='toc'>"));
67+
try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
6768
try!(writeln!(out, "<ul class='chapter'>"));
6869
try!(walk_items(&book.chapters[], "", path_to_root, out));
6970
try!(writeln!(out, "</ul>"));
@@ -102,6 +103,14 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
102103
let prelude = tmp.path().join("prelude.html");
103104
{
104105
let mut toc = BufferedWriter::new(try!(File::create(&prelude)));
106+
try!(writeln!(&mut toc, r#"<div id="nav">
107+
<button id="toggle-nav">
108+
<span class="sr-only">Toggle navigation</span>
109+
<span class="bar"></span>
110+
<span class="bar"></span>
111+
<span class="bar"></span>
112+
</button>
113+
</div>"#));
105114
let _ = write_toc(book, &item.path_to_root, &mut toc);
106115
try!(writeln!(&mut toc, "<div id='page-wrapper'>"));
107116
try!(writeln!(&mut toc, "<div id='page'>"));
@@ -111,6 +120,7 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> {
111120
let postlude = tmp.path().join("postlude.html");
112121
{
113122
let mut toc = BufferedWriter::new(try!(File::create(&postlude)));
123+
try!(toc.write_str(javascript::JAVASCRIPT));
114124
try!(writeln!(&mut toc, "</div></div>"));
115125
}
116126

src/rustbook/css.rs

+55-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ body {
4545
#page {
4646
margin-left: auto;
4747
margin-right:auto;
48-
width: 750px;
48+
max-width: 750px;
4949
}
5050
5151
.chapter {
@@ -69,4 +69,58 @@ body {
6969
.chapter li a {
7070
color: #000000;
7171
}
72+
73+
@media only screen and (max-width: 1060px) {
74+
#toc {
75+
width: 100%;
76+
margin-right: 0;
77+
top: 40px;
78+
}
79+
#page-wrapper {
80+
top: 40px;
81+
left: 15px;
82+
padding-right: 15px;
83+
}
84+
.mobile-hidden {
85+
display: none;
86+
}
87+
}
88+
89+
90+
#toggle-nav {
91+
height: 20px;
92+
width: 30px;
93+
padding: 3px 3px 0 3px;
94+
}
95+
96+
#toggle-nav {
97+
margin-top: 5px;
98+
width: 30px;
99+
height: 30px;
100+
background-color: #FFF;
101+
border: 1px solid #666;
102+
border-radius: 3px 3px 3px 3px;
103+
}
104+
105+
.sr-only {
106+
position: absolute;
107+
width: 1px;
108+
height: 1px;
109+
margin: -1px;
110+
padding: 0;
111+
overflow: hidden;
112+
clip: rect(0, 0, 0, 0);
113+
border: 0;
114+
}
115+
116+
.bar {
117+
display: block;
118+
background-color: #000;
119+
border-radius: 2px;
120+
width: 100%;
121+
height: 2px;
122+
margin: 2px 0 3px;
123+
padding: 0;
124+
}
125+
72126
"#;

src/rustbook/javascript.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015 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 JavaScript in string form.
12+
13+
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+
</script>
43+
"#;

src/rustbook/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod serve;
3939
mod test;
4040

4141
mod css;
42+
mod javascript;
4243

4344
#[cfg(not(test))] // thanks #12327
4445
fn main() {

0 commit comments

Comments
 (0)