Skip to content

Commit

Permalink
Merge pull request #491 from Michael-F-Bryan/book-representation-3
Browse files Browse the repository at this point in the history
WIP: Book representation - Attempt 3
  • Loading branch information
Michael-F-Bryan authored Dec 12, 2017
2 parents 3838fa0 + a46e2e2 commit d69bc9c
Show file tree
Hide file tree
Showing 30 changed files with 2,113 additions and 809 deletions.
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

0 comments on commit d69bc9c

Please sign in to comment.