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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![deny(
bare_trait_objects,
elided_lifetimes_in_paths,
missing_copy_implementations,
missing_debug_implementations,
rust_2018_idioms,
unreachable_pub,
unsafe_code,
unused_qualifications,
variant_size_differences
)]
#[macro_use]
extern crate failure_derive;
#[macro_use]
extern crate serde_derive;
use failure::Error;
use mdbook::config::Config as MdConfig;
use mdbook::renderer::RenderContext;
use semver::{Version, VersionReq};
use std::fs::{create_dir_all, File};
use std::path::{Path, PathBuf};
mod config;
mod generator;
mod resources;
mod utils;
pub use crate::config::Config;
pub use crate::generator::Generator;
pub const DEFAULT_CSS: &str = include_str!("master.css");
pub const MDBOOK_VERSION: &str = mdbook::MDBOOK_VERSION;
#[derive(Debug, Clone, PartialEq, Fail)]
#[fail(
display = "Incompatible mdbook version, expected {} but got {}",
expected, got
)]
struct IncompatibleMdbookVersion {
expected: String,
got: String,
}
fn version_check(ctx: &RenderContext) -> Result<(), Error> {
let provided_version = Version::parse(&ctx.version)?;
let required_version = VersionReq::parse(MDBOOK_VERSION)?;
if !required_version.matches(&provided_version) {
let e = IncompatibleMdbookVersion {
expected: MDBOOK_VERSION.to_string(),
got: ctx.version.clone(),
};
Err(Error::from(e))
} else {
Ok(())
}
}
pub fn generate(ctx: &RenderContext) -> Result<(), Error> {
log::info!("Starting the EPUB generator");
version_check(ctx)?;
let outfile = output_filename(&ctx.destination, &ctx.config);
log::trace!("Output File: {}", outfile.display());
if !ctx.destination.exists() {
log::debug!(
"Creating destination directory ({})",
ctx.destination.display()
);
create_dir_all(&ctx.destination)?;
}
let f = File::create(&outfile)?;
Generator::new(ctx)?.generate(f)?;
Ok(())
}
pub fn output_filename(dest: &Path, config: &MdConfig) -> PathBuf {
match config.book.title {
Some(ref title) => dest.join(title).with_extension("epub"),
None => dest.join("book.epub"),
}
}