diff --git a/book-example/book/README.html b/book-example/book/README.html index 222d65f9..e84bc906 100644 --- a/book-example/book/README.html +++ b/book-example/book/README.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/cli/build.html b/book-example/book/cli/build.html index 7b714c45..d1817cce 100644 --- a/book-example/book/cli/build.html +++ b/book-example/book/cli/build.html @@ -14,7 +14,7 @@
@@ -55,7 +55,7 @@ current working directory.

- diff --git a/book-example/book/cli/cli-tool.html b/book-example/book/cli/cli-tool.html index 4cdc0e95..092236fc 100644 --- a/book-example/book/cli/cli-tool.html +++ b/book-example/book/cli/cli-tool.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/cli/init.html b/book-example/book/cli/init.html index 6eac333e..6bb81684 100644 --- a/book-example/book/cli/init.html +++ b/book-example/book/cli/init.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/format/config.html b/book-example/book/format/config.html new file mode 100644 index 00000000..c6bf15d3 --- /dev/null +++ b/book-example/book/format/config.html @@ -0,0 +1,67 @@ + + + + + mdBook Documentation + + + + + + + + + + + + +
+ +
+ + +
+

Configuration

+

You can configure the parameters for your book in the book.json file.

+

Here is an example of what a book.json file might look like:

+
{
+    "title": "Example book",
+    "author": "Name",
+}
+
+

Supported variables

+
    +
  • title: title of the book
  • +
  • author: author of the book
  • +
  • dest: path to the directory where you want your book to be rendered. If a relative path is given it will be relative to the parent directory of src
  • +
+ +
+
+ + + + + + + + + +
+ + + + + diff --git a/book-example/book/index.html b/book-example/book/index.html index 222d65f9..e84bc906 100644 --- a/book-example/book/index.html +++ b/book-example/book/index.html @@ -14,7 +14,7 @@
diff --git a/book-example/src/SUMMARY.md b/book-example/src/SUMMARY.md index dc62e27d..a4cee1c2 100644 --- a/book-example/src/SUMMARY.md +++ b/book-example/src/SUMMARY.md @@ -6,4 +6,5 @@ - [build](cli/build.md) - [Format]() - [SUMMARY.md]() + - [Configuration](format/config.md) - [Rust Library]() diff --git a/book-example/src/book.json b/book-example/src/book.json index 40f533d9..7bd69391 100644 --- a/book-example/src/book.json +++ b/book-example/src/book.json @@ -1,3 +1,4 @@ { - "title": "mdBook Documentation" + "title": "mdBook Documentation", + "author": "Mathieu David" } diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md new file mode 100644 index 00000000..584c0720 --- /dev/null +++ b/book-example/src/format/config.md @@ -0,0 +1,18 @@ +# Configuration + +You can configure the parameters for your book in the `book.json` file. + +Here is an example of what a `book.json` file might look like: + +``` +{ + "title": "Example book", + "author": "Name", +} +``` + +#### Supported variables + +- **title:** title of the book +- **author:** author of the book +- **dest:** path to the directory where you want your book to be rendered. If a relative path is given it will be relative to the parent directory of `src` diff --git a/src/book/bookconfig.rs b/src/book/bookconfig.rs index f1020345..6c82760d 100644 --- a/src/book/bookconfig.rs +++ b/src/book/bookconfig.rs @@ -4,6 +4,8 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; +use utils; + #[derive(Debug, Clone)] pub struct BookConfig { pub title: String, @@ -42,7 +44,21 @@ impl BookConfig { let config = Json::from_str(&data).unwrap(); // Extract data + + // Title & author if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") } + if let Some(a) = config.find_path(&["author"]) { self.author = a.to_string().replace("\"", "") } + + // Destination + if let Some(a) = config.find_path(&["dest"]) { + let dest = PathBuf::from(&a.to_string().replace("\"", "")); + + // If path is relative make it absolute from the parent directory of src + if dest.is_relative() { + let dest = &self.src().parent().unwrap().join(&dest); + self.set_dest(dest); + } + } self } diff --git a/src/renderer/html_handlebars/mod.rs b/src/renderer/html_handlebars/mod.rs index 28786263..65acadc6 100644 --- a/src/renderer/html_handlebars/mod.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -40,6 +40,12 @@ impl Renderer for HtmlHandlebars { let mut data = try!(make_data(book.clone(), config)); + // Check if dest directory exists + match utils::path::create_path(config.dest()) { + Err(_) => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexcpected error when constructing destination path"))), + _ => {}, + }; + // Render a file for every entry in the book let mut index = true; for (_, item) in book { @@ -71,6 +77,7 @@ impl Renderer for HtmlHandlebars { // Rendere the handlebars template with the data let rendered = try!(handlebars.render("index", &data)); + println!("Write file..."); // Write to file let mut file = try!(create_file(config.dest(), &item.path)); try!(file.write_all(&rendered.into_bytes())); @@ -121,6 +128,8 @@ impl HtmlHandlebars { fn create_file(working_directory: &Path, path: &Path) -> Result> { + println!("[fn]: create_file"); + // Extract filename let mut file_name; if let Some(name) = path.file_stem() { @@ -157,6 +166,7 @@ fn create_file(working_directory: &Path, path: &Path) -> Result if !f.is_dir() { try!(fs::create_dir(&constructed_path)) } else { + println!("[*]: {:?} --> exists", constructed_path); continue } }, diff --git a/src/utils/path.rs b/src/utils/path.rs index adbe5cb9..9f5df5e2 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -1,4 +1,6 @@ -use std::path::{Path, Component}; +use std::path::{Path, PathBuf, Component}; +use std::error::Error; +use std::fs::{self, metadata}; pub fn path_to_root(path: &Path) -> String { // Remove filename and add "../" for every directory @@ -12,3 +14,46 @@ pub fn path_to_root(path: &Path) -> String { s }) } + + +pub fn create_path(path: &Path) -> Result<(), Box> { + + println!("[fn]: create_path"); + + // Create directories if they do not exist + let mut constructed_path = PathBuf::new(); + + for component in path.components() { + + let mut dir; + match component { + Component::Normal(_) => { dir = PathBuf::from(component.as_os_str()); }, + Component::RootDir => { constructed_path.push("/"); continue }, + _ => continue, + } + + constructed_path.push(&dir); + + // Check if path exists + match metadata(&constructed_path) { + // Any way to combine the Err and first Ok branch ?? + Err(_) => { + try!(fs::create_dir(&constructed_path)); + println!("[*]: Directory created {:?}", constructed_path); + }, + Ok(f) => { + if !f.is_dir() { + try!(fs::create_dir(&constructed_path)); + println!("[*]: Directory created {:?}", constructed_path); + } else { + println!("[*]: Directory exists {:?}", constructed_path); + continue + } + }, + } + } + + println!("[*]: Constructed path: {:?}", constructed_path); + + Ok(()) +}