Skip to content

Commit

Permalink
feat(build-templated-pages): remove unused tera dependencies
Browse files Browse the repository at this point in the history
This involves toggling off Tera's default features so crates such as rand are not pulled in. The only thing I had to do was recreate the slugify filter, since that is used in a template.
  • Loading branch information
BD103 committed Jan 13, 2024
1 parent 8e75d00 commit 631308b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tools/build-templated-pages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
toml_edit = { version = "0.21", default-features = false, features = ["parse"] }
tera = "1.15"
tera = { version = "1.19", default-features = false }
serde = { version = "1.0", features = ["derive"] }
bitflags = "2.3"
slug = "0.1"
20 changes: 12 additions & 8 deletions tools/build-templated-pages/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ pub(crate) fn check(what_to_run: Command) {

let mut context = Context::new();
context.insert("all_examples", &examples_by_category);
Tera::new("docs-template/*.md.tpl")
.expect("error parsing template")
.render_to(
"EXAMPLE_README.md.tpl",
&context,
File::create("examples/README.md").expect("error creating file"),
)
.expect("error rendering template");

let mut tera = Tera::new("docs-template/*.md.tpl").expect("error parsing template");

// Instead of automatically registering all of Tera's builtins, we use the one we need.
tera.register_filter("slugify", crate::slugify::slugify);

tera.render_to(
"EXAMPLE_README.md.tpl",
&context,
File::create("examples/README.md").expect("error creating file"),
)
.expect("error rendering template");
}
}
1 change: 1 addition & 0 deletions tools/build-templated-pages/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bitflags::bitflags;

mod examples;
mod features;
mod slugify;

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
10 changes: 10 additions & 0 deletions tools/build-templated-pages/src/slugify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::collections::HashMap;

use tera::{to_value, try_get_value, Result, Value};

// A copy-and-paste from Tera. It is the only builtin we use.
// https://github.com/Keats/tera/blob/290889e61e9fda317f42f284e7a875342424d646/src/builtins/filters/string.rs#L240-L243
pub(crate) fn slugify(value: &Value, _: &HashMap<String, Value>) -> Result<Value> {
let s = try_get_value!("slugify", "value", String, value);
Ok(to_value(slug::slugify(s)).unwrap())
}

0 comments on commit 631308b

Please sign in to comment.