use std::io::Result;
use ropey::Rope;
use starsector::*;
fn main() -> Result<()> {
let mut arena = Arena::default();
let doc = arena.parse_str("");
println!(
"All documents have a root section. This one has level {} and text \"{}\"",
doc.root.level(&arena),
doc.root.text(&arena)
);
let child1 = arena.new_section("* Hello".into()).unwrap();
let grandchild1 = arena.new_section("* Sub Headline".into()).unwrap();
let child2 = arena.new_section("* World".into()).unwrap();
let grandchild2 = arena
.new_section("******* Deep Sub Headline".into())
.unwrap();
child1.checked_append(&mut arena, grandchild1).unwrap_err();
child1.append(&mut arena, grandchild1).unwrap();
child2.checked_append(&mut arena, grandchild2).unwrap();
doc.root.append(&mut arena, child1).unwrap();
doc.root.append(&mut arena, child2).unwrap();
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
grandchild1.checked_append(&mut arena, grandchild2).unwrap();
let duplicate_child2 = arena.clone_section(child2);
grandchild2.prepend(&mut arena, duplicate_child2).unwrap();
let duplicate_child1 = arena.clone_section(child1);
child1.insert_after(&mut arena, duplicate_child1).unwrap();
doc.root
.set_raw(&mut arena, "- This is\n- body text".into())
.unwrap();
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
assert_eq!(child1.level(&arena), 1);
child1.set_level(&mut arena, 0).unwrap_err();
child1.set_level(&mut arena, 1).unwrap();
child1.set_level(&mut arena, 2).unwrap_err();
grandchild1.set_level(&mut arena, 3).unwrap();
child1.set_level(&mut arena, 2).unwrap();
child1
.set_raw(&mut arena, "Not a headline".into())
.unwrap_err();
child1
.set_raw(&mut arena, "* More than\n** one headline".into())
.unwrap_err();
child1
.set_raw(&mut arena, "* More than\n* one headline".into())
.unwrap_err();
child1
.set_raw(
&mut arena,
"* Reduces level and\nchanges all text\n- list\n- ok".into(),
)
.unwrap();
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
#[cfg(feature = "headline-parser")]
{
let headline = child1.headline(&arena, None);
let headline = headline.unwrap();
let mut headline_builder = headline.to_builder();
headline_builder
.keyword(Some("TODO".into()))
.clear_tag("bar")
.add_tag("qux")
.priority(Some('C'))
.add_tag("foo")
.commented(true)
.add_tag("foo")
.clear_tag("qux")
.add_tag("bar");
headline_builder.body(Rope::default());
let headline = headline_builder.headline( None).unwrap();
let tags: Vec<_> = headline.tags().collect();
assert_eq!(tags.len(), 2);
assert!(headline.has_tag("foo"));
assert!(headline.has_tag("bar"));
assert!(!headline.has_tag("qux"));
let headline = headline.to_owned();
child1.set_headline(&mut arena, &headline).unwrap();
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
HeadlineBuilder::default()
.title("My Title\n* Hello".into())
.headline( None)
.unwrap_err();
HeadlineBuilder::default()
.title("My Title".into())
.body("* Foo\n* Bar".into())
.headline( None)
.unwrap_err();
assert_eq!(
Rope::from("* * Hello"),
HeadlineBuilder::default()
.title("* Hello".into())
.headline( None)
.unwrap()
.to_rope()
);
assert_eq!(
grandchild2.keyword(&arena, None).unwrap(),
None
);
grandchild2
.set_keyword(&mut arena, Some("TODO".into()), None)
.unwrap();
assert_eq!(
grandchild2.keyword(&arena, None).unwrap(),
Some("TODO".into())
);
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
}
#[cfg(feature = "orgize-integration")]
{
doc.root.remove_children(&mut arena);
let section = arena
.new_section(
"* TODO do stuff\nDEADLINE: <2020-07-09 Thu>\n:PROPERTIES:\n:ID: myid123\n:END:"
.into(),
)
.unwrap();
doc.root.prepend(&mut arena, section).unwrap();
let mut headline_builder = HeadlineBuilder::default();
headline_builder
.title("Other stuff".into())
.keyword(Some("TODO".into()))
.property("FOO", "BAR")
.unwrap()
.level(1)
.scheduled(Some(Point::new(Date::new(2109, 11, 11)).into()));
let section2 = arena
.new_section(
headline_builder
.headline( None)
.unwrap()
.to_rope(),
)
.unwrap();
doc.root.prepend(&mut arena, section2).unwrap();
let section1 = doc.root.children(&arena).next().unwrap();
eprintln!(
"Builder created node property FOO is {}",
section1
.get_property(&arena, "FOO", None)
.unwrap()
.unwrap()
);
eprintln!(
"Builder created node scheduled isj {:?}",
section1.scheduled(&arena, None).unwrap()
);
eprintln!(
"Builder created node closed is: {:?}",
section1.closed(&arena, None).unwrap()
);
let section2 = doc.root.children(&arena).last().unwrap();
eprintln!(
"Starsector created node property ID is {}",
section2.get_id(&arena, None).unwrap().unwrap()
);
eprintln!(
"Starsector created node scheduled is: {:?}",
section2.scheduled(&arena, None).unwrap()
);
eprintln!(
"Starsector created node deadline is: {:?}",
section2.deadline(&arena, None).unwrap()
);
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
section1
.set_property(&mut arena, "FOO", "CORGE", None)
.unwrap();
section2
.set_scheduled(
&mut arena,
Some(Point::new(Date::new(1971, 11, 11)).into()),
None,
)
.unwrap();
section1.generate_id(&mut arena, None).unwrap();
section2
.add_tag(&mut arena, "mytag", None)
.unwrap();
println!("The document is:\n{}\n\n-------\n\n", doc.to_rope(&arena));
}
#[cfg(not(feature = "headline-parser"))]
println!("Enable headline parser for headline examples.");
Ok(())
}