summaryrefslogtreecommitdiff
path: root/src/journal.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal.rs')
-rw-r--r--src/journal.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/journal.rs b/src/journal.rs
index 8042f83..ad8b4af 100644
--- a/src/journal.rs
+++ b/src/journal.rs
@@ -2,6 +2,8 @@ use crate::error::JournalError;
use crate::git;
use crate::template::Templates;
use chrono::{DateTime, Local};
+use glob::glob;
+use regex::Regex;
use std::path::{Path, PathBuf};
use std::process::Command;
use tera::Context;
@@ -121,6 +123,21 @@ impl Journal {
}
}
+ pub fn list_drafts(&self) -> Result<(), JournalError> {
+ let prefix = format!("{}/", self.drafts().display());
+ let pattern = format!("{}*.md", prefix);
+ let entries =
+ glob(&pattern).map_err(|err| JournalError::PatternError(pattern.to_string(), err))?;
+ for entry in entries {
+ let entry = entry.map_err(|err| JournalError::GlobError(pattern.to_string(), err))?;
+ let title = get_title(&entry)?;
+ let entry = entry.file_stem().unwrap().to_string_lossy();
+ let entry = entry.strip_prefix(&prefix).unwrap_or(&entry);
+ println!("{} {}", entry, title);
+ }
+ Ok(())
+ }
+
fn edit(&self, editor: &str, filename: &Path) -> Result<(), JournalError> {
if editor == "none" {
return Ok(());
@@ -209,3 +226,16 @@ fn current_timestamp() -> String {
let now: DateTime<Local> = Local::now();
now.to_rfc2822()
}
+
+fn get_title(filename: &Path) -> Result<String, JournalError> {
+ let text = std::fs::read(filename)
+ .map_err(|err| JournalError::ReadDraft(filename.to_path_buf(), err))?;
+ let text = String::from_utf8_lossy(&text);
+ let pat = Regex::new(r#"^\[\[!meta title="(?P<title>.*)"\]\]"#).unwrap();
+ if let Some(caps) = pat.captures(&text) {
+ if let Some(m) = caps.name("title") {
+ return Ok(m.as_str().to_string());
+ }
+ }
+ Ok("(untitled)".to_string())
+}