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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! `reorg` is a convenient library to read org-mode files from Rust.
//!
//! It has many limitations as of now, as it can only read very simple
//! files. A file is a collection of Sections, each section contains a
//! heading: a line with one or more '*' and a title. After the heading
//! section a contents section is expected, which is a multi-line string.
//!
//! # Examples
//!
//! ```rust
//! # use reorg;
//! let org_doc = String::from("* This is first item
//! with some content
//! and a second line
//! ** And we have another title
//! also with some content");
//! let doc = reorg::Document::from(org_doc).unwrap();
//!
//! assert_eq!(doc.sections.borrow()[0].heading.stars, 1);
//! assert_eq!(doc.sections.borrow()[0].heading.title, "This is first item");
//! assert_eq!(doc.sections.borrow()[0].children.borrow()[0].heading.stars, 2);
//! assert_eq!(doc.sections.borrow()[0].children.borrow()[0].heading.title, "And we have another title");
//!
//! assert_eq!(doc.sections.borrow()[0].content, "with some content\nand a second line\n");
//! assert_eq!(doc.sections.borrow()[0].children.borrow()[0].content, "also with some content");
//! ```

extern crate regex;
use regex::RegexBuilder;
use std::fs::File;
use std::io::prelude::*;

use std::cell::RefCell;
use std::rc::Rc;

/// `Document` is an org representation of a text file. It is a collection of
/// entries but it can be preceded by some content. The prologue is not yet
/// implemented.
#[derive(Debug)]
pub struct Document<'a> {
    pub sections: RefCell<Vec<Rc<Section<'a>>>>,
}

/// A `Section` is a `Heading` and some optional `Content`. A `Document` is
/// composed of many `Section`s.
#[derive(Debug,Clone)]
pub struct Section<'a> {
    pub heading: Heading,

    // content is a string containing the inner content of a given section
    pub content: String,

    pub children: RefCell<Vec<Rc<Section<'a>>>>,
}

/// `Heading` is the title of each section, it includes a number of stars,
/// which set the "priority" for this given `Section`.
#[derive(Debug,Clone)]
pub struct Heading {
    pub stars: usize,
    pub keyword: String,
    pub title: String,
}

impl<'b> Document<'b> {
    pub fn from_file(filename: &str) -> Option<Document> {
        let mut f = File::open(&filename).expect("file not found");

        let mut doc_text = String::new();

        match f.read_to_string(&mut doc_text) {
            Err(_) => panic!("Error reading file contents"),
            Ok(_) => (),
        }

        Document::from(doc_text)
    }

    /// Reads an org document in a string.
    pub fn from<'a>(document: String) -> Option<Document<'a>> {
        let re = RegexBuilder::new(r"^\*+\s").multi_line(true).build().unwrap();
        let mut section_offsets:Vec<usize> = Vec::new();

        let root:RefCell<Vec<Rc<Section>>> = RefCell::new(Vec::new());
        let insertion_stack:RefCell<Vec<Rc<Section>>> = RefCell::new(Vec::new());
        let mut istack = insertion_stack.borrow_mut();

        let mut iter = re.find_iter(&document);
        iter.next();
        for i in iter {
            section_offsets.push(i.start());
        }
        section_offsets.push(document.len());

        let mut last = 0;
        for offs in section_offsets {
            if let Some(section) = Document::read_section(String::from(&document[last..offs])) {

                while let Some(top) = istack.pop() {
                    if section.heading.stars > top.heading.stars {
                        top.children.borrow_mut().push(Rc::clone(&section));
                        istack.push(Rc::clone(&top));
                        istack.push(Rc::clone(&section));
                        break;
                    }
                }
                if istack.len() == 0 {
                    istack.push(Rc::clone(&section));
                    root.borrow_mut().push(section);
                }
            }
            last = offs;
        }

        Some(Document{
            sections: root,
        })
    }

    fn read_content(section: &str) -> String {
        match section.find('\n') {
            Some(u) => section[u+1..].to_string(),
            None => String::from("")
        }
    }

