Skip to content

Commit e53dcdc

Browse files
authored
Merge pull request #186 from gambhiro/book-json-keys
Book json keys
2 parents cf35e08 + 85d8e2e commit e53dcdc

File tree

5 files changed

+75
-29
lines changed

5 files changed

+75
-29
lines changed

book-example/src/format/config.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ Here is an example of what a ***book.json*** file might look like:
1515

1616
#### Supported variables
1717

18-
- **title:** title of the book
19-
- **author:** author of the book
20-
- **description:** description, which is added as meta in the html head of each page.
21-
- **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
18+
If relative paths are given, they will be relative to the book's root, i.e. the
19+
parent directory of the source directory.
20+
21+
- **title:** The title of the book.
22+
- **author:** The author of the book.
23+
- **description:** The description, which is added as meta in the html head of each page.
24+
- **src:** The path to the book's source files (chapters in Markdown, SUMMARY.md, etc.). Defaults to `root/src`.
25+
- **dest:** The path to the directory where you want your book to be rendered. Defaults to `root/book`.
26+
- **theme_path:** The path to a custom theme directory. Defaults to `root/theme`.
2227

2328
***note:*** *the supported configurable parameters are scarce at the moment, but more will be added in the future*

src/book/bookconfig.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ use std::path::{Path, PathBuf};
55

66
#[derive(Debug, Clone)]
77
pub struct BookConfig {
8-
pub title: String,
9-
pub author: String,
10-
pub description: String,
118
root: PathBuf,
129
pub dest: PathBuf,
1310
pub src: PathBuf,
11+
pub theme_path: PathBuf,
12+
13+
pub title: String,
14+
pub author: String,
15+
pub description: String,
16+
1417
pub indent_spaces: i32,
1518
multilingual: bool,
1619
}
@@ -19,12 +22,15 @@ pub struct BookConfig {
1922
impl BookConfig {
2023
pub fn new(root: &Path) -> Self {
2124
BookConfig {
22-
title: String::new(),
23-
author: String::new(),
24-
description: String::new(),
2525
root: root.to_owned(),
2626
dest: root.join("book"),
2727
src: root.join("src"),
28+
theme_path: root.join("theme"),
29+
30+
title: String::new(),
31+
author: String::new(),
32+
description: String::new(),
33+
2834
indent_spaces: 4, // indentation used for SUMMARY.md
2935
multilingual: false,
3036
}
@@ -70,21 +76,35 @@ impl BookConfig {
7076
self.description = a.to_string().replace("\"", "")
7177
}
7278

73-
// Destination
79+
// Destination folder
7480
if let Some(a) = config.get("dest") {
75-
let dest = PathBuf::from(&a.to_string().replace("\"", ""));
81+
let mut dest = PathBuf::from(&a.to_string().replace("\"", ""));
7682

7783
// If path is relative make it absolute from the parent directory of src
78-
match dest.is_relative() {
79-
true => {
80-
let dest = self.get_root().join(&dest).to_owned();
81-
self.set_dest(&dest);
82-
},
83-
false => {
84-
self.set_dest(&dest);
85-
},
84+
if dest.is_relative() {
85+
dest = self.get_root().join(&dest);
86+
}
87+
self.set_dest(&dest);
88+
}
89+
90+
// Source folder
91+
if let Some(a) = config.get("src") {
92+
let mut src = PathBuf::from(&a.to_string().replace("\"", ""));
93+
if src.is_relative() {
94+
src = self.get_root().join(&src);
95+
}
96+
self.set_src(&src);
97+
}
98+
99+
// Theme path folder
100+
if let Some(a) = config.get("theme_path") {
101+
let mut theme_path = PathBuf::from(&a.to_string().replace("\"", ""));
102+
if theme_path.is_relative() {
103+
theme_path = self.get_root().join(&theme_path);
86104
}
105+
self.set_theme_path(&theme_path);
87106
}
107+
88108
}
89109

90110
self
@@ -116,4 +136,13 @@ impl BookConfig {
116136
self.src = src.to_owned();
117137
self
118138
}
139+
140+
pub fn get_theme_path(&self) -> &Path {
141+
&self.theme_path
142+
}
143+
144+
pub fn set_theme_path(&mut self, theme_path: &Path) -> &mut Self {
145+
self.theme_path = theme_path.to_owned();
146+
self
147+
}
119148
}

src/book/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct MDBook {
2020
root: PathBuf,
2121
dest: PathBuf,
2222
src: PathBuf,
23+
theme_path: PathBuf,
2324

2425
pub title: String,
2526
pub author: String,
@@ -34,8 +35,11 @@ pub struct MDBook {
3435
impl MDBook {
3536
/// Create a new `MDBook` struct with root directory `root`
3637
///
37-
/// - The default source directory is set to `root/src`
38-
/// - The default output directory is set to `root/book`
38+
/// Default directory paths:
39+
///
40+
/// - source: `root/src`
41+
/// - output: `root/book`
42+
/// - theme: `root/theme`
3943
///
4044
/// They can both be changed by using [`set_src()`](#method.set_src) and [`set_dest()`](#method.set_dest)
4145
@@ -49,6 +53,7 @@ impl MDBook {
4953
root: root.to_owned(),
5054
dest: root.join("book"),
5155
src: root.join("src"),
56+
theme_path: root.join("theme"),
5257

5358
title: String::new(),
5459
author: String::new(),
@@ -294,6 +299,7 @@ impl MDBook {
294299

295300
self.dest = config.dest;
296301
self.src = config.src;
302+
self.theme_path = config.theme_path;
297303

298304
self
299305
}
@@ -444,6 +450,18 @@ impl MDBook {
444450
}
445451
}
446452

453+
pub fn set_theme_path(mut self, theme_path: &Path) -> Self {
454+
self.theme_path = match theme_path.is_absolute() {
455+
true => theme_path.to_owned(),
456+
false => self.root.join(theme_path).to_owned(),
457+
};
458+
self
459+
}
460+
461+
pub fn get_theme_path(&self) -> &Path {
462+
&self.theme_path
463+
}
464+
447465
// Construct book
448466
fn parse_summary(&mut self) -> Result<(), Box<Error>> {
449467
// When append becomes stable, use self.content.append() ...

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Renderer for HtmlHandlebars {
3030
let mut handlebars = Handlebars::new();
3131

3232
// Load theme
33-
let theme = theme::Theme::new(book.get_src());
33+
let theme = theme::Theme::new(book.get_theme_path());
3434

3535
// Register template
3636
debug!("[*]: Register handlebars template");

src/theme/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ impl Theme {
5656
return theme;
5757
}
5858

59-
let src = src.join("theme");
60-
// If src does exist, check if there is a theme directory in it
61-
if !src.exists() || !src.is_dir() {
62-
return theme;
63-
}
64-
6559
// Check for individual files if they exist
6660

6761
// index.hbs

0 commit comments

Comments
 (0)