forked from google/mdbook-i18n-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create mdbook-tera-backend renderer (google#80)
Create renderer Co-authored-by: Martin Geisler <martin@geisler.net>
- Loading branch information
Showing
11 changed files
with
860 additions
and
68 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[workspace] | ||
members = ["i18n-helpers"] | ||
members = ["i18n-helpers", "mdbook-tera-backend"] | ||
default-members = ["i18n-helpers"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use regex::Regex; | ||
use std::sync::OnceLock; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum Directive { | ||
Skip, | ||
TranslatorComment(String), | ||
} | ||
|
||
pub fn find(html: &str) -> Option<Directive> { | ||
static RE: OnceLock<Regex> = OnceLock::new(); | ||
let re = RE.get_or_init(|| { | ||
let pattern = r"(?x) | ||
<!-{2,}\s* # the opening of the comment | ||
(?:i18n|mdbook-xgettext) # the prefix | ||
\s*[:-] # delimit between prefix and command | ||
(?<command>.*[^-]) # the command part of the prefix | ||
-{2,}> # the closing of the comment | ||
"; | ||
Regex::new(pattern).expect("well-formed regex") | ||
}); | ||
|
||
let captures = re.captures(html.trim())?; | ||
|
||
let command = captures["command"].trim(); | ||
match command.split(is_delimiter).next() { | ||
Some("skip") => Some(Directive::Skip), | ||
Some("comment") => { | ||
let start_of_comment_offset = std::cmp::min( | ||
command.find("comment").unwrap() + "comment".len() + 1, | ||
command.len(), | ||
); | ||
Some(Directive::TranslatorComment( | ||
command[start_of_comment_offset..].trim().into(), | ||
)) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
fn is_delimiter(c: char) -> bool { | ||
c.is_whitespace() || c == ':' || c == '-' | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_is_comment_skip_directive_simple() { | ||
assert!(matches!(find("<!-- i18n:skip -->"), Some(Directive::Skip))); | ||
} | ||
|
||
#[test] | ||
fn test_is_comment_skip_directive_tolerates_spaces() { | ||
assert!(matches!(find("<!-- i18n: skip -->"), Some(Directive::Skip))); | ||
} | ||
|
||
#[test] | ||
fn test_is_comment_skip_directive_tolerates_dashes() { | ||
assert!(matches!( | ||
find("<!--- i18n:skip ---->"), | ||
Some(Directive::Skip) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_is_comment_skip_directive_needs_skip() { | ||
assert!(find("<!-- i18n: foo -->").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_is_comment_skip_directive_needs_to_be_a_comment() { | ||
assert!(find("<div>i18: skip</div>").is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_different_prefix() { | ||
assert!(matches!( | ||
find("<!-- mdbook-xgettext:skip -->"), | ||
Some(Directive::Skip) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_translator_comment() { | ||
assert!(match find("<!-- i18n-comment: hello world! -->") { | ||
Some(Directive::TranslatorComment(s)) => { | ||
s == "hello world!" | ||
} | ||
_ => false, | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_translator_empty_comment_does_nothing() { | ||
assert!(match find("<!-- i18n-comment -->") { | ||
Some(Directive::TranslatorComment(s)) => { | ||
s.is_empty() | ||
} | ||
_ => false, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "mdbook-tera-backend" | ||
version = "0.0.1" | ||
authors = ["Martin Geisler <mgeisler@google.com>", "Alexandre Senges <asenges@google.com>"] | ||
categories = ["template-engine"] | ||
edition = "2021" | ||
keywords = ["mdbook", "tera", "renderer", "template"] | ||
license = "Apache-2.0" | ||
repository = "https://github.com/google/mdbook-i18n-helpers" | ||
description = "Plugin to extend mdbook with Tera templates and custom HTML components." | ||
|
||
[dependencies] | ||
anyhow = "1.0.75" | ||
mdbook = { version = "0.4.25", default-features = false } | ||
serde = "1.0" | ||
serde_json = "1.0.91" | ||
tera = "1.19.1" | ||
|
||
[dev-dependencies] | ||
tempdir = "0.3.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Tera backend extension for `mdbook` | ||
|
||
[![Visit crates.io](https://img.shields.io/crates/v/mdbook-i18n-helpers?style=flat-square)](https://crates.io/crates/mdbook-tera-backend) | ||
[![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) | ||
[![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. | ||
|
||
## Installation | ||
|
||
Run | ||
|
||
```shell | ||
$ cargo install mdbook-tera-backend | ||
``` | ||
|
||
## Usage | ||
|
||
### Configuring the backend | ||
|
||
To enable the backend, simply add `[output.tera-backend]` to your `book.toml`, | ||
and configure the place where youre templates will live. For instance | ||
`theme/templates`: | ||
|
||
```toml | ||
[output.html] # You must still enable the html backend. | ||
[output.tera-backend] | ||
template_dir = "theme/templates" | ||
``` | ||
|
||
### Creating templates | ||
|
||
Create your template files in the same directory as your book. | ||
|
||
```html | ||
<!-- ./theme/templates/hello_world.html --> | ||
<div> | ||
Hello world! | ||
</div> | ||
``` | ||
|
||
### Using templates in `index.hbs` | ||
|
||
Since the HTML renderer will first render Handlebars templates, we need to tell | ||
it to ignore Tera templates using `{{{{raw}}}}` blocks: | ||
|
||
```html | ||
{{{{raw}}}} | ||
{% set current_language = ctx.config.book.language %} | ||
<p>Current language: {{ current_language }}</p> | ||
{% include "hello_world.html" %} | ||
{{{{/raw}}}} | ||
``` | ||
|
||
Includes names are based on the file name and not the whole file path. | ||
|
||
### Tera documentation | ||
|
||
Find out all you can do with Tera templates | ||
[here](https://keats.github.io/tera/docs/). | ||
|
||
## Changelog | ||
|
||
Please see [CHANGELOG](../CHANGELOG.md) for details on the changes in each | ||
release. | ||
|
||
## Contact | ||
|
||
For questions or comments, please contact | ||
[Martin Geisler](mailto:mgeisler@google.com) or | ||
[Alexandre Senges](mailto:asenges@google.come) or start a | ||
[discussion](https://github.com/google/mdbook-i18n-helpers/discussions). We | ||
would love to hear from you. | ||
|
||
--- | ||
|
||
This is not an officially supported Google product. |
Oops, something went wrong.