Repository: djc/askama
Pull request #188 (2019-01-05 06:41:00)

Fix support for tuple
Thank you for your previous approval!

I fixed it for Jinja style, like so:
`{% for s1, s2 in tuple_strings %}`

And supported trailing comma for something like auto generated templates.


Patch:
diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs
index c5e610e..c4cb52d 100644
--- a/askama_derive/src/parser.rs
+++ b/askama_derive/src/parser.rs
@@ -178,14 +178,14 @@ fn take_content<'a>(
 }
 
 fn identifier(input: Input) -> Result<(Input, &str), nom::Err<Input>> {
-    if !nom::is_alphabetic(input[0]) && input[0] != b'_' {
+    if !nom::is_alphabetic(input[0]) && input[0] != b'_' && !non_ascii(input[0]) {
         return Err(nom::Err::Error(error_position!(
             input,
             nom::ErrorKind::Custom(0)
         )));
     }
     for (i, ch) in input.iter().enumerate() {
-        if i == 0 || nom::is_alphanumeric(*ch) || *ch == b'_' {
+        if i == 0 || nom::is_alphanumeric(*ch) || *ch == b'_' || non_ascii(*ch) {
             continue;
         }
         return Ok((Input(&input[i..]), str::from_utf8(&input[..i]).unwrap()));
@@ -193,6 +193,11 @@ fn identifier(input: Input) -> Result<(Input, &str), nom::Err<Input>> {
     Ok((Input(&input[1..]), str::from_utf8(&input[..1]).unwrap()))
 }
 
+#[inline]
+fn non_ascii(chr: u8) -> bool {
+    chr >= 0x80 && chr <= 0xFD
+}
+
 named!(num_lit<Input, &str>, map!(nom::digit,
     |s| str::from_utf8(s.0).unwrap()
 ));
@@ -291,6 +296,7 @@ named!(target_tuple<Input, Target>, do_parse!(
            res
         })
     )) >>
+    opt!(ws!(tag!(","))) >>
     tag!(")") >>
     (Target::Tuple(args.unwrap_or_default()))
 ));
diff --git a/askama_escape/src/lib.rs b/askama_escape/src/lib.rs
index 8c23530..a591a9c 100644
--- a/askama_escape/src/lib.rs
+++ b/askama_escape/src/lib.rs
@@ -1,4 +1,5 @@
 use std::fmt::{self, Display, Formatter};
+use std::io::{self, prelude::*};
 use std::str;
 
 #[derive(Debug, PartialEq)]
@@ -37,12 +38,33 @@ where
 {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
         match *self {
-            MarkupDisplay::Unsafe(ref t) => escape(&t.to_string()).fmt(f),
+            MarkupDisplay::Unsafe(ref t) => {
+                let mut w = EscapeWriter { fmt: f };
+                write!(w, "{}", t).map_err(|_e| fmt::Error)
+            }
             MarkupDisplay::Safe(ref t) => t.fmt(f),
         }
     }
 }
 
+pub struct EscapeWriter<'a, 'b: 'a> {
+    fmt: &'a mut fmt::Formatter<'b>,
+}
+
+impl io::Write for EscapeWriter<'_, '_> {
+    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
+        let escaped = Escaped { bytes };
+        escaped
+            .fmt(self.fmt)
+            .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
+        Ok(bytes.len())
+    }
+
+    fn flush(&mut self) -> std::io::Result<()> {
+        Ok(())
+    }
+}
+
 pub fn escape(s: &str) -> Escaped<'_> {
     Escaped {
         bytes: s.as_bytes(),


Test patch:
diff --git a/testing/templates/for.html b/testing/templates/for.html
index f749752..3d4533b 100644
--- a/testing/templates/for.html
+++ b/testing/templates/for.html
@@ -1,6 +1,6 @@
 {% for s in strings %}
   {{- loop.index0 }}. {{ s }}{% if loop.first %} (first){% endif %}
 {% endfor %}
-{% for (s1, s2) in tuple_strings %}
+{% for (s1, s2, ) in tuple_strings %}
   {{- loop.index0 }}. {{ s1 }},{{ s2 }}{% if loop.first %} (first){% endif %}
 {% endfor %}