diff options
author | vallentin <mail@vallentin.dev> | 2021-11-18 08:10:28 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2021-11-19 15:30:10 +0100 |
commit | 8a5ff3b41298ebbdd32ac6e74421704d06864a94 (patch) | |
tree | 257c0dd95f2313d48db18ac3decdfca53c6fdf94 /askama_shared/src/generator.rs | |
parent | 36e9d31f6612262d8df29505779e7fe9612b7273 (diff) | |
download | askama-8a5ff3b41298ebbdd32ac6e74421704d06864a94.tar.gz askama-8a5ff3b41298ebbdd32ac6e74421704d06864a94.tar.bz2 askama-8a5ff3b41298ebbdd32ac6e74421704d06864a94.zip |
Added support for optional escaper for escape filter (resolves #556)
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/generator.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 5c72934..78ada4b 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -1091,7 +1091,10 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { mut name: &str, args: &[Expr<'_>], ) -> Result<DisplayWrap, CompileError> { - if name == "format" { + if matches!(name, "escape" | "e") { + self._visit_escape_filter(buf, args)?; + return Ok(DisplayWrap::Wrapped); + } else if name == "format" { self._visit_format_filter(buf, args)?; return Ok(DisplayWrap::Unwrapped); } else if name == "fmt" { @@ -1115,7 +1118,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { return Err("the `yaml` filter requires the `serde-yaml` feature to be enabled".into()); } - const FILTERS: [&str; 5] = ["safe", "escape", "e", "json", "yaml"]; + const FILTERS: [&str; 3] = ["safe", "json", "yaml"]; if FILTERS.contains(&name) { buf.write(&format!( "::askama::filters::{}({}, ", @@ -1135,6 +1138,37 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { }) } + fn _visit_escape_filter( + &mut self, + buf: &mut Buffer, + args: &[Expr<'_>], + ) -> Result<(), CompileError> { + if args.len() > 2 { + return Err("only two arguments allowed to escape filter".into()); + } + let opt_escaper = match args.get(1) { + Some(Expr::StrLit(name)) => Some(*name), + Some(_) => return Err("invalid escaper type for escape filter".into()), + None => None, + }; + let escaper = match opt_escaper { + Some(name) => self + .input + .config + .escapers + .iter() + .find_map(|(escapers, escaper)| escapers.contains(name).then(|| escaper)) + .ok_or(CompileError::Static("invalid escaper for escape filter"))?, + None => self.input.escaper, + }; + buf.write("::askama::filters::escape("); + buf.write(escaper); + buf.write(", "); + self._visit_args(buf, &args[..1])?; + buf.write(")?"); + Ok(()) + } + fn _visit_format_filter( &mut self, buf: &mut Buffer, |