2014-12-02 09:20:48 -05:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Implementation of the `build` subcommand, used to compile a book.
|
|
|
|
|
|
|
|
use std::os;
|
2015-02-16 16:04:02 +02:00
|
|
|
use std::env;
|
2015-01-22 16:31:00 -08:00
|
|
|
use std::old_io;
|
|
|
|
use std::old_io::{fs, File, BufferedWriter, TempDir, IoResult};
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
use subcommand::Subcommand;
|
|
|
|
use term::Term;
|
|
|
|
use error::{Error, CliResult, CommandResult};
|
|
|
|
use book;
|
|
|
|
use book::{Book, BookItem};
|
|
|
|
use css;
|
2015-01-12 16:51:31 -08:00
|
|
|
use javascript;
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
use rustdoc;
|
|
|
|
|
|
|
|
struct Build;
|
|
|
|
|
|
|
|
pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> {
|
|
|
|
if name == "build" {
|
|
|
|
Some(box Build as Box<Subcommand>)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> {
|
|
|
|
fn walk_items(items: &[BookItem],
|
|
|
|
section: &str,
|
|
|
|
path_to_root: &Path,
|
|
|
|
out: &mut Writer) -> IoResult<()> {
|
|
|
|
for (i, item) in items.iter().enumerate() {
|
|
|
|
try!(walk_item(item, &format!("{}{}.", section, i + 1)[], path_to_root, out));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn walk_item(item: &BookItem,
|
|
|
|
section: &str,
|
|
|
|
path_to_root: &Path,
|
|
|
|
out: &mut Writer) -> IoResult<()> {
|
|
|
|
try!(writeln!(out, "<li><a href='{}'><b>{}</b> {}</a>",
|
|
|
|
path_to_root.join(item.path.with_extension("html")).display(),
|
|
|
|
section,
|
|
|
|
item.title));
|
|
|
|
if !item.children.is_empty() {
|
|
|
|
try!(writeln!(out, "<ul class='section'>"));
|
|
|
|
let _ = walk_items(&item.children[], section, path_to_root, out);
|
|
|
|
try!(writeln!(out, "</ul>"));
|
|
|
|
}
|
|
|
|
try!(writeln!(out, "</li>"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-01-12 16:51:31 -08:00
|
|
|
try!(writeln!(out, "<div id='toc' class='mobile-hidden'>"));
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(out, "<ul class='chapter'>"));
|
|
|
|
try!(walk_items(&book.chapters[], "", path_to_root, out));
|
|
|
|
try!(writeln!(out, "</ul>"));
|
|
|
|
try!(writeln!(out, "</div>"));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(book: &Book, tgt: &Path) -> CliResult<()> {
|
2015-01-09 20:25:45 +11:00
|
|
|
let tmp = try!(TempDir::new("rust-book"));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
for (section, item) in book.iter() {
|
|
|
|
println!("{} {}", section, item.title);
|
|
|
|
|
|
|
|
let out_path = tgt.join(item.path.dirname());
|
|
|
|
|
|
|
|
let src;
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 3 {
|
2014-12-02 09:20:48 -05:00
|
|
|
src = os::getcwd().unwrap().clone();
|
|
|
|
} else {
|
2015-02-16 16:04:02 +02:00
|
|
|
src = Path::new(env::args().nth(2).unwrap().clone());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
// preprocess the markdown, rerouting markdown references to html references
|
|
|
|
let markdown_data = try!(File::open(&src.join(&item.path)).read_to_string());
|
|
|
|
let preprocessed_path = tmp.path().join(item.path.filename().unwrap());
|
|
|
|
{
|
2015-01-20 10:45:29 -08:00
|
|
|
let urls = markdown_data.replace(".md)", ".html)");
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(File::create(&preprocessed_path)
|
|
|
|
.write_str(&urls[]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the prelude to a temporary HTML file for rustdoc inclusion
|
|
|
|
let prelude = tmp.path().join("prelude.html");
|
|
|
|
{
|
|
|
|
let mut toc = BufferedWriter::new(try!(File::create(&prelude)));
|
2015-01-12 16:51:31 -08:00
|
|
|
try!(writeln!(&mut toc, r#"<div id="nav">
|
|
|
|
<button id="toggle-nav">
|
|
|
|
<span class="sr-only">Toggle navigation</span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
<span class="bar"></span>
|
|
|
|
</button>
|
|
|
|
</div>"#));
|
2014-12-02 09:20:48 -05:00
|
|
|
let _ = write_toc(book, &item.path_to_root, &mut toc);
|
|
|
|
try!(writeln!(&mut toc, "<div id='page-wrapper'>"));
|
|
|
|
try!(writeln!(&mut toc, "<div id='page'>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the postlude to a temporary HTML file for rustdoc inclusion
|
|
|
|
let postlude = tmp.path().join("postlude.html");
|
|
|
|
{
|
|
|
|
let mut toc = BufferedWriter::new(try!(File::create(&postlude)));
|
2015-01-12 16:51:31 -08:00
|
|
|
try!(toc.write_str(javascript::JAVASCRIPT));
|
2014-12-02 09:20:48 -05:00
|
|
|
try!(writeln!(&mut toc, "</div></div>"));
|
|
|
|
}
|
|
|
|
|
2015-01-22 16:31:00 -08:00
|
|
|
try!(fs::mkdir_recursive(&out_path, old_io::USER_DIR));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
|
|
|
let rustdoc_args: &[String] = &[
|
|
|
|
"".to_string(),
|
|
|
|
preprocessed_path.display().to_string(),
|
|
|
|
format!("-o{}", out_path.display()),
|
|
|
|
format!("--html-before-content={}", prelude.display()),
|
|
|
|
format!("--html-after-content={}", postlude.display()),
|
|
|
|
format!("--markdown-css={}", item.path_to_root.join("rust-book.css").display()),
|
|
|
|
"--markdown-no-toc".to_string(),
|
|
|
|
];
|
|
|
|
let output_result = rustdoc::main_args(rustdoc_args);
|
|
|
|
if output_result != 0 {
|
2015-01-08 10:27:03 -08:00
|
|
|
let message = format!("Could not execute `rustdoc` with {:?}: {}",
|
|
|
|
rustdoc_args, output_result);
|
2014-12-02 09:20:48 -05:00
|
|
|
return Err(box message as Box<Error>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create index.html from the root README
|
|
|
|
try!(fs::copy(&tgt.join("README.html"), &tgt.join("index.html")));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Subcommand for Build {
|
|
|
|
fn parse_args(&mut self, _: &[String]) -> CliResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn usage(&self) {}
|
|
|
|
fn execute(&mut self, term: &mut Term) -> CommandResult<()> {
|
|
|
|
let cwd = os::getcwd().unwrap();
|
|
|
|
let src;
|
|
|
|
let tgt;
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 3 {
|
2014-12-02 09:20:48 -05:00
|
|
|
src = cwd.clone();
|
|
|
|
} else {
|
2015-02-16 16:04:02 +02:00
|
|
|
src = Path::new(env::args().nth(2).unwrap().clone());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
|
2015-02-16 16:04:02 +02:00
|
|
|
if env::args().len() < 4 {
|
2014-12-02 09:20:48 -05:00
|
|
|
tgt = cwd.join("_book");
|
|
|
|
} else {
|
2015-02-16 16:04:02 +02:00
|
|
|
tgt = Path::new(env::args().nth(3).unwrap().clone());
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
|
2015-01-22 16:31:00 -08:00
|
|
|
try!(fs::mkdir(&tgt, old_io::USER_DIR));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
2015-01-09 20:25:45 +11:00
|
|
|
try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE));
|
2014-12-02 09:20:48 -05:00
|
|
|
|
2015-01-09 20:25:45 +11:00
|
|
|
let summary = try!(File::open(&src.join("SUMMARY.md")));
|
2014-12-02 09:20:48 -05:00
|
|
|
match book::parse_summary(summary, &src) {
|
|
|
|
Ok(book) => {
|
|
|
|
// execute rustdoc on the whole book
|
2015-01-09 20:25:45 +11:00
|
|
|
render(&book, &tgt)
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
Err(errors) => {
|
2015-01-09 20:25:45 +11:00
|
|
|
let n = errors.len();
|
2015-01-31 20:03:04 -05:00
|
|
|
for err in errors {
|
2015-01-09 20:25:45 +11:00
|
|
|
term.err(&format!("error: {}", err)[]);
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
2015-01-09 20:25:45 +11:00
|
|
|
|
|
|
|
Err(box format!("{} errors occurred", n) as Box<Error>)
|
2014-12-02 09:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|