Skip to content

Commit

Permalink
Make path relative
Browse files Browse the repository at this point in the history
  • Loading branch information
sakex committed Nov 7, 2023
1 parent a43dd2c commit b9c8514
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ infrastructure for [mdbook](https://github.com/rust-lang/mdBook/):

- [mdbook-i18n-helpers](i18n-helpers/README.md): Gettext translation support for
[mdbook](https://github.com/rust-lang/mdBook/)
- [mdbook-tera-backend](./mdbook-tera-backend/README.md): Tera templates extension
for [mdbook](https://github.com/rust-lang/mdBook/)'s HTML renderer.
- [mdbook-tera-backend](./mdbook-tera-backend/README.md): Tera templates
extension for [mdbook](https://github.com/rust-lang/mdBook/)'s HTML renderer.

## Showcases

Expand Down
6 changes: 3 additions & 3 deletions mdbook-tera-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
[![GitHub contributors](https://img.shields.io/github/contributors/google/mdbook-i18n-helpers?style=flat-square)](https://github.com/google/mdbook-i18n-helpers/graphs/contributors)
[![GitHub stars](https://img.shields.io/github/stars/google/mdbook-i18n-helpers?style=flat-square)](https://github.com/google/mdbook-i18n-helpers/stargazers)

This `mdbook` backend makes it possible to use [tera](https://github.com/Keats/tera)
templates and expand the capabilities of your books. It works on top of the default HTML
backend.
This `mdbook` backend makes it possible to use
[tera](https://github.com/Keats/tera) templates and expand the capabilities of
your books. It works on top of the default HTML backend.

## Installation

Expand Down
9 changes: 6 additions & 3 deletions mdbook-tera-backend/src/tera_renderer/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ impl Renderer {
/// `path`: The path to the file that will be added as extra context to the renderer.
fn create_context(&mut self, path: &Path) -> Result<tera::Context> {
let mut context = tera::Context::new();
context.insert("path", path);
context.insert("ctx", &serde_json::to_value(&self.ctx)?);
let book_dir = self.ctx.destination.parent().unwrap();
let relative_path = path.strip_prefix(book_dir).unwrap();
context.insert("path", &relative_path);
context.insert("book_dir", &self.ctx.destination.parent().unwrap());

Ok(context)
Expand Down Expand Up @@ -126,7 +127,8 @@ mod test {

const HTML_FILE: &str = r#"
<!DOCTYPE html>
{%include "test_template.html" %}
{% include "test_template.html" %}
PATH: {{ path }}
</html>
"#;

Expand All @@ -135,6 +137,7 @@ mod test {
const RENDERED_HTML_FILE: &str = r#"
<!DOCTYPE html>
RENDERED
PATH: html/test.html
</html>
"#;

Expand Down

0 comments on commit b9c8514

Please sign in to comment.