diff options
author | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2021-04-08 09:03:50 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2021-04-08 09:03:50 +0000 |
commit | 924fc1add2de3edac4ad4b1097a3bbbb73d0ad50 (patch) | |
tree | e5c9689e1ff60953502faf55eacb8a82c2726b8a /src/journal.rs | |
parent | 518e7dc2d5f97387702af0a300cc2842bd0deec6 (diff) | |
parent | 0b0cc421e79bff9f8692e8c771c5e72ee4414032 (diff) | |
download | jt2-924fc1add2de3edac4ad4b1097a3bbbb73d0ad50.tar.gz |
Merge branch 'tera' into 'main'
feat! add support for tera templates for new journal entries
Closes #8 and #10
See merge request larswirzenius/jt!11
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() +} |