1use mdbook::book::Book;
2use mdbook::book::{BookItem, Chapter};
3use mdbook::errors::Error;
4use mdbook::preprocess::{Preprocessor, PreprocessorContext};
5use rust_embed::RustEmbed;
6
7mod content_refs;
8mod embeds;
9mod hints;
10
11#[derive(RustEmbed)]
12#[folder = "assets/"]
13pub struct Asset;
14
15pub struct GitBook;
17
18impl Default for GitBook {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl GitBook {
25 pub fn new() -> GitBook {
26 GitBook
27 }
28}
29
30impl Preprocessor for GitBook {
31 fn name(&self) -> &str {
32 "gitbook"
33 }
34
35 fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
36 let mut error: Option<Error> = None;
37 book.for_each_mut(|item: &mut BookItem| {
38 if error.is_some() {
39 return;
40 }
41 if let BookItem::Chapter(ref mut chapter) = *item {
42 if let Err(err) = handle_chapter(chapter) {
43 error = Some(err)
44 }
45 }
46 });
47 error.map_or(Ok(book), Err)
48 }
49
50 fn supports_renderer(&self, renderer: &str) -> bool {
52 renderer == "html"
53 }
54}
55
56fn handle_chapter(chapter: &mut Chapter) -> Result<(), Error> {
58 chapter.content = hints::render(&chapter.content)?;
59 chapter.content = embeds::render(&chapter.content)?;
60 chapter.content = content_refs::render(&chapter.content)?;
61 Ok(())
64}