Compare commits

..

3 commits

2 changed files with 26 additions and 18 deletions

View file

@ -97,14 +97,19 @@ fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
// line, breaking the desired formatting
fn apply_block_quote<S: AsRef<str>>(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<initial>[^>\\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("</p><p>", "\n\n");
let content = content.replace("<p>", "");
let content = content.replace("</p>", "");
// 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| {

View file

@ -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
}
_ => {
@ -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<Local>, 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,