Skip to content

Book representation - Attempt 3 #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4c6c696
Copied across the summary parser and Book structure (doesn't compile)
Michael-F-Bryan Nov 18, 2017
cafb8b7
The library not compiles (probably completely broken)
Michael-F-Bryan Nov 18, 2017
47eb478
Introduced the `BookBuilder`.
Michael-F-Bryan Nov 18, 2017
8b21da9
Fleshed out book creation
Michael-F-Bryan Nov 18, 2017
2149863
Made sure the dummy book can build
Michael-F-Bryan Nov 18, 2017
f993677
All tests finally pass!
Michael-F-Bryan Nov 18, 2017
527fc5c
Completely removed the `create_missing` option from MDBook
Michael-F-Bryan Nov 18, 2017
751da4f
Added a test to make sure you can include rust files in chapters
Michael-F-Bryan Nov 18, 2017
12d1ed5
The example book renders correctly
Michael-F-Bryan Dec 4, 2017
9950f69
Removed the `MDBook::read_config()` method because it's redundant now
Michael-F-Bryan Dec 10, 2017
ace0b51
Put the `create_missing` feature back in
Michael-F-Bryan Dec 10, 2017
be4654c
Fleshed out the docs for the book module
Michael-F-Bryan Dec 11, 2017
1b51cd2
Moved the book examples to the top level lib.rs
Michael-F-Bryan Dec 11, 2017
ebcf41c
Improved error messages using error_chain::ChainedError::display_chain()
Michael-F-Bryan Dec 11, 2017
4f4e86d
Added tests to make sure we parse existing SUMMARY.md's
Michael-F-Bryan Dec 11, 2017
f5e9b85
Rewrote summary parser from a state machine to use recursive descent
Michael-F-Bryan Dec 11, 2017
148511e
Able to parse all existing SUMMARY.md files
Michael-F-Bryan Dec 11, 2017
ff9e0b0
Made sure create_missing also creates the parent directory
Michael-F-Bryan Dec 11, 2017
5041359
Updated the pulldown-cmark patch in Cargo.toml
Michael-F-Bryan Dec 11, 2017
75dac15
Fixed a couple issues with the docs
Michael-F-Bryan Dec 11, 2017
a46e2e2
Merge branch 'master' into book-representation-3
Michael-F-Bryan Dec 11, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ lazy_static = "0.2"
log = "0.3"
env_logger = "0.4.0"
toml = "0.4"
memchr = "2.0.1"
open = "1.1"
regex = "0.2.1"
tempdir = "0.3.4"
Expand Down Expand Up @@ -60,3 +61,6 @@ serve = ["iron", "staticfile", "ws"]
doc = false
name = "mdbook"
path = "src/bin/mdbook.rs"

[patch.crates-io]
pulldown-cmark = { git = "https://github.com/google/pulldown-cmark" }
3 changes: 2 additions & 1 deletion book-example/book.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[book]
title = "mdBook Documentation"
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
author = "Mathieu David"

[output.html]
mathjax-support = true
mathjax-support = true
19 changes: 5 additions & 14 deletions src/bin/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::PathBuf;
use clap::{ArgMatches, SubCommand, App};
use clap::{App, ArgMatches, SubCommand};
use mdbook::MDBook;
use mdbook::errors::Result;
use {get_book_dir, open};
Expand All @@ -10,32 +10,23 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
.about("Build the book from the markdown files")
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
.arg_from_usage(
"-d, --dest-dir=[dest-dir] 'The output directory for your \
book{n}(Defaults to ./book when omitted)'",
)
.arg_from_usage(
"--no-create 'Will not create non-existent files linked from SUMMARY.md (deprecated: use book.toml instead)'",
"-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \
when omitted)'",
)
.arg_from_usage(
"[dir] 'A directory for your book{n}(Defaults to Current Directory \
when omitted)'",
"[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'",
)
}

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

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

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

book.build()?;

if args.is_present("open") {
Expand Down
32 changes: 9 additions & 23 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,31 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
// Init command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir);

// Call the function that does the initialization
book.init()?;
let mut builder = MDBook::init(&book_dir);

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

// Read answer from user and exit if it's not 'yes'
if !confirm() {
println!("\nSkipping...\n");
println!("All done, no errors...");
::std::process::exit(0);
if confirm() {
builder.copy_theme(true);
}
}

// Call the function that copies the theme
book.copy_theme()?;
println!("\nTheme copied.");
}

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

if !args.is_present("force") && is_dest_inside_root {
println!("\nDo you want a .gitignore to be created? (y/n)");

if confirm() {
book.create_gitignore();
println!("\n.gitignore created.");
}
if confirm() {
builder.create_gitignore(true);
}

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

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions src/bin/mdbook.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#[macro_use]
extern crate clap;
extern crate env_logger;
extern crate error_chain;
extern crate log;
extern crate mdbook;
extern crate open;

use std::env;
use std::ffi::OsStr;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use clap::{App, AppSettings, ArgMatches};
use log::{LogLevelFilter, LogRecord};
use env_logger::LogBuilder;
use error_chain::ChainedError;

pub mod build;
pub mod init;
Expand Down Expand Up @@ -59,7 +60,8 @@ fn main() {
};

if let Err(e) = res {
writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok();
eprintln!("{}", e.display_chain());

::std::process::exit(101);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/bin/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
const RELOAD_COMMAND: &'static str = "reload";

let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
let mut book = MDBook::load(&book_dir)?;

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

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

let broadcaster = ws_server.broadcaster();

std::thread::spawn(move || { ws_server.listen(&*ws_address).unwrap(); });
std::thread::spawn(move || {
ws_server.listen(&*ws_address).unwrap();
});

let serving_url = format!("http://{}", address);
println!("\nServing on: {}", serving_url);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
.map(|v| v.collect())
.unwrap_or_default();
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
let mut book = MDBook::load(&book_dir)?;

book.test(library_paths)?;

Expand Down
6 changes: 3 additions & 3 deletions src/bin/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
// Watch command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;
let mut book = MDBook::load(&book_dir)?;

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

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

Expand Down
Loading