mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 07:20:55 +00:00
Properly handle whitespace around comments (fixes #79)
This commit is contained in:
parent
02266bed68
commit
8d8374a10f
@ -310,7 +310,9 @@ impl<'a> Generator<'a> {
|
||||
Node::Lit(lws, val, rws) => {
|
||||
self.write_lit(lws, val, rws);
|
||||
},
|
||||
Node::Comment() => {},
|
||||
Node::Comment(ref ws) => {
|
||||
self.write_comment(ws);
|
||||
},
|
||||
Node::Expr(ref ws, ref val) => {
|
||||
self.write_expr(state, ws, val);
|
||||
},
|
||||
@ -600,6 +602,10 @@ impl<'a> Generator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn write_comment(&mut self, ws: &WS) {
|
||||
self.handle_ws(ws);
|
||||
}
|
||||
|
||||
/* Visitor methods for expression types */
|
||||
|
||||
fn visit_expr_root(&mut self, expr: &Expr) -> DisplayWrap {
|
||||
|
@ -52,7 +52,7 @@ pub struct Macro<'a> {
|
||||
#[derive(Debug)]
|
||||
pub enum Node<'a> {
|
||||
Lit(&'a str, &'a str, &'a str),
|
||||
Comment(),
|
||||
Comment(WS),
|
||||
Expr(WS, Expr<'a>),
|
||||
Call(WS, Option<& 'a str>, &'a str, Vec<Expr<'a>>),
|
||||
LetDecl(WS, Target<'a>),
|
||||
@ -638,9 +638,10 @@ named!(block_node<Node>, do_parse!(
|
||||
|
||||
named!(block_comment<Node>, do_parse!(
|
||||
tag_s!("{#") >>
|
||||
take_until_s!("#}") >>
|
||||
pws: opt!(tag_s!("-")) >>
|
||||
inner: take_until_s!("#}") >>
|
||||
tag_s!("#}") >>
|
||||
(Node::Comment())
|
||||
(Node::Comment(WS(pws.is_some(), inner.len() > 1 && inner[inner.len() - 1] == b'-')))
|
||||
));
|
||||
|
||||
named!(parse_template<Vec<Node<'a>>>, many0!(alt!(
|
||||
|
@ -196,3 +196,14 @@ fn test_slice_literal() {
|
||||
let t = ArrayTemplate {};
|
||||
assert_eq!(t.render().unwrap(), "a");
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(source = " {# foo -#} ", ext = "txt")]
|
||||
struct CommentTemplate {}
|
||||
|
||||
#[test]
|
||||
fn test_comment() {
|
||||
let t = CommentTemplate {};
|
||||
assert_eq!(t.render().unwrap(), " ");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user