Skip to content

Initial Book loader and summary parsing #360

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
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
1e60d6b
Created a new Loader module and some stubs
Michael-F-Bryan Jun 24, 2017
c68a29c
added a parse_summary() method to the Loader
Michael-F-Bryan Jun 24, 2017
eb839e4
Added a SectionNumber newtype to represent section numbering ("1.2.3")
Michael-F-Bryan Jun 25, 2017
588b444
Tried to make summary parsing easier to understand and implement by w…
Michael-F-Bryan Jun 25, 2017
dacb3e0
Can now parse a single item (link to a chapter)
Michael-F-Bryan Jun 25, 2017
dcc8368
Encoded the first state in the SummaryParser
Michael-F-Bryan Jun 27, 2017
df2ee64
Added the prefix chapter parsing step and a failing test for separators
Michael-F-Bryan Jun 27, 2017
bfc9112
Separators and links in the prefix chapters are parsed correctly
Michael-F-Bryan Jun 27, 2017
56dad88
Added some of the infrastructure for parsing the nested section and u…
Michael-F-Bryan Jun 28, 2017
66e4898
Can parse the numbered sections and correctly add the items found to …
Michael-F-Bryan Jun 29, 2017
68ebb62
Forgot PathBuf doesn't implement Default on stable
Michael-F-Bryan Jun 29, 2017
42de0fb
Added a full integration test for the summary parser (ignored for the…
Michael-F-Bryan Jun 29, 2017
1e8bc1c
Fleshed out the step_numbered() method some more
Michael-F-Bryan Jun 29, 2017
1e75752
Added pretty-assertions as a dev dependency (makes comparing deep str…
Michael-F-Bryan Jun 29, 2017
c3124bc
Summary parser handles suffixes and now all that's left to do is adju…
Michael-F-Bryan Jun 29, 2017
01bab56
The summary parser integration test passes!
Michael-F-Bryan Jun 29, 2017
131a404
made clippy and rustfmt happy
Michael-F-Bryan Jun 29, 2017
eb59319
Did a little refactoring to clean up the summary parser
Michael-F-Bryan Jun 30, 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,24 @@ extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate log;

extern crate handlebars;
extern crate pulldown_cmark;
extern crate regex;

#[cfg(test)]
#[macro_use]
extern crate log;
extern crate pretty_assertions;

pub mod book;
pub mod config;
mod parse;
pub mod renderer;
pub mod theme;
pub mod utils;
pub mod loader;

pub use book::MDBook;
pub use book::BookItem;
Expand Down
84 changes: 84 additions & 0 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//! Functionality for loading the internal book representation from disk.
//!
//! The typical use case is to create a `Loader` pointing at the correct
//! source directory then call the `load()` method. Internally this will
//! search for the `SUMMARY.md` file, parse it, then use the parsed
//! `Summary` to construct an in-memory representation of the entire book.
//!
//! # Examples
//!
//! ```rust,no_run
//! # fn run() -> mdbook::errors::Result<()> {
//! use mdbook::loader::Loader;
//! let loader = Loader::new("./src/");
//! let book = loader.load()?;
//! # Ok(())
//! # }
//! # fn main() { run().unwrap() }
//! ```
//!
//! Alternatively, if you are using the `mdbook` crate as a library and
//! only want to read the `SUMMARY.md` file without having to load the
//! entire book from disk, you can use the `parse_summary()` function.
//!
//! ```rust
//! # fn run() -> mdbook::errors::Result<()> {
//! use mdbook::loader::parse_summary;
//! let src = "# Book Summary
//!
//! [Introduction](./index.md)
//! - [First Chapter](./first/index.md)
//! - [Sub-Section](./first/subsection.md)
//! - [Second Chapter](./second/index.md)
//! ";
//! let summary = parse_summary(src)?;
//! println!("{:#?}", summary);
//! # Ok(())
//! # }
//! # fn main() { run().unwrap() }
//! ```

#![deny(missing_docs)]

use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::Read;
use errors::*;

mod summary;

pub use self::summary::{Summary, Link, SummaryItem, parse_summary, SectionNumber};


/// The object in charge of parsing the source directory into a usable
/// `Book` struct.
#[derive(Debug, Clone, PartialEq)]
pub struct Loader {
source_directory: PathBuf,
}

impl Loader {
/// Create a new loader which uses the provided source directory.
pub fn new<P: AsRef<Path>>(source_directory: P) -> Loader {
Loader { source_directory: source_directory.as_ref().to_path_buf() }
}

/// Parse the summary file and use it to load a book from disk.
pub fn load(&self) -> Result<()> {
let summary = self.parse_summary().chain_err(
|| "Couldn't parse `SUMMARY.md`",
)?;

unimplemented!()
}

/// Parse the `SUMMARY.md` file.
pub fn parse_summary(&self) -> Result<Summary> {
let path = self.source_directory.join("SUMMARY.md");

let mut summary_content = String::new();
File::open(&path)?.read_to_string(&mut summary_content)?;

summary::parse_summary(&summary_content)
}
}
Loading