use theme_path key in book.json when given

This commit is contained in:
Gambhiro 2016-12-07 14:22:32 +00:00
parent a9e5dc63f1
commit 85d8e2ebd3
5 changed files with 69 additions and 37 deletions

View File

@ -15,9 +15,14 @@ Here is an example of what a ***book.json*** file might look like:
#### Supported variables #### Supported variables
- **title:** title of the book If relative paths are given, they will be relative to the book's root, i.e. the
- **author:** author of the book parent directory of the source directory.
- **description:** description, which is added as meta in the html head of each page.
- **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 the source directory - **title:** The title of the book.
- **author:** The author of the book.
- **description:** The description, which is added as meta in the html head of each page.
- **src:** The path to the book's source files (chapters in Markdown, SUMMARY.md, etc.). Defaults to `root/src`.
- **dest:** The path to the directory where you want your book to be rendered. Defaults to `root/book`.
- **theme_path:** The path to a custom theme directory. Defaults to `root/theme`.
***note:*** *the supported configurable parameters are scarce at the moment, but more will be added in the future* ***note:*** *the supported configurable parameters are scarce at the moment, but more will be added in the future*

View File

@ -5,12 +5,15 @@ use std::path::{Path, PathBuf};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BookConfig { pub struct BookConfig {
pub title: String,
pub author: String,
pub description: String,
root: PathBuf, root: PathBuf,
pub dest: PathBuf, pub dest: PathBuf,
pub src: PathBuf, pub src: PathBuf,
pub theme_path: PathBuf,
pub title: String,
pub author: String,
pub description: String,
pub indent_spaces: i32, pub indent_spaces: i32,
multilingual: bool, multilingual: bool,
} }
@ -19,12 +22,15 @@ pub struct BookConfig {
impl BookConfig { impl BookConfig {
pub fn new(root: &Path) -> Self { pub fn new(root: &Path) -> Self {
BookConfig { BookConfig {
title: String::new(),
author: String::new(),
description: String::new(),
root: root.to_owned(), root: root.to_owned(),
dest: root.join("book"), dest: root.join("book"),
src: root.join("src"), src: root.join("src"),
theme_path: root.join("theme"),
title: String::new(),
author: String::new(),
description: String::new(),
indent_spaces: 4, // indentation used for SUMMARY.md indent_spaces: 4, // indentation used for SUMMARY.md
multilingual: false, multilingual: false,
} }
@ -72,33 +78,33 @@ impl BookConfig {
// Destination folder // Destination folder
if let Some(a) = config.get("dest") { if let Some(a) = config.get("dest") {
let dest = PathBuf::from(&a.to_string().replace("\"", "")); let mut dest = PathBuf::from(&a.to_string().replace("\"", ""));
// If path is relative make it absolute from the parent directory of src // If path is relative make it absolute from the parent directory of src
match dest.is_relative() { if dest.is_relative() {
true => { dest = self.get_root().join(&dest);
let dest = self.get_root().join(&dest).to_owned();
self.set_dest(&dest);
},
false => {
self.set_dest(&dest);
},
} }
self.set_dest(&dest);
} }
// Source folder // Source folder
if let Some(a) = config.get("src") { if let Some(a) = config.get("src") {
let src = PathBuf::from(&a.to_string().replace("\"", "")); let mut src = PathBuf::from(&a.to_string().replace("\"", ""));
match src.is_relative() { if src.is_relative() {
true => { src = self.get_root().join(&src);
let src = self.get_root().join(&src).to_owned();
self.set_src(&src);
},
false => {
self.set_src(&src);
}
} }
self.set_src(&src);
} }
// Theme path folder
if let Some(a) = config.get("theme_path") {
let mut theme_path = PathBuf::from(&a.to_string().replace("\"", ""));
if theme_path.is_relative() {
theme_path = self.get_root().join(&theme_path);
}
self.set_theme_path(&theme_path);
}
} }
self self
@ -130,4 +136,13 @@ impl BookConfig {
self.src = src.to_owned(); self.src = src.to_owned();
self self
} }
pub fn get_theme_path(&self) -> &Path {
&self.theme_path
}
pub fn set_theme_path(&mut self, theme_path: &Path) -> &mut Self {
self.theme_path = theme_path.to_owned();
self
}
} }

View File

@ -20,6 +20,7 @@ pub struct MDBook {
root: PathBuf, root: PathBuf,
dest: PathBuf, dest: PathBuf,
src: PathBuf, src: PathBuf,
theme_path: PathBuf,
pub title: String, pub title: String,
pub author: String, pub author: String,
@ -34,8 +35,11 @@ pub struct MDBook {
impl MDBook { impl MDBook {
/// Create a new `MDBook` struct with root directory `root` /// Create a new `MDBook` struct with root directory `root`
/// ///
/// - The default source directory is set to `root/src` /// Default directory paths:
/// - The default output directory is set to `root/book` ///
/// - source: `root/src`
/// - output: `root/book`
/// - theme: `root/theme`
/// ///
/// They can both be changed by using [`set_src()`](#method.set_src) and [`set_dest()`](#method.set_dest) /// They can both be changed by using [`set_src()`](#method.set_src) and [`set_dest()`](#method.set_dest)
@ -49,6 +53,7 @@ impl MDBook {
root: root.to_owned(), root: root.to_owned(),
dest: root.join("book"), dest: root.join("book"),
src: root.join("src"), src: root.join("src"),
theme_path: root.join("theme"),
title: String::new(), title: String::new(),
author: String::new(), author: String::new(),
@ -294,6 +299,7 @@ impl MDBook {
self.dest = config.dest; self.dest = config.dest;
self.src = config.src; self.src = config.src;
self.theme_path = config.theme_path;
self self
} }
@ -444,6 +450,18 @@ impl MDBook {
} }
} }
pub fn set_theme_path(mut self, theme_path: &Path) -> Self {
self.theme_path = match theme_path.is_absolute() {
true => theme_path.to_owned(),
false => self.root.join(theme_path).to_owned(),
};
self
}
pub fn get_theme_path(&self) -> &Path {
&self.theme_path
}
// Construct book // Construct book
fn parse_summary(&mut self) -> Result<(), Box<Error>> { fn parse_summary(&mut self) -> Result<(), Box<Error>> {
// When append becomes stable, use self.content.append() ... // When append becomes stable, use self.content.append() ...

View File

@ -30,7 +30,7 @@ impl Renderer for HtmlHandlebars {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
// Load theme // Load theme
let theme = theme::Theme::new(book.get_src()); let theme = theme::Theme::new(book.get_theme_path());
// Register template // Register template
debug!("[*]: Register handlebars template"); debug!("[*]: Register handlebars template");

View File

@ -56,12 +56,6 @@ impl Theme {
return theme; return theme;
} }
let src = src.join("theme");
// If src does exist, check if there is a theme directory in it
if !src.exists() || !src.is_dir() {
return theme;
}
// Check for individual files if they exist // Check for individual files if they exist
// index.hbs // index.hbs