summaryrefslogtreecommitdiff
path: root/src/journal.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal.rs')
-rw-r--r--src/journal.rs32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/journal.rs b/src/journal.rs
index 788a34b..41e6d3d 100644
--- a/src/journal.rs
+++ b/src/journal.rs
@@ -57,7 +57,12 @@ impl Journal {
self.entries.clone()
}
- pub fn new_draft(&self, title: &str, editor: &str) -> Result<(), JournalError> {
+ pub fn new_draft(
+ &self,
+ title: &str,
+ topic: &Option<PathBuf>,
+ editor: &str,
+ ) -> Result<(), JournalError> {
let drafts = self.drafts();
if !drafts.exists() {
std::fs::create_dir(&drafts)
@@ -67,6 +72,13 @@ impl Journal {
let mut context = Context::new();
context.insert("title", title);
context.insert("date", &current_timestamp());
+ if let Some(ref topic) = topic {
+ let pathname = topic_path(self.dirname(), topic);
+ if !pathname.exists() {
+ return Err(JournalError::NoSuchTopic(topic.to_path_buf()));
+ }
+ context.insert("topic", &topic.display().to_string());
+ }
let pathname = self.pick_file_id(&drafts)?;
let text = self.templates.new_draft(&context)?;
@@ -143,6 +155,18 @@ impl Journal {
})?;
Ok(())
}
+
+ pub fn new_topic(&self, path: &Path, title: &str, editor: &str) -> Result<(), JournalError> {
+ let mut context = Context::new();
+ context.insert("title", title);
+
+ let pathname = topic_path(self.dirname(), path);
+ let text = self.templates.new_topic(&context)?;
+ std::fs::write(&pathname, text)
+ .map_err(|err| JournalError::WriteTopic(pathname.to_path_buf(), err))?;
+ self.edit(editor, &pathname)?;
+ Ok(())
+ }
}
fn is_dir(path: &Path) -> bool {
@@ -153,6 +177,12 @@ fn is_dir(path: &Path) -> bool {
}
}
+fn topic_path(dirname: &Path, topic: &Path) -> PathBuf {
+ let mut path = dirname.join(topic);
+ path.set_extension("mdwn");
+ path
+}
+
fn current_timestamp() -> String {
let now = Local::now();
let now: DateTime<Local> = DateTime::from(now);