From 2edc3082d3883565521c4113a7f148f3656b89e9 Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Tue, 9 Apr 2024 15:24:19 -0400 Subject: [PATCH 1/3] Format fixes --- src/format.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/format.rs b/src/format.rs index e6c46dc..b0c3d2b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -97,14 +97,19 @@ fn format_attachments(depth: usize, attachments: &[Attachment]) -> String { // line, breaking the desired formatting fn apply_block_quote>(depth: usize, content: S) -> String { let prefix = quote_prefix(depth); + // TODO use OnceLock let empty = Regex::new("\n\\s").expect("Failed to compiled regex for nested empty lines!"); let non_empty = Regex::new("\n(?P[^>\\s])").expect("Failed to compile regex for nested lines!"); + // TODO break into separate function // parsing some multiline content adds paragraph tags, convert those back to new lines to apply // the block quote level to each line let content = content.as_ref().replace("

", "\n\n"); let content = content.replace("

", ""); let content = content.replace("

", ""); + // TODO refactor into separate function + let content = content.replace("\\*", "*"); + let content = content.replace(":totoro:", "\\:totoro\\:"); // replace separately to avoid trailing spaces when replacing empty lines with the prefix let content = empty.replace_all(content.as_ref(), format!("\n{}\n", prefix.trim())); let content = non_empty.replace_all(&content, |c: &Captures| { From a5016082fc03378bd9b51798a6f71aa643bff76a Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Tue, 9 Apr 2024 15:24:41 -0400 Subject: [PATCH 2/3] Add missing empty line when appending to existing file --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e00b043..498de9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,7 +127,7 @@ async fn main() -> Result<()> { Ok(exists) if exists => { debug!("Appending {}", output); let mut file = File::options().append(true).open(&output)?; - file.write("\n".as_bytes())?; + file.write("\n\n".as_bytes())?; file } _ => { From 1096dd1db8a73e761d63a0f5f527242baf8c39db Mon Sep 17 00:00:00 2001 From: Thomas Gideon Date: Tue, 9 Apr 2024 15:25:02 -0400 Subject: [PATCH 3/3] Extract back link function, add two and three years ago links --- src/main.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 498de9a..62a9afa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -138,30 +138,23 @@ async fn main() -> Result<()> { .open(&output) .with_context(|| format!("Failed to create {}", output))?; file.write(format!("# {}\n\n", day.end.format("%Y-%m-%d")).as_bytes())?; - // TODO extract into a more general function - let week_ago = day.end - Duration::days(7); - // TODO check if the file exists + + // TODO move to separate function + file.write(create_back_link(&day.end, "[One week ago](diary:{})\n", 7).as_bytes())?; file.write( - format!("[One week ago](diary:{})\n", week_ago.format("%Y-%m-%d")).as_bytes(), + create_back_link(&day.end, "[One month ago](diary:{})\n", 30).as_bytes(), )?; - let month_ago = day.end - Duration::days(30); - // TODO check if the file exists file.write( - format!("[One month ago](diary:{})\n", month_ago.format("%Y-%m-%d")).as_bytes(), + create_back_link(&day.end, "[Six months ago](diary:{})\n", 6 * 30).as_bytes(), )?; - let half_year_ago = day.end - Duration::days(6 * 30); - // TODO check if the file exists file.write( - format!( - "[Six months ago](diary:{})\n", - half_year_ago.format("%Y-%m-%d") - ) - .as_bytes(), + create_back_link(&day.end, "[One year ago](diary:{})\n", 365).as_bytes(), )?; - let year_ago = day.end - Duration::days(365); - // TODO check if the file exists file.write( - format!("[One year ago](diary:{})\n", year_ago.format("%Y-%m-%d")).as_bytes(), + create_back_link(&day.end, "[Two years ago](diary:{})\n", 365 * 2).as_bytes(), + )?; + file.write( + create_back_link(&day.end, "[Three years ago](diary:{})\n", 365 * 3).as_bytes(), )?; file } @@ -175,6 +168,16 @@ async fn main() -> Result<()> { Ok(()) } +fn create_back_link(day_end: &DateTime, anchor_text: &str, ago: i64) -> String { + let prior_date = *day_end - Duration::days(ago); + // TODO check if the file exists + format!( + "[{}](diary:{})\n", + anchor_text, + prior_date.format("%Y-%m-%d") + ) +} + enum NextIter { Skip, Stop,