mdbook_theme/
lib.rs

1use mdbook::{
2    book::Book,
3    errors,
4    preprocess::{Preprocessor, PreprocessorContext},
5    Config,
6};
7use std::{
8    path::{Path, PathBuf},
9    result,
10};
11
12/// Generate some default static value. This macro is not public.
13macro_rules! default {
14    ($idt:ident, $e1:expr) => { (CssFile::$idt, $e1) };
15    ($idt:ident, $e1:expr, $e2:expr) => { (CssFile::$idt, Item($e1), Value($e2)) };
16    ($($e1:expr, $idt:ident);*) => {
17        $(pub static $idt: &[u8] = include_bytes!($e1);)*
18        pub static ACE_DEFAULT: &[(&str, &[u8])] = &[$(($e1, $idt),)*];
19    };
20}
21
22pub mod ace;
23pub mod theme;
24
25#[derive(Debug)]
26pub enum Error {
27    StrNotFound,
28    FileNotFound,
29    FileNotCreated,
30    FileNotWritten,
31    DirNotCreated,
32    DirNotRemoved,
33    DirNotRead,
34    AceNotFound,
35    MdbookNotParsed,
36    DeserializedFailed,
37}
38
39pub type Result<T> = std::result::Result<T, Error>;
40// pub mod error;
41
42pub struct PreTheme;
43
44/// absoulte path to theme dir:
45/// if `output.html.theme` is not provided, it defaults to `theme/`
46pub fn theme_dir(root: &Path, config: &Config) -> PathBuf {
47    let theme_dir_ = config
48        .get("output.html.theme")
49        .and_then(|v| v.as_str())
50        .unwrap_or("theme");
51    root.join(theme_dir_)
52}
53
54impl Preprocessor for PreTheme {
55    fn name(&self) -> &str {
56        "theme"
57    }
58
59    fn run(&self, ctx: &PreprocessorContext, book: Book) -> result::Result<Book, errors::Error> {
60        let dir = theme_dir(&ctx.root, &ctx.config);
61        if let Some(theme) = ctx.config.get_preprocessor(self.name()) {
62            theme::config::run(theme, dir);
63        }
64
65        Ok(book)
66    }
67
68    fn supports_renderer(&self, renderer: &str) -> bool {
69        renderer == "html"
70    }
71}