diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-10 17:20:18 +0100 |
---|---|---|
committer | Juan Aguilar <mhpoin@gmail.com> | 2019-01-12 09:16:20 +0100 |
commit | 2dfc3a39eb4943eb5e320c631e7ae9e50ec6357d (patch) | |
tree | 696155c1b50f67e4e19e39eb3298baf6171c9b57 /askama_derive/src/input.rs | |
parent | 467f4ade19fa34983de7e6f6d81c6b4d5ff140fe (diff) | |
download | askama-2dfc3a39eb4943eb5e320c631e7ae9e50ec6357d.tar.gz askama-2dfc3a39eb4943eb5e320c631e7ae9e50ec6357d.tar.bz2 askama-2dfc3a39eb4943eb5e320c631e7ae9e50ec6357d.zip |
Allow configuration of escape modes
Diffstat (limited to 'askama_derive/src/input.rs')
-rw-r--r-- | askama_derive/src/input.rs | 27 |
1 files changed, 18 insertions, 9 deletions
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<String>, 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, |