summaryrefslogtreecommitdiff
path: root/src/org.rs
blob: aca0d6b4170a2c13f4ce2639b7567f48b69de9b1 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use regex::Regex;
use std::str;

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum Org {
    Unknown(String),
    Header(u16, String),
    Properties(Vec<(String, String)>),
}

type OrgDocument = Vec<Org>;

macro_rules! matchers {
    (
            $($id:ident <- $e:expr;)*
    ) => {
        lazy_static! {
            $(static ref $id: Regex = Regex::new($e).unwrap();)*
        }
    }
}

matchers! {
    header           <- r"^(\*+) (.*)";
    properties_start <- r"^\s*:PROPERTIES:\s*$";
    property         <- r"^\s*:([^:]*): (.*)$";
    properties_end   <- r"^\s*:END:\s*$";
}

struct Parser<I: Iterator<Item = String>> {
    doc: OrgDocument,
    iter: I,
}

impl<I: Iterator<Item = String>> Parser<I> {
    fn go(&mut self) {
        while let Some(line) = self.iter.next() {
            if let Some(g) = header.captures(&line) {
                self.doc.push(Org::Header(
                    g.get(1).unwrap().as_str().len() as u16,
                    g.get(2).unwrap().as_str().to_string(),
                ));
            } else if properties_start.is_match(&line) {
                &mut self.go_properties(line.clone());
            } else {
                self.doc.push(Org::Unknown(line.to_string()));
            }
        }
    }

    fn go_properties(&mut self, first_line: String) {
        let mut fallback = vec![first_line];
        let mut properties = vec![];

        while let Some(line) = self.iter.next() {
            fallback.push(line.clone());

            if let Some(g) = property.captures(&line) {
                properties.push((
                    g.get(1).unwrap().as_str().to_string(),
                    g.get(2).unwrap().as_str().to_string(),
                ));
            } else if properties_end.is_match(&line) {
                break;
            } else {
                self.doc.push(Org::Unknown(fallback.join("\n")));
                return;
            }
        }
        self.doc.push(Org::Properties(properties));
    }
}

pub fn parse(input: &str) -> OrgDocument {
    let mut parser = Parser {
        doc: vec![],
        iter: input.split('\n').map(|line| line.to_string()),
    };
    parser.go();
    parser.doc
}

pub fn print(doc: OrgDocument) -> String {
    let mut result = vec![];
    let mut current_level = 0;
    for e in doc {
        match e {
            Org::Unknown(s) => result.push(s),

            Org::Header(level, s) => {
                current_level = level as usize;
                result.push(format!("{} {}", "*".repeat(level as usize), s));
            }

            Org::Properties(_ps) => {
                let indent = " ".repeat(current_level);
                result.push(format!("{}:PROPERTIES:", indent));
                for (key, val) in _ps {
                    result.push(format!("{}:{}: {}", indent, key, val));
                }
                result.push(format!("{}:END:", indent));
            }
        }
    }
    result.join("\n")
}

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

    fn s<S: AsRef<str>>(m: S) -> String {
        m.as_ref().to_string()
    }

    #[test]
    fn parse_unknown() {
        let doc = "hello\nhello";
        assert_eq!(
            parse(doc),
            vec![
                Org::Unknown("hello".to_string()),
                Org::Unknown("hello".to_string()),
            ]
        );
    }

    #[test]
    fn parse_header() {
        let doc = "** hello";
        assert_eq!(parse(doc), vec![Org::Header(2, "hello".to_string())]);
        assert_eq!(
            parse(" * hello"),
            vec![Org::Unknown(" * hello".to_string())]
        );
    }

    #[test]
    fn parse_properties() {
        let doc = ":PROPERTIES:
                   :VERSION: 1.0
                   :ANIMAL: dog
                   :END:";
        assert_eq!(
            parse(doc),
            vec![
                Org::Properties(vec![(s("VERSION"), s("1.0")), (s("ANIMAL"), s("dog"))]),
            ]
        );
        let doc = ":PROPERTIES:\ninvalid\n:END:";
        assert_eq!(
            parse(doc),
            vec![
                Org::Unknown(s(":PROPERTIES:\ninvalid")),
                Org::Unknown(s(":END:")),
            ]
        );
    }

    #[test]
    fn print_id() {
        let doc = "* Hello
 :PROPERTIES:
 :FOO: 1
 :END:
This is an org document.

** Another header
This is deep.
";
        assert_eq!(doc, print(parse(doc)));
    }
}