Skip to content

Commit 2d05ca1

Browse files
committed
Remove counter + only render html
1 parent 56f02de commit 2d05ca1

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

mdbook-tera-backend/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
mod tera_renderer;
22

3+
use anyhow::anyhow;
34
use mdbook::renderer::RenderContext;
45
use std::io;
56

67
use crate::tera_renderer::custom_component::TeraRendererConfig;
78
use crate::tera_renderer::renderer::Renderer;
89

9-
fn main() {
10+
/// Re-renders HTML files outputed by the HTML backend with Tera templates.
11+
/// Please make sure the HTML backend is enabled.
12+
fn main() -> anyhow::Result<()> {
1013
let mut stdin = io::stdin();
1114
let ctx = RenderContext::from_json(&mut stdin).unwrap();
15+
if ctx.config.get_preprocessor("html").is_none() {
16+
return Err(anyhow!(
17+
"Could not find the HTML backend. Please make sure the HTML backend is enabled."
18+
));
19+
}
1220
let config: TeraRendererConfig = ctx
1321
.config
1422
.get_deserialized_opt("output.tera-backend")
@@ -22,4 +30,6 @@ fn main() {
2230
let mut renderer = Renderer::new(ctx, tera_template).expect("Failed to create renderer");
2331

2432
renderer.render_book().expect("Failed to render book");
33+
34+
Ok(())
2535
}

mdbook-tera-backend/src/tera_renderer/renderer.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,14 @@ use tera::Tera;
3131
/// ```
3232
pub(crate) struct Renderer {
3333
ctx: Arc<RenderContext>,
34-
serialized_ctx: serde_json::Value,
35-
counter: u64,
3634
tera_template: Tera,
3735
}
3836

3937
impl Renderer {
4038
/// Create a new `Renderer` from the `RenderContext` and `Tera` template.
41-
///
42-
/// # Arguments
43-
///
44-
/// `ctx`: The `RenderContext` to be used for rendering. This is usually obtained from `stdin`.
45-
/// `tera_template`: A pre-configured `Tera` template.
4639
pub(crate) fn new(ctx: RenderContext, tera_template: Tera) -> Result<Renderer> {
4740
let mut renderer = Renderer {
48-
serialized_ctx: serde_json::to_value(&ctx)?,
4941
ctx: Arc::new(ctx),
50-
counter: 0,
5142
tera_template,
5243
};
5344
renderer
@@ -56,11 +47,21 @@ impl Renderer {
5647
Ok(renderer)
5748
}
5849

59-
/// Render the book.
50+
/// Render the book. This goes through the output of the HTML renderer
51+
/// by considering all the output HTML files as input to the Tera template.
52+
/// It overwrites the preexisting files with their Tera-rendered version.
6053
pub(crate) fn render_book(&mut self) -> Result<()> {
61-
let dest_dir = self.ctx.destination.parent().unwrap().to_owned();
54+
let dest_dir = self
55+
.ctx
56+
.destination
57+
.parent()
58+
.unwrap()
59+
.join("html")
60+
.to_owned();
6261
if !dest_dir.is_dir() {
63-
return Err(anyhow!("{dest_dir:?} is not a directory"));
62+
return Err(anyhow!(
63+
"{dest_dir:?} is not a directory. Please make sure the HTML renderer is enabled."
64+
));
6465
}
6566
self.render_book_directory(&dest_dir)
6667
}
@@ -116,30 +117,19 @@ impl Renderer {
116117
/// # Arguments
117118
///
118119
/// `path`: The path to the file that will be added as extra context to the renderer.
119-
fn create_context(&mut self, path: &Path) -> tera::Context {
120+
fn create_context(&mut self, path: &Path) -> Result<tera::Context> {
120121
let mut context = tera::Context::new();
121122
context.insert("path", path);
122-
context.insert("ctx", &self.serialized_ctx);
123+
context.insert("ctx", &serde_json::to_value(&*self.ctx)?);
123124
context.insert("book_dir", &self.ctx.destination.parent().unwrap());
124-
context.insert("counter", &self.counter);
125125
context.insert("attributes", &BTreeMap::<String, String>::new());
126-
self.counter += 1;
127126

128-
context
127+
Ok(context)
129128
}
130129

131130
/// Rendering logic for an individual file.
132-
///
133-
/// # Arguments
134-
///
135-
/// `file_content`: The content of the file to be rendered.
136-
/// `path`: The path of the file to be rendered.
137-
///
138-
/// # Returns
139-
///
140-
/// The rendered file.
141131
fn render_file_content(&mut self, file_content: &str, path: &Path) -> Result<String> {
142-
let tera_context = self.create_context(path);
132+
let tera_context = self.create_context(path)?;
143133

144134
let rendered_file = self
145135
.tera_template

0 commit comments

Comments
 (0)