From 2dfc3a39eb4943eb5e320c631e7ae9e50ec6357d Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 10 Jan 2019 17:20:18 +0100 Subject: Allow configuration of escape modes --- askama_derive/src/generator.rs | 4 ++-- askama_derive/src/input.rs | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'askama_derive') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index f6c83ec..955d588 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -738,7 +738,7 @@ impl<'a> Generator<'a> { Wrapped => expr_buf.buf, Unwrapped => format!( "::askama::MarkupDisplay::new_unsafe(&{}, {})", - expr_buf.buf, self.input.escaping + expr_buf.buf, self.input.escaper ), }; @@ -879,7 +879,7 @@ impl<'a> Generator<'a> { if name == "escape" || name == "safe" || name == "e" || name == "json" { buf.write(&format!( "::askama::filters::{}({}, &", - name, self.input.escaping + name, self.input.escaper )); } else if filters::BUILT_IN_FILTERS.contains(&name) { buf.write(&format!("::askama::filters::{}(&", name)); diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index b584298..0cc852c 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -15,7 +15,7 @@ pub struct TemplateInput<'a> { pub syntax: &'a Syntax<'a>, pub source: Source, pub print: Print, - pub escaping: &'a str, + pub escaper: &'a str, pub ext: Option, pub parent: Option<&'a syn::Type>, pub path: PathBuf, @@ -165,24 +165,33 @@ impl<'a> TemplateInput<'a> { }, ); - let escaping = escaping.unwrap_or_else(|| { + // Match extension against defined output formats + + let extension = escaping.unwrap_or_else(|| { path.extension() .map(|s| s.to_str().unwrap()) - .unwrap_or("none") + .unwrap_or("") .to_string() }); - let escaping = match escaping.as_str() { - "html" | "htm" | "xml" => "::askama::Html", - "txt" | "none" => "::askama::Text", - val => panic!("unknown value '{}' for escape mode", val), - }; + + let mut escaper = None; + for (extensions, path) in &config.escapers { + if extensions.contains(&extension) { + escaper = Some(path); + break; + } + } + + let escaper = escaper.unwrap_or_else(|| { + panic!("no escaper defined for extension '{}'", extension); + }); TemplateInput { ast, config, source, print, - escaping, + escaper, ext, parent, path, -- cgit