Skip to content

Book representation #371

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

Closed
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ env_logger = "0.4.0"
toml = { version = "0.4", features = ["serde"] }
open = "1.1"
regex = "0.2.1"
tempdir = "0.3.4"

# Watch feature
notify = { version = "4.0", optional = true }
Expand All @@ -41,7 +42,7 @@ ws = { version = "0.7", optional = true}

# Tests
[dev-dependencies]
tempdir = "0.3.4"
pretty_assertions = "0.2.1"

[build-dependencies]
error-chain = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn_args_density = "Compressed"
enum_trailing_comma = true
match_block_trailing_comma = true
struct_trailing_comma = "Always"
wrap_comments = true
wrap_comments = false
use_try_shorthand = true

report_todo = "Always"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
// Build command implementation
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let book = MDBook::new(&book_dir).read_config()?;
let book = MDBook::new(&book_dir)?;

let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(dest_dir),
Expand Down
5 changes: 1 addition & 4 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
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 book = MDBook::init(&book_dir)?;

// If flag `--theme` is present, copy theme to src
if args.is_present("theme") {
Expand Down
12 changes: 7 additions & 5 deletions src/bin/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
const RELOAD_COMMAND: &'static str = "reload";

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

let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
Expand All @@ -53,7 +53,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let address = format!("{}:{}", interface, port);
let ws_address = format!("{}:{}", interface, ws_port);

book.set_livereload(format!(r#"
book.set_livereload(format!(
r#"
<script type="text/javascript">
var socket = new WebSocket("ws://{}:{}");
socket.onmessage = function (event) {{
Expand All @@ -68,9 +69,10 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
}}
</script>
"#,
public_address,
ws_port,
RELOAD_COMMAND));
public_address,
ws_port,
RELOAD_COMMAND
));

book.build()?;

Expand Down
2 changes: 1 addition & 1 deletion src/bin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
pub fn execute(args: &ArgMatches) -> Result<()> {
let library_paths: Vec<&str> = args.values_of("library-path").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::new(&book_dir)?;

book.test(library_paths)?;

Expand Down
19 changes: 12 additions & 7 deletions src/bin/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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 book = MDBook::new(&book_dir).read_config()?;
let book = MDBook::new(&book_dir)?;

let mut book = match args.value_of("dest-dir") {
Some(dest_dir) => book.with_destination(dest_dir),
Expand Down Expand Up @@ -53,7 +53,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

// Calls the closure when a book source file is changed. This is blocking!
pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
where F: Fn(&Path, &mut MDBook) -> ()
where
F: Fn(&Path, &mut MDBook) -> (),
{
use self::notify::RecursiveMode::*;
use self::notify::DebouncedEvent::*;
Expand All @@ -76,19 +77,23 @@ pub fn trigger_on_change<F>(book: &mut MDBook, closure: F) -> ()
};

// Add the theme directory to the watcher
watcher.watch(book.get_theme_path(), Recursive).unwrap_or_default();
watcher
.watch(book.get_theme_path(), Recursive)
.unwrap_or_default();


// Add the book.{json,toml} file to the watcher if it exists, because it's not
// located in the source directory
if watcher
.watch(book.get_root().join("book.json"), NonRecursive)
.is_err() {
.watch(book.get_root().join("book.json"), NonRecursive)
.is_err()
{
// do nothing if book.json is not found
}
if watcher
.watch(book.get_root().join("book.toml"), NonRecursive)
.is_err() {
.watch(book.get_root().join("book.toml"), NonRecursive)
.is_err()
{
// do nothing if book.toml is not found
}

Expand Down
87 changes: 0 additions & 87 deletions src/book/bookitem.rs

This file was deleted.

62 changes: 62 additions & 0 deletions src/book/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::path::{Path, PathBuf};

use config::{self, BookConfig};
use renderer::{Renderer, HtmlHandlebars};
use loader;
use errors::*;
use super::MDBook;


#[derive(Default)]
pub struct Builder {
root: PathBuf,
create_missing: bool,
config: Option<BookConfig>,
renderer: Option<Box<Renderer>>,
livereload: Option<String>,
}

impl Builder {
/// Create a new builder which loads the book from an existing directory.
pub fn new<P: AsRef<Path>>(root: P) -> Builder {
let root = root.as_ref();

Builder {
root: root.to_path_buf(),
..Default::default()
}
}

/// Set the config to use.
pub fn with_config(mut self, config: BookConfig) -> Self {
self.config = Some(config);
self
}

/// Set the renderer to be used.
pub fn set_renderer(mut self, renderer: Box<Renderer>) -> Self {
self.renderer = Some(renderer);
self
}

pub fn build(self) -> Result<MDBook> {
// if no custom config provided, try to read it from disk
let cfg = match self.config {
Some(c) => c,
None => config::read_config(&self.root)?,
};

let book = loader::load_book(cfg.get_source())?;
let renderer: Box<Renderer> = self.renderer.unwrap_or_else(
|| Box::new(HtmlHandlebars::new()),
);

Ok(MDBook {
config: cfg,
book: book,
renderer: renderer,
livereload: self.livereload,
create_missing: self.create_missing,
})
}
}
Loading