diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-05 08:25:42 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-05 08:25:42 +0100 |
commit | 11abb163ec62e5db9166086b8083e002b24aa906 (patch) | |
tree | 16cb521ccf13093eb30cd0862c53bc2b03d5da2d | |
parent | abaa16748a6d951b951114a69f3d0e02457fcffa (diff) | |
download | askama-11abb163ec62e5db9166086b8083e002b24aa906.tar.gz askama-11abb163ec62e5db9166086b8083e002b24aa906.tar.bz2 askama-11abb163ec62e5db9166086b8083e002b24aa906.zip |
Add support for string literals
Diffstat (limited to '')
-rw-r--r-- | askama/src/generator.rs | 5 | ||||
-rw-r--r-- | askama/src/parser.rs | 10 |
2 files changed, 14 insertions, 1 deletions
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<Expr<'a>>), Compare(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), @@ -39,6 +40,10 @@ fn take_content(i: &[u8]) -> IResult<&[u8], Node> { IResult::Done(&i[..0], Node::Lit(&i[..])) } +named!(expr_str_lit<Expr>, map!( + delimited!(char!('"'), is_not!("\""), char!('"')), + |s| Expr::StrLit(str::from_utf8(s).unwrap()))); + named!(expr_var<Expr>, map!(nom::alphanumeric, Expr::Var)); named!(target_single<Target>, map!(nom::alphanumeric, Target::Name)); @@ -77,7 +82,10 @@ named!(expr_compare<Expr>, do_parse!( (Expr::Compare(str::from_utf8(op).unwrap(), Box::new(left), Box::new(right))))); -named!(expr_any<Expr>, alt!(expr_compare | expr_filtered)); +named!(expr_any<Expr>, alt!( + expr_compare | + expr_filtered | + expr_str_lit)); named!(expr_node<Node>, map!( delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")), |