Skip to content

Commit d69bc9c

Browse files
Merge pull request #491 from Michael-F-Bryan/book-representation-3
WIP: Book representation - Attempt 3
2 parents 3838fa0 + a46e2e2 commit d69bc9c

30 files changed

+2113
-809
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ lazy_static = "0.2"
2626
log = "0.3"
2727
env_logger = "0.4.0"
2828
toml = "0.4"
29+
memchr = "2.0.1"
2930
open = "1.1"
3031
regex = "0.2.1"
3132
tempdir = "0.3.4"
@@ -60,3 +61,6 @@ serve = ["iron", "staticfile", "ws"]
6061
doc = false
6162
name = "mdbook"
6263
path = "src/bin/mdbook.rs"
64+
65+
[patch.crates-io]
66+
pulldown-cmark = { git = "https://github.com/google/pulldown-cmark" }

book-example/book.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
[book]
12
title = "mdBook Documentation"
23
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
34
author = "Mathieu David"
45

56
[output.html]
6-
mathjax-support = true
7+
mathjax-support = true

src/bin/build.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::path::PathBuf;
2-
use clap::{ArgMatches, SubCommand, App};
2+
use clap::{App, ArgMatches, SubCommand};
33
use mdbook::MDBook;
44
use mdbook::errors::Result;
55
use {get_book_dir, open};
@@ -10,32 +10,23 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
1010
.about("Build the book from the markdown files")
1111
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
1212
.arg_from_usage(
13-
"-d, --dest-dir=[dest-dir] 'The output directory for your \
14-
book{n}(Defaults to ./book when omitted)'",
15-
)
16-
.arg_from_usage(
17-
"--no-create 'Will not create non-existent files linked from SUMMARY.md (deprecated: use book.toml instead)'",
13+
"-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \
14+
when omitted)'",
1815
)
1916
.arg_from_usage(
20-
"[dir] 'A directory for your book{n}(Defaults to Current Directory \
21-
when omitted)'",
17+
"[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'",
2218
)
2319
}
2420

