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
- **title:** title of the book
- **author:** author of the book
- **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
If relative paths are given, they will be relative to the book's root, i.e. 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*

View File

@ -5,12 +5,15 @@ use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct BookConfig {
pub title: String,
pub author: String,
pub description: String,
root: PathBuf,
pub dest: PathBuf,
pub src: PathBuf,
pub theme_path: PathBuf,
pub title: String,
pub author: String,
pub description: String,
pub indent_spaces: i32,
multilingual: bool,
}
@ -19,12 +22,15 @@ pub struct BookConfig {
impl BookConfig {
pub fn new(root: &Path) -> Self {
BookConfig {
title: String::new(),
author: String::new(),
description: String::new(),
root: root.to_owned(),
dest: root.join("book"),
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
multilingual: false,
}
@ -72,33 +78,33 @@ impl BookConfig {
// Destination folder
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
match dest.is_relative() {
true => {
let dest = self.get_root().join(&dest).to_owned();
self.set_dest(&dest);
},
false => {
self.set_dest(&dest);
},
if dest.is_relative() {
dest = self.get_root().join(&dest);
}
self.set_dest(&dest);
}
// Source folder
if let Some(a) = config.get("src") {
let src = PathBuf::from(&a.to_string().replace("\"", ""));
match src.is_relative() {
true => {
let src = self.get_root().join(&src).to_owned();
self.set_src(&src);
},
false => {
let mut src = PathBuf::from(&a.to_string().replace("\"", ""));
if src.is_relative() {
src = self.get_root().join(&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
@ -130,4 +136,13 @@ impl BookConfig {
self.src = src.to_owned();
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,
dest: PathBuf,
src: PathBuf,
theme_path: PathBuf,
pub title: String,
pub author: String,
@ -34,8 +35,11 @@ pub struct MDBook {
impl MDBook {
/// Create a new `MDBook` struct with root directory `root`
///
/// - The default source directory is set to `root/src`
/// - The default output directory is set to `root/book`
/// Default directory paths:
///
/// - 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)
@ -49,6 +53,7 @@ impl MDBook {
root: root.to_owned(),
dest: root.join("book"),
src: root.join("src"),
theme_path: root.join("theme"),
title: String::new(),
author: String::new(),
@ -294,6 +299,7 @@ impl MDBook {
self.dest = config.dest;
self.src = config.src;
self.theme_path = config.theme_path;
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
fn parse_summary(&mut self) -> Result<(), Box<Error>> {
// When append becomes stable, use self.content.append() ...

View File

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

View File

@ -56,12 +56,6 @@ impl 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
// index.hbs