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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::constants::{COLON, HYPHEN, NEWLINE};
use crate::node_pool::NodeID;
use crate::types::{Cursor, MatchError, ParseOpts, Parseable, Parser, Result};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportSnippet<'a> {
    pub backend: &'a str,
    pub contents: &'a str,
}

impl<'a> Parseable<'a> for ExportSnippet<'a> {
    fn parse(
        parser: &mut Parser<'a>,
        mut cursor: Cursor<'a>,
        parent: Option<NodeID>,
        parse_opts: ParseOpts,
    ) -> Result<NodeID> {
        let start = cursor.index;
        cursor.word("@@")?;
        let backend_match = cursor
            .fn_until(|chr| chr == COLON || !(chr.is_ascii_alphanumeric() || chr == HYPHEN))?;

        cursor.index = backend_match.end;
        cursor.word(":")?;

        let start_contents = cursor.index;
        loop {
            let pot_match = cursor.fn_until(|chr| chr == b'@' || chr == NEWLINE)?;
            cursor.index = pot_match.end;
            match cursor.curr() {
                b'@' => {
                    if cursor.peek(1)? == b'@' {
                        return Ok(parser.alloc(
                            Self {
                                backend: backend_match.obj,
                                contents: cursor.clamp_backwards(start_contents),
                            },
                            start,
                            cursor.index + 2,
                            parent,
                        ));
                    } else {
                        cursor.next();
                    }
                }
                _ => return Err(MatchError::InvalidLogic),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{expr_in_pool, parse_org, types::Expr};

    use super::*;

    #[test]
    fn basic_export() {
        let input = r"
@@:@@
";

        let parsed = parse_org(input);
        let l = expr_in_pool!(parsed, ExportSnippet).unwrap();
        assert_eq!(
            l,
            &ExportSnippet {
                backend: "",
                contents: ""
            }
        );
    }

    #[test]
    fn cool_export() {
        let input = r"
@@html:valuesss@@
";
        let parsed = parse_org(input);
        let l = expr_in_pool!(parsed, ExportSnippet).unwrap();
        assert_eq!(
            l,
            &ExportSnippet {
                backend: "html",
                contents: "valuesss"
            }
        );
    }

    #[test]
    fn newline_export_snippet() {
        let input = r"
@@html:value
sss@@
";
        let pool = parse_org(input);
        let parsed = parse_org(input);
        let l = expr_in_pool!(parsed, ExportSnippet);
        assert!(l.is_none());
    }

    #[test]
    fn at_export_snippet() {
        let input = r"
@@html:va@lue sss@@
";
        let pool = parse_org(input);
        let head = pool.pool.iter().find_map(|x| {
            if let Expr::ExportSnippet(snip) = &x.obj {
                Some(snip)
            } else {
                None
            }
        });
        assert_eq!(
            head.unwrap(),
            &ExportSnippet {
                backend: "html",
                contents: "va@lue sss"
            }
        );
    }
}