From 061330d8509fe987abee5b47f035f088f42c386e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 4 Aug 2017 15:56:30 +0200 Subject: Handle parsing for blocks in one place --- askama_derive/src/parser.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'askama_derive') diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index 6a182d5..d5cea5b 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -260,7 +260,6 @@ named!(cond_block, do_parse!( )); named!(block_if, do_parse!( - tag_s!("{%") >> pws1: opt!(tag_s!("-")) >> cond: ws!(cond_if) >> nws1: opt!(tag_s!("-")) >> @@ -271,7 +270,6 @@ named!(block_if, do_parse!( pws2: opt!(tag_s!("-")) >> ws!(tag_s!("endif")) >> nws2: opt!(tag_s!("-")) >> - tag_s!("%}") >> ({ let mut res = Vec::new(); res.push((WS(pws1.is_some(), nws1.is_some()), Some(cond), block)); @@ -281,7 +279,6 @@ named!(block_if, do_parse!( )); named!(block_for, do_parse!( - tag_s!("{%") >> pws1: opt!(tag_s!("-")) >> ws!(tag_s!("for")) >> var: ws!(target_single) >> @@ -294,22 +291,18 @@ named!(block_for, do_parse!( pws2: opt!(tag_s!("-")) >> ws!(tag_s!("endfor")) >> nws2: opt!(tag_s!("-")) >> - tag_s!("%}") >> (Node::Loop(WS(pws1.is_some(), nws1.is_some()), var, iter, block, WS(pws2.is_some(), pws2.is_some()))) )); named!(block_extends, do_parse!( - tag_s!("{%") >> ws!(tag_s!("extends")) >> name: ws!(expr_str_lit) >> - tag_s!("%}") >> (Node::Extends(name)) )); named!(block_block, do_parse!( - tag_s!("{%") >> pws1: opt!(tag_s!("-")) >> ws!(tag_s!("block")) >> name: ws!(identifier) >> @@ -320,19 +313,16 @@ named!(block_block, do_parse!( pws2: opt!(tag_s!("-")) >> ws!(tag_s!("endblock")) >> nws2: opt!(tag_s!("-")) >> - tag_s!("%}") >> (Node::BlockDef(WS(pws1.is_some(), nws1.is_some()), name, contents, WS(pws2.is_some(), pws2.is_some()))) )); named!(block_include, do_parse!( - tag_s!("{%") >> pws: opt!(tag_s!("-")) >> ws!(tag_s!("include")) >> name: ws!(expr_str_lit) >> nws: opt!(tag_s!("-")) >> - tag_s!("%}") >> ({ let mut src = match name { Expr::StrLit(s) => path::get_template_source(s), @@ -345,6 +335,19 @@ named!(block_include, do_parse!( }) )); +named!(block_node, do_parse!( + tag_s!("{%") >> + contents: alt!( + block_if | + block_for | + block_extends | + block_include | + block_block + ) >> + tag_s!("%}") >> + (contents) +)); + named!(block_comment, do_parse!( tag_s!("{#") >> take_until_s!("#}") >> @@ -356,11 +359,7 @@ named!(parse_template>>, many0!(alt!( take_content | block_comment | expr_node | - block_if | - block_for | - block_extends | - block_include | - block_block + block_node ))); pub fn parse(src: &str) -> Vec { -- cgit