From 11abb163ec62e5db9166086b8083e002b24aa906 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 5 Feb 2017 08:25:42 +0100 Subject: Add support for string literals --- askama/src/generator.rs | 5 +++++ askama/src/parser.rs | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index a5686e6..57d1205 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -75,6 +75,10 @@ impl Generator { self.start = true; } + fn visit_str_lit(&mut self, s: &str) { + self.write(&format!("\"{}\"", s)); + } + fn visit_var(&mut self, s: &[u8]) { let s = str::from_utf8(s).unwrap(); if self.locals.contains(s) { @@ -98,6 +102,7 @@ impl Generator { fn visit_expr(&mut self, expr: &Expr) { match expr { + &Expr::StrLit(s) => self.visit_str_lit(s), &Expr::Var(s) => self.visit_var(s), &Expr::Filter(name, ref val) => self.visit_filter(name, &val), &Expr::Compare(op, ref left, ref right) => diff --git a/askama/src/parser.rs b/askama/src/parser.rs index f395cb6..e29b460 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -2,6 +2,7 @@ use nom::{self, IResult}; use std::str; pub enum Expr<'a> { + StrLit(&'a str), Var(&'a [u8]), Filter(&'a str, Box>), Compare(&'a str, Box>, Box>), @@ -39,6 +40,10 @@ fn take_content(i: &[u8]) -> IResult<&[u8], Node> { IResult::Done(&i[..0], Node::Lit(&i[..])) } +named!(expr_str_lit, map!( + delimited!(char!('"'), is_not!("\""), char!('"')), + |s| Expr::StrLit(str::from_utf8(s).unwrap()))); + named!(expr_var, map!(nom::alphanumeric, Expr::Var)); named!(target_single, map!(nom::alphanumeric, Target::Name)); @@ -77,7 +82,10 @@ named!(expr_compare, do_parse!( (Expr::Compare(str::from_utf8(op).unwrap(), Box::new(left), Box::new(right))))); -named!(expr_any, alt!(expr_compare | expr_filtered)); +named!(expr_any, alt!( + expr_compare | + expr_filtered | + expr_str_lit)); named!(expr_node, map!( delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")), -- cgit