Skip to content

Book json keys #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
61 changes: 45 additions & 16 deletions src/book/bookconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -70,21 +76,35 @@ impl BookConfig {
self.description = a.to_string().replace("\"", "")
}

// Destination
// 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 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
Expand Down Expand Up @@ -116,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
}
}
22 changes: 20 additions & 2 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct MDBook {
root: PathBuf,
dest: PathBuf,
src: PathBuf,
theme_path: PathBuf,

pub title: String,
pub author: String,
Expand All @@ -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)

Expand All @@ -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(),
Expand Down Expand Up @@ -294,6 +299,7 @@ impl MDBook {

self.dest = config.dest;
self.src = config.src;
self.theme_path = config.theme_path;

self
}
Expand Down Expand Up @@ -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() ...
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 0 additions & 6 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down