1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::path::PathBuf;
use std::io::Read;
use std::fs::File;
use failure::{Error, ResultExt};
use mdbook::renderer::RenderContext;
pub const DEFAULT_TEMPLATE: &str = include_str!("index.hbs");
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Config {
pub additional_css: Vec<PathBuf>,
pub use_default_css: bool,
pub index_template: Option<PathBuf>,
pub cover_image: Option<PathBuf>,
pub additional_resources: Vec<PathBuf>,
}
impl Config {
pub fn from_render_context(ctx: &RenderContext) -> Result<Config, Error> {
match ctx.config.get("output.epub") {
Some(table) => {
let mut cfg: Config = table.clone().try_into()?;
if let Some(template_file) = cfg.index_template.take() {
cfg.index_template = Some(ctx.root.join(template_file));
}
Ok(cfg)
}
None => Ok(Config::default()),
}
}
pub fn template(&self) -> Result<String, Error> {
match self.index_template {
Some(ref filename) => {
let mut buffer = String::new();
File::open(filename)
.with_context(|_| format!("Unable to open template ({})", filename.display()))?
.read_to_string(&mut buffer)
.context("Unable to read the template file")?;
Ok(buffer)
}
None => Ok(DEFAULT_TEMPLATE.to_string()),
}
}
}
impl Default for Config {
fn default() -> Config {
Config {
use_default_css: true,
additional_css: Vec::new(),
index_template: None,
cover_image: None,
additional_resources: Vec::new(),
}
}
}