]> git.lizzy.rs Git - rust.git/blob - src/rustbook/build.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / rustbook / build.rs
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 //! Implementation of the `build` subcommand, used to compile a book.
12
13 use std::os;
14 use std::env;
15 use std::old_io;
16 use std::old_io::{fs, File, BufferedWriter, TempDir, IoResult};
17
18 use subcommand::Subcommand;
19 use term::Term;
20 use error::{Error, CliResult, CommandResult};
21 use book;
22 use book::{Book, BookItem};
23 use css;
24 use javascript;
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 Build as Box<Subcommand>)
33     } else {
34         None
35     }
36 }
37
38 fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> {
39     fn walk_items(items: &[BookItem],
40                   section: &str,
41                   path_to_root: &Path,
42                   out: &mut Writer) -> IoResult<()> {
43         for (i, item) in items.iter().enumerate() {
44             try!(walk_item(item, &format!("{}{}.", section, i + 1)[], path_to_root, out));
45         }
46         Ok(())
47     }
48     fn walk_item(item: &BookItem,
49                  section: &str,
50                  path_to_root: &Path,
51                  out: &mut Writer) -> IoResult<()> {
52         try!(writeln!(out, "<li><a href='{}'><b>{}</b> {}</a>",
53                  path_to_root.join(item.path.with_extension("html")).display(),
54                  section,
55                  item.title));
56         if !item.children.is_empty() {
57             try!(writeln!(out, "<ul class='section'>"));
58             let _ = walk_items(&item.children[], section, path_to_root, out);
59             try!(writeln!(out, "</ul>"));
60         }
61         try!(writeln!(out, "</li>"));
62
63         Ok(())
64     }
65
66     try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
67     try!(writeln!(out, "<ul class='chapter'>"));
68     try!(walk_items(&book.chapters[], "", path_to_root, out));
69     try!(writeln!(out, "</ul>"));
70     try!(writeln!(out, "</div>"));
71
72     Ok(())
73 }
74
75 fn render(book: &Book, tgt: &Path) -> CliResult<()> {
76     let tmp = try!(TempDir::new("rust-book"));
77
78     for (section, item) in book.iter() {
79         println!("{} {}", section, item.title);
80
81         let out_path = tgt.join(item.path.dirname());
82
83         let src;
84         if env::args().len() < 3 {
85             src = os::getcwd().unwrap().clone();
86         } else {
87             src = Path::new(env::args().nth(2).unwrap().clone());
88         }
89         // preprocess the markdown, rerouting markdown references to html references
90         let markdown_data = try!(File::open(&src.join(&item.path)).read_to_string());
91         let preprocessed_path = tmp.path().join(item.path.filename().unwrap());
92         {
93             let urls = markdown_data.replace(".md)", ".html)");
94             try!(File::create(&preprocessed_path)
95                       .write_str(&urls[]));
96         }
97
98         // write the prelude to a temporary HTML file for rustdoc inclusion
99         let prelude = tmp.path().join("prelude.html");
100         {
101             let mut toc = BufferedWriter::new(try!(File::create(&prelude)));
102             try!(writeln!(&mut toc, r#"<div id="nav">
103                 <button id="toggle-nav">
104                   <span class="sr-only">Toggle navigation</span>
105                   <span class="bar"></span>
106                   <span class="bar"></span>
107                   <span class="bar"></span>
108                 </button>
109               </div>"#));
110             let _ = write_toc(book, &item.path_to_root, &mut toc);
111             try!(writeln!(&mut toc, "<div id='page-wrapper'>"));
112             try!(writeln!(&mut toc, "<div id='page'>"));
113         }
114
115         // write the postlude to a temporary HTML file for rustdoc inclusion
116         let postlude = tmp.path().join("postlude.html");
117         {
118             let mut toc = BufferedWriter::new(try!(File::create(&postlude)));
119             try!(toc.write_str(javascript::JAVASCRIPT));
120             try!(writeln!(&mut toc, "</div></div>"));
121         }
122
123         try!(fs::mkdir_recursive(&out_path, old_io::USER_DIR));
124
125         let rustdoc_args: &[String] = &[
126             "".to_string(),
127             preprocessed_path.display().to_string(),
128             format!("-o{}", out_path.display()),
129             format!("--html-before-content={}", prelude.display()),
130             format!("--html-after-content={}", postlude.display()),
131             format!("--markdown-css={}", item.path_to_root.join("rust-book.css").display()),
132             "--markdown-no-toc".to_string(),
133         ];
134         let output_result = rustdoc::main_args(rustdoc_args);
135         if output_result != 0 {
136             let message = format!("Could not execute `rustdoc` with {:?}: {}",
137                                   rustdoc_args, output_result);
138             return Err(box message as Box<Error>);
139         }
140     }
141
142     // create index.html from the root README
143     try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
144     Ok(())
145 }
146
147 impl Subcommand for Build {
148     fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
149         Ok(())
150     }
151     fn usage(&self) {}
152     fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
153         let cwd = os::getcwd().unwrap();
154         let src;
155         let tgt;
156
157         if env::args().len() < 3 {
158             src = cwd.clone();
159         } else {
160             src = Path::new(env::args().nth(2).unwrap().clone());
161         }
162
163         if env::args().len() < 4 {
164             tgt = cwd.join("_book");
165         } else {
166             tgt = Path::new(env::args().nth(3).unwrap().clone());
167         }
168
169         try!(fs::mkdir(&tgt, old_io::USER_DIR));
170
171         try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE));
172
173         let summary = try!(File::open(&src.join("SUMMARY.md")));
174         match book::parse_summary(summary, &src) {
175             Ok(book) => {
176                 // execute rustdoc on the whole book
177                 render(&book, &tgt)
178             }
179             Err(errors) => {
180                 let n = errors.len();
181                 for err in errors {
182                     term.err(&format!("error: {}", err)[]);
183                 }
184
185                 Err(box format!("{} errors occurred", n) as Box<Error>)
186             }
187         }
188     }
189 }