Added author and dest field in book.json, fixed bug when output directory did not exist. Closes #7
This commit is contained in:
parent
9b4298c066
commit
c64824a18c
|
@ -14,7 +14,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="sidebar" class="sidebar">
|
<div id="sidebar" class="sidebar">
|
||||||
<ul class="chapter"><li><a href="README.html"class="active"><strong>1.</strong> mdBook</a></li><li><a href="cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
<ul class="chapter"><li><a href="README.html"class="active"><strong>1.</strong> mdBook</a></li><li><a href="cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="format/config.html"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="sidebar" class="sidebar">
|
<div id="sidebar" class="sidebar">
|
||||||
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"class="active"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"class="active"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="../format/config.html"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
@ -55,7 +55,7 @@ current working directory.</p>
|
||||||
<!-- -->
|
<!-- -->
|
||||||
|
|
||||||
<!-- -->
|
<!-- -->
|
||||||
<a href="" class="nav-chapters next">
|
<a href="../format/config.html" class="nav-chapters next">
|
||||||
<i class="fa fa-angle-right"></i>
|
<i class="fa fa-angle-right"></i>
|
||||||
</a>
|
</a>
|
||||||
<!-- -->
|
<!-- -->
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="sidebar" class="sidebar">
|
<div id="sidebar" class="sidebar">
|
||||||
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"class="active"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"class="active"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="../format/config.html"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="sidebar" class="sidebar">
|
<div id="sidebar" class="sidebar">
|
||||||
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"class="active"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"class="active"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="../format/config.html"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>mdBook Documentation</title>
|
||||||
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||||
|
<meta name="description" content="{% block description %}{% endblock %}">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<link rel="stylesheet" href="../book.css" media="screen" title="no title" charset="utf-8">
|
||||||
|
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
||||||
|
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="sidebar" class="sidebar">
|
||||||
|
<ul class="chapter"><li><a href="../README.html"><strong>1.</strong> mdBook</a></li><li><a href="../cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="../cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="../cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="../format/config.html"class="active"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
<div id="menu-bar" class="menu-bar">
|
||||||
|
<i id="sidebar-toggle" class="fa fa-bars left"></i>
|
||||||
|
<h1 class="menu-title">mdBook Documentation</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="content" class="content">
|
||||||
|
<h1>Configuration</h1>
|
||||||
|
<p>You can configure the parameters for your book in the <code>book.json</code> file.</p>
|
||||||
|
<p>Here is an example of what a <code>book.json</code> file might look like:</p>
|
||||||
|
<pre><code>{
|
||||||
|
"title": "Example book",
|
||||||
|
"author": "Name",
|
||||||
|
}
|
||||||
|
</code></pre>
|
||||||
|
<h4>Supported variables</h4>
|
||||||
|
<ul>
|
||||||
|
<li><strong>title:</strong> title of the book</li>
|
||||||
|
<li><strong>author:</strong> author of the book</li>
|
||||||
|
<li><strong>dest:</strong> 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 <code>src</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Doesn't seem to work: using JavaScript
|
||||||
|
alternative until I find a way to do it in the template
|
||||||
|
-->
|
||||||
|
<a href="../cli/build.html" class="nav-chapters previous">
|
||||||
|
<i class="fa fa-angle-left"></i>
|
||||||
|
</a>
|
||||||
|
<!-- -->
|
||||||
|
|
||||||
|
<!-- -->
|
||||||
|
<a href="" class="nav-chapters next">
|
||||||
|
<i class="fa fa-angle-right"></i>
|
||||||
|
</a>
|
||||||
|
<!-- -->
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
|
||||||
|
<script src="../book.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -14,7 +14,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="sidebar" class="sidebar">
|
<div id="sidebar" class="sidebar">
|
||||||
<ul class="chapter"><li><a href="README.html"class="active"><strong>1.</strong> mdBook</a></li><li><a href="cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
<ul class="chapter"><li><a href="README.html"class="active"><strong>1.</strong> mdBook</a></li><li><a href="cli/cli-tool.html"><strong>2.</strong> Command Line Tool</a></li><li><ul class="section"><li><a href="cli/init.html"><strong>2.1.</strong> init</a></li><li><a href="cli/build.html"><strong>2.2.</strong> build</a></li></ul><li><strong>3.</strong> Format</li><li><ul class="section"><li><strong>3.1.</strong> SUMMARY.md</li><li><a href="format/config.html"><strong>3.2.</strong> Configuration</a></li></ul><li><strong>4.</strong> Rust Library</li></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="page-wrapper" class="page-wrapper">
|
<div id="page-wrapper" class="page-wrapper">
|
||||||
|
|
|
@ -6,4 +6,5 @@
|
||||||
- [build](cli/build.md)
|
- [build](cli/build.md)
|
||||||
- [Format]()
|
- [Format]()
|
||||||
- [SUMMARY.md]()
|
- [SUMMARY.md]()
|
||||||
|
- [Configuration](format/config.md)
|
||||||
- [Rust Library]()
|
- [Rust Library]()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
{
|
{
|
||||||
"title": "mdBook Documentation"
|
"title": "mdBook Documentation",
|
||||||
|
"author": "Mathieu David"
|
||||||
}
|
}
|
||||||
|
|
|
@ -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`
|
|
@ -4,6 +4,8 @@ use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use utils;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct BookConfig {
|
pub struct BookConfig {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
@ -42,7 +44,21 @@ impl BookConfig {
|
||||||
let config = Json::from_str(&data).unwrap();
|
let config = Json::from_str(&data).unwrap();
|
||||||
|
|
||||||
// Extract data
|
// 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(&["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
|
self
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,12 @@ impl Renderer for HtmlHandlebars {
|
||||||
|
|
||||||
let mut data = try!(make_data(book.clone(), config));
|
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
|
// Render a file for every entry in the book
|
||||||
let mut index = true;
|
let mut index = true;
|
||||||
for (_, item) in book {
|
for (_, item) in book {
|
||||||
|
@ -71,6 +77,7 @@ impl Renderer for HtmlHandlebars {
|
||||||
// Rendere the handlebars template with the data
|
// Rendere the handlebars template with the data
|
||||||
let rendered = try!(handlebars.render("index", &data));
|
let rendered = try!(handlebars.render("index", &data));
|
||||||
|
|
||||||
|
println!("Write file...");
|
||||||
// Write to file
|
// Write to file
|
||||||
let mut file = try!(create_file(config.dest(), &item.path));
|
let mut file = try!(create_file(config.dest(), &item.path));
|
||||||
try!(file.write_all(&rendered.into_bytes()));
|
try!(file.write_all(&rendered.into_bytes()));
|
||||||
|
@ -121,6 +128,8 @@ impl HtmlHandlebars {
|
||||||
|
|
||||||
fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> {
|
fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> {
|
||||||
|
|
||||||
|
println!("[fn]: create_file");
|
||||||
|
|
||||||
// Extract filename
|
// Extract filename
|
||||||
let mut file_name;
|
let mut file_name;
|
||||||
if let Some(name) = path.file_stem() {
|
if let Some(name) = path.file_stem() {
|
||||||
|
@ -157,6 +166,7 @@ fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>
|
||||||
if !f.is_dir() {
|
if !f.is_dir() {
|
||||||
try!(fs::create_dir(&constructed_path))
|
try!(fs::create_dir(&constructed_path))
|
||||||
} else {
|
} else {
|
||||||
|
println!("[*]: {:?} --> exists", constructed_path);
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -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 {
|
pub fn path_to_root(path: &Path) -> String {
|
||||||
// Remove filename and add "../" for every directory
|
// Remove filename and add "../" for every directory
|
||||||
|
@ -12,3 +14,46 @@ pub fn path_to_root(path: &Path) -> String {
|
||||||
s
|
s
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue