Skip to content

Commit c5e6500

Browse files
committed
Add test
1 parent 5ebc365 commit c5e6500

File tree

8 files changed

+201
-111
lines changed

8 files changed

+201
-111
lines changed

Cargo.lock

Lines changed: 67 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mdbook-tera-backend/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ description = "Plugin to extend mdbook with Tera templates and custom HTML compo
1111

1212
[dependencies]
1313
anyhow = "1.0.75"
14-
chrono = { version = "0.4.31", default-features = false, features = ["alloc"] }
1514
mdbook = { version = "0.4.25", default-features = false }
1615
serde = "1.0"
1716
serde_json = "1.0.91"
1817
tera = "1.19.1"
1918

2019
[dev-dependencies]
21-
tempfile = "3.5.0"
20+
tempdir = "0.3.7"

mdbook-tera-backend/README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [Tera](https://github.com/Keats/tera) backend extension for `mdbook`'s HTML backend
1+
# Tera backend extension for `mdbook`
22

33
[![Visit crates.io](https://img.shields.io/crates/v/mdbook-i18n-helpers?style=flat-square)](https://crates.io/crates/mdbook-tera-backend)
44
[![Build workflow](https://img.shields.io/github/actions/workflow/status/google/mdbook-i18n-helpers/test.yml?style=flat-square)](https://github.com/google/mdbook-i18n-helpers/actions/workflows/test.yml?query=branch%3Amain)
@@ -26,12 +26,9 @@ and configure the place where youre templates will live.
2626
For instance `theme/templates`:
2727

2828
```toml
29-
...
30-
29+
[output.html] # You must still enable the html backend.
3130
[output.tera-backend]
32-
templates_dir = "theme/templates"
33-
34-
...
31+
template_dir = "theme/templates"
3532
```
3633

3734
### Creating templates
@@ -41,20 +38,20 @@ Create your template files in the same directory as your book.
4138
```html
4239
<!-- ./theme/templates/language_list.html -->
4340
<ul>
44-
{% for identifier, language_name in get_context(key="output.i18n.languages") %}
45-
<li>{{ identifier }}: {{ language_name }}</li>
46-
{% endfor %}
41+
{% for identifier, language_name in get_context(key="output.i18n.languages")
42+
%}
43+
<li>{{ identifier }}: {{ language_name }}</li>
44+
{% endfor %}
4745
</ul>
4846
```
4947

5048
### Using templates in `index.hbs`
5149

5250
Since the HTML renderer will first render Handlebars templates, we need to tell it to
53-
ignore tera templates using `{{{{raw}}}}` blocks:
51+
ignore Tera templates using `{{{{raw}}}}` blocks:
5452

5553
```html
56-
{{{{raw}}}}
57-
{% set current_language = ctx | get(key="config") | get(key="book") | get(key="language", default="en") %}
54+
{{{{raw}}}} {% set current_language = ctx.config.book.language %}
5855
<p>CURRENT LANGUAGE: {{ current_language }}</p>
5956
<p>All languages: {% include "language_list.html" %}</p>
6057
{{{{/raw}}}}
@@ -74,7 +71,7 @@ release.
7471
## Contact
7572

7673
For questions or comments, please contact
77-
[Martin Geisler](mailto:[email protected]) or
74+
[Martin Geisler](mailto:[email protected]) or
7875
[Alexandre Senges](mailto:[email protected]) or start a
7976
[discussion](https://github.com/google/mdbook-i18n-helpers/discussions). We
8077
would love to hear from you.

mdbook-tera-backend/src/main.rs

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

3-
use anyhow::anyhow;
3+
use anyhow::{anyhow, Context};
44
use mdbook::renderer::RenderContext;
55
use std::io;
66

@@ -12,24 +12,24 @@ use crate::tera_renderer::renderer::Renderer;
1212
fn main() -> anyhow::Result<()> {
1313
let mut stdin = io::stdin();
1414
let ctx = RenderContext::from_json(&mut stdin).unwrap();
15-
if ctx.config.get_preprocessor("html").is_none() {
15+
if ctx.config.get_renderer("html").is_none() {
1616
return Err(anyhow!(
1717
"Could not find the HTML backend. Please make sure the HTML backend is enabled."
1818
));
1919
}
2020
let config: TeraRendererConfig = ctx
2121
.config
2222
.get_deserialized_opt("output.tera-backend")
23-
.expect("Failed to get tera-backend config")
24-
.unwrap();
23+
.context("Failed to get tera-backend config")?
24+
.context("No tera-backend config found")?;
2525

2626
let tera_template = config
2727
.create_template(&ctx.root)
28-
.expect("Failed to create components");
28+
.context("Failed to create components")?;
2929

30-
let mut renderer = Renderer::new(ctx, tera_template).expect("Failed to create renderer");
30+
let mut renderer = Renderer::new(ctx, tera_template);
3131

32-
renderer.render_book().expect("Failed to render book");
32+
renderer.render_book().context("Failed to render book")?;
3333

3434
Ok(())
3535
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod custom_component;
2+
pub mod renderer;

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,30 @@ use tera::Tera;
77
#[derive(Deserialize)]
88
pub struct TeraRendererConfig {
99
/// Relative path to the templates directory from the `book.toml` directory.
10-
pub templates_dir: PathBuf,
10+
pub template_dir: Option<PathBuf>,
1111
}
1212

13-
impl TeraRendererConfig {
14-
/// Recursively add all templates in the `templates_dir` to the `tera_template`.
15-
fn add_templates_recursively(tera_template: &mut Tera, directory: &Path) -> Result<()> {
16-
for entry in std::fs::read_dir(directory)? {
17-
let entry = entry?;
18-
let path = entry.path();
19-
if path.is_dir() {
20-
Self::add_templates_recursively(tera_template, &path)?;
21-
} else {
22-
tera_template.add_template_file(&path, path.file_name().unwrap().to_str())?;
23-
}
13+
/// Recursively add all templates in the `template_dir` to the `tera_template`.
14+
fn add_templates_recursively(tera_template: &mut Tera, directory: &Path) -> Result<()> {
15+
for entry in std::fs::read_dir(directory)? {
16+
let entry = entry?;
17+
let path = entry.path();
18+
if path.is_dir() {
19+
add_templates_recursively(tera_template, &path)?;
20+
} else {
21+
tera_template.add_template_file(&path, path.file_name().unwrap().to_str())?;
2422
}
25-
Ok(())
2623
}
24+
Ok(())
25+
}
2726

28-
/// Create the `tera_template` and add all templates in the `templates_dir` to it.
27+
impl TeraRendererConfig {
28+
/// Create the `tera_template` and add all templates in the `template_dir` to it.
2929
pub fn create_template(&self, current_dir: &Path) -> Result<Tera> {
3030
let mut tera_template = Tera::default();
31-
Self::add_templates_recursively(
32-
&mut tera_template,
33-
&current_dir.join(&self.templates_dir),
34-
)?;
31+
if let Some(template_dir) = &self.template_dir {
32+
add_templates_recursively(&mut tera_template, &current_dir.join(template_dir))?;
33+
}
3534

3635
Ok(tera_template)
3736
}

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

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)