From 8c0f4358309b79ab58a2adfdacd5546ffe1545b1 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 3 Mar 2017 20:43:02 +0100 Subject: Add basic support for comments --- askama/src/generator.rs | 1 + askama/src/parser.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 6975f25..dcd50e8 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -316,6 +316,7 @@ impl<'a> Generator<'a> { for n in nodes { match *n { Node::Lit(lws, val, rws) => { self.write_lit(lws, val, rws); } + Node::Comment() => {}, Node::Expr(ref ws, ref val) => { self.write_expr(ws, val); }, Node::Cond(ref conds, ref ws) => { self.write_cond(conds, ws); diff --git a/askama/src/parser.rs b/askama/src/parser.rs index b955f0a..ce13add 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -24,6 +24,7 @@ pub struct WS(pub bool, pub bool); #[derive(Debug)] pub enum Node<'a> { Lit(&'a str, &'a str, &'a str), + Comment(), Expr(WS, Expr<'a>), Cond(Vec<(WS, Option>, Vec>)>, WS), Loop(WS, Target<'a>, Expr<'a>, Vec>, WS), @@ -68,7 +69,7 @@ fn take_content(i: &[u8]) -> IResult<&[u8], Node> { if *c == b'{' { if i.len() < j + 2 { return IResult::Done(&i[..0], split_ws_parts(&i[..])); - } else if i[j + 1] == b'{' || i[j + 1] == b'%' { + } else if i[j + 1] == b'{' || i[j + 1] == b'%' || i[j + 1] == b'#' { return IResult::Done(&i[j..], split_ws_parts(&i[..j])); } } @@ -299,8 +300,16 @@ named!(block_block, do_parse!( WS(pws2.is_some(), pws2.is_some()))) )); +named!(block_comment, do_parse!( + tag_s!("{#") >> + take_until_s!("#}") >> + tag_s!("#}") >> + (Node::Comment()) +)); + named!(parse_template>>, many0!(alt!( take_content | + block_comment | expr_node | block_if | block_for | -- cgit