diff options
-rw-r--r-- | src/org.rs | 45 |
1 files changed, 42 insertions, 3 deletions
@@ -21,7 +21,7 @@ macro_rules! matchers { } matchers! { - header <- r"^(\*)+ (.*)"; + header <- r"^(\*+) (.*)"; properties_start <- r"^\s*:PROPERTIES:\s*$"; property <- r"^\s*:([^:]*): (.*)$"; properties_end <- r"^\s*:END:\s*$"; @@ -80,6 +80,31 @@ pub fn parse(input: &str) -> OrgDocument { 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::*; @@ -102,8 +127,8 @@ mod tests { #[test] fn parse_header() { - let doc = "* hello"; - assert_eq!(parse(doc), vec![Org::Header(1, "hello".to_string())]); + let doc = "** hello"; + assert_eq!(parse(doc), vec![Org::Header(2, "hello".to_string())]); assert_eq!( parse(" * hello"), vec![Org::Unknown(" * hello".to_string())] @@ -131,4 +156,18 @@ mod tests { ] ); } + + #[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))); + } } |