1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::io::Write;

use liquid_core::parser::BlockElement;
use liquid_core::Language;
use liquid_core::Renderable;
use liquid_core::Result;
use liquid_core::Runtime;
use liquid_core::{BlockReflection, ParseBlock, TagBlock, TagTokenIter};

#[derive(Copy, Clone, Debug, Default)]
pub struct CommentBlock;

impl CommentBlock {
    pub fn new() -> Self {
        Self::default()
    }
}

impl BlockReflection for CommentBlock {
    fn start_tag(&self) -> &str {
        "comment"
    }

    fn end_tag(&self) -> &str {
        "endcomment"
    }

    fn description(&self) -> &str {
        ""
    }
}

impl ParseBlock for CommentBlock {
    fn parse(
        &self,
        mut arguments: TagTokenIter<'_>,
        mut tokens: TagBlock<'_, '_>,
        options: &Language,
    ) -> Result<Box<dyn Renderable>> {
        // no arguments should be supplied, trying to supply them is an error
        arguments.expect_nothing()?;

        while let Some(token) = tokens.next()? {
            // Only needs to parse tags. Expressions and raw text will never have side effects.
            if let BlockElement::Tag(tag) = token {
                if tag.name() == self.start_tag() {
                    // Parses `{% comment %}` tags (in order to allow nesting)
                    tag.parse(&mut tokens, options)?;
                } else {
                    // Other tags are parsed (because of possible side effects, such as in `{% raw %}`)
                    // But their errors are ignored
                    let _ = tag.parse(&mut tokens, options);
                }
            }
        }

        tokens.assert_empty();
        Ok(Box::new(Comment))
    }

    fn reflection(&self) -> &dyn BlockReflection {
        self
    }
}

#[derive(Copy, Clone, Debug)]
struct Comment;

impl Renderable for Comment {
    fn render_to(&self, _writer: &mut dyn Write, _runtime: &dyn Runtime) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use liquid_core::parser;
    use liquid_core::runtime;
    use liquid_core::runtime::RuntimeBuilder;

    fn options() -> Language {
        let mut options = Language::default();
        options
            .blocks
            .register("comment".to_string(), CommentBlock.into());
        options
    }

    fn unit_parse(text: &str) -> String {
        let options = options();
        let template = parser::parse(text, &options)
            .map(runtime::Template::new)
            .unwrap();

        let runtime = RuntimeBuilder::new().build();

        template.render(&runtime).unwrap()
    }

    #[test]
    fn test_comment() {
        let output = unit_parse("{% comment %} This is a test {% endcomment %}");
        assert_eq!(output, "");
    }
}