diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-04 13:19:30 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-08-04 13:21:55 +0200 |
commit | 51eee57b84adf7fe72b7aa9b23e3ef974baad5a4 (patch) | |
tree | 5a4e70a1a1fc73f95dc21bfef6ffb6a6f296c06a /askama_derive/src/parser.rs | |
parent | 02e0f783f93d6336015736d4adfd2cebca275b00 (diff) | |
download | askama-51eee57b84adf7fe72b7aa9b23e3ef974baad5a4.tar.gz askama-51eee57b84adf7fe72b7aa9b23e3ef974baad5a4.tar.bz2 askama-51eee57b84adf7fe72b7aa9b23e3ef974baad5a4.zip |
Add support for include blocks (see #25)
Diffstat (limited to 'askama_derive/src/parser.rs')
-rw-r--r-- | askama_derive/src/parser.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index 61ccb09..6a182d5 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -1,4 +1,5 @@ use nom::{self, IResult}; +use path; use std::str; #[derive(Debug)] @@ -31,6 +32,7 @@ pub enum Node<'a> { Extends(Expr<'a>), BlockDef(WS, &'a str, Vec<Node<'a>>, WS), Block(WS, &'a str, WS), + Include(WS, String), } pub type Cond<'a> = (WS, Option<Expr<'a>>, Vec<Node<'a>>); @@ -324,6 +326,25 @@ named!(block_block<Node>, do_parse!( WS(pws2.is_some(), pws2.is_some()))) )); +named!(block_include<Node>, 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), + _ => panic!("include path must be a string literal"), + }; + if src.ends_with('\n') { + let _ = src.pop(); + } + Node::Include(WS(pws.is_some(), nws.is_some()), src) + }) +)); + named!(block_comment<Node>, do_parse!( tag_s!("{#") >> take_until_s!("#}") >> @@ -338,6 +359,7 @@ named!(parse_template<Vec<Node<'a>>>, many0!(alt!( block_if | block_for | block_extends | + block_include | block_block ))); |