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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
mod context;
use std::path::Path;
use anyhow::anyhow;
use globwalk::GlobWalkerBuilder;
use mdbook::book::{Book, BookItem};
use mdbook::errors::Error;
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use tera::{Context, Tera};
pub use self::context::{ContextSource, StaticContextSource};
#[derive(Clone)]
pub struct TeraPreprocessor<C = StaticContextSource> {
tera: Tera,
context: C,
}
impl<C> TeraPreprocessor<C> {
pub fn new(context: C) -> Self {
Self {
context,
tera: Tera::default(),
}
}
pub fn include_templates<P>(&mut self, root: P, glob_str: &str) -> Result<(), Error>
where
P: AsRef<Path>,
{
let root = &root.as_ref().canonicalize()?;
let paths = GlobWalkerBuilder::from_patterns(root, &[glob_str])
.build()?
.filter_map(|r| r.ok())
.map(|p| {
let path = p.into_path();
let name = path
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.into_owned();
(path, Some(name))
});
self.tera.add_template_files(paths)?;
Ok(())
}
pub fn tera_mut(&mut self) -> &mut Tera {
&mut self.tera
}
}
impl<C: Default> Default for TeraPreprocessor<C> {
fn default() -> Self {
Self::new(Default::default())
}
}
impl<C> Preprocessor for TeraPreprocessor<C>
where
C: ContextSource,
{
fn name(&self) -> &str {
"tera"
}
fn run(&self, book_ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
let mut tera = Tera::default();
tera.extend(&self.tera).unwrap();
let mut ctx = Context::new();
ctx.insert("ctx", &book_ctx);
ctx.extend(self.context.context());
render_book_items(&mut book, &mut tera, &ctx)?;
Ok(book)
}
}
fn render_book_items(book: &mut Book, tera: &mut Tera, context: &Context) -> Result<(), Error> {
let mut templates = Vec::new();
collect_item_chapters(&mut templates, book.sections.as_slice())?;
tera.add_raw_templates(templates)?;
render_item_chapters(tera, context, book.sections.as_mut_slice())
}
fn collect_item_chapters<'a>(
templates: &mut Vec<(&'a str, &'a str)>,
items: &'a [BookItem],
) -> Result<(), Error> {
for item in items {
match item {
BookItem::Chapter(chapter) => {
if let Some(ref path) = chapter.path {
let path = path
.to_str()
.ok_or_else(|| anyhow!("invalid chapter path"))?;
templates.push((path, chapter.content.as_str()));
collect_item_chapters(templates, chapter.sub_items.as_slice())?;
}
}
BookItem::PartTitle(_) | BookItem::Separator => (),
}
}
Ok(())
}
fn render_item_chapters(
tera: &mut Tera,
context: &Context,
items: &mut [BookItem],
) -> Result<(), Error> {
for item in items {
match item {
BookItem::Chapter(chapter) => {
if let Some(ref path) = chapter.path {
let path = path
.to_str()
.ok_or_else(|| anyhow!("invalid chapter path"))?;
chapter.content = tera.render(path, context)?;
render_item_chapters(tera, context, chapter.sub_items.as_mut_slice())?;
}
}
BookItem::PartTitle(_) | BookItem::Separator => (),
}
}
Ok(())
}