2521
// Build command implementation
2622
pub fn execute(args: &ArgMatches) -> Result<()> {
2723
let book_dir = get_book_dir(args);
28-
let mut book = MDBook::new(&book_dir).read_config()?;
24+
let mut book = MDBook::load(&book_dir)?;
2925

3026
if let Some(dest_dir) = args.value_of("dest-dir") {
3127
book.config.build.build_dir = PathBuf::from(dest_dir);
3228
}
3329

34-
// This flag is deprecated in favor of being set via `book.toml`.
35-
if args.is_present("no-create") {
36-
book.config.build.create_missing = false;
37-
}
38-
3930
book.build()?;
4031

4132
if args.is_present("open") {

src/bin/init.rs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,31 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
1919
// Init command implementation
2020
pub fn execute(args: &ArgMatches) -> Result<()> {
2121
let book_dir = get_book_dir(args);
22-
let mut book = MDBook::new(&book_dir);
23-
24-
// Call the function that does the initialization
25-
book.init()?;
22+
let mut builder = MDBook::init(&book_dir);
2623

2724
// If flag `--theme` is present, copy theme to src
2825
if args.is_present("theme") {
2926
// Skip this if `--force` is present
3027
if !args.is_present("force") {
3128
// Print warning
32-
print!("\nCopying the default theme to {:?}", book.get_source());
33-
println!("could potentially overwrite files already present in that directory.");
29+
print!("\nCopying the default theme to {}", builder.config().book.src.display());
30+
println!("This could potentially overwrite files already present in that directory.");
3431
print!("\nAre you sure you want to continue? (y/n) ");
3532

3633
// Read answer from user and exit if it's not 'yes'
37-
if !confirm() {
38-
println!("\nSkipping...\n");
39-
println!("All done, no errors...");
40-
::std::process::exit(0);
34+
if confirm() {
35+
builder.copy_theme(true);
4136
}
4237
}
43-
44-
// Call the function that copies the theme
45-
book.copy_theme()?;
46-
println!("\nTheme copied.");
4738
}
4839

49-
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`
50-
let is_dest_inside_root = book.get_destination().starts_with(&book.root);
40+
println!("\nDo you want a .gitignore to be created? (y/n)");
5141

52-
if !args.is_present("force") && is_dest_inside_root {
53-
println!("\nDo you want a .gitignore to be created? (y/n)");
54-
55-
if confirm() {
56-
book.create_gitignore();
57-
println!("\n.gitignore created.");
58-
}
42+
if confirm() {
43+
builder.create_gitignore(true);
5944
}
6045

46+
builder.build()?;
6147
println!("\nAll done, no errors...");
6248

6349
Ok(())

src/bin/mdbook.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#[macro_use]
22
extern crate clap;
33
extern crate env_logger;
4+
extern crate error_chain;
45
extern crate log;
56
extern crate mdbook;
67
extern crate open;
78

89
use std::env;
910
use std::ffi::OsStr;
10-
use std::io::{self, Write};
1111
use std::path::{Path, PathBuf};
1212
use clap::{App, AppSettings, ArgMatches};
1313
use log::{LogLevelFilter, LogRecord};
1414
use env_logger::LogBuilder;
15+
use error_chain::ChainedError;
1516

1617
pub mod build;
1718
pub mod init;
@@ -59,7 +60,8 @@ fn main() {
5960
};
6061

6162
if let Err(e) = res {
62-
writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok();
63+
eprintln!("{}", e.display_chain());
64+
6365
::std::process::exit(101);
6466
}
6567
}

src/bin/serve.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4949
const RELOAD_COMMAND: &'static str = "reload";
5050

5151
let book_dir = get_book_dir(args);
52-
let mut book = MDBook::new(&book_dir).read_config()?;
52+
let mut book = MDBook::load(&book_dir)?;
5353

5454
if let Some(dest_dir) = args.value_of("dest-dir") {
5555
book.config.build.build_dir = PathBuf::from(dest_dir);
@@ -64,7 +64,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6464
let address = format!("{}:{}", interface, port);
6565
let ws_address = format!("{}:{}", interface, ws_port);
6666

67-
book.livereload = Some(format!(r#"
67+
book.livereload = Some(format!(
68+
r#"
6869
<script type="text/javascript">
6970
var socket = new WebSocket("ws://{}:{}");
7071
socket.onmessage = function (event) {{
@@ -94,7 +95,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
9495

9596
let broadcaster = ws_server.broadcaster();
9697

97-
std::thread::spawn(move || { ws_server.listen(&*ws_address).unwrap(); });
98+
std::thread::spawn(move || {
99+
ws_server.listen(&*ws_address).unwrap();
100+
});
98101

99102
let serving_url = format!("http://{}", address);
100103
println!("\nServing on: {}", serving_url);

src/bin/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
1818
.map(|v| v.collect())
1919
.unwrap_or_default();
2020
let book_dir = get_book_dir(args);
21-
let mut book = MDBook::new(&book_dir).read_config()?;
21+
let mut book = MDBook::load(&book_dir)?;
2222

2323
book.test(library_paths)?;
2424

src/bin/watch.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
2727
// Watch command implementation
2828
pub fn execute(args: &ArgMatches) -> Result<()> {
2929
let book_dir = get_book_dir(args);
30-
let mut book = MDBook::new(&book_dir).read_config()?;
30+
let mut book = MDBook::load(&book_dir)?;
3131

3232
if let Some(dest_dir) = args.value_of("dest-dir") {
3333
book.config.build.build_dir = PathBuf::from(dest_dir);
@@ -69,8 +69,8 @@ where
6969
};
7070

7171
// Add the source directory to the watcher
72-
if let Err(e) = watcher.watch(book.get_source(), Recursive) {
73-
println!("Error while watching {:?}:\n {:?}", book.get_source(), e);
72+
if let Err(e) = watcher.watch(book.source_dir(), Recursive) {
73+
println!("Error while watching {:?}:\n {:?}", book.source_dir(), e);
7474
::std::process::exit(0);
7575
};
7676

0 commit comments

Comments
 (0)