From 6323b5e17198f6d91cb1d880823168a43b264ebf Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 03:48:24 +0200 Subject: parser: add type for `Node::Raw` --- askama_derive/src/generator.rs | 8 +++--- askama_parser/src/lib.rs | 2 +- askama_parser/src/node.rs | 65 ++++++++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 5711c36..9781cda 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -666,10 +666,10 @@ impl<'a> Generator<'a> { self.flush_ws(m.ws1); self.prepare_ws(m.ws2); } - Node::Raw(ws1, ref lit, ws2) => { - self.handle_ws(ws1); - self.visit_lit(lit); - self.handle_ws(ws2); + Node::Raw(ref raw) => { + self.handle_ws(raw.ws1); + self.visit_lit(&raw.lit); + self.handle_ws(raw.ws2); } Node::Import(ref i) => { if level != AstLevel::Top { diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 8138a19..c0650ee 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -16,7 +16,7 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; pub use self::expr::Expr; pub use self::node::{ - BlockDef, Call, Cond, CondTest, Import, Lit, Loop, Macro, Match, Node, Target, When, + BlockDef, Call, Cond, CondTest, Import, Lit, Loop, Macro, Match, Node, Raw, Target, When, Whitespace, Ws, }; diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index a743b53..ebab746 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -30,7 +30,7 @@ pub enum Node<'a> { Include(Ws, &'a str), Import(Import<'a>), Macro(Macro<'a>), - Raw(Ws, Lit<'a>, Ws), + Raw(Raw<'a>), Break(Ws), Continue(Ws), } @@ -59,7 +59,7 @@ impl<'a> Node<'a> { map(Import::parse, Self::Import), map(|i| BlockDef::parse(i, s), Self::BlockDef), map(|i| Macro::parse(i, s), Self::Macro), - |i| Self::raw(i, s), + map(|i| Raw::parse(i, s), Self::Raw), |i| Self::r#break(i, s), |i| Self::r#continue(i, s), )), @@ -204,32 +204,6 @@ impl<'a> Node<'a> { Ok((i, Self::Include(Ws(pws, nws), name))) } - fn raw(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - let endraw = tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("endraw")), - opt(Whitespace::parse), - peek(|i| s.tag_block_end(i)), - )); - - let mut p = tuple(( - opt(Whitespace::parse), - ws(keyword("raw")), - cut(tuple(( - opt(Whitespace::parse), - |i| s.tag_block_end(i), - consumed(skip_till(endraw)), - ))), - )); - - let (_, (pws1, _, (nws1, _, (contents, (i, (_, pws2, _, nws2, _)))))) = p(i)?; - let val = Lit::split_ws_parts(contents); - let ws1 = Ws(pws1, nws1); - let ws2 = Ws(pws2, nws2); - Ok((i, Self::Raw(ws1, val, ws2))) - } - fn r#break(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { let mut p = tuple(( opt(Whitespace::parse), @@ -812,6 +786,41 @@ impl<'a> Lit<'a> { } } +#[derive(Debug, PartialEq)] +pub struct Raw<'a> { + pub ws1: Ws, + pub lit: Lit<'a>, + pub ws2: Ws, +} + +impl<'a> Raw<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let endraw = tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("endraw")), + opt(Whitespace::parse), + peek(|i| s.tag_block_end(i)), + )); + + let mut p = tuple(( + opt(Whitespace::parse), + ws(keyword("raw")), + cut(tuple(( + opt(Whitespace::parse), + |i| s.tag_block_end(i), + consumed(skip_till(endraw)), + ))), + )); + + let (_, (pws1, _, (nws1, _, (contents, (i, (_, pws2, _, nws2, _)))))) = p(i)?; + let lit = Lit::split_ws_parts(contents); + let ws1 = Ws(pws1, nws1); + let ws2 = Ws(pws2, nws2); + Ok((i, Self { ws1, lit, ws2 })) + } +} + /// First field is "minus/plus sign was used on the left part of the item". /// /// Second field is "minus/plus sign was used on the right part of the item". -- cgit