Merge branch 'minor_refactor' of https://github.com/budziq/mdBook into budziq-minor_refactor
This commit is contained in:
commit
28afebdca2
|
@ -9,11 +9,13 @@ extern crate mdbook;
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
# #[allow(unused_variables)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut book = MDBook::new(Path::new("my-book")) // Path to root
|
let mut book = MDBook::new("my-book") // Path to root
|
||||||
.set_src(Path::new("src")) // Path from root to source directory
|
.with_source("src") // Path from root to source directory
|
||||||
.set_dest(Path::new("book")) // Path from root to output directory
|
.with_destination("book") // Path from root to output directory
|
||||||
.read_config(); // Parse book.toml or book.json file for configuration
|
.read_config() // Parse book.toml or book.json configuration file
|
||||||
|
.expect("I don't handle configuration file error, but you should!");
|
||||||
|
|
||||||
book.build().unwrap(); // Render the book
|
book.build().unwrap(); // Render the book
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,7 +176,7 @@ fn build(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
let mut book = match args.value_of("dest-dir") {
|
let mut book = match args.value_of("dest-dir") {
|
||||||
Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
|
Some(dest_dir) => book.with_destination(dest_dir),
|
||||||
None => book,
|
None => book,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -207,7 +207,7 @@ fn watch(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
let mut book = match args.value_of("dest-dir") {
|
let mut book = match args.value_of("dest-dir") {
|
||||||
Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
|
Some(dest_dir) => book.with_destination(dest_dir),
|
||||||
None => book,
|
None => book,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -243,7 +243,7 @@ fn serve(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
let book = MDBook::new(&book_dir).read_config()?;
|
let book = MDBook::new(&book_dir).read_config()?;
|
||||||
|
|
||||||
let mut book = match args.value_of("dest-dir") {
|
let mut book = match args.value_of("dest-dir") {
|
||||||
Some(dest_dir) => book.with_destination(Path::new(dest_dir)),
|
Some(dest_dir) => book.with_destination(dest_dir),
|
||||||
None => book,
|
None => book,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -39,9 +39,9 @@ impl MDBook {
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # extern crate mdbook;
|
/// # extern crate mdbook;
|
||||||
/// # use mdbook::MDBook;
|
/// # use mdbook::MDBook;
|
||||||
/// # use std::path::Path;
|
/// # #[allow(unused_variables)]
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let book = MDBook::new(Path::new("root_dir"));
|
/// let book = MDBook::new("root_dir");
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -59,8 +59,9 @@ impl MDBook {
|
||||||
/// They can both be changed by using [`set_src()`](#method.set_src) and
|
/// They can both be changed by using [`set_src()`](#method.set_src) and
|
||||||
/// [`set_dest()`](#method.set_dest)
|
/// [`set_dest()`](#method.set_dest)
|
||||||
|
|
||||||
pub fn new(root: &Path) -> MDBook {
|
pub fn new<P: Into<PathBuf>>(root: P) -> MDBook {
|
||||||
|
|
||||||
|
let root = root.into();
|
||||||
if !root.exists() || !root.is_dir() {
|
if !root.exists() || !root.is_dir() {
|
||||||
warn!("{:?} No directory with that name", root);
|
warn!("{:?} No directory with that name", root);
|
||||||
}
|
}
|
||||||
|
@ -84,9 +85,9 @@ impl MDBook {
|
||||||
/// # extern crate mdbook;
|
/// # extern crate mdbook;
|
||||||
/// # use mdbook::MDBook;
|
/// # use mdbook::MDBook;
|
||||||
/// # use mdbook::BookItem;
|
/// # use mdbook::BookItem;
|
||||||
/// # use std::path::Path;
|
/// # #[allow(unused_variables)]
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// # let mut book = MDBook::new(Path::new("mybook"));
|
/// # let book = MDBook::new("mybook");
|
||||||
/// for item in book.iter() {
|
/// for item in book.iter() {
|
||||||
/// match item {
|
/// match item {
|
||||||
/// &BookItem::Chapter(ref section, ref chapter) => {},
|
/// &BookItem::Chapter(ref section, ref chapter) => {},
|
||||||
|
@ -347,10 +348,10 @@ impl MDBook {
|
||||||
/// extern crate mdbook;
|
/// extern crate mdbook;
|
||||||
/// use mdbook::MDBook;
|
/// use mdbook::MDBook;
|
||||||
/// use mdbook::renderer::HtmlHandlebars;
|
/// use mdbook::renderer::HtmlHandlebars;
|
||||||
/// # use std::path::Path;
|
|
||||||
///
|
///
|
||||||
|
/// # #[allow(unused_variables)]
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let mut book = MDBook::new(Path::new("mybook"))
|
/// let book = MDBook::new("mybook")
|
||||||
/// .set_renderer(Box::new(HtmlHandlebars::new()));
|
/// .set_renderer(Box::new(HtmlHandlebars::new()));
|
||||||
///
|
///
|
||||||
/// // In this example we replace the default renderer
|
/// // In this example we replace the default renderer
|
||||||
|
|
25
src/lib.rs
25
src/lib.rs
|
@ -21,14 +21,14 @@
|
||||||
//! extern crate mdbook;
|
//! extern crate mdbook;
|
||||||
//!
|
//!
|
||||||
//! use mdbook::MDBook;
|
//! use mdbook::MDBook;
|
||||||
//! use std::path::Path;
|
|
||||||
//!
|
//!
|
||||||
|
//! # #[allow(unused_variables)]
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let mut book = MDBook::new(Path::new("my-book")) // Path to root
|
//! let mut book = MDBook::new("my-book") // Path to root
|
||||||
//! .with_source(Path::new("src")) // Path from root to source directory
|
//! .with_source("src") // Path from root to source directory
|
||||||
//! .with_destination(Path::new("book")) // Path from root to output directory
|
//! .with_destination("book") // Path from root to output directory
|
||||||
//! .read_config() // Parse book.json file for configuration
|
//! .read_config() // Parse book.toml configuration file
|
||||||
//! .expect("I don't handle the error for the configuration file, but you should!");
|
//! .expect("I don't handle configuration file error, but you should!");
|
||||||
//!
|
//!
|
||||||
//! book.build().unwrap(); // Render the book
|
//! book.build().unwrap(); // Render the book
|
||||||
//! }
|
//! }
|
||||||
|
@ -46,12 +46,12 @@
|
||||||
//! #
|
//! #
|
||||||
//! # use mdbook::MDBook;
|
//! # use mdbook::MDBook;
|
||||||
//! # use mdbook::renderer::HtmlHandlebars;
|
//! # use mdbook::renderer::HtmlHandlebars;
|
||||||
//! # use std::path::Path;
|
|
||||||
//! #
|
//! #
|
||||||
|
//! # #[allow(unused_variables)]
|
||||||
//! # fn main() {
|
//! # fn main() {
|
||||||
//! # let your_renderer = HtmlHandlebars::new();
|
//! # let your_renderer = HtmlHandlebars::new();
|
||||||
//! #
|
//! #
|
||||||
//! let book = MDBook::new(Path::new("my-book")).set_renderer(Box::new(your_renderer));
|
//! let book = MDBook::new("my-book").set_renderer(Box::new(your_renderer));
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//! If you make a renderer, you get the book constructed in form of `Vec<BookItems>` and you get
|
//! If you make a renderer, you get the book constructed in form of `Vec<BookItems>` and you get
|
||||||
|
@ -61,12 +61,11 @@
|
||||||
//!
|
//!
|
||||||
//! ## utils
|
//! ## utils
|
||||||
//!
|
//!
|
||||||
//! I have regrouped some useful functions in the [utils](utils/index.html) module, like the following function
|
//! I have regrouped some useful functions in the [utils](utils/index.html) module, like the
|
||||||
|
//! following function [`utils::fs::create_file(path:
|
||||||
|
//! &Path)`](utils/fs/fn.create_file.html)
|
||||||
//!
|
//!
|
||||||
//! ```ignore
|
//! This function creates a file and returns it. But before creating the file it checks every directory in the path to see if it exists, and if it does not it will be created.
|
||||||
//! utils::fs::create_path(path: &Path)
|
|
||||||
//! ```
|
|
||||||
//! This function creates all the directories in a given path if they do not exist
|
|
||||||
//!
|
//!
|
||||||
//! Make sure to take a look at it.
|
//! Make sure to take a look at it.
|
||||||
|
|
||||||
|
|
|
@ -152,7 +152,7 @@ impl Renderer for HtmlHandlebars {
|
||||||
// Update the context with data for this file
|
// Update the context with data for this file
|
||||||
data.insert("path".to_owned(), json!("print.md"));
|
data.insert("path".to_owned(), json!("print.md"));
|
||||||
data.insert("content".to_owned(), json!(print_content));
|
data.insert("content".to_owned(), json!(print_content));
|
||||||
data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md"))));
|
data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root("print.md")));
|
||||||
|
|
||||||
// Render the handlebars template with the data
|
// Render the handlebars template with the data
|
||||||
debug!("[*]: Render template");
|
debug!("[*]: Render template");
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
|
|
||||||
pub fn render_playpen(s: &str, path: &Path) -> String {
|
pub fn render_playpen<P: AsRef<Path>>(s: &str, path: P) -> String {
|
||||||
// When replacing one thing in a string by something with a different length,
|
// When replacing one thing in a string by something with a different length,
|
||||||
// the indices after that will not correspond,
|
// the indices after that will not correspond,
|
||||||
// we therefore have to store the difference to correct this
|
// we therefore have to store the difference to correct this
|
||||||
|
@ -65,7 +65,8 @@ struct Playpen {
|
||||||
escaped: bool,
|
escaped: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {
|
fn find_playpens<P: AsRef<Path>>(s: &str, base_path: P) -> Vec<Playpen> {
|
||||||
|
let base_path = base_path.as_ref();
|
||||||
let mut playpens = vec![];
|
let mut playpens = vec![];
|
||||||
for (i, _) in s.match_indices("{{#playpen") {
|
for (i, _) in s.match_indices("{{#playpen") {
|
||||||
debug!("[*]: find_playpen");
|
debug!("[*]: find_playpen");
|
||||||
|
@ -127,28 +128,28 @@ fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_playpens_no_playpen() {
|
fn test_find_playpens_no_playpen() {
|
||||||
let s = "Some random text without playpen...";
|
let s = "Some random text without playpen...";
|
||||||
assert!(find_playpens(s, Path::new("")) == vec![]);
|
assert!(find_playpens(s, "") == vec![]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_playpens_partial_playpen() {
|
fn test_find_playpens_partial_playpen() {
|
||||||
let s = "Some random text with {{#playpen...";
|
let s = "Some random text with {{#playpen...";
|
||||||
assert!(find_playpens(s, Path::new("")) == vec![]);
|
assert!(find_playpens(s, "") == vec![]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_playpens_empty_playpen() {
|
fn test_find_playpens_empty_playpen() {
|
||||||
let s = "Some random text with {{#playpen}} and {{#playpen }}...";
|
let s = "Some random text with {{#playpen}} and {{#playpen }}...";
|
||||||
assert!(find_playpens(s, Path::new("")) == vec![]);
|
assert!(find_playpens(s, "") == vec![]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_playpens_simple_playpen() {
|
fn test_find_playpens_simple_playpen() {
|
||||||
let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}...";
|
let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}...";
|
||||||
|
|
||||||
println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("")));
|
println!("\nOUTPUT: {:?}\n", find_playpens(s, ""));
|
||||||
|
|
||||||
assert!(find_playpens(s, Path::new("")) ==
|
assert!(find_playpens(s, "") ==
|
||||||
vec![Playpen {
|
vec![Playpen {
|
||||||
start_index: 22,
|
start_index: 22,
|
||||||
end_index: 42,
|
end_index: 42,
|
||||||
|
@ -169,9 +170,9 @@ fn test_find_playpens_simple_playpen() {
|
||||||
fn test_find_playpens_complex_playpen() {
|
fn test_find_playpens_complex_playpen() {
|
||||||
let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}...";
|
let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}...";
|
||||||
|
|
||||||
println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("dir")));
|
println!("\nOUTPUT: {:?}\n", find_playpens(s, "dir"));
|
||||||
|
|
||||||
assert!(find_playpens(s, Path::new("dir")) ==
|
assert!(find_playpens(s, "dir") ==
|
||||||
vec![Playpen {
|
vec![Playpen {
|
||||||
start_index: 22,
|
start_index: 22,
|
||||||
end_index: 51,
|
end_index: 51,
|
||||||
|
@ -192,9 +193,9 @@ fn test_find_playpens_complex_playpen() {
|
||||||
fn test_find_playpens_escaped_playpen() {
|
fn test_find_playpens_escaped_playpen() {
|
||||||
let s = "Some random text with escaped playpen \\{{#playpen file.rs editable}} ...";
|
let s = "Some random text with escaped playpen \\{{#playpen file.rs editable}} ...";
|
||||||
|
|
||||||
println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("")));
|
println!("\nOUTPUT: {:?}\n", find_playpens(s, ""));
|
||||||
|
|
||||||
assert!(find_playpens(s, Path::new("")) ==
|
assert!(find_playpens(s, "") ==
|
||||||
vec![Playpen {
|
vec![Playpen {
|
||||||
start_index: 39,
|
start_index: 39,
|
||||||
end_index: 68,
|
end_index: 68,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::path::{Path, Component};
|
use std::path::{Path, PathBuf, Component};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::{self, Read};
|
use std::io::{self, Read};
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
|
@ -30,16 +30,16 @@ pub fn file_to_string(path: &Path) -> Result<String, Box<Error>> {
|
||||||
/// This is mostly interesting for a relative path to point back to the
|
/// This is mostly interesting for a relative path to point back to the
|
||||||
/// directory from where the path starts.
|
/// directory from where the path starts.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```rust
|
||||||
/// let mut path = Path::new("some/relative/path");
|
/// # extern crate mdbook;
|
||||||
///
|
/// #
|
||||||
/// println!("{}", path_to_root(&path));
|
/// # use std::path::Path;
|
||||||
/// ```
|
/// # use mdbook::utils::fs::path_to_root;
|
||||||
///
|
/// #
|
||||||
/// **Outputs**
|
/// # fn main() {
|
||||||
///
|
/// let path = Path::new("some/relative/path");
|
||||||
/// ```text
|
/// assert_eq!(path_to_root(path), "../../");
|
||||||
/// "../../"
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// **note:** it's not very fool-proof, if you find a situation where
|
/// **note:** it's not very fool-proof, if you find a situation where
|
||||||
|
@ -47,11 +47,11 @@ pub fn file_to_string(path: &Path) -> Result<String, Box<Error>> {
|
||||||
/// Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues)
|
/// Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues)
|
||||||
/// or a [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it.
|
/// or a [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it.
|
||||||
|
|
||||||
pub fn path_to_root(path: &Path) -> String {
|
pub fn path_to_root<P: Into<PathBuf>>(path: P) -> String {
|
||||||
debug!("[fn]: path_to_root");
|
debug!("[fn]: path_to_root");
|
||||||
// Remove filename and add "../" for every directory
|
// Remove filename and add "../" for every directory
|
||||||
|
|
||||||
path.to_path_buf()
|
path.into()
|
||||||
.parent()
|
.parent()
|
||||||
.expect("")
|
.expect("")
|
||||||
.components()
|
.components()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate mdbook;
|
extern crate mdbook;
|
||||||
extern crate tempdir;
|
extern crate tempdir;
|
||||||
|
|
||||||
use std::path::Path;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
@ -15,8 +14,8 @@ fn do_not_overwrite_unspecified_config_values() {
|
||||||
let dir = TempDir::new("mdbook").expect("Could not create a temp dir");
|
let dir = TempDir::new("mdbook").expect("Could not create a temp dir");
|
||||||
|
|
||||||
let book = MDBook::new(dir.path())
|
let book = MDBook::new(dir.path())
|
||||||
.with_source(Path::new("bar"))
|
.with_source("bar")
|
||||||
.with_destination(Path::new("baz"));
|
.with_destination("baz");
|
||||||
|
|
||||||
assert_eq!(book.get_root(), dir.path());
|
assert_eq!(book.get_root(), dir.path());
|
||||||
assert_eq!(book.get_source(), dir.path().join("bar"));
|
assert_eq!(book.get_source(), dir.path().join("bar"));
|
||||||
|
|
Loading…
Reference in New Issue