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.
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.
11 //! Implementation of the `build` subcommand, used to compile a book.
16 use std::old_io::{fs, File, BufferedWriter, TempDir, IoResult};
18 use subcommand::Subcommand;
20 use error::{Error, CliResult, CommandResult};
22 use book::{Book, BookItem};
30 pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
32 Some(box Build as Box<Subcommand>)
38 fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> {
39 fn walk_items(items: &[BookItem],
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));
48 fn walk_item(item: &BookItem,
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(),
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>"));
61 try!(writeln!(out, "</li>"));
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>"));
75 fn render(book: &Book, tgt: &Path) -> CliResult<()> {
76 let tmp = try!(TempDir::new("rust-book"));
78 for (section, item) in book.iter() {
79 println!("{} {}", section, item.title);
81 let out_path = tgt.join(item.path.dirname());
84 if env::args().len() < 3 {
85 src = os::getcwd().unwrap().clone();
87 src = Path::new(env::args().nth(2).unwrap().clone());
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());
93 let urls = markdown_data.replace(".md)", ".html)");
94 try!(File::create(&preprocessed_path)
98 // write the prelude to a temporary HTML file for rustdoc inclusion
99 let prelude = tmp.path().join("prelude.html");
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>
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'>"));
115 // write the postlude to a temporary HTML file for rustdoc inclusion
116 let postlude = tmp.path().join("postlude.html");
118 let mut toc = BufferedWriter::new(try!(File::create(&postlude)));
119 try!(toc.write_str(javascript::JAVASCRIPT));
120 try!(writeln!(&mut toc, "</div></div>"));
123 try!(fs::mkdir_recursive(&out_path, old_io::USER_DIR));
125 let rustdoc_args: &[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(),
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>);
142 // create index.html from the root README
143 try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
147 impl Subcommand for Build {
148 fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
152 fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
153 let cwd = os::getcwd().unwrap();
157 if env::args().len() < 3 {
160 src = Path::new(env::args().nth(2).unwrap().clone());
163 if env::args().len() < 4 {
164 tgt = cwd.join("_book");
166 tgt = Path::new(env::args().nth(3).unwrap().clone());
169 try!(fs::mkdir(&tgt, old_io::USER_DIR));
171 try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE));
173 let summary = try!(File::open(&src.join("SUMMARY.md")));
174 match book::parse_summary(summary, &src) {
176 // execute rustdoc on the whole book
180 let n = errors.len();
182 term.err(&format!("error: {}", err)[]);
185 Err(box format!("{} errors occurred", n) as Box<Error>)