]> git.proxmox.com Git - rustc.git/blob - src/rustbook/build.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / rustbook / build.rs
1 // Copyright 2014-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 //! Implementation of the `build` subcommand, used to compile a book.
12
13 use std::env;
14 use std::fs::{self, File};
15 use std::io::prelude::*;
16 use std::io::{self, BufWriter};
17 use std::path::{Path, PathBuf};
18 use rustc_back::tempdir::TempDir;
19
20 use subcommand::Subcommand;
21 use term::Term;
22 use error::{err, CliResult, CommandResult};
23 use book;
24 use book::{Book, BookItem};
25
26 use rustdoc;
27
28 struct Build;
29
30 pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
31 if name == "build" {
32 Some(Box::new(Build))
33 } else {
34 None
35 }
36 }
37
38 fn write_toc(book: &Book, current_page: &BookItem, out: &mut Write) -> io::Result<()> {
39 fn walk_items(items: &[BookItem],
40 section: &str,
41 current_page: &BookItem,
42 out: &mut Write) -> io::Result<()> {
43 for (i, item) in items.iter().enumerate() {
44 try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], current_page, out));
45 }
46 Ok(())
47 }
48 fn walk_item(item: &BookItem,
49 section: &str,
50 current_page: &BookItem,
51 out: &mut Write) -> io::Result<()> {
52 let class_string = if item.path == current_page.path {
53 "class='active'"
54 } else {
55 ""
56 };
57
58 try!(writeln!(out, "<li><a {} href='{}'><b>{}</b> {}</a>",
59 class_string,
60 current_page.path_to_root.join(&item.path).with_extension("html").display(),
61 section,
62 item.title));
63 if !item.children.is_empty() {
64 try!(writeln!(out, "<ul class='section'>"));
65 let _ = walk_items(&item.children[..], section, current_page, out);
66 try!(writeln!(out, "</ul>"));
67 }
68 try!(writeln!(out, "</li>"));
69
70 Ok(())
71 }
72
73 try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
74 try!(writeln!(out, "<ul class='chapter'>"));
75 try!(walk_items(&book.chapters[..], "", &current_page, out));
76 try!(writeln!(out, "</ul>"));
77 try!(writeln!(out, "</div>"));
78
79 Ok(())
80 }
81
82 fn render(book: &Book, tgt: &Path) -> CliResult<()> {
83 let tmp = try!(TempDir::new("rustbook"));
84
85 for (_section, item) in book.iter() {
86 let out_path = match item.path.parent() {
87 Some(p) => tgt.join(p),
88 None => tgt.to_path_buf(),
89 };
90
91 let src;
92 if env::args().len() < 3 {
93 src = env::current_dir().unwrap().clone();
94 } else {
95 src = PathBuf::from(&env::args().nth(2).unwrap());
96 }
97 // preprocess the markdown, rerouting markdown references to html
98 // references
99 let mut markdown_data = String::new();
100 try!(File::open(&src.join(&item.path)).and_then(|mut f| {
101 f.read_to_string(&mut markdown_data)
102 }));
103 let preprocessed_path = tmp.path().join(item.path.file_name().unwrap());
104 {
105 let urls = markdown_data.replace(".md)", ".html)");
106 try!(File::create(&preprocessed_path).and_then(|mut f| {
107 f.write_all(urls.as_bytes())
108 }));
109 }
110
111 // write the prelude to a temporary HTML file for rustdoc inclusion
112 let prelude = tmp.path().join("prelude.html");
113 {
114 let mut buffer = BufWriter::new(try!(File::create(&prelude)));
115 try!(writeln!(&mut buffer, r#"
116 <div id="nav">
117 <button id="toggle-nav">
118 <span class="sr-only">Toggle navigation</span>
119 <span class="bar"></span>
120 <span class="bar"></span>
121 <span class="bar"></span>
122 </button>
123 </div>"#));
124 let _ = write_toc(book, &item, &mut buffer);
125 try!(writeln!(&mut buffer, "<div id='page-wrapper'>"));
126 try!(writeln!(&mut buffer, "<div id='page'>"));
127 }
128
129 // write the postlude to a temporary HTML file for rustdoc inclusion
130 let postlude = tmp.path().join("postlude.html");
131 {
132 let mut buffer = BufWriter::new(try!(File::create(&postlude)));
133 try!(writeln!(&mut buffer, "<script src='rustbook.js'></script>"));
134 try!(writeln!(&mut buffer, "<script src='playpen.js'></script>"));
135 try!(writeln!(&mut buffer, "</div></div>"));
136 }
137
138 try!(fs::create_dir_all(&out_path));
139
140 let rustdoc_args: &[String] = &[
141 "".to_string(),
142 preprocessed_path.display().to_string(),
143 format!("-o{}", out_path.display()),
144 format!("--html-before-content={}", prelude.display()),
145 format!("--html-after-content={}", postlude.display()),
146 format!("--markdown-playground-url=https://play.rust-lang.org"),
147 format!("--markdown-css={}", item.path_to_root.join("rustbook.css").display()),
148 "--markdown-no-toc".to_string(),
149 ];
150 let output_result = rustdoc::main_args(rustdoc_args);
151 if output_result != 0 {
152 let message = format!("Could not execute `rustdoc` with {:?}: {}",
153 rustdoc_args, output_result);
154 return Err(err(&message));
155 }
156 }
157
158 // create index.html from the root README
159 try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
160
161 // Copy js for playpen
162 let mut playpen = try!(File::create(tgt.join("playpen.js")));
163 let js = include_bytes!("../librustdoc/html/static/playpen.js");
164 try!(playpen.write_all(js));
165 Ok(())
166 }
167
168 impl Subcommand for Build {
169 fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
170 Ok(())
171 }
172 fn usage(&self) {}
173 fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
174 let cwd = env::current_dir().unwrap();
175 let src;
176 let tgt;
177
178 if env::args().len() < 3 {
179 src = cwd.clone();
180 } else {
181 src = PathBuf::from(&env::args().nth(2).unwrap());
182 }
183
184 if env::args().len() < 4 {
185 tgt = cwd.join("_book");
186 } else {
187 tgt = PathBuf::from(&env::args().nth(3).unwrap());
188 }
189
190 // `_book` directory may already exist from previous runs. Check and
191 // delete it if it exists.
192 for entry in try!(fs::read_dir(&cwd)) {
193 let path = try!(entry).path();
194 if path == tgt { try!(fs::remove_dir_all(&tgt)) }
195 }
196 try!(fs::create_dir(&tgt));
197
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("rustbook.css")));
203 try!(css_file.write_all(css));
204
205 let mut js_file = try!(File::create(tgt.join("rustbook.js")));
206 try!(js_file.write_all(js));
207
208
209 let mut summary = try!(File::open(&src.join("SUMMARY.md")));
210 match book::parse_summary(&mut summary, &src) {
211 Ok(book) => {
212 // execute rustdoc on the whole book
213 render(&book, &tgt)
214 }
215 Err(errors) => {
216 let n = errors.len();
217 for err in errors {
218 term.err(&format!("error: {}", err)[..]);
219 }
220
221 Err(err(&format!("{} errors occurred", n)))
222 }
223 }
224 }
225 }