Skip to content

Commit

Permalink
Merge pull request #546 from Kijewski/pr-fix-raw
Browse files Browse the repository at this point in the history
Allow whitespace trimming in {{raw}} blocks
  • Loading branch information
Kijewski authored Nov 30, 2021
2 parents 4940b5d + ef3e840 commit 1762792
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions askama_shared/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
self.flush_ws(m.ws1);
self.prepare_ws(m.ws2);
}
Node::Raw(ws1, contents, ws2) => {
Node::Raw(ws1, lws, val, rws, ws2) => {
self.handle_ws(ws1);
self.buf_writable.push(Writable::Lit(contents));
self.visit_lit(lws, val, rws);
self.handle_ws(ws2);
}
Node::Import(ws, _, _) => {
Expand Down
35 changes: 19 additions & 16 deletions askama_shared/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_till, take_until};
use nom::character::complete::{anychar, char, digit1};
use nom::combinator::{complete, cut, eof, map, not, opt, recognize, value};
use nom::combinator::{complete, consumed, cut, eof, map, not, opt, peek, recognize, value};
use nom::error::{Error, ErrorKind};
use nom::multi::{fold_many0, many0, many1, separated_list0, separated_list1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
Expand All @@ -28,7 +28,7 @@ pub enum Node<'a> {
Include(Ws, &'a str),
Import(Ws, &'a str, &'a str),
Macro(&'a str, Macro<'a>),
Raw(Ws, &'a str, Ws),
Raw(Ws, &'a str, &'a str, &'a str, Ws),
Break(Ws),
Continue(Ws),
}
Expand Down Expand Up @@ -1048,29 +1048,32 @@ fn block_macro<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
}

fn block_raw<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
let endraw = tuple((
|i| tag_block_start(i, s),
opt(char('-')),
ws(tag("endraw")),
opt(char('-')),
peek(|i| tag_block_end(i, s)),
));

let mut p = tuple((
opt(char('-')),
ws(tag("raw")),
cut(tuple((
opt(char('-')),
|i| tag_block_end(i, s),
take_until("{% endraw %}"),
|i| tag_block_start(i, s),
opt(char('-')),
ws(tag("endraw")),
opt(char('-')),
consumed(skip_till(endraw)),
))),
));

let (i, (pws1, _, (nws1, _, contents, _, pws2, _, nws2))) = p(i)?;
Ok((
i,
Node::Raw(
Ws(pws1.is_some(), nws1.is_some()),
contents,
Ws(pws2.is_some(), nws2.is_some()),
),
))
let (_, (pws1, _, (nws1, _, (contents, (i, (_, pws2, _, nws2, _)))))) = p(i)?;
let (lws, val, rws) = match split_ws_parts(contents) {
Node::Lit(lws, val, rws) => (lws, val, rws),
_ => unreachable!(),
};
let ws1 = Ws(pws1.is_some(), nws1.is_some());
let ws2 = Ws(pws2.is_some(), nws2.is_some());
Ok((i, Node::Raw(ws1, lws, val, rws, ws2)))
}

fn break_statement<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Node<'a>> {
Expand Down
2 changes: 2 additions & 0 deletions testing/templates/raw-ws.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<{% raw -%} {{hello}} {%- endraw %}>
< {%- raw %}{{bye}}{% endraw -%} >
10 changes: 10 additions & 0 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,16 @@ fn test_raw_complex() {
);
}

#[derive(Template)]
#[template(path = "raw-ws.html")]
struct RawTemplateWs;

#[test]
fn test_raw_ws() {
let template = RawTemplateWs;
assert_eq!(template.render().unwrap(), "<{{hello}}>\n<{{bye}}>");
}

mod without_import_on_derive {
#[derive(askama::Template)]
#[template(source = "foo", ext = "txt")]
Expand Down

0 comments on commit 1762792

Please sign in to comment.