diff options
Diffstat (limited to 'src/journal.rs')
-rw-r--r-- | src/journal.rs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/journal.rs b/src/journal.rs index f58e49d..788a34b 100644 --- a/src/journal.rs +++ b/src/journal.rs @@ -1,13 +1,16 @@ use crate::error::JournalError; -use chrono::Local; +use crate::template::Templates; +use chrono::{DateTime, Local}; use std::path::{Path, PathBuf}; use std::process::Command; +use tera::Context; const MAX_DRAFT_COUNT: usize = 1000; pub struct Journal { dirname: PathBuf, entries: PathBuf, + templates: Templates, } impl Journal { @@ -23,6 +26,7 @@ impl Journal { Ok(Self { dirname: path.to_path_buf(), entries: entries.to_path_buf(), + templates: Templates::new(path)?, }) } @@ -30,7 +34,12 @@ impl Journal { if Self::is_journal(path, entries) { let dirname = path.to_path_buf(); let entries = entries.to_path_buf(); - Ok(Self { dirname, entries }) + let templates = Templates::new(path)?; + Ok(Self { + dirname, + entries, + templates, + }) } else { Err(JournalError::NotAJournal(path.display().to_string())) } @@ -55,9 +64,13 @@ impl Journal { .map_err(|err| JournalError::CreateDirectory(drafts.to_path_buf(), err))?; } + let mut context = Context::new(); + context.insert("title", title); + context.insert("date", ¤t_timestamp()); + let pathname = self.pick_file_id(&drafts)?; - let text = format!(r#"[[!meta title="{}"]]"#, title); - std::fs::write(&pathname, format!("{}\n\n", text)) + let text = self.templates.new_draft(&context)?; + std::fs::write(&pathname, text) .map_err(|err| JournalError::WriteEntry(pathname.to_path_buf(), err))?; self.edit(editor, &pathname)?; Ok(()) @@ -139,3 +152,9 @@ fn is_dir(path: &Path) -> bool { false } } + +fn current_timestamp() -> String { + let now = Local::now(); + let now: DateTime<Local> = DateTime::from(now); + now.to_rfc2822() +} |