aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-09-07 20:42:54 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-09-07 20:42:54 +0200
commitcd4ccd260db6414638ee038d198108face777982 (patch)
treef7f3ff5950b90015e06803f24da93be6a9db2477 /askama_shared/src
parent20803616dc3ecea0f770fb6a0e4b6eb73f598dd6 (diff)
downloadaskama-cd4ccd260db6414638ee038d198108face777982.tar.gz
askama-cd4ccd260db6414638ee038d198108face777982.tar.bz2
askama-cd4ccd260db6414638ee038d198108face777982.zip
Infer escaping mode based on template extension
Diffstat (limited to 'askama_shared/src')
-rw-r--r--askama_shared/src/input.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs
index 95233d3..f71f8d0 100644
--- a/askama_shared/src/input.rs
+++ b/askama_shared/src/input.rs
@@ -1,7 +1,7 @@
use path;
use std::borrow::Cow;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use syn;
@@ -54,7 +54,7 @@ impl<'a> TemplateMeta<'a> {
let attr = attr.unwrap();
let mut source = None;
let mut print = Print::None;
- let mut escaping = EscapeMode::Html;
+ let mut escaping = None;
let mut ext = None;
if let syn::MetaItem::List(_, ref inner) = attr.value {
for nm_item in inner {
@@ -83,7 +83,7 @@ impl<'a> TemplateMeta<'a> {
panic!("print value must be string literal");
},
"escape" => if let syn::Lit::Str(ref s, _) = *val {
- escaping = (s.as_ref() as &str).into();
+ escaping = Some((s.as_ref() as &str).into());
} else {
panic!("escape value must be string literal");
},
@@ -109,6 +109,22 @@ impl<'a> TemplateMeta<'a> {
},
_ => {},
}
+ let escaping = match escaping {
+ Some(m) => m,
+ None => {
+ let ext = match source {
+ Source::Path(p) => {
+ Path::new(p).extension().map(|s| s.to_str().unwrap()).unwrap_or("")
+ },
+ Source::Source(_) => ext.unwrap(), // Already panicked if None
+ };
+ if HTML_EXTENSIONS.contains(&ext) {
+ EscapeMode::Html
+ } else {
+ EscapeMode::None
+ }
+ }
+ };
TemplateMeta { source, print, escaping, ext }
}
}
@@ -155,3 +171,5 @@ impl<'a> From<&'a str> for Print {
}
}
}
+
+const HTML_EXTENSIONS: [&str; 3] = ["html", "htm", "xml"];