    /// Reads a full `Section`. The `section` parameter is expected to only
    /// contain one `Section`.
    pub fn read_section<'a>(section: String) -> Option<Rc<Section<'a>>> {
        let heading = Document::read_heading(&section)?;
        let content = Document::read_content(&section);

        Some(Rc::new(Section{
            heading: heading,
            content: content.to_string(),
            children: RefCell::new(Vec::new()),
        }))
    }

    /// Returns number of stars from beginning of 1st line of section text.
    fn read_stars(section: &str) -> usize {
        let mut stars:usize = 0;
        for c in section.to_string().chars() {
            if c == '*' {
                stars += 1;
            } else {
                break;
            }
        }

        stars
    }

    /// Reads a title: everything after stars and 1st whitespace.
    fn read_title(section: &str) -> &str {
        let start = section.find("* ").unwrap();
        match section.find('\n') {
            Some(u) => &section[start+2..u],
            None => &section[start+2..]
        }
    }

    /// Reads a heading, this is, a number of stars from the beginning, and
    /// a title.
    pub fn read_heading(section: &str) -> Option<Heading> {
        let stars = Document::read_stars(section);
        let title = Document::read_title(section);

        Some(Heading{
            stars,
            title: title.to_string(),
            keyword: "".to_string(),
        })
    }


}


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

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    fn one_section() {
        let text = String::from("* One section");
        let doc = Document::from(text).unwrap();

        assert_eq!(doc.sections.borrow().len(), 1);
    }

    #[test]
    fn two_section() {
        let text = String::from("* One section
* Two sections");
        let doc = Document::from(text).unwrap();

        assert_eq!(doc.sections.borrow().len(), 2);
    }

    #[test]
    fn correct_number_of_sections() {
        // all sections have level 1 asterisk
        let simple_doc = String::from("* This is a simple document
with some content
here and there
* With a second section with
some data
* And a third and final one
with some data");

        let doc = Document::from(simple_doc).unwrap();

        assert_eq!(doc.sections.borrow().len(), 3);
    }


    #[test]
    fn all_sections_are_read() {
        let simple_doc = String::from("* This is a simple document
with some content
here and there
* With a second section with
some data
** And a third and final one
with some data");

        let doc = Document::from(simple_doc).unwrap();

        assert_eq!(doc.sections.borrow().len(), 2);
        assert_eq!(doc.sections.borrow()[1].children.borrow().len(), 1);
    }

    #[test]
    fn title_is_obtained_correctly () {
        let simple_doc = String::from("* This is a simple document
with some content
here and there
* With a second section with
some data
** And a third and final one
with some data");

        let doc = Document::from(simple_doc).unwrap();

        assert_eq!(doc.sections.borrow()[0].heading.title, "This is a simple document");
        assert_eq!(doc.sections.borrow()[1].heading.title, "With a second section with");
        assert_eq!(doc.sections.borrow()[1].children.borrow()[0].heading.title, "And a third and final one");
    }

    #[test]
    fn section_level_is_obtained_correctly () {
        let simple_doc = String::from("* This is a simple document
with some content
here and there
* With a second section with
some data
** And a third and final one
with some data");

        let doc = Document::from(simple_doc).unwrap();

        assert_eq!(doc.sections.borrow()[0].heading.stars, 1);
        assert_eq!(doc.sections.borrow()[1].heading.stars, 1);
        assert_eq!(doc.sections.borrow()[1].children.borrow()[0].heading.stars, 2);
    }

    #[test]
    fn subsection_is_obtained_correctly () {
        let simple_doc = String::from("* This is a simple document
with some content
here and there
* With a second section with
some data
** And a third and final one
with some data");

        let doc = Document::from(simple_doc).unwrap();

        assert_eq!(doc.sections.borrow()[0].heading.stars, 1);
        assert_eq!(doc.sections.borrow()[1].heading.stars, 1);
        assert_eq!(doc.sections.borrow()[1].children.borrow()[0].heading.title, "And a third and final one");
        assert_eq!(doc.sections.borrow()[1].children.borrow()[0].heading.stars, 2);
    }

    #[test]
    fn read_org_from_file() {
        match Document::from_file("todo.org") {
            Some(doc) => {
                assert_eq!(doc.sections.borrow().len(), 6)
            }
            _ => {
                panic!("Could not read file")
            }
        }
    }
}