Skip to content

Commit 4b7aff6

Browse files
committed
Enable better minijinja compatibility with the Python implementation
1 parent daa3b7f commit 4b7aff6

File tree

8 files changed

+30
-24
lines changed

8 files changed

+30
-24
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ features = [
177177
# Templates
178178
[workspace.dependencies.minijinja]
179179
version = "2.4.0"
180+
features = ["loader", "json", "speedups", "unstable_machinery"]
181+
182+
# Additional filters for minijinja
183+
[workspace.dependencies.minijinja-contrib]
184+
version = "2.3.1"
185+
features = ["pycompat"]
180186

181187
# Utilities to deal with non-zero values
182188
[workspace.dependencies.nonzero_ext]

crates/handlers/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ time = "0.3.36"
7878
url.workspace = true
7979
mime = "0.3.17"
8080
minijinja.workspace = true
81+
minijinja-contrib.workspace = true
8182
nonzero_ext.workspace = true
8283
rand.workspace = true
8384
rand_chacha = "0.3.1"

crates/handlers/src/upstream_oauth2/template.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ use std::{collections::HashMap, sync::Arc};
99
use base64ct::{Base64, Base64Unpadded, Base64Url, Base64UrlUnpadded, Encoding};
1010
use minijinja::{Environment, Error, ErrorKind, Value};
1111

12-
fn split(value: &str, separator: Option<&str>) -> Vec<String> {
13-
value
14-
.split(separator.unwrap_or(" "))
15-
.map(ToOwned::to_owned)
16-
.collect::<Vec<_>>()
17-
}
18-
1912
fn b64decode(value: &str) -> Result<Value, Error> {
2013
// We're not too concerned about the performance of this filter, so we'll just
2114
// try all the base64 variants when decoding
@@ -78,12 +71,15 @@ fn string(value: &Value) -> String {
7871
pub fn environment() -> Environment<'static> {
7972
let mut env = Environment::new();
8073

81-
env.add_filter("split", split);
74+
minijinja_contrib::add_to_environment(&mut env);
75+
8276
env.add_filter("b64decode", b64decode);
8377
env.add_filter("b64encode", b64encode);
8478
env.add_filter("tlvdecode", tlvdecode);
8579
env.add_filter("string", string);
8680

81+
env.set_unknown_method_callback(minijinja_contrib::pycompat::unknown_method_callback);
82+
8783
env
8884
}
8985

crates/i18n-scan/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ workspace = true
1414
[dependencies]
1515
camino.workspace = true
1616
clap.workspace = true
17-
minijinja = { workspace = true, features = ["unstable_machinery"] }
17+
minijinja.workspace = true
1818
serde_json.workspace = true
1919
tracing-subscriber.workspace = true
2020
tracing.workspace = true

crates/templates/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ walkdir = "2.5.0"
2020
anyhow.workspace = true
2121
thiserror.workspace = true
2222

23-
minijinja = { workspace = true, features = ["loader", "json", "speedups", "unstable_machinery"] }
23+
minijinja.workspace = true
24+
minijinja-contrib.workspace = true
2425
serde.workspace = true
2526
serde_json.workspace = true
2627
serde_urlencoded = "0.7.1"

crates/templates/src/functions.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ pub fn register(
3434
vite_manifest: ViteManifest,
3535
translator: Arc<Translator>,
3636
) {
37+
env.set_unknown_method_callback(minijinja_contrib::pycompat::unknown_method_callback);
38+
39+
minijinja_contrib::add_to_environment(env);
3740
env.add_test("empty", self::tester_empty);
38-
env.add_test("starting_with", tester_starting_with);
3941
env.add_filter("to_params", filter_to_params);
4042
env.add_filter("simplify_url", filter_simplify_url);
4143
env.add_filter("add_slashes", filter_add_slashes);
42-
env.add_filter("split", filter_split);
4344
env.add_function("add_params_to_url", function_add_params_to_url);
4445
env.add_function("counter", || Ok(Value::from_object(Counter::default())));
4546
env.add_global(
@@ -72,17 +73,6 @@ fn tester_empty(seq: Value) -> bool {
7273
seq.len() == Some(0)
7374
}
7475

75-
fn tester_starting_with(value: &str, prefix: &str) -> bool {
76-
value.starts_with(prefix)
77-
}
78-
79-
fn filter_split(value: &str, separator: &str) -> Vec<String> {
80-
value
81-
.split(separator)
82-
.map(std::borrow::ToOwned::to_owned)
83-
.collect()
84-
}
85-
8676
fn filter_add_slashes(value: &str) -> String {
8777
value
8878
.replace('\\', "\\\\")

templates/components/scope.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<li>{{ icon.error() }}<p>{{ _("mas.scope.synapse_admin") }}</p></li>
2222
{% elif scope == "urn:mas:admin" %}
2323
<li>{{ icon.error() }}<p>{{ _("mas.scope.mas_admin") }}</p></li>
24-
{% elif scope is starting_with("urn:matrix:org.matrix.msc2967.client:device:") %}
24+
{% elif scope is startingwith("urn:matrix:org.matrix.msc2967.client:device:") %}
2525
{# We hide this scope #}
2626
{% else %}
2727
<li>{{ icon.info() }}<p>{{ scope }}</p></li>

0 commit comments

Comments
 (0)